import { ICosmosBalance , ITxMsg , ITxValue } from "@anthem/utils" ; /** ===========================================================================
 * Types & Config
 * ============================================================================
 */ interface SignBytesInput { fee : { gas : string ; amount : Maybe < readonly ICosmosBalance [ ] > ; } ; memo : string ; msgs : Maybe < readonly ITxMsg [ ] > ; sequence : string ; account_number : string ; chain_id : string ; } interface JsonTransaction { txMsg : ITxValue ; txRequestMetadata : any ; } /** ===========================================================================
 * Ledger Utils
 * ----------------------------------------------------------------------------
 * Utils for interacting with a Ledger Device.
 * ============================================================================
 */ /**
 * Transactions often have amino decoded objects in them `{type, value}`.
 * We need to strip this clutter as we need to sign only the values.
 *
 * TODO: Add note about fixed order.
 */ const prepareSignBytes = ( jsonTransaction ) : any => { if ( Array . isArray ( jsonTransaction ) ) { return jsonTransaction . map ( prepareSignBytes ) ; } if ( typeof jsonTransaction !== "object" ) { return jsonTransaction ; } const sorted = { } ; Object . keys ( jsonTransaction ) . sort ( ) . forEach ( key => { if ( jsonTransaction [ key ] === undefined || jsonTransaction [ key ] === null ) { return ; } sorted [ key ] = prepareSignBytes ( jsonTransaction [ key ] ) ; } ) ; return sorted ; } ; /**
 * The SDK expects a certain message format to serialize and then sign:
 *
 * TODO: types.
 *
 * type StdSignMsg struct {
 *   ChainID       string      `json:"chain_id"`
 *   AccountNumber uint64      `json:"account_number"`
 *   Sequence      uint64      `json:"sequence"`
 *   Fee           auth.StdFee `json:"fee"`
 *   Msgs          []sdk.Msg   `json:"msgs"`
 *   Memo          string      `json:"memo"`
 * }
 */ export const createSignMessage = ( jsonTransaction ) => { const { txMsg , txRequestMetadata } = jsonTransaction ; const fee = { amount : txMsg . fee . amount || [ ] , gas : txMsg . fee . gas , } ; return JSON . stringify ( prepareSignBytes ( { fee , memo : txMsg . memo , msgs : txMsg . msg , sequence : txRequestMetadata . sequence , account_number : txRequestMetadata . account_number , chain_id : txRequestMetadata . chain_id , } ) ) ; } ;