Skip to content

Commit

Permalink
Fallback to relayer if signature is 0x (#880)
Browse files Browse the repository at this point in the history
* Fallback to relayer

* Update src/sign/index.ts

* v0.5.0
  • Loading branch information
ChaituVR authored Aug 10, 2023
1 parent 6ec7c66 commit a2db2c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.4.110",
"version": "0.5.0",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
10 changes: 7 additions & 3 deletions src/sign/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export const domain = {

export default class Client {
readonly address: string;
readonly options: any;

constructor(address: string = constants.livenet.sequencer) {
constructor(address: string = constants.livenet.sequencer, options = {}) {
address = address.replace(
constants.livenet.hub,
constants.livenet.sequencer
Expand All @@ -59,6 +60,7 @@ export default class Client {
);
address = address.replace(constants.local.hub, constants.local.sequencer);
this.address = address;
this.options = options;
}

async sign(web3: Web3Provider | Wallet, address: string, message, types) {
Expand All @@ -70,11 +72,13 @@ export default class Client {
message.timestamp = parseInt((Date.now() / 1e3).toFixed());
const data: any = { domain, types, message };
const sig = await signer._signTypedData(domain, data.types, message);
//console.log('Sign', { address: checksumAddress, sig, data });
return await this.send({ address: checksumAddress, sig, data });
}

async send(envelop) {
let address = this.address;
if (envelop.sig === '0x' && this.options.relayerURL)
address = this.options.relayerURL;
const init = {
method: 'POST',
headers: {
Expand All @@ -84,7 +88,7 @@ export default class Client {
body: JSON.stringify(envelop)
};
return new Promise((resolve, reject) => {
fetch(this.address, init)
fetch(address, init)
.then((res) => {
if (res.ok) return resolve(res.json());
throw res;
Expand Down

0 comments on commit a2db2c0

Please sign in to comment.