/** * Removes a stale `nonce` from EVM tx payloads planned ahead of signing so the * signer can fetch a fresh nonce at broadcast time (multi-step Yield / LiFi). */ function unwrapSingleKeyTransactionObject( obj: Record, ): Record { const keys = Object.keys(obj) if (keys.length !== 1) { return obj } const firstKey = keys[0] const inner = obj[firstKey] // Type guard: only unwrap if inner value is a plain object if ( typeof inner === 'object' && inner !== null && !Array.isArray(inner) ) { return inner as Record } return obj } /** * If the payload is JSON (object or JSON string), drop `nonce` from the inner tx object. * Otherwise returns the input unchanged. */ export function stripStalePlanningNonceIfJsonObject( unsigned: string | Record, ): unknown { if ( typeof unsigned === 'object' && unsigned !== null && !Array.isArray(unsigned) ) { const spread = { ...unsigned } as Record const inner = unwrapSingleKeyTransactionObject(spread) const { nonce: _staleNonce, ...rest } = inner return rest } if (typeof unsigned === 'string') { try { const parsed = JSON.parse(unsigned) as unknown // Validate parsed result is a plain object if ( typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) ) { const inner = unwrapSingleKeyTransactionObject( parsed as Record, ) const { nonce: _staleNonce, ...rest } = inner return rest } } catch { return unsigned } } return unsigned }