-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.js
70 lines (61 loc) · 1.96 KB
/
send.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const ethers = require('ethers');
const fs = require('fs').promises;
require('dotenv').config();
const snapshot = require('@snapshot-labs/snapshot.js');
const hub = process.env.SPACE === "main" ? 'https://hub.snapshot.org' : "https://testnet.hub.snapshot.org";
const client = new snapshot.Client712(hub);
let r = "";
let s = "";
let v = "";
if (ethers.isHexString(process.env.R, 32)) {
r = process.env.R;
console.log("R:", r);
} else {
console.log("R is incorrect!");
return 0;
}
if (ethers.isHexString(process.env.S, 32)) {
s = process.env.S;
console.log("S:", s);
} else {
console.log("S is incorrect!");
return 0;
}
if (parseInt(process.env.V) === 0) {
v = "1b";
console.log("V:", v);
} else if (parseInt(process.env.V) === 1) {
v = "1C";
console.log("V:", v);
} else {
console.log("V is incorrect, please input V in .env - 0 or 1");
return 0;
}
const signature = "0x" + r.slice(2) + s.slice(2) + v;
async function checkAndSend() {
const result = JSON.parse(await fs.readFile("./data/result.json", "utf-8"));
const verifyAddress = ethers.verifyTypedData(result.domain, result.types, result.message, signature);
console.log("Retrieved address:", verifyAddress);
console.log("Actual address:", result.message.from);
if (ethers.getAddress(verifyAddress) !== ethers.getAddress(result.message.from)) {
console.log("Verification does not pass!");
return 0;
}
const envelop = {
address: ethers.getAddress(verifyAddress),
sig: signature,
data: result
};
console.log("\nSending this envelop");
console.log(envelop, "\n");
try {
const receipt = await client.send(envelop);
console.log("\nReceive receipt:", receipt, "\n");
console.log("\nSuccessful!!! Check the vote on Snapshot!\n");
} catch (e) {
console.log("\nError:\n");
console.log(e);
console.log("\nSend envelop failed! \n");
}
}
checkAndSend();