or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

decoding.mdindex.mdpubkey.mdregistry.mdsigning.mdwallets.md
tile.json

tessl/npm-cosmjs--proto-signing

Utilities for protobuf based signing (Cosmos SDK 0.40+)

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@cosmjs/proto-signing@0.36.x

To install, run

npx @tessl/cli install tessl/npm-cosmjs--proto-signing@0.36.0

index.mddocs/

@cosmjs/proto-signing

@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.

Package Information

  • Package Name: @cosmjs/proto-signing
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @cosmjs/proto-signing

Core Imports

import {
  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");

Basic Usage

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);

Architecture

@cosmjs/proto-signing is built around several key components:

  • Wallet System: Support for both hierarchical deterministic (HD) wallets with BIP39 mnemonics and simple single-key wallets
  • Direct Signing: Implementation of Cosmos SDK's direct signing mode using protobuf serialization
  • Type Registry: Management system for protobuf message types with encoding/decoding capabilities
  • Cryptographic Operations: secp256k1 signature operations, public key encoding/decoding, and address derivation
  • Transaction Utilities: Tools for creating signing documents, auth info, and decoding raw transactions

Capabilities

Wallet Management

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>;
}

Wallet Management

Transaction Signing

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;

Transaction Signing

Protobuf Registry

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;
}

Protobuf Registry

Public Key Operations

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;

Public Key Operations

Transaction Decoding

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[];
}

Transaction Decoding

Coin and Amount Utilities (Re-exported from @cosmjs/amino)

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[];

Core Types

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;
}