import { ProtoSignDocDecoder } from "../decoder" ; import { Coin , StdSignDoc } from "@cosmjs/launchpad" ; import { cosmos } from "../proto" ; import { checkAndValidateADR36AminoSignDoc } from "../../adr-36" ; export class SignDocWrapper { protected _protoSignDoc ? : ProtoSignDocDecoder ; public readonly isADR36SignDoc : boolean ; public readonly mode : "amino" | "direct" ; constructor ( protected readonly signDoc ) { if ( "msgs" in signDoc ) { this . mode = "amino" ; } else { this . mode = "direct" ; } if ( this . mode === "amino" ) { this . isADR36SignDoc = checkAndValidateADR36AminoSignDoc ( this . aminoSignDoc ) ; } else { this . isADR36SignDoc = false ; } } static fromAminoSignDoc ( signDoc ) { return new SignDocWrapper ( signDoc ) ; } static fromDirectSignDoc ( signDoc ) { return new SignDocWrapper ( signDoc ) ; } static fromDirectSignDocBytes ( signDocBytes ) { return new SignDocWrapper ( cosmos . tx . v1beta1 . SignDoc . decode ( signDocBytes ) ) ; } clone ( ) { return new SignDocWrapper ( this . signDoc ) ; } get protoSignDoc ( ) : ProtoSignDocDecoder { if ( this . mode === "amino" ) { throw new Error ( "Sign doc is encoded as Amino Json" ) ; } if ( "msgs" in this . signDoc ) { throw new Error ( "Unexpected error" ) ; } if ( ! this . _protoSignDoc ) { this . _protoSignDoc = new ProtoSignDocDecoder ( this . signDoc ) ; } return this . _protoSignDoc ; } get aminoSignDoc ( ) : StdSignDoc { if ( this . mode === "direct" ) { throw new Error ( "Sign doc is encoded as Protobuf" ) ; } if ( ! ( "msgs" in this . signDoc ) ) { throw new Error ( "Unexpected error" ) ; } return this . signDoc ; } get chainId ( ) : string { if ( this . mode === "direct" ) { return this . protoSignDoc . chainId ; } return this . aminoSignDoc . chain_id ; } get memo ( ) : string { if ( this . mode === "direct" ) { return this . protoSignDoc . txBody . memo ; } return this . aminoSignDoc . memo ; } get fees ( ) : readonly Coin [ ] { if ( this . mode === "direct" ) { const fees = [ ] ; for ( const coinObj of this . protoSignDoc . authInfo . fee ?. amount ?? [ ] ) { if ( coinObj . denom == null || coinObj . amount == null ) { throw new Error ( "Invalid fee" ) ; } fees . push ( { denom : coinObj . denom , amount : coinObj . amount , } ) ; } return fees ; } return this . aminoSignDoc . fee . amount ; } get gas ( ) : number { if ( this . mode === "direct" ) { return this . protoSignDoc . authInfo . fee ?. gasLimit ?. toNumber ( ) ?? 0 ; } return parseInt ( this . aminoSignDoc . fee . gas ) ; } }