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.
Service | Master | Develop |
---|---|---|
Unit Tests | ||
Coverage |
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.
ToC
- Type Doc
- Introduction
- Prerequisites
- Installation
- Modules
- CORE
- RPC
- Utils
- Errors
- Examples
- Troubleshooting
- Development Process
@nervosnetwork/ckb-sdk-core
is the SDK used to interact with Nervos CKB, which is an open source project of public blockchain.
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.
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.
@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.
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.
$ yarn add @nervosnetwork/ckb-sdk-core # install the SDK into your project
This SDK includes several modules:
RPC Code
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.
Utils Code
The Utils module provides useful methods for other modules.
Types Code
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.
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.
Please see Basic RPC
/**
* 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()
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 })
- RPC Errors
The rpc module will throw an error when the result contains an error field, you need to handle it manually.
- Send Simple Transaction
- Send All Balance
- Send Transaction with multiple private key
- Deposit to and withdraw from Nervos DAO
- Send Transaction with Lumos Collector
- SUDT
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.
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.