> For the complete documentation index, see [llms.txt](/llms.txt).

# Integrate Embedded Wallets with the Cosmos Blockchain

While using the Embedded Wallets Web SDK (formerly Web3Auth) for a non-EVM chain like [Cosmos](https://cosmos.network/), you can get the user's private key from the provider. Using this private key, you can use the corresponding libraries of the blockchain to make blockchain calls like getting the user's `account`, `chain ID`, fetching `balance`, `send transaction`, etc. We have highlighted a few methods here to get you started quickly.

note

The SDKs are now branded as MetaMask Embedded Wallet SDKs (formerly Web3Auth Plug and Play SDKs). Package names and APIs remain Web3Auth (for example, Web3Auth React SDK), and code snippets may reference `web3auth` identifiers.

## Installation[​](#installation "Direct link to Installation")

- npm
- Yarn
- pnpm
- Bun

```
npm install --save @web3auth/no-modal @web3auth/auth-adapter @web3auth/base @cosmjs/stargate @cosmjs/proto-signing

```

```
yarn add @web3auth/no-modal @web3auth/auth-adapter @web3auth/base @cosmjs/stargate @cosmjs/proto-signing

```

```
pnpm add @web3auth/no-modal @web3auth/auth-adapter @web3auth/base @cosmjs/stargate @cosmjs/proto-signing

```

```
bun add @web3auth/no-modal @web3auth/auth-adapter @web3auth/base @cosmjs/stargate @cosmjs/proto-signing

```

## Initializing provider[​](#initializing-provider "Direct link to Initializing provider")

### Getting the `chainConfig`[​](#getting-the-chainconfig "Direct link to getting-the-chainconfig")

- Mainnet
- Testnet

- Chain Namespace: other
- Chain ID: cosmoshub-4
- Public RPC URL: `https://cosmos-rpc.quickapi.com` (Avoid using public rpcTarget in production)
- Display Name: Cosmos Mainnet
- Block Explorer Link: `https://www.mintscan.io/cosmos`
- Ticker: ATOM
- Ticker Name: Cosmos

- Chain Namespace: other
- Chain ID: theta-testnet-001
- Public RPC URL: `https://rpc.sentry-02.theta-testnet.polypore.xyz` (Avoid using public rpcTarget in production)
- Display Name: Cosmos Testnet
- Block Explorer Link: `https://explorer.theta-testnet.polypore.xyz`
- Ticker: ATOM
- Ticker Name: Cosmos

## Get Chain ID[​](#get-chain-id "Direct link to Get Chain ID")

Once a user logs in, the Embedded Wallets SDK returns a provider. Since there isn't a native provider for Cosmos, we directly use the private key to make RPC calls.

### CosmJS Libraries[​](#cosmjs-libraries "Direct link to CosmJS Libraries")

`@cosmjs/stargate` A client library for the Cosmos SDK 0.40+. Provides a high-level client for querying, signing and broadcasting. This is an npm library that provides a JavaScript/TypeScript interface to interact with the Cosmos SDK blockchain via the Tendermint RPC interface.

`@cosmjs/proto-signing` It is an npm library for signing messages using protobuf serialization, which is commonly used in Cosmos SDK-based blockchains. It provides a simple API for generating and verifying signatures using different algorithms and key types.

In order to get the ChainId, we connect to the RPC using StargateClient and make a call using the Cosmos RPC client to get the chainId. You can get the private key using `this.provider.request({method: "private_key",})`.

```
import type { IProvider } from '@web3auth/base'
import { SigningStargateClient, StargateClient } from '@cosmjs/stargate'
import { DirectSecp256k1Wallet, OfflineDirectSigner } from '@cosmjs/proto-signing'

const rpc = 'https://rpc.sentry-02.theta-testnet.polypore.xyz'
export default class CosmosRPC {
  private provider: IProvider

  constructor(provider: IProvider) {
    this.provider = provider
  }

  async getPrivateKey(): Promise<any> {
    try {
      return await this.provider.request({
        method: 'private_key',
      })
    } catch (error) {
      return error as string
    }
  }

  async getChainId(): Promise<string> {
    try {
      const client = await StargateClient.connect(rpc)

      // Get the connected Chain's ID
      const chainId = await client.getChainId()

      return chainId.toString()
    } catch (error) {
      return error as string
    }
  }
}

```

## Get account[​](#get-account "Direct link to Get account")

Using `DirectSecp256k1Wallet.fromKey()` we can get the accounts via the private key we get through the provider.

```
import type { IProvider } from '@web3auth/base'
import { SigningStargateClient, StargateClient } from '@cosmjs/stargate'
import { DirectSecp256k1Wallet, OfflineDirectSigner } from '@cosmjs/proto-signing'

const rpc = 'https://rpc.sentry-02.theta-testnet.polypore.xyz'
export default class CosmosRPC {
  private provider: IProvider

  constructor(provider: IProvider) {
    this.provider = provider
  }

  async getAccounts(): Promise<any> {
    try {
      const privateKey = Buffer.from(await this.getPrivateKey(), 'hex')
      const walletPromise = await DirectSecp256k1Wallet.fromKey(privateKey, 'cosmos')
      return (await walletPromise.getAccounts())[0].address
    } catch (error) {
      return error
    }
  }
}

```

## Get balance[​](#get-balance "Direct link to Get balance")

Using the account address we received in the previous step, we can fetch the balance using an RPC call to `client.getAllBalances(address)`.

```
import type { IProvider } from '@web3auth/base'
import { SigningStargateClient, StargateClient } from '@cosmjs/stargate'
import { DirectSecp256k1Wallet, OfflineDirectSigner } from '@cosmjs/proto-signing'

const rpc = 'https://rpc.sentry-02.theta-testnet.polypore.xyz'
export default class CosmosRPC {
  private provider: IProvider

  constructor(provider: IProvider) {
    this.provider = provider
  }

  async getBalance(): Promise<any> {
    try {
      const client = await StargateClient.connect(rpc)

      const privateKey = Buffer.from(await this.getPrivateKey(), 'hex')
      const walletPromise = await DirectSecp256k1Wallet.fromKey(privateKey, 'cosmos')
      const address = (await walletPromise.getAccounts())[0].address
      // Get user's balance in uAtom
      return await client.getAllBalances(address)
    } catch (error) {
      return error as string
    }
  }
}

```

## Send transaction[​](#send-transaction "Direct link to Send transaction")

```
import type { IProvider } from '@web3auth/base'
import { SigningStargateClient, StargateClient } from '@cosmjs/stargate'
import { DirectSecp256k1Wallet, OfflineDirectSigner } from '@cosmjs/proto-signing'

const rpc = 'https://rpc.sentry-02.theta-testnet.polypore.xyz'
export default class CosmosRPC {
  private provider: IProvider

  constructor(provider: IProvider) {
    this.provider = provider
  }

  async sendTransaction(): Promise<any> {
    try {
      await StargateClient.connect(rpc)
      const privateKey = Buffer.from(await this.getPrivateKey(), 'hex')
      const walletPromise = await DirectSecp256k1Wallet.fromKey(privateKey, 'cosmos')
      const fromAddress = (await walletPromise.getAccounts())[0].address

      const destination = 'cosmos15aptdqmm7ddgtcrjvc5hs988rlrkze40l4q0he'

      const getSignerFromKey = async (): Promise<OfflineDirectSigner> => {
        return DirectSecp256k1Wallet.fromKey(privateKey, 'cosmos')
      }
      const signer: OfflineDirectSigner = await getSignerFromKey()

      const signingClient = await SigningStargateClient.connectWithSigner(rpc, signer)

      const result = await signingClient.sendTokens(
        fromAddress,
        destination,
        [{ denom: 'uatom', amount: '250' }],
        {
          amount: [{ denom: 'uatom', amount: '250' }],
          gas: '100000',
        }
      )
      const transactionHash = result.transactionHash
      const height = result.height
      return { transactionHash, height }
    } catch (error) {
      return error as string
    }
  }
}

```
