1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.verifyRangeProof = void 0;
|
4 | const nibbles_1 = require("./util/nibbles");
|
5 | const baseTrie_1 = require("./baseTrie");
|
6 | const trieNode_1 = require("./trieNode");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | async function unset(trie, parent, child, key, pos, removeLeft, stack) {
|
20 | if (child instanceof trieNode_1.BranchNode) {
|
21 | |
22 |
|
23 |
|
24 |
|
25 | if (removeLeft) {
|
26 | for (let i = 0; i < key[pos]; i++) {
|
27 | child.setBranch(i, null);
|
28 | }
|
29 | }
|
30 | else {
|
31 | for (let i = key[pos] + 1; i < 16; i++) {
|
32 | child.setBranch(i, null);
|
33 | }
|
34 | }
|
35 |
|
36 | stack.push(child);
|
37 |
|
38 | const next = child.getBranch(key[pos]);
|
39 | const _child = next && (await trie.lookupNode(next));
|
40 | return await unset(trie, child, _child, key, pos + 1, removeLeft, stack);
|
41 | }
|
42 | else if (child instanceof trieNode_1.ExtensionNode || child instanceof trieNode_1.LeafNode) {
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | if (key.length - pos < child.keyLength ||
|
49 | (0, nibbles_1.nibblesCompare)(child._nibbles, key.slice(pos, pos + child.keyLength)) !== 0) {
|
50 | if (removeLeft) {
|
51 | if ((0, nibbles_1.nibblesCompare)(child._nibbles, key.slice(pos)) < 0) {
|
52 | ;
|
53 | parent.setBranch(key[pos - 1], null);
|
54 | }
|
55 | }
|
56 | else {
|
57 | if ((0, nibbles_1.nibblesCompare)(child._nibbles, key.slice(pos)) > 0) {
|
58 | ;
|
59 | parent.setBranch(key[pos - 1], null);
|
60 | }
|
61 | }
|
62 | return pos - 1;
|
63 | }
|
64 | if (child instanceof trieNode_1.LeafNode) {
|
65 |
|
66 | ;
|
67 | parent.setBranch(key[pos - 1], null);
|
68 | return pos - 1;
|
69 | }
|
70 | else {
|
71 | const _child = await trie.lookupNode(child.value);
|
72 | if (_child && _child instanceof trieNode_1.LeafNode) {
|
73 |
|
74 | ;
|
75 | parent.setBranch(key[pos - 1], null);
|
76 | return pos - 1;
|
77 | }
|
78 |
|
79 | stack.push(child);
|
80 |
|
81 | return await unset(trie, child, _child, key, pos + child.keyLength, removeLeft, stack);
|
82 | }
|
83 | }
|
84 | else if (child === null) {
|
85 | return pos - 1;
|
86 | }
|
87 | else {
|
88 | throw new Error('invalid node');
|
89 | }
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | async function unsetInternal(trie, left, right) {
|
99 |
|
100 | let pos = 0;
|
101 |
|
102 | let parent = null;
|
103 |
|
104 | let node = await trie.lookupNode(trie.root);
|
105 | let shortForkLeft;
|
106 | let shortForkRight;
|
107 |
|
108 | const stack = [];
|
109 |
|
110 |
|
111 | while (true) {
|
112 | if (node instanceof trieNode_1.ExtensionNode || node instanceof trieNode_1.LeafNode) {
|
113 |
|
114 | stack.push(node);
|
115 | if (left.length - pos < node.keyLength) {
|
116 | shortForkLeft = (0, nibbles_1.nibblesCompare)(left.slice(pos), node._nibbles);
|
117 | }
|
118 | else {
|
119 | shortForkLeft = (0, nibbles_1.nibblesCompare)(left.slice(pos, pos + node.keyLength), node._nibbles);
|
120 | }
|
121 | if (right.length - pos < node.keyLength) {
|
122 | shortForkRight = (0, nibbles_1.nibblesCompare)(right.slice(pos), node._nibbles);
|
123 | }
|
124 | else {
|
125 | shortForkRight = (0, nibbles_1.nibblesCompare)(right.slice(pos, pos + node.keyLength), node._nibbles);
|
126 | }
|
127 |
|
128 | if (shortForkLeft !== 0 || shortForkRight !== 0) {
|
129 | break;
|
130 | }
|
131 | if (node instanceof trieNode_1.LeafNode) {
|
132 |
|
133 | throw new Error('invalid node');
|
134 | }
|
135 |
|
136 | parent = node;
|
137 | pos += node.keyLength;
|
138 | node = await trie.lookupNode(node.value);
|
139 | }
|
140 | else if (node instanceof trieNode_1.BranchNode) {
|
141 |
|
142 | stack.push(node);
|
143 | const leftNode = node.getBranch(left[pos]);
|
144 | const rightNode = node.getBranch(right[pos]);
|
145 |
|
146 | if (leftNode === null || rightNode === null) {
|
147 | break;
|
148 | }
|
149 |
|
150 | if (!(leftNode instanceof Buffer)) {
|
151 | if (rightNode instanceof Buffer) {
|
152 | break;
|
153 | }
|
154 | if (leftNode.length !== rightNode.length) {
|
155 | break;
|
156 | }
|
157 | let abort = false;
|
158 | for (let i = 0; i < leftNode.length; i++) {
|
159 | if (leftNode[i].compare(rightNode[i]) !== 0) {
|
160 | abort = true;
|
161 | break;
|
162 | }
|
163 | }
|
164 | if (abort) {
|
165 | break;
|
166 | }
|
167 | }
|
168 | else {
|
169 | if (!(rightNode instanceof Buffer)) {
|
170 | break;
|
171 | }
|
172 | if (leftNode.compare(rightNode) !== 0) {
|
173 | break;
|
174 | }
|
175 | }
|
176 |
|
177 | parent = node;
|
178 | node = await trie.lookupNode(leftNode);
|
179 | pos += 1;
|
180 | }
|
181 | else {
|
182 | throw new Error('invalid node');
|
183 | }
|
184 | }
|
185 |
|
186 | const saveStack = (key, stack) => {
|
187 | return trie._saveStack(key, stack, []);
|
188 | };
|
189 | if (node instanceof trieNode_1.ExtensionNode || node instanceof trieNode_1.LeafNode) {
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | const removeSelfFromParentAndSaveStack = async (key) => {
|
199 | if (parent === null) {
|
200 | return true;
|
201 | }
|
202 | stack.pop();
|
203 | parent.setBranch(key[pos - 1], null);
|
204 | await saveStack(key.slice(0, pos - 1), stack);
|
205 | return false;
|
206 | };
|
207 | if (shortForkLeft === -1 && shortForkRight === -1) {
|
208 | throw new Error('invalid range');
|
209 | }
|
210 | if (shortForkLeft === 1 && shortForkRight === 1) {
|
211 | throw new Error('invalid range');
|
212 | }
|
213 | if (shortForkLeft !== 0 && shortForkRight !== 0) {
|
214 |
|
215 | return await removeSelfFromParentAndSaveStack(left);
|
216 | }
|
217 |
|
218 | if (shortForkRight !== 0) {
|
219 | if (node instanceof trieNode_1.LeafNode) {
|
220 | return await removeSelfFromParentAndSaveStack(left);
|
221 | }
|
222 | const child = await trie.lookupNode(node._value);
|
223 | if (child && child instanceof trieNode_1.LeafNode) {
|
224 | return await removeSelfFromParentAndSaveStack(left);
|
225 | }
|
226 | const endPos = await unset(trie, node, child, left.slice(pos), node.keyLength, false, stack);
|
227 | await saveStack(left.slice(0, pos + endPos), stack);
|
228 | return false;
|
229 | }
|
230 |
|
231 | if (shortForkLeft !== 0) {
|
232 | if (node instanceof trieNode_1.LeafNode) {
|
233 | return await removeSelfFromParentAndSaveStack(right);
|
234 | }
|
235 | const child = await trie.lookupNode(node._value);
|
236 | if (child && child instanceof trieNode_1.LeafNode) {
|
237 | return await removeSelfFromParentAndSaveStack(right);
|
238 | }
|
239 | const endPos = await unset(trie, node, child, right.slice(pos), node.keyLength, true, stack);
|
240 | await saveStack(right.slice(0, pos + endPos), stack);
|
241 | return false;
|
242 | }
|
243 | return false;
|
244 | }
|
245 | else if (node instanceof trieNode_1.BranchNode) {
|
246 |
|
247 | for (let i = left[pos] + 1; i < right[pos]; i++) {
|
248 | node.setBranch(i, null);
|
249 | }
|
250 | {
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 | const _stack = [...stack];
|
257 | const next = node.getBranch(left[pos]);
|
258 | const child = next && (await trie.lookupNode(next));
|
259 | const endPos = await unset(trie, node, child, left.slice(pos), 1, false, _stack);
|
260 | await saveStack(left.slice(0, pos + endPos), _stack);
|
261 | }
|
262 | {
|
263 | const _stack = [...stack];
|
264 | const next = node.getBranch(right[pos]);
|
265 | const child = next && (await trie.lookupNode(next));
|
266 | const endPos = await unset(trie, node, child, right.slice(pos), 1, true, _stack);
|
267 | await saveStack(right.slice(0, pos + endPos), _stack);
|
268 | }
|
269 | return false;
|
270 | }
|
271 | else {
|
272 | throw new Error('invalid node');
|
273 | }
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | async function verifyProof(rootHash, key, proof) {
|
284 | let proofTrie = new baseTrie_1.Trie(null, rootHash);
|
285 | try {
|
286 | proofTrie = await baseTrie_1.Trie.fromProof(proof, proofTrie);
|
287 | }
|
288 | catch (e) {
|
289 | throw new Error('Invalid proof nodes given');
|
290 | }
|
291 | try {
|
292 | const value = await proofTrie.get(key, true);
|
293 | return {
|
294 | trie: proofTrie,
|
295 | value,
|
296 | };
|
297 | }
|
298 | catch (err) {
|
299 | if (err.message == 'Missing node in DB') {
|
300 | throw new Error('Invalid proof provided');
|
301 | }
|
302 | else {
|
303 | throw err;
|
304 | }
|
305 | }
|
306 | }
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | async function hasRightElement(trie, key) {
|
314 | let pos = 0;
|
315 | let node = await trie.lookupNode(trie.root);
|
316 | while (node !== null) {
|
317 | if (node instanceof trieNode_1.BranchNode) {
|
318 | for (let i = key[pos] + 1; i < 16; i++) {
|
319 | if (node.getBranch(i) !== null) {
|
320 | return true;
|
321 | }
|
322 | }
|
323 | const next = node.getBranch(key[pos]);
|
324 | node = next && (await trie.lookupNode(next));
|
325 | pos += 1;
|
326 | }
|
327 | else if (node instanceof trieNode_1.ExtensionNode) {
|
328 | if (key.length - pos < node.keyLength ||
|
329 | (0, nibbles_1.nibblesCompare)(node._nibbles, key.slice(pos, pos + node.keyLength)) !== 0) {
|
330 | return (0, nibbles_1.nibblesCompare)(node._nibbles, key.slice(pos)) > 0;
|
331 | }
|
332 | pos += node.keyLength;
|
333 | node = await trie.lookupNode(node._value);
|
334 | }
|
335 | else if (node instanceof trieNode_1.LeafNode) {
|
336 | return false;
|
337 | }
|
338 | else {
|
339 | throw new Error('invalid node');
|
340 | }
|
341 | }
|
342 | return false;
|
343 | }
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | async function verifyRangeProof(rootHash, firstKey, lastKey, keys, values, proof) {
|
373 | if (keys.length !== values.length) {
|
374 | throw new Error('invalid keys length or values length');
|
375 | }
|
376 |
|
377 | for (let i = 0; i < keys.length - 1; i++) {
|
378 | if ((0, nibbles_1.nibblesCompare)(keys[i], keys[i + 1]) >= 0) {
|
379 | throw new Error('invalid keys order');
|
380 | }
|
381 | }
|
382 |
|
383 | for (const value of values) {
|
384 | if (value.length === 0) {
|
385 | throw new Error('invalid values');
|
386 | }
|
387 | }
|
388 |
|
389 | if (proof === null && firstKey === null && lastKey === null) {
|
390 | const trie = new baseTrie_1.Trie();
|
391 | for (let i = 0; i < keys.length; i++) {
|
392 | await trie.put((0, nibbles_1.nibblesToBuffer)(keys[i]), values[i]);
|
393 | }
|
394 | if (rootHash.compare(trie.root) !== 0) {
|
395 | throw new Error('invalid all elements proof: root mismatch');
|
396 | }
|
397 | return false;
|
398 | }
|
399 | if (proof === null || firstKey === null || lastKey === null) {
|
400 | throw new Error('invalid all elements proof: proof, firstKey, lastKey must be null at the same time');
|
401 | }
|
402 |
|
403 | if (keys.length === 0) {
|
404 | const { trie, value } = await verifyProof(rootHash, (0, nibbles_1.nibblesToBuffer)(firstKey), proof);
|
405 | if (value !== null || (await hasRightElement(trie, firstKey))) {
|
406 | throw new Error('invalid zero element proof: value mismatch');
|
407 | }
|
408 | return false;
|
409 | }
|
410 |
|
411 | if (keys.length === 1 && (0, nibbles_1.nibblesCompare)(firstKey, lastKey) === 0) {
|
412 | const { trie, value } = await verifyProof(rootHash, (0, nibbles_1.nibblesToBuffer)(firstKey), proof);
|
413 | if ((0, nibbles_1.nibblesCompare)(firstKey, keys[0]) !== 0) {
|
414 | throw new Error('invalid one element proof: firstKey should be equal to keys[0]');
|
415 | }
|
416 | if (value === null || value.compare(values[0]) !== 0) {
|
417 | throw new Error('invalid one element proof: value mismatch');
|
418 | }
|
419 | return hasRightElement(trie, firstKey);
|
420 | }
|
421 |
|
422 | if ((0, nibbles_1.nibblesCompare)(firstKey, lastKey) >= 0) {
|
423 | throw new Error('invalid two edge elements proof: firstKey should be less than lastKey');
|
424 | }
|
425 | if (firstKey.length !== lastKey.length) {
|
426 | throw new Error('invalid two edge elements proof: the length of firstKey should be equal to the length of lastKey');
|
427 | }
|
428 | let trie = new baseTrie_1.Trie(null, rootHash);
|
429 | trie = await baseTrie_1.Trie.fromProof(proof, trie);
|
430 |
|
431 | const empty = await unsetInternal(trie, firstKey, lastKey);
|
432 | if (empty) {
|
433 | trie.root = trie.EMPTY_TRIE_ROOT;
|
434 | }
|
435 |
|
436 | for (let i = 0; i < keys.length; i++) {
|
437 | await trie.put((0, nibbles_1.nibblesToBuffer)(keys[i]), values[i]);
|
438 | }
|
439 |
|
440 | if (trie.root.compare(rootHash) !== 0) {
|
441 | throw new Error('invalid two edge elements proof: root mismatch');
|
442 | }
|
443 | return hasRightElement(trie, keys[keys.length - 1]);
|
444 | }
|
445 | exports.verifyRangeProof = verifyRangeProof;
|
446 |
|
\ | No newline at end of file |