Skip to content

Cosmos Chain

The Cosmos chain implementation in Signet.js provides support for Cosmos SDK-based networks (Cosmos Hub, Osmosis, etc.) with a focus on standard transactions and IBC transfers.

Overview

The Cosmos implementation allows you to:

  • Generate addresses and public keys
  • Check balances
  • Prepare, sign, and broadcast transactions
  • Support for various Cosmos SDK message types

Complete Transaction Example

Below is a complete example of sending a transaction on a Cosmos chain using Signet.js:

import { Cosmos } from 'signet.js'
import { utils } from 'signet.js'
import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { sepolia } from 'viem/chains'
 
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`)
 
const publicClient = createPublicClient({
  chain: sepolia,
  transport: http(),
})
 
const walletClient = createWalletClient({
  account,
  chain: sepolia,
  transport: http(),
})
 
const chainSigContract = new utils.chains.evm.ChainSignatureContract({
  publicClient,
  walletClient,
  contractAddress: utils.constants.CONTRACT_ADDRESSES.ETHEREUM
    .TESTNET_DEV as `0x${string}`,
})
 
const cosmos = new Cosmos({
  chainId: 'cosmoshub-4',
  contract: chainSigContract,
})
 
const path = 'cosmos'
const predecessorId = walletClient.account.address
 
const { address: from, publicKey } = await cosmos.deriveAddressAndPublicKey(
  predecessorId,
  path
)
 
const { balance, decimals } = await cosmos.getBalance(from)
 
const { transaction, hashesToSign } = await cosmos.prepareTransactionForSigning(
  {
    address: from,
    publicKey,
    messages: [
      {
        typeUrl: '/cosmos.bank.v1beta1.MsgSend',
        value: {
          fromAddress: from,
          toAddress: 'cosmos1jq304cthpx0lwhpqzrdjrcza559ukyy347xu57',
          amount: [
            {
              denom: 'uatom',
              amount: '1000000', // 1 ATOM (1,000,000 uatom)
            },
          ],
        },
      },
    ],
    memo: 'Sent via Signet.js',
  }
)
 
const rsvSignature = await chainSigContract.sign({
  payload: hashesToSign[0],
  path,
  key_version: 0,
})
 
const tx = cosmos.finalizeTransactionSigning({
  transaction,
  rsvSignatures: [rsvSignature],
})
 
const txHash = await cosmos.broadcastTx(tx)

Supported Networks

You can use any Cosmos SDK-based network by providing the appropriate chain ID:

import { Cosmos } from 'signet.js'
 
// Cosmos Hub
const cosmosHub = new Cosmos({
  chainId: 'cosmoshub-4',
  contract: chainSigContract,
})
 
// Osmosis
const osmosis = new Cosmos({
  chainId: 'osmosis-1',
  contract: chainSigContract,
})
 
// Juno
const juno = new Cosmos({
  chainId: 'juno-1',
  contract: chainSigContract,
})

Types

The following types are used on the Cosmos chain:

import { type EncodeObject } from '@cosmjs/proto-signing'
import { type TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'
 
export type CosmosNetworkIds = string
 
export type CosmosUnsignedTransaction = TxRaw
 
export interface CosmosTransactionRequest {
  address: string
  publicKey: string
  messages: EncodeObject[]
  memo?: string
  gas?: number
}
 
export interface BalanceResponse {
  balances: Array<{
    denom: string
    amount: string
  }>
  pagination: {
    next_key: string | null
    total: string
  }
}
 
export interface ChainInfo {
  prefix: string
  denom: string
  rpcUrl: string
  restUrl: string
  expectedChainId: string
  gasPrice: number
  decimals: number
}