-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (138 loc) · 4.78 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var cmd = _interopDefault(require('node-cmd'));
var ethers = _interopDefault(require('ethers'));
require('babel-polyfill');
function save (path) {
console.log('\n Saving ' + path + ' to IPFS');
cmd.get('ipfs add ' + path, function (err, data, stderr) {
console.log(data);
});
}
/*=============================================
from Merkle Aidrop Repo:
reduceMerkleParents,
merkleProof
and reduceMerkleRoot
=============================================*/
/**
* Reduce children nodes to thier merkle parent nodes.
* This method is side effect free and returns a new array.
* @param {Array} leaves - Child nodes.
* @return {Array} - Parent nodes.
*/
function reduceMerkleParents(leaves) {
var output = [];
for (var i = 0; i < leaves.length; i += 2) {
var left = leaves[i];
var right = leaves[i + 1];
// If only left node, hash left with left.
output.push(ethers.utils.keccak256(left + (right ? right.substring(2) : left.substring(2))));
}
return output;
}
/**
* Reduce children to Merkle root hash.
* @param {Array} leaves - Child nodes.
* @return {String} - Root hash.
*/
function reduceMerkleRoot(leaves) {
var output = leaves;
while (output.length > 1) {
output = reduceMerkleParents(output);
}
return output[0];
}
function merkleProof(index, leaves) {
var path = index;
var proof = [];
while (leaves.length > 1) {
if (path % 2 == 1) {
proof.push(leaves[path - 1]);
} else {
proof.push(leaves[path + 1]);
}
// Reduce the merkle tree one level
leaves = reduceMerkleParents(leaves);
// Move up
path = parseInt(path / 2);
}
return proof;
}
function hash(arr) {
var zeros32 = '0000000000000000000000000000000000000000000000000000000000000000';
function _hash(_index, _address, _permission) {
var address = _address.substring(2);
var index = zeros32 + _index.toString(16);
index = index.substring(index.length - 64);
var permission = zeros32 + _permission.toString(16);
permission = permission.substring(permission.length - 64);
return ethers.utils.keccak256('0x' + index + address + permission);
}
return arr.map(function (leaf, i) {
return _hash(i, leaf.address, leaf.permission);
});
}
/**
*
* @param {Number} index - Index of value in leaf array.
* @param {String} leafHash - Hashed leaf node.
* @param {Array} merkleProof - Merkle proof hashes.
* @param {String} merkleRoot - Merkle root to check against.
*/
function checkMerkleProof(index, leafHash, merkleProof, merkleRoot) {
var node = leafHash;
var path = index;
for (var i = 0; i < merkleProof.length; i += 1) {
// Odd / Even check
if ((path & 0x01) == 1) {
node = ethers.utils.keccak256(merkleProof[i] + node.substring(2));
} else {
node = ethers.utils.keccak256(node + merkleProof[i].substring(2));
}
path = parseInt(path / 2);
}
return node === merkleRoot;
}
var utils = {
reduceMerkleParents: reduceMerkleParents,
reduceMerkleRoot: reduceMerkleRoot,
merkleProof: merkleProof,
hash: hash,
checkMerkleProof: checkMerkleProof
};
function merkleRoot (ipfsHash) {
console.log('\n Getting ' + ipfsHash + ' from IPFS \n');
cmd.get('ipfs cat ' + ipfsHash, function (err, data, stderr) {
console.log(' Computed Merkle Root: \n ' + utils.reduceMerkleRoot(utils.hash(JSON.parse(data))));
});
}
function merkleProof$1 (index, ipfsHash) {
console.log('\n Getting ' + ipfsHash + ' from IPFS \n');
cmd.get('ipfs cat ' + ipfsHash, function (err, data, stderr) {
console.log(' Computed Merkle Proof: \n ' + utils.merkleProof(parseInt(index), utils.hash(JSON.parse(data))));
});
}
function publish (rootHash, ipfsHash) {
console.log('\n Publishing Merkle Root: ' + rootHash);
console.log(' Publishing IPFS Hash: ' + ipfsHash);
cmd.get('truffle exec publishHashes.js ' + rootHash + ' ' + ipfsHash, function (err, data, stderr) {
console.log(data);
});
}
function verify (index, address, permission, proof) {
console.log('\n Verifying permission == ' + permission);
console.log(' For address: ' + address);
cmd.get('truffle exec verifyProof.js ' + index + ' ' + address + ' ' + permission + ' ' + JSON.stringify(proof), function (err, data, stderr) {
console.log(data);
});
}
var index = {
save: save,
merkleRoot: merkleRoot,
merkleProof: merkleProof$1,
publish: publish,
verify: verify,
utils: utils
};
module.exports = index;