Giter Club home page Giter Club logo

ckb-sdk-js's Introduction

CKB SDK JavaScript

Service Master Develop
Travis Build Status Build Status
Coverage Codecov Codecov

NPM Package Quality License Telegram Group

JavaScript SDK for Nervos CKB.

The ckb-sdk-js is still under development and NOT production ready. You should get familiar with CKB transaction structure and RPC before using it.

ToC


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 32 -hex

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

Default RPC

Please see Default RPC

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 })

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

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.

ckb-sdk-js's People

Contributors

keith-cy avatar ashchan avatar dependabot-support avatar dependabot-preview[bot] avatar classicalliu avatar snyk-bot avatar luckyyang avatar louzhixian avatar orangemio avatar mine77 avatar happypeter avatar dependabot[bot] avatar xwartz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.