@@ -17,11 +17,10 @@ import { assert, describe, it } from 'vitest'
17
17
18
18
import { MerkleStateManager } from '../src/index.js'
19
19
20
- import * as ropsten_contractWithStorage from './testdata/ropsten_contractWithStorage.json '
21
- import * as ropsten_nonexistentAccount from './testdata/ropsten_nonexistentAccount.json '
22
- import * as ropsten_validAccount from './testdata/ropsten_validAccount.json '
20
+ import { ropstenContractWithStorageData } from './testdata/ropsten_contractWithStorage.js '
21
+ import { ropstenNonexistentAccountData } from './testdata/ropsten_nonexistentAccount.js '
22
+ import { ropstenValidAccountData } from './testdata/ropsten_validAccount.js '
23
23
24
- import type { Proof } from '@ethereumjs/common'
25
24
import type { PrefixedHexString } from '@ethereumjs/util'
26
25
27
26
describe ( 'ProofStateManager' , ( ) => {
@@ -120,8 +119,8 @@ describe('ProofStateManager', () => {
120
119
const stateManager = new MerkleStateManager ( { trie } )
121
120
// Dump all the account proof data in the DB
122
121
let stateRoot : Uint8Array | undefined
123
- for ( const proofData of ropsten_validAccount . accountProof ) {
124
- const bufferData = hexToBytes ( proofData as PrefixedHexString )
122
+ for ( const proofData of ropstenValidAccountData . accountProof ) {
123
+ const bufferData = hexToBytes ( proofData )
125
124
const key = keccak256 ( bufferData )
126
125
if ( stateRoot === undefined ) {
127
126
stateRoot = key
@@ -130,8 +129,8 @@ describe('ProofStateManager', () => {
130
129
}
131
130
trie . root ( stateRoot ! )
132
131
const proof = await stateManager . getProof ( address )
133
- assert . deepEqual ( ( ropsten_validAccount as any ) . default , proof )
134
- assert . ok ( await stateManager . verifyProof ( ( ropsten_validAccount as any ) . default ) )
132
+ assert . deepEqual ( ropstenValidAccountData , proof )
133
+ assert . ok ( await stateManager . verifyProof ( ropstenValidAccountData ) )
135
134
} )
136
135
137
136
it ( 'should report data equal to geth output for EIP 1178 proofs - nonexistent account' , async ( ) => {
@@ -144,8 +143,8 @@ describe('ProofStateManager', () => {
144
143
const stateManager = new MerkleStateManager ( { trie } )
145
144
// Dump all the account proof data in the DB
146
145
let stateRoot : Uint8Array | undefined
147
- for ( const proofData of ropsten_nonexistentAccount . accountProof ) {
148
- const bufferData = hexToBytes ( proofData as PrefixedHexString )
146
+ for ( const proofData of ropstenNonexistentAccountData . accountProof ) {
147
+ const bufferData = hexToBytes ( proofData )
149
148
const key = keccak256 ( bufferData )
150
149
if ( stateRoot === undefined ) {
151
150
stateRoot = key
@@ -154,8 +153,8 @@ describe('ProofStateManager', () => {
154
153
}
155
154
trie . root ( stateRoot ! )
156
155
const proof = await stateManager . getProof ( address )
157
- assert . deepEqual ( ( ropsten_nonexistentAccount as any ) . default , proof )
158
- assert . ok ( await stateManager . verifyProof ( ropsten_nonexistentAccount as Proof ) )
156
+ assert . deepEqual ( ropstenNonexistentAccountData , proof )
157
+ assert . ok ( await stateManager . verifyProof ( ropstenNonexistentAccountData ) )
159
158
} )
160
159
161
160
it ( 'should report data equal to geth output for EIP 1178 proofs - account with storage' , async ( ) => {
@@ -168,22 +167,22 @@ describe('ProofStateManager', () => {
168
167
const stateManager = new MerkleStateManager ( { trie } )
169
168
// Dump all the account proof data in the DB
170
169
let stateRoot : Uint8Array | undefined
171
- for ( const proofData of ropsten_contractWithStorage . accountProof ) {
172
- const bufferData = hexToBytes ( proofData as PrefixedHexString )
170
+ for ( const proofData of ropstenContractWithStorageData . accountProof ) {
171
+ const bufferData = hexToBytes ( proofData )
173
172
const key = keccak256 ( bufferData )
174
173
if ( stateRoot === undefined ) {
175
174
stateRoot = key
176
175
}
177
176
await trie [ '_db' ] . put ( key , bufferData )
178
177
}
179
- const storageRoot = ropsten_contractWithStorage . storageHash as PrefixedHexString
178
+ const storageRoot = ropstenContractWithStorageData . storageHash
180
179
const storageTrie = new Trie ( { useKeyHashing : true } )
181
180
const storageKeys : Uint8Array [ ] = [ ]
182
- for ( const storageProofsData of ropsten_contractWithStorage . storageProof ) {
183
- storageKeys . push ( hexToBytes ( storageProofsData . key as PrefixedHexString ) )
181
+ for ( const storageProofsData of ropstenContractWithStorageData . storageProof ) {
182
+ storageKeys . push ( hexToBytes ( storageProofsData . key ) )
184
183
for ( const storageProofData of storageProofsData . proof ) {
185
- const key = keccak256 ( hexToBytes ( storageProofData as PrefixedHexString ) )
186
- await storageTrie [ '_db' ] . put ( key , hexToBytes ( storageProofData as PrefixedHexString ) )
184
+ const key = keccak256 ( hexToBytes ( storageProofData ) )
185
+ await storageTrie [ '_db' ] . put ( key , hexToBytes ( storageProofData ) )
187
186
}
188
187
}
189
188
storageTrie . root ( hexToBytes ( storageRoot ) )
@@ -192,8 +191,8 @@ describe('ProofStateManager', () => {
192
191
trie . root ( stateRoot ! )
193
192
194
193
const proof = await stateManager . getProof ( address , storageKeys )
195
- assert . deepEqual ( ( ropsten_contractWithStorage as any ) . default , proof )
196
- await stateManager . verifyProof ( ropsten_contractWithStorage as Proof )
194
+ assert . deepEqual ( ropstenContractWithStorageData , proof )
195
+ await stateManager . verifyProof ( ropstenContractWithStorageData )
197
196
} )
198
197
199
198
it ( `should throw on invalid proofs - existing accounts/slots` , async ( ) => {
@@ -206,22 +205,22 @@ describe('ProofStateManager', () => {
206
205
const stateManager = new MerkleStateManager ( { trie } )
207
206
// Dump all the account proof data in the DB
208
207
let stateRoot : Uint8Array | undefined
209
- for ( const proofData of ropsten_contractWithStorage . accountProof ) {
210
- const bufferData = hexToBytes ( proofData as PrefixedHexString )
208
+ for ( const proofData of ropstenContractWithStorageData . accountProof ) {
209
+ const bufferData = hexToBytes ( proofData )
211
210
const key = keccak256 ( bufferData )
212
211
if ( stateRoot === undefined ) {
213
212
stateRoot = key
214
213
}
215
214
await trie [ '_db' ] . put ( key , bufferData )
216
215
}
217
- const storageRoot = ropsten_contractWithStorage . storageHash as PrefixedHexString
216
+ const storageRoot = ropstenContractWithStorageData . storageHash
218
217
const storageTrie = new Trie ( { useKeyHashing : true } )
219
218
const storageKeys : Uint8Array [ ] = [ ]
220
- for ( const storageProofsData of ropsten_contractWithStorage . storageProof ) {
221
- storageKeys . push ( hexToBytes ( storageProofsData . key as PrefixedHexString ) )
219
+ for ( const storageProofsData of ropstenContractWithStorageData . storageProof ) {
220
+ storageKeys . push ( hexToBytes ( storageProofsData . key ) )
222
221
for ( const storageProofData of storageProofsData . proof ) {
223
- const key = keccak256 ( hexToBytes ( storageProofData as PrefixedHexString ) )
224
- await storageTrie [ '_db' ] . put ( key , hexToBytes ( storageProofData as PrefixedHexString ) )
222
+ const key = keccak256 ( hexToBytes ( storageProofData ) )
223
+ await storageTrie [ '_db' ] . put ( key , hexToBytes ( storageProofData ) )
225
224
}
226
225
}
227
226
storageTrie . root ( hexToBytes ( storageRoot ) )
@@ -230,29 +229,29 @@ describe('ProofStateManager', () => {
230
229
trie . root ( stateRoot ! )
231
230
232
231
// tamper with account data
233
- const testdata = { ...( ropsten_contractWithStorage as any ) }
232
+ const testData = { ...ropstenContractWithStorageData }
234
233
for ( const tamper of [ 'nonce' , 'balance' , 'codeHash' , 'storageHash' ] ) {
235
- const original = testdata [ tamper ]
234
+ const original = testData [ tamper as keyof typeof testData ] as PrefixedHexString
236
235
try {
237
- const newField = `0x9 ${ original . slice ( 3 ) } `
238
- testdata [ tamper ] = newField
239
- await stateManager . verifyProof ( testdata )
236
+ ; ( testData [ tamper as keyof typeof testData ] as PrefixedHexString ) =
237
+ `0x9 ${ original . slice ( 3 ) } `
238
+ await stateManager . verifyProof ( testData )
240
239
// note: this implicitly means that newField !== original,
241
240
// if newField === original then the proof would be valid and test would fail
242
241
assert . fail ( 'should throw' )
243
242
} catch ( e ) {
244
243
assert . ok ( true , 'threw on invalid proof' )
245
244
} finally {
246
- testdata [ tamper ] = original
245
+ ; ( testData [ tamper as keyof typeof testData ] as PrefixedHexString ) = original
247
246
}
248
247
}
249
248
250
249
// tamper with storage slots
251
- for ( const slot of testdata . storageProof ) {
250
+ for ( const slot of testData . storageProof ) {
252
251
const original = slot . value
253
252
slot . value = `0x9${ original . slice ( 3 ) } `
254
253
try {
255
- await stateManager . verifyProof ( testdata )
254
+ await stateManager . verifyProof ( testData )
256
255
assert . fail ( 'should throw' )
257
256
} catch {
258
257
assert . ok ( true , 'threw on invalid proof' )
@@ -272,23 +271,23 @@ describe('ProofStateManager', () => {
272
271
const stateManager = new MerkleStateManager ( { trie } )
273
272
// Dump all the account proof data in the DB
274
273
let stateRoot : Uint8Array | undefined
275
- for ( const proofData of ropsten_nonexistentAccount . accountProof ) {
276
- const bufferData = hexToBytes ( proofData as PrefixedHexString )
274
+ for ( const proofData of ropstenNonexistentAccountData . accountProof ) {
275
+ const bufferData = hexToBytes ( proofData )
277
276
const key = keccak256 ( bufferData )
278
277
if ( stateRoot === undefined ) {
279
278
stateRoot = key
280
279
}
281
280
await trie [ '_db' ] . put ( key , bufferData )
282
281
}
283
- const storageRoot = ropsten_nonexistentAccount . storageHash as PrefixedHexString
282
+ const storageRoot = ropstenNonexistentAccountData . storageHash
284
283
const storageTrie = new Trie ( { useKeyHashing : true } )
285
284
storageTrie . root ( hexToBytes ( storageRoot ) )
286
285
const addressHex = bytesToHex ( address . bytes )
287
286
stateManager [ '_storageTries' ] [ addressHex ] = storageTrie
288
287
trie . root ( stateRoot ! )
289
288
290
289
// tamper with account data
291
- const testdata = { ...( ropsten_nonexistentAccount as any ) }
290
+ const testdata = { ...( ropstenNonexistentAccountData as any ) }
292
291
for ( const tamper of [ 'nonce' , 'balance' , 'codeHash' , 'storageHash' ] ) {
293
292
const original = testdata [ tamper ]
294
293
try {
0 commit comments