celesosjs v1.0.2
celesjs
Javascript API for integration with CELESIO-based blockchains using CELESIO RPC API.
Documentation can be found here
Installation
NodeJS Dependency
yarn add celesosjs
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 ES6 module syntax in the browser is supported if you have a transpiler, such as Babel.
import { Api, JsonRpc, RpcError } from 'celesosjs';
import { JsSignatureProvider } from 'celesosjs/dist/celesjs-jssig'; // development only
CommonJS
Importing using commonJS syntax is supported by NodeJS out of the box.
const { Api, JsonRpc, RpcError } = require('celesosjs');
const { JsSignatureProvider } = require('celesosjs/dist/celesjs-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 NodeJS.
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: 'celes.token',
name: 'transfer',
authorization: [{
actor: 'useraaaaaaaa',
permission: 'active',
}],
data: {
from: 'useraaaaaaaa',
to: 'useraaaaaaab',
quantity: '0.0001 CELES',
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));
}
...
Contribution
Check out the Contributing guide and please adhere to the Code of Conduct.