@acctoken/accjs 中文文档教程

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

accjs npm

用于使用 ACC RPC API 与基于 ACC 的区块链集成的 ​​Javascript API。

文档可以在这里找到

Installation

NPM

官方分发包可以在npm

Add dependency to your project

yarn add accjs

Browser Distribution

在本地克隆此存储库,然后运行 ​​yarn build-web。 浏览器分布将位于 dist-web 中,可以直接复制到您的项目存储库中。 dist-web 文件夹包含准备用于生产的缩小包,以及用于调试的库的源映射版本。 有关完整的浏览器使用示例,请参阅文档

Import

ES Modules

使用 TypeScript、webpack带有 --experimental-modules 标志

import { Api, JsonRpc, RpcError } from 'accjs';
import { JsSignatureProvider } from 'accjs/dist/accjs-jssig';           // development only

CommonJS

的 Node.js 开箱即用的 Node.js 支持使用 commonJS 语法导入。

const { Api, JsonRpc, RpcError } = require('accjs');
const { JsSignatureProvider } = require('accjs/dist/accjs-jssig');      // development only
const fetch = require('node-fetch');                                    // node only; not needed in browsers
const { TextEncoder, TextDecoder } = require('util');                   // node only; native TextEncoder/Decoder
const { TextEncoder, TextDecoder } = require('text-encoding');          // React Native, IE11, and Edge Browsers only

Basic Usage

Signature Provider

签名提供者持有私钥并负责签署交易。

在浏览器中使用 JsSignatureProvider 是不安全的,只能用于开发目的。 在网页上下文之外使用安全保险库以确保在生产中签署交易时的安全性

const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // bob
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

JSON-RPC

打开与 JSON-RPC 的连接,在 Node.js 上包括 fetch

const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });

API

在 Node、React Native、IE11 或 Edge 浏览器中使用时包括 textDecoder 和 textEncoder。

const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

Sending a transaction

transact() 用于通过可选的配置对象参数签署交易并将其推送到区块链上。 此参数可以覆盖 broadcast: true 的默认值,并可用于填充给定 blocksBehindexpireSeconds 的 TAPOS 字段。 如果没有配置选项,交易预计将使用 TAPOS 字段(expirationref_block_numref_block_prefix)解包,并自动广播到链上.

(async () => {
  const result = await api.transact({
    actions: [{
      account: 'acc.token',
      name: 'transfer',
      authorization: [{
        actor: 'useraaaaaaaa',
        permission: 'active',
      }],
      data: {
        from: 'useraaaaaaaa',
        to: 'useraaaaaaab',
        quantity: '0.0001 SYS',
        memo: '',
      },
    }]
  }, {
    blocksBehind: 3,
    expireSeconds: 30,
  });
  console.dir(result);
})();

Error handling

使用 RpcError 处理 RPC 错误

...
try {
  const result = await api.transact({
  ...
} catch (e) {
  console.log('\nCaught exception: ' + e);
  if (e instanceof RpcError)
    console.log(JSON.stringify(e.json, null, 2));
}
...

Contributing

贡献指南

行为准则

License

MIT

Important

有关版权和许可条款,请参阅许可。 Block.one 作为 ACC 社区的成员在自愿的基础上做出贡献,不负责确保软件或任何相关应用程序的整体性能。 我们对软件或任何相关文档不作任何明示或暗示的陈述、保证、担保或承诺,包括但不限于适销性、特定用途适用性和不侵权等方面的保证。 在任何情况下,我们均不对任何索赔、损害或其他责任负责,无论是在合同诉讼、侵权诉讼还是其他诉讼中,因软件或文档或软件或软件的使用或其他交易而产生或与之相关的文档。 任何测试结果或性能数据都是指示性的,不会反映所有条件下的性能。 任何对任何第三方或第三方产品、服务或其他资源的引用均不是 Block.one 的背书或推荐。 对于您使用或依赖任何这些资源,我们不承担任何责任,也不承担任何责任和义务。 第三方资源可能随时更新、更改或终止,因此此处的信息可能已过时或不准确。

accjs npm

Javascript API for integration with ACC-based blockchains using ACC RPC API.

Documentation can be found here

Installation

NPM

The official distribution package can be found at npm.

Add dependency to your project

yarn add accjs

Browser Distribution

Clone this repository locally then run yarn build-web. The browser distribution will be located in dist-web and can be directly copied into your project repository. The dist-web folder contains minified bundles ready for production, along with source mapped versions of the library for debugging. For full browser usage examples, see the documentation.

Import

ES Modules

Importing using ESM syntax is supported using TypeScript, webpack, or Node.js with --experimental-modules flag

import { Api, JsonRpc, RpcError } from 'accjs';
import { JsSignatureProvider } from 'accjs/dist/accjs-jssig';           // development only

CommonJS

Importing using commonJS syntax is supported by Node.js out of the box.

const { Api, JsonRpc, RpcError } = require('accjs');
const { JsSignatureProvider } = require('accjs/dist/accjs-jssig');      // development only
const fetch = require('node-fetch');                                    // node only; not needed in browsers
const { TextEncoder, TextDecoder } = require('util');                   // node only; native TextEncoder/Decoder
const { TextEncoder, TextDecoder } = require('text-encoding');          // React Native, IE11, and Edge Browsers only

Basic Usage

Signature Provider

The Signature Provider holds private keys and is responsible for signing transactions.

Using the JsSignatureProvider in the browser is not secure and should only be used for development purposes. Use a secure vault outside of the context of the webpage to ensure security when signing transactions in production

const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // bob
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

JSON-RPC

Open a connection to JSON-RPC, include fetch when on Node.js.

const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });

API

Include textDecoder and textEncoder when using in Node, React Native, IE11 or Edge Browsers.

const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

Sending a transaction

transact() is used to sign and push transactions onto the blockchain with an optional configuration object parameter. This parameter can override the default value of broadcast: true, and can be used to fill TAPOS fields given blocksBehind and expireSeconds. Given no configuration options, transactions are expected to be unpacked with TAPOS fields (expiration, ref_block_num, ref_block_prefix) and will automatically be broadcast onto the chain.

(async () => {
  const result = await api.transact({
    actions: [{
      account: 'acc.token',
      name: 'transfer',
      authorization: [{
        actor: 'useraaaaaaaa',
        permission: 'active',
      }],
      data: {
        from: 'useraaaaaaaa',
        to: 'useraaaaaaab',
        quantity: '0.0001 SYS',
        memo: '',
      },
    }]
  }, {
    blocksBehind: 3,
    expireSeconds: 30,
  });
  console.dir(result);
})();

Error handling

use RpcError for handling RPC Errors

...
try {
  const result = await api.transact({
  ...
} catch (e) {
  console.log('\nCaught exception: ' + e);
  if (e instanceof RpcError)
    console.log(JSON.stringify(e.json, null, 2));
}
...

Contributing

Contributing Guide

Code of Conduct

License

MIT

Important

See LICENSE for copyright and license terms. Block.one makes its contribution on a voluntary basis as a member of the ACC community and is not responsible for ensuring the overall performance of the software or any related applications. We make no representation, warranty, guarantee or undertaking in respect of the software or any related documentation, whether expressed or implied, including but not limited to the warranties or merchantability, fitness for a particular purpose and noninfringement. In no event shall we be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or documentation or the use or other dealings in the software or documentation. Any test results or performance figures are indicative and will not reflect performance under all conditions. Any reference to any third party or third-party product, service or other resource is not an endorsement or recommendation by Block.one. We are not responsible, and disclaim any and all responsibility and liability, for your use of or reliance on any of these resources. Third-party resources may be updated, changed or terminated at any time, so the information here may be out of date or inaccurate.

更多

友情链接

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