General purpose library for the EOS blockchain.
Clone or download
jeffreyssmith2nd Merge pull request #464 from channprj/master
Update: Support reverse order and show payer option - Fix #324
Latest commit 41bb99b Dec 20, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE Update issue templates Oct 4, 2018
docs Update: Support reverse order and show payer option - Fix #324 Dec 20, 2018
scripts publish latest and beta releases on tag from master Dec 3, 2018
src Update: Support reverse order and show payer option - Fix #324 Dec 20, 2018
.babelrc remove polyfill from src Oct 23, 2018
.gitignore Docs: typedoc.json, static docs, config Oct 1, 2018
.gitmodules Docs: typedoc.json, static docs, config Oct 1, 2018
.npmrc.template includes template for npmrc Oct 25, 2018
.travis.yml publish latest and beta releases on tag from master Dec 3, 2018
LICENSE.txt Move license to LICENSE.txt Jun 4, 2018
README.md Update: Support reverse order and show payer option - Fix #324 Dec 20, 2018
package.json Merge branch 'develop' into beta3-prep Dec 6, 2018
tsconfig.json file clean up all around, always strict flag, moved setupJest Oct 18, 2018
tsconfig.test.json Cleaned up tsconfigs a bit Sep 5, 2018
tsconfig.web.json minified web build and separate debug build created automatically, fi… Dec 5, 2018
tslint.json api 1.1: variant and binary extensions Sep 18, 2018
typedoc.json Rename eosjs2 -> eosjs Oct 2, 2018
webpack.debug.js minified web build and separate debug build created automatically, fi… Dec 5, 2018
webpack.prod.js minified web build and separate debug build created automatically, fi… Dec 5, 2018
yarn.lock locked dependencies to working version in yarn.lock and bumped version Dec 6, 2018

README.md

⚠️ Important! We recently released a major breaking rewrite for eosjs. Be sure to lock your dependencies. ⚠️

If you are looking for the the previous version of eosjs you can find it here.

eosjs

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

Documentation can be found here

Installation

NodeJS Dependency

npm install eosjs@beta or yarn add eosjs@beta

Browser Distribution

Clone this repository locally then run npm run build-web or 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 ES6 module syntax in the browser is supported if you have a transpiler, such as Babel.

import { Api, JsonRpc, RpcError } from 'eosjs';

import JsSignatureProvider from 'eosjs/dist/eosjs-jssig'; // development only

CommonJS

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

const { Api, JsonRpc, RpcError } = require('eosjs');
const JsSignatureProvider = require('eosjs/dist/eosjs-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"; // useraaaaaaaa
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

JSON-RPC

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

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

API

Include textDecoder and textEncoder when using in browser.

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: 'eosio.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));
}
...

Running Tests

Automated Unit Test Suite

npm run test or yarn test

Web Integration Test Suite

Run npm run build-web to build the browser distrubution then open src/tests/web.html in the browser of your choice. The file should run through 6 tests, relaying the results onto the webpage with a 2 second delay after each test. The final 2 tests should relay the exceptions being thrown onto the webpage for an invalid transaction and invalid rpc call.