import { Message } from "@keplr-wallet/router" ; import { ROUTE } from "./constants" ; import { KeyRing , KeyRingStatus , MultiKeyStoreInfoWithSelected , } from "./keyring" ; import { BIP44HDPath , ExportKeyRingData } from "./types" ; import { Bech32Address , checkAndValidateADR36AminoSignDoc , } from "@keplr-wallet/cosmos" ; import { BIP44 , KeplrSignOptions , Key } from "@keplr-wallet/types" ; import { StdSignDoc , AminoSignResponse , StdSignature } from "@cosmjs/launchpad" ; const bip39 = require ( "bip39" ) ; import { cosmos } from "@keplr-wallet/cosmos" ; import Long from "long" ; export class RestoreKeyRingMsg extends Message < { status : KeyRingStatus ; multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "restore-keyring" ; } constructor ( ) { super ( ) ; } validateBasic ( ) { } route ( ) { return ROUTE ; } type ( ) { return RestoreKeyRingMsg . type ( ) ; } } export class DeleteKeyRingMsg extends Message < { status : KeyRingStatus ; multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "delete-keyring" ; } constructor ( public readonly index , public readonly password ) { super ( ) ; } validateBasic ( ) { if ( ! Number . isInteger ( this . index ) ) { throw new Error ( "Invalid index" ) ; } if ( ! this . password ) { throw new Error ( "password not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return DeleteKeyRingMsg . type ( ) ; } } export class UpdateNameKeyRingMsg extends Message < { multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "update-name-keyring" ; } constructor ( public readonly index , public readonly name ) { super ( ) ; } validateBasic ( ) { if ( ! Number . isInteger ( this . index ) ) { throw new Error ( "Invalid index" ) ; } if ( ! this . name ) { throw new Error ( "name not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return UpdateNameKeyRingMsg . type ( ) ; } } export class ShowKeyRingMsg extends Message < string > { public static type ( ) { return "show-keyring" ; } constructor ( public readonly index , public readonly password ) { super ( ) ; } validateBasic ( ) { if ( ! Number . isInteger ( this . index ) ) { throw new Error ( "Invalid index" ) ; } if ( ! this . password ) { throw new Error ( "password not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return ShowKeyRingMsg . type ( ) ; } } export class CreateMnemonicKeyMsg extends Message < { status : KeyRingStatus ; multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "create-mnemonic-key" ; } constructor ( public readonly kdf , public readonly mnemonic , public readonly password , public readonly meta , public readonly bip44HDPath ) { super ( ) ; } validateBasic ( ) { if ( this . kdf !== "scrypt" && this . kdf !== "sha256" && this . kdf !== "pbkdf2" ) { throw new Error ( "Invalid kdf" ) ; } if ( ! this . mnemonic ) { throw new Error ( "mnemonic not set" ) ; } if ( ! this . password ) { throw new Error ( "password not set" ) ; } try { bip39 . mnemonicToEntropy ( this . mnemonic ) ; } catch ( e ) { if ( e . message !== "Invalid mnemonic checksum" ) { throw e ; } } KeyRing . validateBIP44Path ( this . bip44HDPath ) ; } route ( ) { return ROUTE ; } type ( ) { return CreateMnemonicKeyMsg . type ( ) ; } } export class AddMnemonicKeyMsg extends Message < { multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "add-mnemonic-key" ; } constructor ( public readonly kdf , public readonly mnemonic , public readonly meta , public readonly bip44HDPath ) { super ( ) ; } validateBasic ( ) { if ( this . kdf !== "scrypt" && this . kdf !== "sha256" && this . kdf !== "pbkdf2" ) { throw new Error ( "Invalid kdf" ) ; } if ( ! this . mnemonic ) { throw new Error ( "mnemonic not set" ) ; } try { bip39 . mnemonicToEntropy ( this . mnemonic ) ; } catch ( e ) { if ( e . message !== "Invalid mnemonic checksum" ) { throw e ; } } KeyRing . validateBIP44Path ( this . bip44HDPath ) ; } route ( ) { return ROUTE ; } type ( ) { return AddMnemonicKeyMsg . type ( ) ; } } export class CreatePrivateKeyMsg extends Message < { status : KeyRingStatus ; multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "create-private-key" ; } constructor ( public readonly kdf , public readonly privateKey , public readonly password , public readonly meta ) { super ( ) ; } validateBasic ( ) { if ( this . kdf !== "scrypt" && this . kdf !== "sha256" && this . kdf !== "pbkdf2" ) { throw new Error ( "Invalid kdf" ) ; } if ( ! this . privateKey || this . privateKey . length === 0 ) { throw new Error ( "private key not set" ) ; } if ( this . privateKey . length !== 32 ) { throw new Error ( "invalid length of private key" ) ; } if ( ! this . password ) { throw new Error ( "password not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return CreatePrivateKeyMsg . type ( ) ; } } export class CreateLedgerKeyMsg extends Message < { status : KeyRingStatus ; multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "create-ledger-key" ; } constructor ( public readonly kdf , public readonly password , public readonly meta , public readonly bip44HDPath ) { super ( ) ; } validateBasic ( ) { if ( this . kdf !== "scrypt" && this . kdf !== "sha256" && this . kdf !== "pbkdf2" ) { throw new Error ( "Invalid kdf" ) ; } if ( ! this . password ) { throw new Error ( "password not set" ) ; } KeyRing . validateBIP44Path ( this . bip44HDPath ) ; } route ( ) { return ROUTE ; } type ( ) { return CreateLedgerKeyMsg . type ( ) ; } } export class AddPrivateKeyMsg extends Message < { multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "add-private-key" ; } constructor ( public readonly kdf , public readonly privateKey , public readonly meta ) { super ( ) ; } validateBasic ( ) { if ( this . kdf !== "scrypt" && this . kdf !== "sha256" && this . kdf !== "pbkdf2" ) { throw new Error ( "Invalid kdf" ) ; } if ( ! this . privateKey || this . privateKey . length === 0 ) { throw new Error ( "private key not set" ) ; } if ( this . privateKey . length !== 32 ) { throw new Error ( "invalid length of private key" ) ; } } route ( ) { return ROUTE ; } type ( ) { return AddPrivateKeyMsg . type ( ) ; } } export class AddLedgerKeyMsg extends Message < { multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "add-ledger-key" ; } constructor ( public readonly kdf , public readonly meta , public readonly bip44HDPath ) { super ( ) ; } validateBasic ( ) { if ( this . kdf !== "scrypt" && this . kdf !== "sha256" && this . kdf !== "pbkdf2" ) { throw new Error ( "Invalid kdf" ) ; } KeyRing . validateBIP44Path ( this . bip44HDPath ) ; } route ( ) { return ROUTE ; } type ( ) { return AddLedgerKeyMsg . type ( ) ; } } export class LockKeyRingMsg extends Message < { status : KeyRingStatus ; } > { public static type ( ) { return "lock-keyring" ; } constructor ( ) { super ( ) ; } validateBasic ( ) { } route ( ) { return ROUTE ; } type ( ) { return LockKeyRingMsg . type ( ) ; } } export class UnlockKeyRingMsg extends Message < { status : KeyRingStatus ; } > { public static type ( ) { return "unlock-keyring" ; } constructor ( public readonly password = "" ) { super ( ) ; } validateBasic ( ) { if ( ! this . password ) { throw new Error ( "password not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return UnlockKeyRingMsg . type ( ) ; } } export class GetKeyMsg extends Message < Key > { public static type ( ) { return "get-key" ; } constructor ( public readonly chainId ) { super ( ) ; } validateBasic ( ) { if ( ! this . chainId ) { throw new Error ( "chain id not set" ) ; } } approveExternal ( ) { return true ; } route ( ) { return ROUTE ; } type ( ) { return GetKeyMsg . type ( ) ; } } export class RequestSignAminoMsg extends Message < AminoSignResponse > { public static type ( ) { return "request-sign-amino" ; } constructor ( public readonly chainId , public readonly signer , public readonly signDoc , public readonly signOptions = { } ) { super ( ) ; } validateBasic ( ) { if ( ! this . chainId ) { throw new Error ( "chain id not set" ) ; } if ( ! this . signer ) { throw new Error ( "signer not set" ) ; } Bech32Address . validate ( this . signer ) ; if ( ! checkAndValidateADR36AminoSignDoc ( this . signDoc ) ) { if ( this . signDoc . chain_id !== this . chainId ) { throw new Error ( "Chain id in the message is not matched with the requested chain id" ) ; } } else { if ( this . signDoc . msgs [ 0 ] . value . signer !== this . signer ) { throw new Error ( "Unmatched signer in sign doc" ) ; } } if ( ! this . signOptions ) { throw new Error ( "Sign options are null" ) ; } } approveExternal ( ) { return true ; } route ( ) { return ROUTE ; } type ( ) { return RequestSignAminoMsg . type ( ) ; } } export class RequestVerifyADR36AminoSignDoc extends Message < boolean > { public static type ( ) { return "request-verify-adr-36-amino-doc" ; } constructor ( public readonly chainId , public readonly signer , public readonly data , public readonly signature ) { super ( ) ; } validateBasic ( ) { if ( ! this . chainId ) { throw new Error ( "chain id not set" ) ; } if ( ! this . signer ) { throw new Error ( "signer not set" ) ; } if ( ! this . signature ) { throw new Error ( "Signature not set" ) ; } Bech32Address . validate ( this . signer ) ; } approveExternal ( ) { return true ; } route ( ) { return ROUTE ; } type ( ) { return RequestVerifyADR36AminoSignDoc . type ( ) ; } } export class RequestSignDirectMsg extends Message < { readonly signed : { bodyBytes : Uint8Array ; authInfoBytes : Uint8Array ; chainId : string ; accountNumber : string ; } ; readonly signature : StdSignature ; } > { public static type ( ) { return "request-sign-direct" ; } constructor ( public readonly chainId , public readonly signer , public readonly signDoc , public readonly signOptions = { } ) { super ( ) ; } validateBasic ( ) { if ( ! this . chainId ) { throw new Error ( "chain id not set" ) ; } if ( ! this . signer ) { throw new Error ( "signer not set" ) ; } Bech32Address . validate ( this . signer ) ; const signDoc = cosmos . tx . v1beta1 . SignDoc . create ( { bodyBytes : this . signDoc . bodyBytes , authInfoBytes : this . signDoc . authInfoBytes , chainId : this . signDoc . chainId , accountNumber : this . signDoc . accountNumber ? Long . fromString ( this . signDoc . accountNumber ) : undefined , } ) ; if ( signDoc . chainId !== this . chainId ) { throw new Error ( "Chain id in the message is not matched with the requested chain id" ) ; } if ( ! this . signOptions ) { throw new Error ( "Sign options are null" ) ; } } approveExternal ( ) { return true ; } route ( ) { return ROUTE ; } type ( ) { return RequestSignDirectMsg . type ( ) ; } } export class GetMultiKeyStoreInfoMsg extends Message < { multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "get-multi-key-store-info" ; } constructor ( ) { super ( ) ; } validateBasic ( ) { } route ( ) { return ROUTE ; } type ( ) { return GetMultiKeyStoreInfoMsg . type ( ) ; } } export class ChangeKeyRingMsg extends Message < { multiKeyStoreInfo : MultiKeyStoreInfoWithSelected ; } > { public static type ( ) { return "change-keyring" ; } constructor ( public readonly index ) { super ( ) ; } validateBasic ( ) { if ( this . index < 0 ) { throw new Error ( "Index is negative" ) ; } if ( ! Number . isInteger ( this . index ) ) { throw new Error ( "Invalid index" ) ; } } route ( ) { return ROUTE ; } type ( ) { return ChangeKeyRingMsg . type ( ) ; } } export class GetIsKeyStoreCoinTypeSetMsg extends Message < { readonly path : BIP44 ; readonly bech32Address : string ; } [ ] > { public static type ( ) { return "get-is-keystore-coin-type-set" ; } constructor ( public readonly chainId , public readonly paths ) { super ( ) ; } validateBasic ( ) { if ( ! this . chainId ) { throw new Error ( "chain id not set" ) ; } if ( this . paths . length === 0 ) { throw new Error ( "empty bip44 path list" ) ; } } route ( ) { return ROUTE ; } type ( ) { return GetIsKeyStoreCoinTypeSetMsg . type ( ) ; } } export class SetKeyStoreCoinTypeMsg extends Message < KeyRingStatus > { public static type ( ) { return "set-keystore-coin-type" ; } constructor ( public readonly chainId , public readonly coinType ) { super ( ) ; } validateBasic ( ) { if ( ! this . chainId ) { throw new Error ( "chain id not set" ) ; } if ( this . coinType < 0 ) { throw new Error ( "coin type can not be negative" ) ; } if ( ! Number . isInteger ( this . coinType ) ) { throw new Error ( "coin type should be integer" ) ; } } route ( ) { return ROUTE ; } type ( ) { return SetKeyStoreCoinTypeMsg . type ( ) ; } } export class CheckPasswordMsg extends Message < boolean > { public static type ( ) { return "check-keyring-password" ; } constructor ( public readonly password ) { super ( ) ; } validateBasic ( ) { if ( ! this . password ) { throw new Error ( "password not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return CheckPasswordMsg . type ( ) ; } } export class ExportKeyRingDatasMsg extends Message < ExportKeyRingData [ ] > { public static type ( ) { return "export-keyring-datas" ; } constructor ( public readonly password ) { super ( ) ; } validateBasic ( ) { if ( ! this . password ) { throw new Error ( "password not set" ) ; } } route ( ) { return ROUTE ; } type ( ) { return ExportKeyRingDatasMsg . type ( ) ; } }