AI agents: the machine-readable documentation index for this site is at https://docs.ckbccc.com/llms.txt. Append ".md" to any documentation page URL to fetch its canonical Markdown source, which is preferred over rendered HTML for retrieval, indexing, question answering, and code generation.

Product-specific agent operating guidance (read before generating CKB/CCC code): https://docs.ckbccc.com/skill.md

包

@ckb-ccc/core

CCC 核心组件——Transaction、Script、Address、Client、Signer 等。

Edit on GitHub

@ckb-ccc/core 是 CCC 的基础层,包含所有 CKB 数据类型、编解码器、哈希工具,以及供上层所有包使用的抽象 Signer、Client 接口。

大多数项目无需直接安装 @ckb-ccc/core。Node.js 环境使用 @ckb-ccc/shell,React 环境使用 @ckb-ccc/connector-react——两者均重新导出了 core 里的全部内容。

安装

@ckb-ccc/core npm version@ckb-ccc/core npm downloads per week
npm install @ckb-ccc/core

导入

import { ccc } from "@ckb-ccc/core";

导出模块

@ckb-ccc/core 在 ccc 命名空间下重新导出以下子模块:

模块内容
addressAddress、AddressLike,地址解析与格式化
bytesbytesFrom、bytesTo,字节数组工具
ckbTransaction、Script、Cell、CellInput、CellOutput、OutPoint、WitnessArgs
clientClient(抽象类)、ClientPublicMainnet、ClientPublicTestnet
fixedPointfixedPointFrom、fixedPointToString——以 Shannon 为单位的 CKB 容量
hasherhashCkb、Hasher——CKB Blake2b 哈希
hexhexFrom、HexLike,十六进制编码工具
jsonRpcRequestorJsonRpc——底层 JSON-RPC 客户端
keystoreKeyStore——加密密钥存储
moleculeMolecule 编解码基础组件
numnumFrom、numFromBytes、numToBytes、numLeToBytes——数值转换
signerSigner(抽象类)、SignerInfo、SignerType、SignerSignType
utils其他辅助工具

核心类

Transaction

构建 CKB 交易的核心类型:

// 构建一笔 CKB 转账交易
const tx = ccc.Transaction.from({
  outputs: [{ lock: toLock, capacity: ccc.fixedPointFrom(amount) }],
});

await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer);
const txHash = await signer.sendTransaction(tx);

常用方法:

方法说明
Transaction.from(like)从普通对象构造交易
tx.addInput(cell)添加 Cell 输入
tx.addOutput(output, data?)添加 Cell 输出
tx.completeInputsByCapacity(signer)自动选取输入以凑足所需容量
tx.completeFeeBy(signer, feeRate?)添加找零输出并计算手续费
tx.addCellDepInfos(client, deps)解析并添加 Cell 依赖
signer.sendTransaction(tx)签名并广播交易

Script

const script = ccc.Script.from({
  codeHash: "0x...",
  hashType: "type",
  args: "0x...",
});

script.eq(otherScript); // 结构相等性比较

Address

const addr = await ccc.Address.fromString(
  "ckb1qzda0cr08m85hc8jlnfp3gog...",
  client,
);
const { script, prefix } = addr;

Signer(抽象类)

每个钱包集成都实现了 Signer 抽象类。核心成员:

abstract class Signer {
  readonly client: Client;

  abstract getInternalAddress(): Promise<string>;
  abstract getRecommendedAddressObj(preference?: unknown): Promise<Address>;
  abstract signTransaction(tx: Transaction): Promise<Transaction>;

  async sendTransaction(tx: Transaction): Promise<Hex>;
  async findCells(filter, withData?, order?, limit?): AsyncGenerator<Cell>;
}

Client(抽象类)

// 连接公共测试网
const testnetClientOwner = ccc.ClientPublicTestnet.open();
const testnetClient = testnetClientOwner.value;

// 连接公共主网
const mainnetClientOwner = ccc.ClientPublicMainnet.open();
const mainnetClient = mainnetClientOwner.value;

// 连接自定义节点
const customClientOwner = ccc.ClientPublicTestnet.open({
  urls: ["https://my-node.example.com/rpc"],
});
const customClient = customClientOwner.value;

// 不再使用各个 Client 时,释放对应的 Owner。

核心函数

// 将任意值转换为 以 Shannon 为单位的 CKB 容量
const capacity = ccc.fixedPointFrom("100"); // 100 CKB = 10_000_000_000n Shannon

// 使用 CKB 的 Blake2b 算法对数据进行哈希
const hash = ccc.hashCkb(data);

// 编码为十六进制
const hex = ccc.hexFrom(bytes);

// 数值转换
const n = ccc.numFrom("0xff"); // BigInt
const bytes = ccc.numToBytes(n, 8); // 小端序 Uint8Array

// 字节转换
const arr = ccc.bytesFrom("0xdeadbeef");
const str = ccc.bytesTo(arr, "utf8");

Last updated on

On this page