/ckb-sdk-js

JavaScript SDK for CKB

Primary LanguageTypeScriptMIT LicenseMIT

CKB SDK JavaScript

Service Master Develop
Travis Build Status Build Status
Coverage Codecov Codecov
NPM NPM NPM

Telegram Group License

JavaScript SDK for Nervos CKB.

ToC


Introduction

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

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.

Noteice: 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 a 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 cuz it's source code is implemented by TypeScript, which is a superset of JavaScript and compiled into ES6. For some browsers with old versions, some polyfills might be injected.

Prerequisites

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

For the developers who are interested in contribution.

This project depends on , which depends on a C library. Due to that, the tiny-secp256k1 might require rebuilding in some cases.

$ yarn rebuild tiny-secp256k1 # rebuild tiny-secp256k1

If you still encounter problems, please read this guide at secp256k1-node, as the build instruction shoud be exactly the same.

Installation

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

Modules

This SDK includes several modules:

Address Code

Used to create an address object, whose value is the address we are going to use.

Default address algorithm is the pubkeyToAddress in utils module, which generates it in bech32 format.

Default rule to generate the address from a public key is:

  • Blake160(public key): blake2b(public key) then trauncate it for fist 20 bytes.
  • Specify options used: Address Type, Address Bin Index, Prefix. The options will be explained in an RFC.
  • Bech32 the blake160ed public key with specified options: bech32Address(blake160Pubkey, {prefix, type, binIndex})
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.

Wallet Code

The wallet module used to be a demo, will be deprecated in the future for its fuzzy concept.

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 above modules are integrated into the core module. You can find rpc, utils, wallet in the core instance.

The address module has not been integrated yet, and the wallet module will be removed in the future.

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 CKBCore = require('@nervosnetwork/ckb-sdk-core').default

const node = {
  url: 'http://localhost:8114,
}

const core = new CKBCore(node)

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

RPC

TODO:

Errors

  1. RPC Errors

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

Related Projects

  1. Neuron: a blockchain wallet for CKB.
  2. CLI: a terminal tool based on this SDK.