-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.js
84 lines (71 loc) · 2.28 KB
/
hash.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const ethers = require('ethers');
require('dotenv').config();
const fs = require('fs').promises;
let validatorAddress = "";
let proposal = "";
let space = "skalenetwotk.eth";
let choice = 0;
try {
validatorAddress = ethers.getAddress(process.env.VALIDATOR_ADDRESS);
console.log("Validator address:", validatorAddress);
} catch (e) {
console.log("Validator address is incorrect!");
console.log(e);
return 0;
}
if (ethers.isHexString(process.env.PROPOSAL_ID, 32)) {
proposal = process.env.PROPOSAL_ID;
console.log("Proposal ID:", proposal);
} else {
console.log("Proposal ID is incorrect!");
return 0;
}
if (process.env.SPACE === "main") {
space = "skale.eth";
console.log("Using production with space:", space);
} else {
console.log("Using test with space:", space);
}
if (parseInt(process.env.VOTE) === 1 || parseInt(process.env.VOTE) === 2 || parseInt(process.env.VOTE) === 3) {
choice = parseInt(process.env.VOTE);
console.log("Vote:", choice);
} else {
console.log("Vote is incorrect, please input VOTE in .env - 1 or 2 or 3");
return 0;
}
const vote2Types = {
Vote: [
{ name: 'from', type: 'address' },
{ name: 'space', type: 'string' },
{ name: 'timestamp', type: 'uint64' },
{ name: 'proposal', type: 'bytes32' },
{ name: 'choice', type: 'uint32' },
{ name: 'reason', type: 'string' },
{ name: 'app', type: 'string' },
{ name: 'metadata', type: 'string' }
]
};
const domain = {
name: "snapshot",
version: "0.1.4"
}
const vote = {
from: validatorAddress, // validator address
space, // space name
timestamp: parseInt((Date.now() / 1e3).toFixed()),
proposal, // proposal Id
choice, // choice at the proposal (first answer - 1, second answer - 2 ...)
reason: "",
app: "snapshot",
metadata: "{}"
}
console.log("\nTyped data for the vote:");
const voteTypedJSON = {domain, types: vote2Types, message: vote};
async function write(jsonObj) {
await fs.writeFile('data/result.json', JSON.stringify(jsonObj, null, 4));
}
write(voteTypedJSON);
console.log(voteTypedJSON, "\n")
const structHash = ethers.TypedDataEncoder.hash(domain, vote2Types, vote);
console.log("\nResult hash which need to be signed by SGX:");
console.log(structHash, "\n");