@cosmos-client/core

中文文档
0.47.4发布于 2023/10/18ISCESM + CJS近 30 天下载 3258

Cosmos SDK 块链的 REST API 客户端

中文使用文档
文档目录8 节
  1. 安装
  2. 谨慎
  3. Discord 社区
  4. 插件模块
  5. 实例
  6. 前端开发者
  7. 用于图书馆开发者
  8. 页:1 proto.d.ts 错误

@ 宇宙客户/核心

Cosmos SDK 块链的 REST API 客户端

安装

npm install @cosmos-client/core

谨慎

软件包名称 cosmos-client 已贬值。 请使用 @cosmos-client/core 换句话说.

Discord 社区

邀请链接

插件模块

实例

import cosmosclient from '@cosmos-client/core';
import Long from 'long';

describe('bank', () => {
  it('send', async () => {
    expect.hasAssertions();

    const sdk = new cosmosclient.CosmosSDK('http://localhost:1317', 'testchain');

    const privKey = new cosmosclient.proto.cosmos.crypto.secp256k1.PrivKey({
      key: await cosmosclient.generatePrivKeyFromMnemonic('joke door law post fragile cruel torch silver siren mechanic flush surround'),
    });
    const pubKey = privKey.pubKey();
    const address = cosmosclient.AccAddress.fromPublicKey(pubKey);

    expect(address.toString()).toStrictEqual('cosmos14ynfqqa6j5k3kcqm2ymf3l66d9x07ysxgnvdyx');

    const fromAddress = address;
    const toAddress = address;

    // get account info
    const account = await cosmosclient.rest.auth
      .account(sdk, fromAddress)
      .then((res) => cosmosclient.codec.protoJSONToInstance(cosmosclient.codec.castProtoJSONOfProtoAny(res.data.account)))
      .catch((_) => undefined);

    if (!(account instanceof cosmosclient.proto.cosmos.auth.v1beta1.BaseAccount)) {
      console.log(account);
      return;
    }

    // build tx
    const msgSend = new cosmosclient.proto.cosmos.bank.v1beta1.MsgSend({
      from_address: fromAddress.toString(),
      to_address: toAddress.toString(),
      amount: [{ denom: 'token', amount: '1' }],
    });

    const txBody = new cosmosclient.proto.cosmos.tx.v1beta1.TxBody({
      messages: [cosmosclient.codec.instanceToProtoAny(msgSend)],
    });
    const authInfo = new cosmosclient.proto.cosmos.tx.v1beta1.AuthInfo({
      signer_infos: [
        {
          public_key: cosmosclient.codec.instanceToProtoAny(pubKey),
          mode_info: {
            single: {
              mode: cosmosclient.proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
            },
          },
          sequence: account.sequence,
        },
      ],
      fee: {
        gas_limit: Long.fromString('200000'),
      },
    });

    // sign
    const txBuilder = new cosmosclient.TxBuilder(sdk, txBody, authInfo);
    const signDocBytes = txBuilder.signDocBytes(account.account_number);
    txBuilder.addSignature(privKey.sign(signDocBytes));

    // broadcast
    const res = await cosmosclient.rest.tx.broadcastTx(sdk, {
      tx_bytes: txBuilder.txBytes(),
      mode: rest.tx.BroadcastTxMode.Block,
    });
    console.log(res);

    expect(res.data.tx_response?.raw_log?.match('failed')).toBeFalsy();
  });
});

前端开发者

在类似的东西 polyfill.ts 在Angular(当然在其他前端框架中类似的东西)中,不要忘记输入这个语句. 如果没有这个声明, 串接 JSON 事件的结果包括 Long 类型字段会错误.

import Long from 'long';
import * as $protobuf from 'protobufjs/minimal';

$protobuf.util.Long = Long;
$protobuf.configure();

用于图书馆开发者

安装协议缓冲编译器

sudo apt -y install protobuf-compiler

或者说

brew install protobuf 

使用 星点 测试.

第一个数位主版本和第二个数位小版本应该与宇宙SDK相匹配. 第三位数补丁版本可以独立递增.

页:1 proto.d.ts 错误

在 proto.d.ts 文件结尾处添加以下一行,并在出错点时将 pardmint 替换为 global termint .

import global_tendermint = tendermint;

版本与发布记录

@cosmos-client/core 共发布 34 个版本,首次发布于 2021/10/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.47.4latest2023/10/183 年前
0.47.4-rc12023/10/063 年前
0.47.32023/09/273 年前
0.47.3-rc22023/09/223 年前
0.47.3-rc12023/09/123 年前
0.47.12023/06/023 年前

关于 @cosmos-client/core 的常见问题

@cosmos-client/core 是什么?

Cosmos SDK 块链的 REST API 客户端

如何安装 @cosmos-client/core?

在项目目录运行 npm install @cosmos-client/core,或使用 pnpm add / yarn add / bun add。

@cosmos-client/core 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cosmos-client__core 是否存在。

@cosmos-client/core 支持 ESM 还是 CommonJS?

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

@cosmos-client/core 可以商用吗?

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

@cosmos-client/core 的下载量有多少?

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

@cosmos-client/core 还在维护吗?

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