11/* eslint-disable @typescript-eslint/no-explicit-any */
22import { Network } from '@0xsequence/wallet-primitives'
33import { Bytes , Hex } from 'ox'
4- import { NODES_URL , RELAYER_URL } from './constants.js'
54
65type JsonReplacer = ( key : string , value : any ) => any
76type JsonReviver = ( key : string , value : any ) => any
@@ -36,10 +35,23 @@ function chainRevivers(revivers: JsonReviver[]): JsonReviver {
3635 }
3736}
3837
38+ /**
39+ * A JSON replacer that serializes Map objects into a structured object.
40+ */
41+ const mapReplacer : JsonReplacer = ( key , value ) => {
42+ if ( value instanceof Map ) {
43+ return {
44+ _isMap : true ,
45+ data : Array . from ( value . entries ( ) ) ,
46+ }
47+ }
48+ return value
49+ }
50+
3951/**
4052 * A JSON replacer that serializes BigInt values into a structured object.
4153 */
42- const bigIntReplacer : JsonReplacer = ( _key , value ) => {
54+ const bigIntReplacer : JsonReplacer = ( key , value ) => {
4355 if ( typeof value === 'bigint' ) {
4456 return {
4557 _isBigInt : true ,
@@ -52,7 +64,7 @@ const bigIntReplacer: JsonReplacer = (_key, value) => {
5264/**
5365 * A JSON replacer that serializes Uint8Array values into a structured object.
5466 */
55- const uint8ArrayReplacer : JsonReplacer = ( _key , value ) => {
67+ const uint8ArrayReplacer : JsonReplacer = ( key , value ) => {
5668 if ( value instanceof Uint8Array ) {
5769 return {
5870 _isUint8Array : true ,
@@ -62,6 +74,23 @@ const uint8ArrayReplacer: JsonReplacer = (_key, value) => {
6274 return value
6375}
6476
77+ /**
78+ * A JSON reviver that deserializes a structured object back into a Map.
79+ */
80+ const mapReviver : JsonReviver = ( key , value ) => {
81+ if ( value !== null && typeof value === 'object' && value . _isMap === true && Array . isArray ( value . data ) ) {
82+ try {
83+ // The key-value pairs in value.data will have already been processed
84+ // by other revivers in the chain because JSON.parse works bottom-up.
85+ return new Map ( value . data )
86+ } catch ( e ) {
87+ console . error ( `Failed to revive Map for key "${ key } ":` , e )
88+ return value // Return original object if revival fails
89+ }
90+ }
91+ return value
92+ }
93+
6594/**
6695 * A JSON reviver that deserializes a structured object back into a BigInt.
6796 */
@@ -92,9 +121,8 @@ const uint8ArrayReviver: JsonReviver = (key, value) => {
92121 return value
93122}
94123
95- export const jsonReplacers = chainReplacers ( [ bigIntReplacer , uint8ArrayReplacer ] )
96-
97- export const jsonRevivers = chainRevivers ( [ bigIntReviver , uint8ArrayReviver ] )
124+ export const jsonRevivers = chainRevivers ( [ mapReviver , bigIntReviver , uint8ArrayReviver ] )
125+ export const jsonReplacers = chainReplacers ( [ mapReplacer , bigIntReplacer , uint8ArrayReplacer ] )
98126
99127/**
100128 * Apply a template to a string.
0 commit comments