/ckb-sdk-js

JavaScript SDK for CKB

Primary LanguageJavaScriptMIT LicenseMIT

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.

ToC


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:

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.

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.