@@ -9,6 +9,39 @@ import type { DelegateAction } from './delegate.js';
99import { DelegateActionPrefix } from './prefix.js' ;
1010import type { Signature } from './signature.js' ;
1111
12+ /**
13+ * Recursively converts numeric values to BigInt for u128/u64 fields in objects
14+ * and undefined to null for option types to ensure compatibility with zorsh serialization
15+ */
16+ function ensureBigInt ( obj : any ) : any {
17+ if ( obj === null || obj === undefined ) return obj ;
18+ if ( typeof obj === 'number' ) return BigInt ( obj ) ;
19+ if ( typeof obj === 'bigint' ) return obj ;
20+ if ( obj instanceof Uint8Array ) return obj ;
21+ if ( Array . isArray ( obj ) ) return obj . map ( ensureBigInt ) ;
22+ if ( typeof obj === 'object' ) {
23+ // Preserve the prototype chain for class instances
24+ const result : any = Object . create ( Object . getPrototypeOf ( obj ) ) ;
25+ for ( const [ key , value ] of Object . entries ( obj ) ) {
26+ // Convert numeric fields that should be BigInt
27+ if ( [ 'deposit' , 'stake' , 'gas' , 'nonce' , 'allowance' , 'maxBlockHeight' ] . includes ( key ) ) {
28+ if ( value === null || value === undefined ) {
29+ // zorsh's option type requires null, not undefined
30+ result [ key ] = value === undefined ? null : value ;
31+ } else if ( typeof value === 'number' || typeof value === 'bigint' ) {
32+ result [ key ] = BigInt ( value ) ;
33+ } else {
34+ result [ key ] = value ;
35+ }
36+ } else {
37+ result [ key ] = ensureBigInt ( value ) ;
38+ }
39+ }
40+ return result ;
41+ }
42+ return obj ;
43+ }
44+
1245/**
1346 * Borsh-encode a delegate action for inclusion as an action within a meta transaction
1447 * NB per NEP-461 this requires a Borsh-serialized prefix specific to delegate actions, ensuring
@@ -18,7 +51,7 @@ import type { Signature } from './signature.js';
1851export function encodeDelegateAction ( delegateAction : DelegateAction ) {
1952 return new Uint8Array ( [
2053 ...SCHEMA . DelegateActionPrefix . serialize ( new DelegateActionPrefix ( ) ) ,
21- ...SCHEMA . DelegateAction . serialize ( delegateAction as any ) ,
54+ ...SCHEMA . DelegateAction . serialize ( ensureBigInt ( delegateAction ) ) ,
2255 ] ) ;
2356}
2457
@@ -27,7 +60,7 @@ export function encodeDelegateAction(delegateAction: DelegateAction) {
2760 * @param signedDelegate Signed delegate to be executed in a meta transaction
2861 */
2962export function encodeSignedDelegate ( signedDelegate : SignedDelegate ) {
30- return SCHEMA . SignedDelegate . serialize ( signedDelegate as any ) ;
63+ return SCHEMA . SignedDelegate . serialize ( ensureBigInt ( signedDelegate ) ) ;
3164}
3265
3366/**
@@ -37,7 +70,7 @@ export function encodeSignedDelegate(signedDelegate: SignedDelegate) {
3770 */
3871export function encodeTransaction ( transaction : Transaction | SignedTransaction ) {
3972 const schema = 'signature' in transaction ? SCHEMA . SignedTransaction : SCHEMA . Transaction ;
40- return schema . serialize ( transaction as any ) ;
73+ return schema . serialize ( ensureBigInt ( transaction ) ) ;
4174}
4275
4376/**
0 commit comments