# 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:

```ts twoslash
import { chainAdapters } from 'signet.js'
// [!include ~/snippets/code/evm/contract.ts]

const cosmos = new chainAdapters.cosmos.Cosmos({
  chainId: 'cosmoshub-4',
  contract: evmChainSigContract,
  // Optional: override chain-registry endpoints
  // endpoints: { rpcUrl: 'http://localhost:26657', restUrl: 'http://localhost:1317' },
})

const path = 'cosmos'
const predecessorId = walletClient.account.address
const keyVersion = 1

const { address: from, publicKey } = await cosmos.deriveAddressAndPublicKey(
  predecessorId,
  path,
  keyVersion
)

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 evmChainSigContract.sign({
  payload: hashesToSign[0],
  path,
  key_version: keyVersion,
})

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:

```ts twoslash
// [!include ~/snippets/code/contract.ts]
// ---cut---
import { chainAdapters } from 'signet.js'

// Cosmos Hub
const cosmosHub = new chainAdapters.cosmos.Cosmos({
  chainId: 'cosmoshub-4',
  contract: evmChainSigContract,
})

// Osmosis
const osmosis = new chainAdapters.cosmos.Cosmos({
  chainId: 'osmosis-1',
  contract: evmChainSigContract,
})

// Juno
const juno = new chainAdapters.cosmos.Cosmos({
  chainId: 'juno-1',
  contract: evmChainSigContract,
})
```

## Working Examples

See the test suite for complete, runnable examples:

* [Unit tests](https://github.com/sig-net/signet.js/blob/main/tests/unit/cosmos/Cosmos.test.ts) — bank send with mocked signing, including custom `endpoints`
* [E2E tests](https://github.com/sig-net/signet.js/blob/main/tests/e2e/cosmos/Cosmos.e2e.test.ts) — MPC signing on Sepolia, broadcast on local gaiad

## Types

The following types are used on the Cosmos chain:

```ts twoslash
// [!include ~/../src/chain-adapters/Cosmos/types.ts]
```
