This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
proof.spec.ts
125 lines (95 loc) · 5.3 KB
/
proof.spec.ts
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
import tape from 'tape'
import { CheckpointTrie } from '../src'
tape('simple merkle proofs generation and verification', function (tester) {
const it = tester.test
it('create a merkle proof and verify it', async (t) => {
const trie = new CheckpointTrie()
await trie.put(Buffer.from('key1aa'), Buffer.from('0123456789012345678901234567890123456789xx'))
await trie.put(Buffer.from('key2bb'), Buffer.from('aval2'))
await trie.put(Buffer.from('key3cc'), Buffer.from('aval3'))
let proof = await CheckpointTrie.createProof(trie, Buffer.from('key2bb'))
let val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key2bb'), proof)
t.equal(val!.toString('utf8'), 'aval2')
proof = await CheckpointTrie.createProof(trie, Buffer.from('key1aa'))
val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key1aa'), proof)
t.equal(val!.toString('utf8'), '0123456789012345678901234567890123456789xx')
proof = await CheckpointTrie.createProof(trie, Buffer.from('key2bb'))
val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key2'), proof)
// 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')
let myKey = Buffer.from('anyrandomkey')
proof = await CheckpointTrie.createProof(trie, myKey)
val = await CheckpointTrie.verifyProof(trie.root, myKey, proof)
t.equal(val, null, 'Expected value to be null')
myKey = Buffer.from('anothergarbagekey') // should generate a valid proof of null
proof = await CheckpointTrie.createProof(trie, myKey)
proof.push(Buffer.from('123456')) // extra nodes are just ignored
val = await CheckpointTrie.verifyProof(trie.root, myKey, proof)
t.equal(val, null, 'Expected value to be null')
await trie.put(Buffer.from('another'), Buffer.from('3498h4riuhgwe'))
// to fail our proof we can request a proof for one key
proof = await CheckpointTrie.createProof(trie, Buffer.from('another'))
// and use that proof on another key
const result = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key1aa'), proof)
t.equal(result, null)
t.end()
})
it('create a merkle proof and verify it with a single long key', async (t) => {
const trie = new CheckpointTrie()
await trie.put(Buffer.from('key1aa'), Buffer.from('0123456789012345678901234567890123456789xx'))
const proof = await CheckpointTrie.createProof(trie, Buffer.from('key1aa'))
const val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key1aa'), proof)
t.equal(val!.toString('utf8'), '0123456789012345678901234567890123456789xx')
t.end()
})
it('create a merkle proof and verify it with a single short key', async (t) => {
const trie = new CheckpointTrie()
await trie.put(Buffer.from('key1aa'), Buffer.from('01234'))
const proof = await CheckpointTrie.createProof(trie, Buffer.from('key1aa'))
const val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key1aa'), proof)
t.equal(val!.toString('utf8'), '01234')
t.end()
})
it('create a merkle proof and verify it whit keys in the middle', async (t) => {
const trie = new CheckpointTrie()
await trie.put(
Buffer.from('key1aa'),
Buffer.from('0123456789012345678901234567890123456789xxx')
)
await trie.put(
Buffer.from('key1'),
Buffer.from('0123456789012345678901234567890123456789Very_Long')
)
await trie.put(Buffer.from('key2bb'), Buffer.from('aval3'))
await trie.put(Buffer.from('key2'), Buffer.from('short'))
await trie.put(Buffer.from('key3cc'), Buffer.from('aval3'))
await trie.put(Buffer.from('key3'), Buffer.from('1234567890123456789012345678901'))
let proof = await CheckpointTrie.createProof(trie, Buffer.from('key1'))
let val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key1'), proof)
t.equal(val!.toString('utf8'), '0123456789012345678901234567890123456789Very_Long')
proof = await CheckpointTrie.createProof(trie, Buffer.from('key2'))
val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key2'), proof)
t.equal(val!.toString('utf8'), 'short')
proof = await CheckpointTrie.createProof(trie, Buffer.from('key3'))
val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('key3'), proof)
t.equal(val!.toString('utf8'), '1234567890123456789012345678901')
t.end()
})
it('should succeed with a simple embedded extension-branch', async (t) => {
const trie = new CheckpointTrie()
await trie.put(Buffer.from('a'), Buffer.from('a'))
await trie.put(Buffer.from('b'), Buffer.from('b'))
await trie.put(Buffer.from('c'), Buffer.from('c'))
let proof = await CheckpointTrie.createProof(trie, Buffer.from('a'))
let val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('a'), proof)
t.equal(val!.toString('utf8'), 'a')
proof = await CheckpointTrie.createProof(trie, Buffer.from('b'))
val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('b'), proof)
t.equal(val!.toString('utf8'), 'b')
proof = await CheckpointTrie.createProof(trie, Buffer.from('c'))
val = await CheckpointTrie.verifyProof(trie.root, Buffer.from('c'), proof)
t.equal(val!.toString('utf8'), 'c')
t.end()
})
})