forked from zmitton/eth-proof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifyProof.js
211 lines (192 loc) · 8.08 KB
/
verifyProof.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
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
const sha3 = require('js-sha3').keccak_256
const rlp = require('rlp');
// public methods all prove commitment to a blockHash
// all args currently expect Buffers
//to do: they should all only take one proofNodes array merge header into parentNodes
VerifyProof = () => {}
VerifyProof.header = (header, blockHash) => {
try{
return Buffer.from(sha3(rlp.encode(header)),'hex').equals(blockHash);
}catch(e){ console.log(e) }
return false
}
VerifyProof.headerElement = (index, value, header, blockHash) => {
try{
if(value.equals(header[index])){
return Buffer.from(sha3(rlp.encode(header)),'hex').equals(blockHash);
}
}catch(e){ console.log(e) }
return false
}
VerifyProof.accountNonce = (address, nonce, parentNodes, header, blockHash) => {
return VerifyProof._accountElement(0, address, nonce, parentNodes, header, blockHash)
}
VerifyProof.balance = (address, balance, parentNodes, header, blockHash) => {
return VerifyProof._accountElement(1, address, balance, parentNodes, header, blockHash)
}
VerifyProof.storageRoot = (address, storageRoot, parentNodes, header, blockHash) => {
return VerifyProof._accountElement(2, address, storageRoot, parentNodes, header, blockHash)
}
VerifyProof.codeHash = (address, codeHash, parentNodes, header, blockHash) => {
return VerifyProof._accountElement(3, address, codeHash, parentNodes, header, blockHash)
}
VerifyProof.code = (address, code, parentNodes, header, blockHash) => {
try{
var account = valueFrom(parentNodes)
if(Buffer.from(sha3(code),'hex').equals(account[3])){
return VerifyProof.codeHash(address, account[3], parentNodes, header, blockHash)
}
}catch(e){ console.log(e) }
return false
}
VerifyProof._accountElement = (accountIndex, address, targetValue, parentNodes, header, blockHash) => {
try{
var account = valueFrom(parentNodes) // decoded last last
if(VerifyProof.account(address, account, parentNodes, header, blockHash)){
return account[accountIndex].equals(targetValue)
}
}catch(e){ console.log(e) }
return false
}
VerifyProof.storageAtIndex = (storageIndex, storageValue, storageParentNodes, address, accountParentNodes, header, blockHash) => {
try{
var storagePath = Buffer.from(sha3(Buffer.from(leftPad(storageIndex.toString('hex')),'hex')),'hex')
return VerifyProof.storage(storagePath, storageValue, storageParentNodes, address, accountParentNodes, header, blockHash)
}catch(e){ console.log(e) }
return false
}
// untested for multi dimensional mappings
VerifyProof.storageMapping = (storageIndex, mappings, storageValue, storageParentNodes, address, accountParentNodes, header, blockHash) => {
try{
var pathBuilder = Buffer.from(leftPad(storageIndex.toString('hex')),'hex')
for(var i = 0 ; i < mappings.length ; i++){
pathBuilder = Buffer.concat([Buffer.from(leftPad(mappings[i].toString('hex')),'hex'), pathBuilder])
}
pathBuilder = Buffer.from(sha3(pathBuilder),'hex')
var storagePath = Buffer.from(sha3(pathBuilder),'hex')
return VerifyProof.storage(storagePath, storageValue, storageParentNodes, address, accountParentNodes, header, blockHash)
}catch(e){ console.log(e) }
return false
}
VerifyProof.storage = (storagePath, storageValue, storageParentNodes, address, accountParentNodes, header, blockHash) => {
try{
var storageTrieRoot = valueFrom(accountParentNodes)[2]
if(VerifyProof.storageRoot(address, storageTrieRoot, accountParentNodes, header, blockHash)){
//account is already proven during `storageRoot`
return VerifyProof.trieValue(storagePath, storageValue, storageParentNodes, storageTrieRoot)
}
}catch(e){ console.log(e) }
return false
}
// VerifyProof.logBloom = () => {/* probably dont care. Arent these just for fast lookups? */}
// VerifyProof.postTransactionState = () => { }
// VerifyProof.cummulativeGas = () => { }
VerifyProof.log = (logIndex, txPath, log, parentNodes, header, blockHash) => {
try{
var receipt = valueFrom(parentNodes)
if(VerifyProof.receipt(txPath, receipt, parentNodes, header, blockHash)){
if(receipt[3][logIndex][0].equals(log[0]) && receipt[3][logIndex][2].equals(log[2])){
return receipt[3][logIndex][1].every((elem, i)=>{
return elem.equals(log[1][i])
})
}
}
}catch(e){ console.log(e) }
}
// VerifyProof._receiptElement = (receiptIndex, path, targetValue, parentNodes, header, blockHash) => {
// }
// account can be a contract
VerifyProof.account = (address, account, parentNodes, header, blockHash) => {
var sha3OfAddress = Buffer.from(sha3(address),'hex')
return VerifyProof._valueInTrieIndex(3, sha3OfAddress, account, parentNodes, header, blockHash)
}
VerifyProof.transaction = (path, tx, parentNodes, header, blockHash) => {
return VerifyProof._valueInTrieIndex(4, path, tx, parentNodes, header, blockHash)
}
VerifyProof.receipt = (path, receipt, parentNodes, header, blockHash) => {
return VerifyProof._valueInTrieIndex(5, path, receipt, parentNodes, header, blockHash)
}
VerifyProof._valueInTrieIndex = (trieIndex, path, value, parentNodes, header, blockHash) => {
try{
var trieRoot = header[trieIndex]
if(VerifyProof.headerElement(trieIndex, trieRoot, header, blockHash)){
return VerifyProof.trieValue(path, value, parentNodes, trieRoot)
}
}catch(e){ console.log(e) }
return false;
}
// proves commitment to its root only (not a blockHash). I should almost make this
// private although its very fundamental so i wont.
VerifyProof.trieValue = (path, value, parentNodes, root) => {
try{
var currentNode;
var len = parentNodes.length;
var rlpTxFromPrf = parentNodes[len - 1][parentNodes[len - 1].length - 1];
var nodeKey = root;
var pathPtr = 0;
path = path.toString('hex')
for (var i = 0 ; i < len ; i++) {
currentNode = parentNodes[i];
if(!nodeKey.equals( Buffer.from(sha3(rlp.encode(currentNode)),'hex'))){
console.log("nodeKey != sha3(rlp.encode(currentNode)): ", nodeKey, Buffer.from(sha3(rlp.encode(currentNode)),'hex'))
return false;
}
if(pathPtr > path.length){
console.log("pathPtr >= path.length ", pathPtr, path.length)
return false
}
switch(currentNode.length){
case 17:https://branch node
if(pathPtr == path.length){
if(currentNode[16] == rlp.encode(value)){
return true;
}else{
console.log('currentNode[16],rlp.encode(value): ', currentNode[16], rlp.encode(value))
return false
}
}
nodeKey = currentNode[parseInt(path[pathPtr],16)] //must == sha3(rlp.encode(currentNode[path[pathptr]]))
pathPtr += 1
// console.log(nodeKey, pathPtr, path[pathPtr])
break;
case 2:
// console.log(currentNode[0].toString('hex'), path, pathPtr)
pathPtr += nibblesToTraverse(currentNode[0].toString('hex'), path, pathPtr)
if(pathPtr == path.length){//leaf node
if(currentNode[1].equals(rlp.encode(value))){
return true
}else{
console.log("currentNode[1] == rlp.encode(value) ", currentNode[1], rlp.encode(value))
return false
}
}else{//extension node
nodeKey = currentNode[1]
}
break;
default:
console.log("all nodes must be length 17 or 2");
return false
}
}
}catch(e){ console.log(e); return false }
return false
}
var nibblesToTraverse = (encodedPartialPath, path, pathPtr) => {
if(encodedPartialPath[0] == 0 || encodedPartialPath[0] == 2){
var partialPath = encodedPartialPath.slice(2)
}else{
var partialPath = encodedPartialPath.slice(1)
}
if(partialPath == path.slice(pathPtr, pathPtr + partialPath.length)){
return partialPath.length
}else{
throw new Error("path was wrong")
}
}
var valueFrom = (parentNodes) => { // last last item decoded
return rlp.decode(parentNodes[parentNodes.length - 1][parentNodes[parentNodes.length - 1].length - 1])
}
var leftPad = (str) => {
return ("0000000000000000000000000000000000000000000000000000000000000000"+str).substring(str.length)
}
module.exports = VerifyProof