11import { assert , describe , it } from 'vitest'
22
3- import { bytesToBigInt , randomBytes } from '../src/bytes.js'
4- import {
5- CLRequestFactory ,
6- CLRequestType ,
7- createConsolidationRequest ,
8- createDepositRequest ,
9- createWithdrawalRequest ,
10- } from '../src/request.js'
3+ import { concatBytes , randomBytes } from '../src/bytes.js'
4+ import { CLRequestType , createCLRequest } from '../src/request.js'
115
12- import type {
13- CLRequest ,
14- ConsolidationRequest ,
15- DepositRequest ,
16- WithdrawalRequest ,
17- } from '../src/request.js'
6+ import type { CLRequest } from '../src/request.js'
187
198describe ( 'Requests' , ( ) => {
20- const testCases : [
21- string ,
22- any ,
23- CLRequestType ,
24- ( ...args : any ) => ConsolidationRequest | DepositRequest | WithdrawalRequest ,
25- ] [ ] = [
9+ const testCases : [ string , any , CLRequestType ] [ ] = [
2610 [
2711 'DepositRequest' ,
2812 {
2913 pubkey : randomBytes ( 48 ) ,
3014 withdrawalCredentials : randomBytes ( 32 ) ,
31- amount : bytesToBigInt ( randomBytes ( 8 ) ) ,
15+ amount : randomBytes ( 8 ) ,
3216 signature : randomBytes ( 96 ) ,
33- index : bytesToBigInt ( randomBytes ( 8 ) ) ,
17+ index : randomBytes ( 8 ) ,
3418 } ,
3519 CLRequestType . Deposit ,
36- createDepositRequest ,
3720 ] ,
3821 [
3922 'WithdrawalRequest' ,
4023 {
4124 sourceAddress : randomBytes ( 20 ) ,
4225 validatorPubkey : randomBytes ( 48 ) ,
43- amount : bytesToBigInt ( randomBytes ( 8 ) ) ,
26+ amount : randomBytes ( 8 ) ,
4427 } ,
4528 CLRequestType . Withdrawal ,
46- createWithdrawalRequest ,
4729 ] ,
4830 [
4931 'ConsolidationRequest' ,
@@ -53,22 +35,22 @@ describe('Requests', () => {
5335 targetPubkey : randomBytes ( 48 ) ,
5436 } ,
5537 CLRequestType . Consolidation ,
56- createConsolidationRequest ,
5738 ] ,
5839 ]
59- for ( const [ requestName , requestData , requestType , requestInstanceConstructor ] of testCases ) {
40+ for ( const [ requestName , requestData , requestType ] of testCases ) {
6041 it ( `${ requestName } ` , ( ) => {
61- const requestObject = requestInstanceConstructor ( requestData ) as CLRequest < CLRequestType >
62- const requestJSON = requestObject . toJSON ( )
63- const serialized = requestObject . serialize ( )
64- assert . equal ( serialized [ 0 ] , requestType )
42+ // flatten request bytes as per EIP-7685
43+ const depositRequestBytes = new Uint8Array (
44+ Object . values ( requestData )
45+ . map ( ( arr ) => Array . from ( arr ) ) // Convert Uint8Arrays to regular arrays
46+ . reduce ( ( acc , curr ) => acc . concat ( curr ) , [ ] ) , // Concatenate arrays
47+ )
48+ const requestObject = createCLRequest (
49+ concatBytes ( new Uint8Array ( [ requestType ] ) , depositRequestBytes ) ,
50+ ) as CLRequest < CLRequestType >
6551
66- const deserialized = CLRequestFactory . fromSerializedRequest ( serialized )
67- const deserializedJSON = deserialized . toJSON ( )
68- assert . deepEqual ( deserializedJSON , requestJSON )
69-
70- const reserialized = deserialized . serialize ( )
71- assert . deepEqual ( serialized , reserialized )
52+ assert . equal ( requestObject . type , requestType )
53+ assert . deepEqual ( requestObject . data , depositRequestBytes )
7254 } )
7355 }
7456} )
0 commit comments