Packages

@ckb-ccc/core

The foundational CCC primitives — Transaction, Script, Address, Client, Signer, and more.

Edit on GitHub

@ckb-ccc/core is the base layer of CCC. It contains all CKB data types, encoders, hashers, and the abstract Signer / Client interfaces used by every higher-level package.

Most projects do not need to install @ckb-ccc/core directly. Use @ckb-ccc/shell for Node.js or @ckb-ccc/connector-react for React — both re-export everything from core.

Installation

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

Imports

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

Exported modules

@ckb-ccc/core re-exports the following sub-modules under the ccc namespace:

ModuleContents
addressAddress, AddressLike, address parsing and formatting
bytesbytesFrom, bytesTo, byte array utilities
ckbTransaction, Script, Cell, CellInput, CellOutput, OutPoint, WitnessArgs
clientClient (abstract), ClientPublicMainnet, ClientPublicTestnet
fixedPointfixedPointFrom, fixedPointToString — CKB capacity in shannons
hasherhashCkb, Hasher — CKB Blake2b hashing
hexhexFrom, HexLike, hex encoding helpers
jsonRpcRequestorJsonRpc — low-level JSON-RPC client
keystoreKeyStore — encrypted key storage
moleculeMolecule codec primitives
numnumFrom, numFromBytes, numToBytes, numLeToBytes — numeric conversions
signerSigner (abstract), SignerInfo, SignerType, SignerSignType
utilsMiscellaneous helpers

Key classes

Transaction

The primary type for building CKB transactions:

// Build a CKB transfer transaction
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);

Important methods:

MethodDescription
Transaction.from(like)Create from a plain object
tx.addInput(cell)Add a cell input
tx.addOutput(output, data?)Add a cell output
tx.completeInputsByCapacity(signer)Auto-select inputs to cover capacity
tx.completeFeeBy(signer, feeRate?)Add a change output and compute fee
tx.addCellDepInfos(client, deps)Resolve and add cell dependencies
signer.sendTransaction(tx)Sign and broadcast

Script

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

script.eq(otherScript); // structural equality

Address

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

Signer (abstract)

The Signer abstract class is implemented by every wallet integration. Key members:

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 (abstract)

// Connect to public testnet
const client = new ccc.ClientPublicTestnet();

// Connect to public mainnet
const client = new ccc.ClientPublicMainnet();

// Connect to a custom node
const client = new ccc.ClientPublicTestnet("https://my-node.example.com/rpc");

Key functions

// Convert any value to a fixed-point CKB capacity (shannons)
const capacity = ccc.fixedPointFrom("100"); // 100 CKB = 10_000_000_000n shannons

// Hash data with CKB's Blake2b
const hash = ccc.hashCkb(data);

// Encode to hex
const hex = ccc.hexFrom(bytes);

// Numeric conversion
const n = ccc.numFrom("0xff"); // BigInt
const bytes = ccc.numToBytes(n, 8); // little-endian Uint8Array

// Byte conversions
const arr = ccc.bytesFrom("0xdeadbeef");
const str = ccc.bytesTo(arr, "utf8");

On this page