This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathproof.js
More file actions
295 lines (283 loc) · 9.35 KB
/
proof.js
File metadata and controls
295 lines (283 loc) · 9.35 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const Trie = require('../src/index.js')
const async = require('async')
const tape = require('tape')
tape('simple merkle proofs generation and verification', function (tester) {
var it = tester.test
it('create a merkle proof and verify it', function (t) {
var trie = new Trie()
async.series([
function (cb) {
trie.put('key1aa', '0123456789012345678901234567890123456789xx', cb)
},
function (cb) {
trie.put('key2bb', 'aval2', cb)
},
function (cb) {
trie.put('key3cc', 'aval3', cb)
},
function (cb) {
Trie.prove(trie, 'key2bb', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key2bb', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), 'aval2')
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key1aa', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789xx')
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key2bb', function (err, prove) {
t.equal(err, null, 'Path to key2 should create valid proof of absence')
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key2', prove, function (err, val) {
// In this case, the proof _happens_ to contain enough nodes to prove `key2` because
// traversing into `key22` would touch all the same nodes as traversing into `key2`
t.equal(val, null, 'Expected value at a random key to be null')
t.equal(err, null, 'Path to key2 should show a null value')
cb()
})
})
},
function (cb) {
let myKey = 'anyrandomkey'
Trie.prove(trie, myKey, function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, myKey, prove, function (err, val) {
t.equal(val, null, 'Expected value to be null')
t.equal(err, null, err)
cb()
})
})
},
function (cb) {
let myKey = 'anothergarbagekey' // should generate a valid proof of null
Trie.prove(trie, myKey, function (err, prove) {
if (err) return cb(err)
prove.push(Buffer.from('123456'))// extra nodes are just ignored
Trie.verifyProof(trie.root, myKey, prove, function (err, val) {
t.equal(val, null, 'Expected value to be null')
t.equal(err, null, err)
cb()
})
})
},
function (cb) {
trie.put('another', '3498h4riuhgwe', cb)
},
function (cb) {
// to throw an error we need to request proof for one key
Trie.prove(trie, 'another', function (err, prove) {
if (err) return cb(err)
// and try to use that proof on another key
Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) {
t.equal(val, null, 'Expected value: to be null ')
// this proof would be insignificant to prove `key1aa`
t.notEqual(err, null, 'Expected error: Missing node in DB')
t.notEqual(err, undefined, 'Expected error: Missing node in DB')
cb()
})
})
}
], function (err) {
t.end(err)
})
})
it('create a merkle proof and verify it with a single long key', function (t) {
var trie = new Trie()
async.series([
function (cb) {
trie.put('key1aa', '0123456789012345678901234567890123456789xx', cb)
},
function (cb) {
Trie.prove(trie, 'key1aa', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789xx')
cb()
})
})
}
], function (err) {
t.end(err)
})
})
it('create a merkle proof and verify it with a single short key', function (t) {
var trie = new Trie()
async.series([
function (cb) {
trie.put('key1aa', '01234', cb)
},
function (cb) {
Trie.prove(trie, 'key1aa', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key1aa', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), '01234')
cb()
})
})
}
], function (err) {
t.end(err)
})
})
it('create a merkle proof and verify it whit keys in the midle', function (t) {
var trie = new Trie()
async.series([
function (cb) {
trie.put('key1aa', '0123456789012345678901234567890123456789xxx', cb)
},
function (cb) {
trie.put('key1', '0123456789012345678901234567890123456789Very_Long', cb)
},
function (cb) {
trie.put('key2bb', 'aval3', cb)
},
function (cb) {
trie.put('key2', 'short', cb)
},
function (cb) {
trie.put('key3cc', 'aval3', cb)
},
function (cb) {
trie.put('key3', '1234567890123456789012345678901', cb)
},
function (cb) {
Trie.prove(trie, 'key1', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key1', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), '0123456789012345678901234567890123456789Very_Long')
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key2', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key2', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), 'short')
cb()
})
})
},
function (cb) {
Trie.prove(trie, 'key3', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'key3', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), '1234567890123456789012345678901')
cb()
})
})
}
], function (err) {
t.end(err)
})
})
it('should succeed with a simple embedded extension-branch', function (t) {
var trie = new Trie()
async.series([
(cb) => {
trie.put('a', 'a', cb)
}, (cb) => {
trie.put('b', 'b', cb)
}, (cb) => {
trie.put('c', 'c', cb)
}, (cb) => {
Trie.prove(trie, 'a', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'a', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), 'a')
cb()
})
})
}, (cb) => {
Trie.prove(trie, 'b', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'b', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), 'b')
cb()
})
})
}, (cb) => {
Trie.prove(trie, 'c', function (err, prove) {
if (err) return cb(err)
Trie.verifyProof(trie.root, 'c', prove, function (err, val) {
if (err) return cb(err)
t.equal(val.toString('utf8'), 'c')
cb()
})
})
}], function (err) {
t.end(err)
})
})
})
tape('multiproof generation and verification', function (tester) {
var it = tester.test
it('create a multiproof and verify it', function (t) {
var trie = new Trie()
async.series([
function (cb) {
trie.put('key1aa', '0123456789012345678901234567890123456789xx', cb)
},
function (cb) {
trie.put('key2bb', 'aval2', cb)
},
function (cb) {
trie.put('key3cc', 'aval3', cb)
},
function (cb) {
Trie.proveMultiple(trie, ['key2bb', 'key3cc'], function (err, proofNodes) {
if (err) return cb(err)
Trie.verifyMultiproof(trie.root, ['key2bb', 'key3cc'], proofNodes, function (err, values) {
if (err) return cb(err)
t.equal(values.length, 2)
t.equal(values[0].toString('utf8'), 'aval2')
t.equal(values[1].toString('utf8'), 'aval3')
cb()
})
})
},
function (cb) {
Trie.proveMultiple(trie, ['nono', 'key3cc'], function (err, proofNodes) {
if (err) return cb(err)
Trie.verifyMultiproof(trie.root, ['nono', 'key3cc'], proofNodes, function (err, values) {
if (err) return cb(err)
t.equal(values.length, 2)
t.equal(values[0], null)
t.equal(values[1].toString('utf8'), 'aval3')
cb()
})
})
},
function (cb) {
// proof shouldn't work for different keys
Trie.proveMultiple(trie, ['key2bb', 'key3cc'], function (err, proofNodes) {
if (err) return cb(err)
Trie.verifyMultiproof(trie.root, ['key3cc', 'key1aa'], proofNodes, function (err, values) {
t.ok(err)
cb()
})
})
}
], function (err) {
t.end(err)
})
})
})