npm install @cosmjs/amino
Secp256k1HdWallet
HD wallet using BIP-39 mnemonic with Amino JSON signing. ImplementsOfflineAminoSigner.
Static Methods
| Method | Parameters | Returns |
|---|---|---|
generate | length?: 12 | 15 | 18 | 21 | 24, options?: Partial<Secp256k1HdWalletOptions> | Promise<Secp256k1HdWallet> |
fromMnemonic | mnemonic: string, options?: Partial<Secp256k1HdWalletOptions> | Promise<Secp256k1HdWallet> |
deserialize | serialization: string, password: string | Promise<Secp256k1HdWallet> |
deserializeWithEncryptionKey | serialization: string, encryptionKey: Uint8Array | Promise<Secp256k1HdWallet> |
Instance Methods
| Method | Parameters | Returns |
|---|---|---|
getAccounts | — | Promise<readonly AccountData[]> |
signAmino | signerAddress: string, signDoc: StdSignDoc | Promise<AminoSignResponse> |
serialize | password: string | Promise<string> |
serializeWithEncryptionKey | encryptionKey: Uint8Array, kdfConfiguration: KdfConfiguration | Promise<string> |
Instance Properties
| Property | Type |
|---|---|
mnemonic | string |
Options
interface Secp256k1HdWalletOptions {
readonly bip39Password: string; // default: ""
readonly hdPaths: readonly HdPath[]; // default: [makeCosmoshubPath(0)]
readonly prefix: string; // default: "cosmos"
}
Usage
import { Secp256k1HdWallet, makeCosmoshubPath } from "@cosmjs/amino";
const wallet = await Secp256k1HdWallet.fromMnemonic("your mnemonic ...", {
prefix: "cosmos",
hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1)],
});
const [account1, account2] = await wallet.getAccounts();
Secp256k1Wallet
Single-key wallet for Amino JSON signing. ImplementsOfflineAminoSigner.
| Method | Parameters | Returns |
|---|---|---|
fromKey (static) | privkey: Uint8Array, prefix?: string | Promise<Secp256k1Wallet> |
getAccounts | — | Promise<readonly AccountData[]> |
signAmino | signerAddress: string, signDoc: StdSignDoc | Promise<AminoSignResponse> |
Address Functions
| Function | Parameters | Returns |
|---|---|---|
pubkeyToAddress | pubkey: Pubkey, prefix: string | string |
pubkeyToRawAddress | pubkey: Pubkey | Uint8Array |
rawSecp256k1PubkeyToRawAddress | pubkeyRaw: Uint8Array | Uint8Array |
rawEd25519PubkeyToRawAddress | pubkeyRaw: Uint8Array | Uint8Array |
rawEthSecp256k1PubkeyToRawAddress | pubkeyRaw: Uint8Array | Uint8Array |
Coin Utilities
| Function | Parameters | Returns |
|---|---|---|
coin | amount: number | string, denom: string | Coin |
coins | amount: number | string, denom: string | Coin[] |
parseCoins | input: string | Coin[] |
addCoins | lhs: Coin, rhs: Coin | Coin |
import { coin, coins, parseCoins, addCoins } from "@cosmjs/amino";
const amount = coin(1000000, "uatom");
const amounts = coins(500, "uatom");
const parsed = parseCoins("1000uatom,500ustake");
const total = addCoins(coin(100, "uatom"), coin(50, "uatom"));
Pubkey Encoding
| Function | Parameters | Returns |
|---|---|---|
encodeSecp256k1Pubkey | pubkey: Uint8Array | Secp256k1Pubkey |
encodeEd25519Pubkey | pubkey: Uint8Array | Ed25519Pubkey |
encodeEthSecp256k1Pubkey | pubkey: Uint8Array | EthSecp256k1Pubkey |
encodeAminoPubkey | pubkey: Pubkey | Uint8Array |
decodeAminoPubkey | amino: Uint8Array | Pubkey |
encodeBech32Pubkey | pubkey: Pubkey, prefix: string | string |
decodeBech32Pubkey | bechEncoded: string | Pubkey |
Signature Functions
| Function | Parameters | Returns |
|---|---|---|
encodeSecp256k1Signature | pubkey: Uint8Array, signature: Uint8Array | StdSignature |
encodeEthSecp256k1Signature | pubkey: Uint8Array, signature: Uint8Array | StdSignature |
decodeSignature | signature: StdSignature | { pubkey: Uint8Array; signature: Uint8Array } |
Sign Document Functions
| Function | Parameters | Returns |
|---|---|---|
makeSignDoc | msgs: readonly AminoMsg[], fee: StdFee, chainId: string, memo: string | undefined, accountNumber: number | string, sequence: number | string, timeout_height?: bigint | StdSignDoc |
serializeSignDoc | signDoc: StdSignDoc | Uint8Array |
Multisig
| Function | Parameters | Returns |
|---|---|---|
createMultisigThresholdPubkey | pubkeys: readonly SinglePubkey[], threshold: number, nosort?: boolean | MultisigThresholdPubkey |
nosort is false (default), pubkeys are sorted by their raw address to match the Cosmos SDK client behavior. Pass true to preserve the input order.
Key Types
Coin
interface Coin {
readonly denom: string;
readonly amount: string;
}
StdFee
interface StdFee {
readonly amount: readonly Coin[];
readonly gas: string;
readonly granter?: string;
readonly payer?: string;
}
AminoMsg
interface AminoMsg {
readonly type: string;
readonly value: Record<string, any>;
}
StdSignDoc
interface StdSignDoc {
readonly chain_id: string;
readonly account_number: string;
readonly sequence: string;
readonly fee: StdFee;
readonly msgs: readonly AminoMsg[];
readonly memo: string;
readonly timeout_height?: string;
}
AccountData
interface AccountData {
readonly address: string;
readonly algo: Algo;
readonly pubkey: Uint8Array;
}
OfflineAminoSigner
interface OfflineAminoSigner {
readonly getAccounts: () => Promise<readonly AccountData[]>;
readonly signAmino: (signerAddress: string, signDoc: StdSignDoc) => Promise<AminoSignResponse>;
}
AminoSignResponse
interface AminoSignResponse {
readonly signed: StdSignDoc;
readonly signature: StdSignature;
}
StdSignature
interface StdSignature {
readonly pub_key: Pubkey;
readonly signature: string;
}
Pubkey Types
interface Secp256k1Pubkey {
readonly type: "tendermint/PubKeySecp256k1";
readonly value: string;
}
interface Ed25519Pubkey {
readonly type: "tendermint/PubKeyEd25519";
readonly value: string;
}
interface EthSecp256k1Pubkey {
readonly type: "os/PubKeyEthSecp256k1";
readonly value: string;
}
interface MultisigThresholdPubkey {
readonly type: "tendermint/PubKeyMultisigThreshold";
readonly value: {
readonly threshold: string;
readonly pubkeys: readonly SinglePubkey[];
};
}
Other Exports
| Function/Type | Purpose |
|---|---|
makeCosmoshubPath(account) | Creates HD path m/44'/118'/0'/0/{account} |
omitDefault(value) | Omit protobuf default values |
isStdTx(value) | Type guard for StdTx |
makeStdTx(signDoc, signature) | Create StdTx from sign doc and signature |
extractKdfConfiguration(serialization) | Extract KDF config from serialized wallet |
executeKdf(password, config) | Execute key derivation function |
isEthereumSecp256k1Account(account) | Check if account uses Ethereum secp256k1 |
getAminoPubkey(account) | Get amino pubkey from account data |