@cosmsnap/snapper

中文文档
0.2.7发布于 2024/08/29MITTypeScriptESM + CJS近 30 天下载 2.4万

A helper package with utilities to interact with the Cosmos Extension for MetaMask.

原始使用文档
文档目录15 节
  1. Global Provider (Drop In Replacement for window.keplr)
  2. Direct Interaction Functions
  3. Check If Installed
  4. Check If Initialized
  5. Install The Cosmos Extension.
  6. Suggest Chain.
  7. Get Chains.
  8. Delete Chain.
  9. Sign & Broadcast.
  10. Sign Transaction.
  11. Add Address to Address Book.
  12. Get Address Book
  13. Delete Address from Address Book
  14. Get All Bech32 Addresses
  15. Get All Bech32 Addresses

Provider Library for Cosmos Extension for Metamask

Snapper is a utility library for interacting with the official Cosmos Extension for Metamask.

Global Provider (Drop In Replacement for window.keplr)

Add the provider as a global object within your dApp.

import { CosmosSnap } from "@cosmsnap/snapper";
window.cosmos = new CosmosSnap();

const memo = "Hello from Metamask!";

let offlineSigner = await window.cosmos.getOfflineSigner("cosmoshub-4");

let wallet = await window.cosmos.getAccount("cosmoshub-4");

// Create a send token message
const msg = {
    typeUrl: "/cosmos.bank.v1beta1.MsgSend",
    value: {
    fromAddress: wallet.address,
    toAddress: "cosmos123456789",
    amount: [
        {
        denom: "uatom",
        amount: "10000",
        },
    ],
    },
};

// Create fee
const fee = {
    amount: [],
    gas: "200000",
};

const signingClient = await SigningStargateClient.connectWithSigner(
    "https://rpc-cosmoshub.whispernode.com:443",
    offlineSigner
);

const result = await signingClient.sign(wallet.address, [msg], fee, memo);

Use in development mode. Note: You must run the snap locally for this to run properly.

window.cosmos.changeSnapId("local:http://localhost:8080");

Structure of the provider.

export interface SnapProvider {
  experimentalSuggestChain(chainInfo: ChainInfo): Promise<void>;
  signAmino(
    chainId: string,
    signer: string,
    signDoc: StdSignDoc,
  ): Promise<AminoSignResponse>;
  signDirect(
    chainId: string,
    signer: string,
    signDoc: {
      /** SignDoc bodyBytes */
      bodyBytes?: Uint8Array | null;

      /** SignDoc authInfoBytes */
      authInfoBytes?: Uint8Array | null;

      /** SignDoc chainId */
      chainId?: string | null;

      /** SignDoc accountNumber */
      accountNumber?: Long | null;
    },
  ): Promise<DirectSignResponse>;
  sendTx(
    chainId: string,
    tx: Uint8Array,
  ): Promise<DeliverTxResponse>;
  getOfflineSigner(chainId: string): Promise<OfflineAminoSigner & OfflineDirectSigner>;
  enabled(): Promise<boolean>;
  install(): Promise<void>;
  getChains(): Promise<Chain[]>;
  deleteChain(chain_id: string): Promise<void>;
  signAndBroadcast(chain_id: string, msgs: Msg[], fees: Fees): Promise<DeliverTxResponse>;
  sign(chain_id: string, msgs: Msg[], fees: Fees): Promise<Int8Array>;
  addAddressToBook(chain_id: string, address: string, name: string): Promise<void>;
  getAddressBook(): Promise<Address[]>;
  deleteAddressFromBook(address: string): Promise<void>;
  getBech32Addresses(): Promise<CosmosAddress[]>;
  getBech32Address(chain_id: string): Promise<CosmosAddress>;
  getAccount(chain_id: string): Promise<AccountData>;
  changeSnapId(snap_id: string): void;
}

Direct Interaction Functions

You can interact with the snap directly to with these functions although we highly suggest using the provider through the window as it is a drop in replacement for other Cosmos wallets.

Check If Installed

import { isSnapInstalled } from '@cosmsnap/snapper';
let result: boolean = await isSnapInstalled();

Check If Initialized

import { isSnapInitialized } from '@cosmsnap/snapper';
let result: boolean = await isSnapInitialized();

Install The Cosmos Extension.

import { installSnap } from '@cosmsnap/snapper';
await installSnap();

Suggest Chain.

Use the format like here in the chain registry. The more info the better.

import { suggestChain } from '@cosmsnap/snapper';
let chain: Chain = {};
await suggestChain(chain);

Get Chains.

Gets all the chains that are in state within the snap.

import { getChains } from '@cosmsnap/snapper';
let chains: []Chain = await getChains();

Delete Chain.

Delete a chain from wallet state.

import { deleteChain } from '@cosmsnap/snapper';
await deleteChain("cosmoshub-4");

Sign & Broadcast.

Sign and broadcast a transaction. Note, you can use our production grade infrastructure (RPCs) to broadcast transactions using this function.

import { signAndBroadcast } from '@cosmsnap/snapper';
import { DeliverTxResponse } from "@cosmjs/stargate";

const msgs = [
    {
        typeUrl: "/cosmos.bank.v1beta1.MsgSend",
        value: {
            fromAddress: senderAddress,
            toAddress: recipientAddress,
            amount: [{
                denom: "uatom",
                amount: "500000"
            }],
        },
    }
];
const fees = {
    amount: [{
        denom: "uatom",
        amount: "500"
    }],
    gas: "200000"
};
let txResult: DeliverTxResponse = await signAndBroadcast("cosmoshub-4", msgs, fees);

Sign Transaction.

Just sign a transaction and return it. Note, this does not use our production grade infrastructure. You must use your own to broadcast the transaction.

import { sign } from '@cosmsnap/snapper';
import { DeliverTxResponse } from "@cosmjs/stargate";

const msgs = [
    {
        typeUrl: "/cosmos.bank.v1beta1.MsgSend",
        value: {
            fromAddress: senderAddress,
            toAddress: recipientAddress,
            amount: [{
                denom: "uatom",
                amount: "500000"
            }],
        },
    }
];
const fees = {
    amount: [{
        denom: "uatom",
        amount: "500"
    }],
    gas: "200000"
};
let txResult: DeliverTxResponse = await sign("cosmoshub-4", msgs, fees);

Add Address to Address Book.

import { addAddressToBook } from '@cosmsnap/snapper';
await addAddressToBook("cosmoshub-4", "cosmos123456789", "John Doe");

Get Address Book

import { getAddressBook } from '@cosmsnap/snapper';
let book: Address[] = await getAddressBook();

Delete Address from Address Book

import { deleteAddressFromBook } from '@cosmsnap/snapper';
await deleteAddressFromBook("cosmos123456789");

Get All Bech32 Addresses

Gets all the Bech32 addresses for all chains in state. Note, this is an expensively heavy operation so use wisely.

import { getBech32Addresses } from '@cosmsnap/snapper';
let allAddresses: CosmosAddress[] = await getBech32Addresses();

Get All Bech32 Addresses

Gets a Bech32 address for a chain.

import { getBech32Addresses } from '@cosmsnap/snapper';
let address: CosmosAddress = await getBech32Address("cosmoshub-4");

版本与发布记录

@cosmsnap/snapper 共发布 40 个版本,首次发布于 2023/09/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.7latest2024/08/292 年前
0.2.62024/08/202 年前
0.1.322024/08/202 年前
0.2.52024/08/022 年前
0.2.42024/06/272 年前
0.1.312024/06/272 年前

关于 @cosmsnap/snapper 的常见问题

@cosmsnap/snapper 是什么?

A helper package with utilities to interact with the Cosmos Extension for MetaMask.

如何安装 @cosmsnap/snapper?

在项目目录运行 npm install @cosmsnap/snapper,或使用 pnpm add / yarn add / bun add。

@cosmsnap/snapper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cosmsnap/snapper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cosmsnap/snapper 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cosmsnap/snapper 可以商用吗?

@cosmsnap/snapper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cosmsnap/snapper 的下载量有多少?

根据 npm 官方下载统计,@cosmsnap/snapper 在 2026/10/11 – 2026/11/09 这 30 天内被下载 24,175 次,日均约 806 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cosmsnap/snapper 还在维护吗?

最新版本 0.2.7 发布于 2024/08/29(2 年前);至今共发布 40 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。