-
Notifications
You must be signed in to change notification settings - Fork 44
/
verifyProof.js
81 lines (69 loc) · 2.59 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
const { keccak, encode, decode, toBuffer, toWord } = require('eth-util-lite')
const Tree = require('merkle-patricia-tree')
const { Account, Receipt, Transaction } = require('eth-object')
const ACCOUNTS_ROOT_INDEX = 3 // within header
const TXS_ROOT_INDEX = 4 // within header
const RECEIPTS_ROOT_INDEX = 5 // within header
const STORAGE_ROOT_INDEX = 2 // within account
const SET_OF_LOGS_INDEX = 3 // within receipt
class Verify{
static chainContainsBlockHash(_chain, _blockHash){ throw new Error("Feature not yet available") }
static getRootFromProof(proof){ return keccak(encode(proof[0])) }
static accountContainsStorageRoot(account){
return account[STORAGE_ROOT_INDEX]
}
static getBlockHashFromHeader(header){
return keccak(encode(header))
}
static getElemFromHeaderAt(header, indexOfRoot){
return header[indexOfRoot]
}
static getStateRootFromHeader(header){
return this.getElemFromHeaderAt(header, ACCOUNTS_ROOT_INDEX)
}
static getTxsRootFromHeader(header){
return this.getElemFromHeaderAt(header, TXS_ROOT_INDEX)
}
static getReceiptsRootFromHeader(header){
return this.getElemFromHeaderAt(header, RECEIPTS_ROOT_INDEX)
}
static receiptContainsLogAt(receipt, indexOfLog){
return receipt[SET_OF_LOGS_INDEX][indexOfLog]
}
static async getStorageFromStorageProofAt(proof, position){
let fromProof = await this.proofContainsValueAt(proof, keccak(toWord(position)))
if(!fromProof || fromProof.equals(toBuffer())){
return toBuffer() // null returned as <buffer >
}else{
return decode(fromProof)
}
}
static async getAccountFromProofAt(proof, address){
let accountBuffer = await this.proofContainsValueAt(proof, keccak(address))
return Account.fromBuffer(accountBuffer) // null returned as Account.NULL
}
static async getTxFromTxProofAt(proof, indexOfTx){
let txBuffer = await this.proofContainsValueAt(proof, encode(indexOfTx))
return Transaction.fromBuffer(txBuffer)
}
static async getReceiptFromReceiptProofAt(proof, indexOfTx){
let receiptBuffer = await this.proofContainsValueAt(proof, encode(indexOfTx))
return Receipt.fromBuffer(receiptBuffer)
}
static async proofContainsValueAt(proof, path){
return new Promise((accept, reject) => {
let encodedProof = []
for (let i = 0; i < proof.length; i++) {
encodedProof.push(encode(proof[i]))
}
Tree.verifyProof(toBuffer(this.getRootFromProof(proof)) , path, encodedProof, (e,r)=>{
if(e){
return reject(e)
}else{
return accept(r)
}
})
})
}
}
module.exports = Verify