JSPM

eosjs

20.0.0-beta2.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 189697
  • Score
    100M100P100Q161018F
  • License MIT

Talk to eos API

Package Exports

  • eosjs
  • eosjs/dist
  • eosjs/dist/eosjs-jssig
  • eosjs/dist/eosjs-numeric
  • eosjs/dist/eosjs-serialize
  • eosjs/dist/ripemd

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eosjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

⚠️ 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

npm install eosjs@beta

Basic Usage

Browser

Importing using ES6 module syntax in the browser is supported if you have a transpiler, such as Babel.

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

// If using default signature provider
import JsSignatureProvider from 'eosjs/dist/eosjs-jssig'

NodeJS

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

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

SignatureProvider

SignatureProvider holds private keys and is responsible for signing transactions.

Using the default 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:8000', { fetch });

API Constructor

Include textDecoder and textEncoder when using in browser.

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

Sending a transaction

(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));
}
...

Browsers

After running npm run build-web or yarn build-web, the browser distribution will be located in dist. For full browser usage examples, see the documentation.

How it works

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.

Running Tests

Automated Test Suite

npm run test or yarn test