Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nervosnetwork/ckb-types

ckb-js3.5kMIT0.109.5TypeScript support: included

Type module of @nervosnetwork/ckb-sdk-core

readme

⚠️ This SDK is obsolete and maintained passively.

It’s highly recommended to use the CCC SDK as your primary development tool within the CKB ecosystem. CCC is the latest and most robust JavaScript SDK designed to streamline and enhance your development process. By choosing CCC, you'll be leveraging the best practices and most up-to-date features available for CKB development.

To get started with CCC, follow the installation guide and explore the documentation to integrate it seamlessly into your projects.


CKB SDK JavaScript

Service Master Develop
Unit Tests Unit Tests Unit Tests
Coverage Codecov Codecov

NPM Package Quality License Telegram Group SNYK Deploy Docs

JavaScript SDK for Nervos CKB.

The ckb-sdk-js is still under development and aim for providing low-level APIs of data construction. You should get familiar with CKB transaction structure and RPCs before using it and design your own DApp SDK based on this one.

<summary>ToC</summary>


TypeDoc

Introduction

@nervosnetwork/ckb-sdk-core is the SDK used to interact with Nervos CKB, which is an open source project of public blockchain.

Before everything

Due to safety concern, the SDK won’t generate private keys for you. You can use openssl to generate a private key:

$ openssl rand -hex 32

For other cases, say, you're going to generate it in a JavaScript Project(Please don't), Elliptic may be the one you can use.

About Nervos CKB

Nervos CKB is the layer 1 of Nervos Network, a public blockchain with PoW and cell model.

Nervos project defines a suite of scalable and interoperable blockchain protocols. Nervos CKB uses those protocols to create a self-evolving distributed network with a novel economic model, data model and more.

Notice: The ckb process will send stack trace to sentry on Rust panics. This is enabled by default before mainnet, which can be opted out by setting the option dsn to empty in the config file.

About @nervosnetwork/ckb-sdk-core

@nervosnetwork/ckb-sdk-core is an SDK implemented by JavaScript, and published in NPM Registry, which provides APIs for developers to send requests to the CKB blockchain.

This SDK can be used both in Browsers and Node.js as it's source code is implemented by TypeScript, which is a superset of JavaScript and compiled into ES6. For some browsers that have old versions, some polyfills might be injected.

Prerequisites

We are going to use yarn for the next steps, which is similar to npm, so feel free to pick one.

For the developers who are interested in contribution.

Installation

$ yarn add @nervosnetwork/ckb-sdk-core # install the SDK into your project

Modules

This SDK includes several modules:

<summary> RPC Code </summary>

Used to send RPC request to the CKB, the list could be found in CKB Project

Interfaces could be found in DefaultRPC class in this module.

<summary> Utils Code </summary>

The Utils module provides useful methods for other modules.

<summary> Types Code </summary>

The Types module used to provide the type definition of CKB Components according to the CKB Project.

CKB Project compiles to the snake case convetion, which listed in the types/CKB_RPC in the RPC module.

TypeScript compiles to the PascalCase convention, which listed in this module.

CORE

All the modules above are integrated into the core module. You can find rpc and utils in the core instance.

To use the core module, you need to import it in your project and instantiate it with a node object. For now, the node object only contains one field named url, the URI of the blockchain node your are going to communicate with.

const CKB = require('@nervosnetwork/ckb-sdk-core').default

const nodeUrl = 'http://localhost:8114'

const ckb = new CKB(nodeUrl)

After that you can use the ckb object to generate addresses, send requests, etc.

RPC

Basic RPC

Please see Basic RPC

Batch Request

/**
 * The following batch includes two requests
 *   1. getBlock('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
 *   2. getTransactionsByLockHash('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x0', '0x1)
 */
const batch = rpc.createBatchRequest([
  ['getBlock', '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'],
  ['getTransactionsByLockHash', '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x0', '0x1'],
])

/**
 * Add a request and the batch should include three requests
 *  1. getBlock('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
 *  2. getTransactionsByLockHash('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x0', '0x1)
 *  3. getTransactionsByLockHash('0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc', '0x0', '0x1)
 */
batch.add(
  'getTransactionsByLockHash',
  '0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
  '0x0',
  '0x1',
)

/**
 * Remove a request by index and the batch should include two requests
 *  1. getBlock('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
 *  2. getTransactionsByLockHash('0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc', '0x0', '0x1)
 */
batch.remove(1)

/**
 * Send the batch request
 */
batch.exec().then(console.log)
// [
//   { "header": { }, "uncles": [], "transactions": [], "proposals": [] },
//   [ { "consumedBy": { }, "createdBy": { } } ]
// ]

// chaning usage
batch.add().remove().exec()

Persistent Connection

Please add httpAgent or httpsAgent to enable the persistent connection.

If the SDK is running in Node.js, the following steps make the persistent connection available.

// HTTP Agent
const http = require('http')
const httpAgent = new http.Agent({ keepAlive: true })
ckb.rpc.setNode({ httpAgent })

// HTTPS Agent
const https = require('https')
const httpsAgent = new https.Agent({ keepAlive: true })
ckb.rpc.setNode({ httpsAgent })

Utils

Most used utilities

Errors

  1. RPC Errors

The rpc module will throw an error when the result contains an error field, you need to handle it manually.

Examples

  1. Send Simple Transaction
  2. Send All Balance
  3. Send Transaction with multiple private key
  4. Deposit to and withdraw from Nervos DAO
  5. Send Transaction with Lumos Collector
  6. SUDT

Troubleshooting

Indexer Module

The Indexer Module in CKB has been deprecated since v0.36.0, please use ckb-indexer or lumos-indexer instead.

A simple example sendTransactionWithLumosCollector of wokring with lumos has beed added.

Development Process

This project used lerna for packages management, which needs to be bootstrapped by the following steps:

$ yarn add lerna --exact --ignore-workspace-root-check # to install the lerna package in this project, could be skipped if the lerna has been installed globally
$ npx lerna bootstrap # install the depedencies and link the packages in the project
$ yarn run tsc # build packages with tsc command

After the second step, namely the bootstrap, all module packages are linked together, and used as in one package.

changelog

Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

0.109.5 (2025-02-14)

Note: Version bump only for package @nervosnetwork/ckb-types

0.109.4 (2024-11-15)

Note: Version bump only for package @nervosnetwork/ckb-types

0.109.3 (2024-08-01)

Note: Version bump only for package @nervosnetwork/ckb-types

0.109.2 (2024-07-19)

Note: Version bump only for package @nervosnetwork/ckb-types

0.109.2-alpha.1 (2024-07-18)

Note: Version bump only for package @nervosnetwork/ckb-types

0.109.2-alpha.0 (2024-07-16)

Note: Version bump only for package @nervosnetwork/ckb-types

0.109.1 (2024-03-13)

Features

  • add data2 to script hash_type (e887c7a)

0.109.0 (2023-04-26)

Note: Version bump only for package @nervosnetwork/ckb-types

0.107.0 (2023-04-03)

Note: Version bump only for package @nervosnetwork/ckb-types

0.103.1 (2022-05-31)

Note: Version bump only for package @nervosnetwork/ckb-types

0.103.0 (2022-05-09)

Note: Version bump only for package @nervosnetwork/ckb-types

0.102.3 (2022-03-24)

Note: Version bump only for package @nervosnetwork/ckb-types

0.102.2 (2022-03-02)

Note: Version bump only for package @nervosnetwork/ckb-types

0.102.1 (2022-02-17)

Note: Version bump only for package @nervosnetwork/ckb-types

0.102.0 (2022-02-16)

Note: Version bump only for package @nervosnetwork/ckb-types

0.101.0 (2021-10-25)

Features

  • extends hash_type to 'data' | 'type' | 'data1' (#555) (30c49e8)
  • rpc: a new field 'extension' is added to the block body (285a829)
  • rpc: add a new field 'hardforkFeatures' in response of getConsensus (133fe26)
  • rpc: rename 'uncles_hash' to 'extra_hash' in block header (58c055c)

BREAKING CHANGES

  • rpc: Add a new field 'hardforkFeatures' in response of getConsensus

ref https://github.com/nervosnetwork/ckb/pull/2879

  • rpc: A new field 'extension' is added to the block body

ref https://github.com/nervosnetwork/rfcs/pull/224

  • rpc: The field 'uncles_hash' in block header is renamed to 'extra_hash'

ref: https://github.com/nervosnetwork/rfcs/pull/224

0.43.0 (2021-06-29)

Note: Version bump only for package @nervosnetwork/ckb-types

0.41.1 (2021-05-08)

Note: Version bump only for package @nervosnetwork/ckb-types

0.41.0 (2021-05-06)

Note: Version bump only for package @nervosnetwork/ckb-types

0.40.0 (2021-03-08)

Note: Version bump only for package @nervosnetwork/ckb-types

0.39.0 (2021-01-13)

Note: Version bump only for package @nervosnetwork/ckb-types

0.38.2 (2020-11-30)

Note: Version bump only for package @nervosnetwork/ckb-types

0.38.1 (2020-11-27)

Note: Version bump only for package @nervosnetwork/ckb-types

0.38.0 (2020-11-20)

Note: Version bump only for package @nervosnetwork/ckb-types

0.37.0 (2020-10-25)

Features

  • rpc: add rpc#pingPeers, rpc#getTransactionProof, rpc#verifyTransactionProof and rpc#clearBannedAddresses (#506) (61efe5e)

0.36.2 (2020-10-20)

Note: Version bump only for package @nervosnetwork/ckb-types

0.36.1 (2020-09-26)

Note: Version bump only for package @nervosnetwork/ckb-types

0.36.0 (2020-09-21)

Note: Version bump only for package @nervosnetwork/ckb-types

0.35.0 (2020-08-24)

Features

  • rpc: update RPCs (#477)
    • separate basic RPC by group
    • update teh API of rpc#localNodeInfo, rpc#txPoolInfo, and rpc#getPeers
    • add new RPCs: rpc#syncState, rpc#clearTxPool, rpc#setNetworkActive, rpc#addNode and rpc#removeNode

BREAKING CHANGES

  • rpc: rpc#getPeers returns connectedDuration, lastPingDuration, protocols, syncState
  • rpc: rpc#localNodeInfo returns active, connections, protocols

0.34.0 (2020-07-21)

Note: Version bump only for package @nervosnetwork/ckb-types

0.33.0 (2020-06-22)

Note: Version bump only for package @nervosnetwork/ckb-types

0.32.0 (2020-05-26)

Note: Version bump only for package @nervosnetwork/ckb-types

0.31.0 (2020-04-21)

Note: Version bump only for package @nervosnetwork/ckb-types

0.30.0 (2020-03-23)

Features

  • rpc: add a new field in the tx pool info (c1cbac9)
  • rpc: add the new RPC getBlockEconomicState (0c9e248)

0.29.1 (2020-02-28)

Note: Version bump only for package @nervosnetwork/ckb-types

0.29.0 (2020-02-28)

Note: Version bump only for package @nervosnetwork/ckb-types

0.28.0 (2020-02-07)

Note: Version bump only for package @nervosnetwork/ckb-types

0.27.1 (2020-02-01)

Features

  • rpc: add the second paramter outputsValidator in the sendTransaction RPC method (0c7b7b1)

BREAKING CHANGES

  • rpc: Default outputsValidator on sending transactions requires the lock of outputs to be the default lock script

0.27.0 (2020-01-11)

Note: Version bump only for package @nervosnetwork/ckb-types

0.26.4 (2020-01-02)

Features

  • rpc: add the new rpc method of get_capacity_by_lock_hash (9628084)

0.26.3 (2019-12-23)

Note: Version bump only for package @nervosnetwork/ckb-types

0.26.2 (2019-12-16)

Note: Version bump only for package @nervosnetwork/ckb-types

0.26.1 (2019-12-16)

Note: Version bump only for package @nervosnetwork/ckb-types

0.26.0 (2019-12-16)

Features

  • type: update the return type of rpc.getCellsByLockHash (30aa494)

0.25.0 (2019-11-16)

Note: Version bump only for package @nervosnetwork/ckb-types

0.25.0-alpha.1 (2019-11-13)

Note: Version bump only for package @nervosnetwork/ckb-types

0.25.0-alpha.0 (2019-11-12)

Features

  • utils: add parseEpoch method (c29aca6)

0.24.2 (2019-11-08)

Note: Version bump only for package @nervosnetwork/ckb-types

0.24.1 (2019-11-07)

Note: Version bump only for package @nervosnetwork/ckb-types

0.24.0 (2019-11-02)

Features

0.23.1 (2019-10-22)

Note: Version bump only for package @nervosnetwork/ckb-types

0.23.0 (2019-10-19)

Note: Version bump only for package @nervosnetwork/ckb-types

0.22.1 (2019-10-12)

Note: Version bump only for package @nervosnetwork/ckb-types

0.22.0 (2019-10-05)

Features

  • type: update the fields of BlockHeader (55de626)
  • type: update the result of getCellsByLockHash method (31eb97e)
  • type: update the structure of Epoch (76770f4)
  • type: update the type of args (09d649a)
  • type: update the type of witness (71f53b0)

BREAKING CHANGES

  • type: replace difficulty with compactTarget in Epoch
  • type: 1. remove unclesCount
  • merge witnessesRoot and transactionRoot
  • replace difficulty with compactTarget
  • type: change the type of args from string[] to string
  • type: change the type of witnes from { data: string[] } to string
  • type: update the result of getCellsByLockHash method

0.21.1 (2019-09-24)

Bug Fixes

  • rpc: update the returned cell type of getLiveCell from cell to liveCell (4a69d85)

0.21.0 (2019-09-21)

Features

  • rpc: format the outputs of the params formatter (740b403)
  • rpc: update the interface of getLiveCell (0280d7f)

BREAKING CHANGES

  • rpc: update the interface of getLiveCell
  • rpc: hexilize the outputs of the params formatter

0.20.0 (2019-09-07)

Note: Version bump only for package @nervosnetwork/ckb-types

0.19.1 (2019-08-28)

Features

  • rpc: enable custom http agent and https agent (34fca52)

0.19.0 (2019-08-27)

Features

  • type: block header structure changes (ce48faf)
  • type: CellOutput and Epoch structures change (8346ae4)
  • type: Input structure changes (ba16d1b)
  • type: OutPoint structure changes (38ba007)
  • type: rename is_dep_group to dep_type (06c324a)
  • type: set the value of hash_type to "data" and "type" (36a5512)
  • type: Transaction structure changes (30c84bb)
  • type: Transaction structure changes (71625fa)
  • type: Transaction structure changes (df65152)

BREAKING CHANGES

  • type: set the value of hash_type to "data" and "type"
  • type: rename is_dep_group to dep_type
  • type: block header structure changes
  • type: CellOutput and Epoch structures change
  • type: Transaction structure chagnes
  • type: OutPoint structure changes
  • type: Input structure chagnes

0.18.0 (2019-08-10)

Features

  • rpc: add get_cellbase_output_capacity_details rpc (fa3aea3)
  • rpc: add get_header rpc (d2a6bbb)
  • rpc: add rpc of get banned addresses (323b8ac)

0.17.1 (2019-07-29)

Bug Fixes

  • types: remove the hash_type field from the type of cell_out_point (58c019f)

BREAKING CHANGES

  • types: the type of cell out point changes

0.17.0 (2019-07-27)

Features

  • types: add a new field named 'hash_type' in types of script and cell output (2692c0a)

BREAKING CHANGES

  • types: types of script and cell output changed

0.16.0 (2019-07-13)

Note: Version bump only for package @nervosnetwork/ckb-types

0.15.1 (2019-07-12)

Bug Fixes

  • rpc: fix the return type of get_transaction api (a1a5cf4)

0.15.0 (2019-06-29)

Features

  • rpc: add index related rpc (2a7d403)
  • rpc: update get blockchain info rpc (7382458)
  • rpc: update the type of epoch (509a79b)

BREAKING CHANGES

  • rpc: update the type of epoch, remove block_reward, last_block_hash_in_previous_epoch, remainder_reward fields, add epoch_reward field.
  • rpc: replace warnings field with alerts field in the response of get blockchain info rpc

0.14.0 (2019-06-15)

Features

  • types: remove the args field in cell input type (d07f253)

BREAKING CHANGES

  • types: the type of cell input changes, its args field is removed.

0.13.0 (2019-06-01)

Features

  • rpc: add total_tx_cycles and total_tx_size in tx_pool_info (5db06fa)

0.12.0 (2019-05-18)

Features

  • types: generalize outpoint (2a41797)
  • types: rename the proposals_root field in block header to proposals_hash (b2db527)

BREAKING CHANGES

  • types: update type of outpoint
  • types: rename the proposals_root field in block header to proposals_hash

0.11.0 (2019-05-14)

Features

  • types: update basic types in CKB (71d4e5)
  • types: set the type of arg in script to string (2aaa75)
  • types: add shannon as a new capacity unit (910cc5)

BREAKING CHANGES

  • types: update basic types according to ckb-types update

0.10.0 (2019-05-06)

Features

  • types: add field of validSince in cell input of a transaction (54770fc)
  • types: update ckb-types (e6af3b5)

BREAKING CHANGES

  • types: replace type of u64 with type of string in ckb-types, remove version field from script interface
  • types: rpc interface updated, add field of validSince in cell input of a transaction

0.9.1 (2019-04-24)

Bug Fixes

  • types: set the type of arg in script to string (7ea3ad2)

0.9.0 (2019-04-22)

Features

  • rpc: add segrated witnesses (bc8339f)
  • types: update types of script, cell input, cell output (ee405bb)
  • types: remove cellbase id from block header, remove cellbase from uncle block (de9f50d)

BREAKING CHANGES

  • types: block header and uncle block in rpc updated
  • types: script model updated

0.8.0 (2019-04-08)

0.7.0 (2019-03-25)

Note: Version bump only for package @nervosnetwork/ckb-types