forked from zmitton/eth-proof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAndVerify.js
64 lines (60 loc) · 3.78 KB
/
getAndVerify.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
const GetProof = require('./getProof')
const VerifyProof = require('./verifyProof')
const { toBuffer } = require('eth-util-lite')
class GetAndVerify{
constructor(rpcProvider = "https://mainnet.infura.io"){
this.get = new GetProof(rpcProvider)
}
async txAgainstBlockHash(txHash, trustedBlockHash){
let resp = await this.get.transactionProof(txHash)
let blockHashFromHeader = VerifyProof.getBlockHashFromHeader(resp.header)
if(!toBuffer(trustedBlockHash).equals(blockHashFromHeader)) throw new Error('BlockHash mismatch')
let txRootFromHeader = VerifyProof.getTxsRootFromHeader(resp.header)
let txRootFromProof = VerifyProof.getRootFromProof(resp.txProof)
if(!txRootFromHeader.equals(txRootFromProof)) throw new Error('TxRoot mismatch')
return VerifyProof.getTxFromTxProofAt(resp.txProof, resp.txIndex)
}
async receiptAgainstBlockHash(txHash, trustedBlockHash){
let resp = await this.get.receiptProof(txHash)
let blockHashFromHeader = VerifyProof.getBlockHashFromHeader(resp.header)
if(!toBuffer(trustedBlockHash).equals(blockHashFromHeader)) throw new Error('BlockHash mismatch')
let receiptsRoot = VerifyProof.getReceiptsRootFromHeader(resp.header)
let receiptsRootFromProof = VerifyProof.getRootFromProof(resp.receiptProof)
if(!receiptsRoot.equals(receiptsRootFromProof)) throw new Error('ReceiptsRoot mismatch')
return VerifyProof.getReceiptFromReceiptProofAt(resp.receiptProof, resp.txIndex)
}
async accountAgainstBlockHash(accountAddress, trustedBlockHash){
let resp = await this.get.accountProof(accountAddress, trustedBlockHash)
let blockHashFromHeader = VerifyProof.getBlockHashFromHeader(resp.header)
if(!toBuffer(trustedBlockHash).equals(blockHashFromHeader)) throw new Error('BlockHash mismatch')
let stateRoot = VerifyProof.getStateRootFromHeader(resp.header)
let stateRootFromProof = VerifyProof.getRootFromProof(resp.accountProof)
if(!stateRoot.equals(stateRootFromProof)) throw new Error('StateRoot mismatch')
return VerifyProof.getAccountFromProofAt(resp.accountProof, accountAddress)
}
async storageAgainstBlockHash(accountAddress, position, trustedBlockHash){
let resp = await this.get.storageProof(accountAddress, position, trustedBlockHash)
let blockHashFromHeader = VerifyProof.getBlockHashFromHeader(resp.header)
if(!toBuffer(trustedBlockHash).equals(blockHashFromHeader)) throw new Error('BlockHash mismatch')
let stateRoot = VerifyProof.getStateRootFromHeader(resp.header)
let stateRootFromProof = VerifyProof.getRootFromProof(resp.accountProof)
if(!stateRoot.equals(stateRootFromProof)) throw new Error('StateRoot mismatch')
let account = await VerifyProof.getAccountFromProofAt(resp.accountProof, accountAddress)
let storageRoot = VerifyProof.accountContainsStorageRoot(account)
let storageRootFromProof = VerifyProof.getRootFromProof(resp.storageProof)
if(!storageRoot.equals(storageRootFromProof)) throw new Error('StorageRoot mismatch')
return VerifyProof.getStorageFromStorageProofAt(resp.storageProof, position)
}
async _logAgainstBlockHash(txHash, indexOfLog, trustedBlockHash){
// untested as of yet
let resp = await this.get.receiptProof(txHash)
let blockHashFromHeader = VerifyProof.getBlockHashFromHeader(resp.header)
if(!toBuffer(trustedBlockHash).equals(blockHashFromHeader)) throw new Error('BlockHash mismatch')
let receiptsRoot = VerifyProof.getReceiptsRootFromHeader(resp.header)
let receiptsRootFromProof = VerifyProof.getRootFromProof(resp.receiptProof)
if(!receiptsRoot.equals(receiptsRootFromProof)) throw new Error('ReceiptsRoot mismatch')
let receipt = await VerifyProof.getReceiptFromReceiptProofAt(resp.receiptProof, resp.txIndex)
return VerifyProof.receiptContainsLogAt(receipt, indexOfLog)
}
}
module.exports = GetAndVerify