@@ -2,7 +2,9 @@ import { createBlock } from '@ethereumjs/block'
22import { Common , Hardfork , Mainnet , getPresetChainConfig } from '@ethereumjs/common'
33import { createTx } from '@ethereumjs/tx'
44import {
5+ bytesToBigInt ,
56 bytesToHex ,
7+ bytesToInt ,
68 createAccount ,
79 createAddressFromPrivateKey ,
810 createAddressFromString ,
@@ -12,10 +14,10 @@ import {
1214import { keccak256 } from 'ethereum-cryptography/keccak.js'
1315import { assert , describe , it } from 'vitest'
1416
17+ import { CLRequestType } from '../../../../util/src/request.js'
1518import { buildBlock , runBlock } from '../../../src/index.js'
1619import { setupVM } from '../utils.js'
1720
18- import type { DepositRequest } from '../../../../util/src/request.js'
1921import type { PrefixedHexString } from '@ethereumjs/util'
2022
2123const depositContractByteCode = hexToBytes (
@@ -64,8 +66,10 @@ describe('EIP-6110 runBlock tests', () => {
6466 )
6567 const res = await runBlock ( vm , { block, generate : true , skipBlockValidation : true } )
6668 assert . equal ( res . requests ?. length , 1 )
67- const reqPubkey = ( res . requests ! [ 0 ] as DepositRequest ) . pubkey
68- assert . equal ( bytesToHex ( reqPubkey ) , pubkey )
69+ const depositRequest = res . requests ! [ 0 ]
70+ assert . equal ( depositRequest . type , CLRequestType . Deposit )
71+ const parsedRequest = parseDepositRequest ( depositRequest . data )
72+ assert . equal ( bytesToHex ( parsedRequest . pubkey ) , pubkey )
6973 } )
7074} )
7175
@@ -95,7 +99,75 @@ describe('EIP-7685 buildBlock tests', () => {
9599 await blockBuilder . addTransaction ( depositTx )
96100 const res = await blockBuilder . build ( )
97101 assert . equal ( res . requests ?. length , 1 )
98- const reqPubkey = ( res . requests ! [ 0 ] as DepositRequest ) . pubkey
99- assert . equal ( bytesToHex ( reqPubkey ) , pubkey )
102+
103+ const depositRequest = res . requests ! [ 0 ]
104+ assert . equal ( depositRequest . type , CLRequestType . Deposit )
105+ const parsedRequest = parseDepositRequest ( depositRequest . data )
106+ assert . equal ( bytesToHex ( parsedRequest . pubkey ) , pubkey )
100107 } )
101108} )
109+
110+ function parseDepositRequest ( requestData : Uint8Array ) {
111+ // Extracts validator pubkey, withdrawal credential, deposit amount, signature,
112+ // and validator index from Deposit Event log.
113+ // The event fields are non-indexed so contained in one byte array (log[2]) so parsing is as follows:
114+ // 1. Read the first 32 bytes to get the starting position of the first field.
115+ // 2. Continue reading the byte array in 32 byte increments to get all the field starting positions
116+ // 3. Read 32 bytes starting with the first field position to get the size of the first field
117+ // 4. Read the bytes from first field position + 32 + the size of the first field to get the first field value
118+ // 5. Repeat steps 3-4 for each field
119+ const pubKeyIdx = bytesToInt ( requestData . slice ( 0 , 32 ) )
120+ const pubKeySize = bytesToInt ( requestData . slice ( pubKeyIdx , pubKeyIdx + 32 ) )
121+ const withdrawalCreditsIdx = bytesToInt ( requestData . slice ( 32 , 64 ) )
122+ const withdrawalCreditsSize = bytesToInt (
123+ requestData . slice ( withdrawalCreditsIdx , withdrawalCreditsIdx + 32 ) ,
124+ )
125+ const amountIdx = bytesToInt ( requestData . slice ( 64 , 96 ) )
126+ const amountSize = bytesToInt ( requestData . slice ( amountIdx , amountIdx + 32 ) )
127+ const sigIdx = bytesToInt ( requestData . slice ( 96 , 128 ) )
128+ const sigSize = bytesToInt ( requestData . slice ( sigIdx , sigIdx + 32 ) )
129+ const indexIdx = bytesToInt ( requestData . slice ( 128 , 160 ) )
130+ const indexSize = bytesToInt ( requestData . slice ( indexIdx , indexIdx + 32 ) )
131+ const pubkey = requestData . slice ( pubKeyIdx + 32 , pubKeyIdx + 32 + pubKeySize )
132+ const withdrawalCredentials = requestData . slice (
133+ withdrawalCreditsIdx + 32 ,
134+ withdrawalCreditsIdx + 32 + withdrawalCreditsSize ,
135+ )
136+ const amountBytes = requestData . slice ( amountIdx + 32 , amountIdx + 32 + amountSize )
137+ const amountBytesBigEndian = new Uint8Array ( [
138+ amountBytes [ 7 ] ,
139+ amountBytes [ 6 ] ,
140+ amountBytes [ 5 ] ,
141+ amountBytes [ 4 ] ,
142+ amountBytes [ 3 ] ,
143+ amountBytes [ 2 ] ,
144+ amountBytes [ 1 ] ,
145+ amountBytes [ 0 ] ,
146+ ] )
147+ const amount = bytesToBigInt ( amountBytesBigEndian )
148+
149+ const signature = requestData . slice ( sigIdx + 32 , sigIdx + 32 + sigSize )
150+
151+ const indexBytes = requestData . slice ( indexIdx + 32 , indexIdx + 32 + indexSize )
152+
153+ // Convert the little-endian array to big-endian array
154+ const indexBytesBigEndian = new Uint8Array ( [
155+ indexBytes [ 7 ] ,
156+ indexBytes [ 6 ] ,
157+ indexBytes [ 5 ] ,
158+ indexBytes [ 4 ] ,
159+ indexBytes [ 3 ] ,
160+ indexBytes [ 2 ] ,
161+ indexBytes [ 1 ] ,
162+ indexBytes [ 0 ] ,
163+ ] )
164+ const index = bytesToBigInt ( indexBytesBigEndian )
165+
166+ return {
167+ pubkey,
168+ withdrawalCredentials,
169+ amount,
170+ signature,
171+ index,
172+ }
173+ }
0 commit comments