2ab2-js 中文文档教程

发布于 3年前 浏览 41 项目主页 更新于 3年前

2ab2 Blockchain JS (2ab2-js)

用于 Node.js 和浏览器的纯 JavaScript 2AB2 区块链库。 可用于在 JavaScript 中构造、签署和广播交易,并通过公共 api 轻松地从区块链获取数据。

大部分代码是由 jcalfeee 编写的,我的工作主要是重新打包成一个独立的 npm 包。

Setup

这个库可以通过npm获取:

npm install 2ab2-js

Usage

包含三个子库:ECCChainSerializer。 一般只需要直接使用ECCChain库即可。

Chain

该库提供实用函数来处理区块链状态以及可用于使用特定密钥种子进行简单登录功能的登录类。

Login

登录类使用以下密钥格式:

keySeed = accountName + role + password

使用此种子,为默认角色 active、owner、memo 或指定的角色生成私钥。 密码的最小长度强制为 12 个字符,但建议使用更长的密码。 提供了三种方法:

generateKeys(account, password, [roles])
checkKeys(account, password, auths)
signTransaction(tr)

auths 对象应包含帐户对象中的 auth 数组。 一个例子是这样的:

{
    active: [
        ["GPH5Abm5dCdy3hJ1C5ckXkqUH2Me7dXqi9Y7yjn9ACaiSJ9h8r8mL", 1]
    ]
}

如果 checkKeys 成功,您可以使用 signTransaction 使用该帐户的私钥签署 TransactionBuilder 交易……

State container

Chain 库包含一个完整的状态容器,称为 ChainStore。 ChainStore 将自动配置 set_subscribe_callback 并适当地处理任何传入的状态更改。 它使用 Immutable.js 来存储状态,因此所有对象都作为不可变对象返回。 它有自己的 subscribe 方法,可用于注册一个回调,只要状态发生变化就会被调用。

ChainStore 有几个有用的方法可以使用对象 ID 或资产/账户名称来检索对象、资产和账户。 这些方法是同步的,将返回 undefined 表示正在获取,返回 null 表示对象不存在。

var {Apis} = require("2ab2-ws-js");
var {ChainStore} = require("2ab2-js");

Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then((res) => {
    console.log("connected to:", res[0].network);
    ChainStore.init().then(() => {
        ChainStore.subscribe(updateState);
    });
});

let dynamicGlobal = null;
function updateState(object) {
    dynamicGlobal = ChainStore.getObject("2.1.0");
    console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal);
}

ECC

ECC 库包含用于私钥和公钥以及交易创建/签名的所有加密函数。

Private keys

作为一个简单的例子,下面是如何从种子(例如 brainkey)生成新的私钥:

var {PrivateKey, key} = require("2ab2-js");

let seed = "THIS IS A TERRIBLE BRAINKEY SEED WORD SEQUENCE";
let pkey = PrivateKey.fromSeed( key.normalize_brainKey(seed) );

console.log("\nPrivate key:", pkey.toWif());
console.log("Public key :", pkey.toPublicKey().toString(), "\n");

Transactions

TODO 交易签名示例

ESDoc (beta)

npm i -g esdoc esdoc-es7-plugin
esdoc -c ./esdoc.json
open out/esdoc/index.html

2ab2 Blockchain JS (2ab2-js)

Pure JavaScript 2AB2 Blockchain library for Node.js and browsers. Can be used to construct, sign and broadcast transactions in JavaScript, and to easily obtain data from the blockchain via public apis.

Most of this code was written by jcalfeee, my work was mostly just repackaging to a discrete npm package.

Setup

This library can be obtained through npm:

npm install 2ab2-js

Usage

Three sub-libraries are included: ECC, Chain and Serializer. Generally only the ECC and Chain libraries need to be used directly.

Chain

This library provides utility functions to handle blockchain state as well as a login class that can be used for simple login functionality using a specific key seed.

Login

The login class uses the following format for keys:

keySeed = accountName + role + password

Using this seed, private keys are generated for either the default roles active, owner, memo, or as specified. A minimum password length of 12 characters is enforced, but an even longer password is recommended. Three methods are provided:

generateKeys(account, password, [roles])
checkKeys(account, password, auths)
signTransaction(tr)

The auths object should contain the auth arrays from the account object. An example is this:

{
    active: [
        ["GPH5Abm5dCdy3hJ1C5ckXkqUH2Me7dXqi9Y7yjn9ACaiSJ9h8r8mL", 1]
    ]
}

If checkKeys is successful, you can use signTransaction to sign a TransactionBuilder transaction using the private keys for that account…

State container

The Chain library contains a complete state container called the ChainStore. The ChainStore will automatically configure the set_subscribe_callback and handle any incoming state changes appropriately. It uses Immutable.js for storing the state, so all objects are return as immutable objects. It has its own subscribe method that can be used to register a callback that will be called whenever a state change happens.

The ChainStore has several useful methods to retrieve, among other things, objects, assets and accounts using either object ids or asset/account names. These methods are synchronous and will return undefined to indicate fetching in progress, and null to indicate that the object does not exist.

var {Apis} = require("2ab2-ws-js");
var {ChainStore} = require("2ab2-js");

Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then((res) => {
    console.log("connected to:", res[0].network);
    ChainStore.init().then(() => {
        ChainStore.subscribe(updateState);
    });
});

let dynamicGlobal = null;
function updateState(object) {
    dynamicGlobal = ChainStore.getObject("2.1.0");
    console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal);
}

ECC

The ECC library contains all the crypto functions for private and public keys as well as transaction creation/signing.

Private keys

As a quick example, here's how to generate a new private key from a seed (a brainkey for example):

var {PrivateKey, key} = require("2ab2-js");

let seed = "THIS IS A TERRIBLE BRAINKEY SEED WORD SEQUENCE";
let pkey = PrivateKey.fromSeed( key.normalize_brainKey(seed) );

console.log("\nPrivate key:", pkey.toWif());
console.log("Public key :", pkey.toPublicKey().toString(), "\n");

Transactions

TODO transaction signing example

ESDoc (beta)

npm i -g esdoc esdoc-es7-plugin
esdoc -c ./esdoc.json
open out/esdoc/index.html
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文