@cosmjs/proto-signing provides comprehensive utilities for protobuf-based transaction signing in Cosmos SDK applications (version 0.40+). It implements the signing infrastructure for direct signing mode as specified in ADR-020, offering wallet functionality, transaction encoding/decoding capabilities, registry management for protobuf message types, and cryptographic operations required for Cosmos-based blockchain interactions.
npm install @cosmjs/proto-signingimport {
DirectSecp256k1HdWallet,
DirectSecp256k1Wallet,
Registry,
makeSignDoc,
makeAuthInfoBytes,
makeSignBytes,
encodePubkey,
decodeTxRaw,
coin,
coins,
parseCoins
} from "@cosmjs/proto-signing";For CommonJS:
const {
DirectSecp256k1HdWallet,
DirectSecp256k1Wallet,
Registry,
makeSignDoc,
makeAuthInfoBytes,
makeSignBytes,
encodePubkey,
decodeTxRaw,
coin,
coins,
parseCoins
} = require("@cosmjs/proto-signing");import { DirectSecp256k1HdWallet, makeSignDoc, Registry } from "@cosmjs/proto-signing";
import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx";
// Create an HD wallet from mnemonic
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
"surround miss nominee dream gap cross assault thank captain prosper drop duty group candy wealth weather scale put"
);
// Get accounts
const accounts = await wallet.getAccounts();
const signerAddress = accounts[0].address;
// Create a registry for message types
const registry = new Registry();
registry.register("/cosmos.bank.v1beta1.MsgSend", MsgSend);
// Create and sign a transaction
const signDoc = makeSignDoc(
bodyBytes,
authInfoBytes,
"cosmoshub-4",
123
);
const signResponse = await wallet.signDirect(signerAddress, signDoc);@cosmjs/proto-signing is built around several key components:
Hierarchical deterministic (HD) wallets and single-key wallets for managing cryptographic keys and signing transactions.
class DirectSecp256k1HdWallet implements OfflineDirectSigner {
static fromMnemonic(
mnemonic: string,
options?: DirectSecp256k1HdWalletOptions
): Promise<DirectSecp256k1HdWallet>;
static generate(
length?: number,
options?: DirectSecp256k1HdWalletOptions
): Promise<DirectSecp256k1HdWallet>;
getAccounts(): Promise<readonly AccountData[]>;
signDirect(signerAddress: string, signDoc: SignDoc): Promise<DirectSignResponse>;
}
class DirectSecp256k1Wallet implements OfflineDirectSigner {
static fromKey(privkey: Uint8Array, prefix?: string): Promise<DirectSecp256k1Wallet>;
getAccounts(): Promise<readonly AccountData[]>;
signDirect(address: string, signDoc: SignDoc): Promise<DirectSignResponse>;
}Direct signing utilities for creating signing documents and authentication information for Cosmos SDK transactions.
function makeSignDoc(
bodyBytes: Uint8Array,
authInfoBytes: Uint8Array,
chainId: string,
accountNumber: number
): SignDoc;
function makeAuthInfoBytes(
signers: ReadonlyArray<{ readonly pubkey: Any; readonly sequence: bigint | number }>,
feeAmount: readonly Coin[],
gasLimit: number,
feeGranter: string | undefined,
feePayer: string | undefined,
signMode?: SignMode
): Uint8Array;
function makeSignBytes(signDoc: SignDoc): Uint8Array;Type registry system for managing protobuf message types with encoding and decoding capabilities.
class Registry {
constructor(customTypes?: Iterable<[string, GeneratedType]>);
register(typeUrl: string, type: GeneratedType): void;
lookupType(typeUrl: string): GeneratedType | undefined;
encode(encodeObject: EncodeObject): Any;
decode(decodeObject: DecodeObject): any;
}
interface EncodeObject {
readonly typeUrl: string;
readonly value: any;
}
interface DecodeObject {
readonly typeUrl: string;
readonly value: Uint8Array;
}Utilities for encoding and decoding public keys between Amino JSON format and protobuf Any format.
function encodePubkey(pubkey: Pubkey): Any;
function decodePubkey(pubkey: Any): Pubkey;
function decodeOptionalPubkey(pubkey: Any | null | undefined): Pubkey | null;
function anyToSinglePubkey(pubkey: Any): SinglePubkey;Utilities for decoding raw transaction data into structured format.
function decodeTxRaw(tx: Uint8Array): DecodedTxRaw;
interface DecodedTxRaw {
readonly authInfo: AuthInfo;
readonly body: TxBody;
readonly signatures: readonly Uint8Array[];
}Utility functions for working with Cosmos SDK coin amounts and denominations.
/**
* Creates a single coin object
* @param amount - Amount as number or string
* @param denom - Denomination (e.g., "uatom", "stake")
* @returns Coin object
*/
function coin(amount: number | string, denom: string): Coin;
/**
* Creates an array with a single coin
* @param amount - Amount as number or string
* @param denom - Denomination (e.g., "uatom", "stake")
* @returns Array containing one coin
*/
function coins(amount: number | string, denom: string): Coin[];
/**
* Parses a coin string into an array of coins
* @param input - Coin string (e.g., "5000uatom,1000stake")
* @returns Array of parsed coins
*/
function parseCoins(input: string): Coin[];interface AccountData {
/** A printable address (typically bech32 encoded) */
readonly address: string;
readonly algo: Algo;
readonly pubkey: Uint8Array;
}
interface DirectSignResponse {
/**
* The sign doc that was signed.
* This may be different from the input signDoc when the signer modifies it as part of the signing process.
*/
readonly signed: SignDoc;
readonly signature: StdSignature;
}
interface OfflineDirectSigner {
readonly getAccounts: () => Promise<readonly AccountData[]>;
readonly signDirect: (signerAddress: string, signDoc: SignDoc) => Promise<DirectSignResponse>;
}
type Algo = "secp256k1" | "ed25519" | "sr25519";
interface DirectSecp256k1HdWalletOptions {
bip39Password?: string;
hdPaths?: readonly HdPath[];
prefix?: string;
}