Getting Started
This documentation is for developers using caver-js v1.5.0 or higher. If you are using an older version, see Getting Started (~v1.4.1).
Prerequisites
Dependencies
The following packages are required to use the caver-js library.
Note caver-js can run on Node.js versions 12 and 14. The recommended versions are as follows:
If you use a different version of the Node (for example, Node v15), utilize the Node Version Manager(nvm) to install and use the version supported by caver-js.
Installation
To try it out, install caver-js with npm using the following command:
$ npm install caver-js
Note: package.json
file should exist on the same install path. If it does not exist, package.json
can be generated via npm init
.
To install a specific version of caver-js, try the following command:
$ npm install caver-js@X.X.X
Starting with caver-js
Once you have finished installing caver-js, you can now connect to a kaia Node using caver-js.
To practice the examples below, first create a test file in the working directory.
$ touch test.js
You can see the test.js
file created in the working directory.
Write the following code in test.js.
// test.jsconst Caver = require('caver-js')const caver = new Caver('https://public-en-kairos.node.kaia.io/')async function testFunction() { const version = await caver.rpc.klay.getClientVersion() console.log(version)}testFunction()
Running the above code gives you the following result.
$ node ./test.jskaia/v1.4.0/linux-amd64/go1.14.1
If you see the output of console.log like above, proceed with the steps below. The version number can be different according to the version of the connected kaia node.
Connecting to a kaia Node
You can import the caver-js module and connect it to a kaia Node in the Kairos testnet as shown in the example below:
const Caver = require('caver-js')const caver = new Caver('https://public-en-kairos.node.kaia.io/')
If you are running an EN, you can connect it to your own node by changing the host and port like below:
const Caver = require('caver-js')const caver = new Caver('https://your.en.url:8651/')
Managing Keyrings
Keyring is a structure that contains the address of the kaia account and the private key(s).
Keyring can be classified into three types depending on the type of key being stored: SingleKeyring to store one address and one private key, MultipleKeyring to store one address and multiple private keys, and RoleBasedKeyring to store one address and one or more private keys for each role.
SingleKeyring defines key
property inside, and this key
store one private key.
MultipleKeyring defines keys
property inside, and this keys
is implemented as an array to store multiple private keys.
The keys
property defined in RoleBasedKeyring is implemented as a two-dimensional array (empty keys
will look like [ [], [], [] ]
) that can include multiple keys for each role. The first element of the array is filled with the private key(s) to be used for roleTransactionKey
, the second element the private key(s) to be used for roleAccountUpdateKey
, and the third element the private key(s) to be used for roleFeePayerKey
.
Creating a Keyring
Generating a SingleKeyring
You can randomly generate a single keyring as shown below.
// test.jsconst Caver = require('caver-js')const caver = new Caver('https://public-en-kairos.node.kaia.io/')async function testFunction() { const keyring = caver.wallet.keyring.generate() console.log(keyring)}testFunction()
Running the above code gives you the following result.
$ node ./test.jsSingleKeyring { _address: '0x3d263c3c0df60c5516f932d244531742f45eed5c', _key: PrivateKey { _privateKey: '0x{private key}' }}
The execution result is shown above. Member variables defined inside the instance can be accessed through keyring.address
and keyring.key
.
Creating a SingleKeyring from private key
Also, if you own a specific private key, you can use it to create a keyring as shown below.
// test.jsconst Caver = require('caver-js')const caver = new Caver('https://public-en-kairos.node.kaia.io/')async function testFunction() { // Create a keyring from a private key const keyringFromPrivateKey = caver.wallet.keyring.createFromPrivateKey('0x{private key}') console.log(keyringFromPrivateKey)}testFunction()
Running the above code gives you the following result.
$ node ./test.jsSingleKeyring { _address: '0xf5a9079f311f9ec55170af351627aff0c5d2e287', _key: PrivateKey { _privateKey: '0x{private key}' } }
The result of caver.wallet.keyring.createFromPrivateKey
, like the result of caver.wallet.keyring.generate
above, is a SingleKeyring instance with an address defined inside it and a [PrivateKey] instance in keyring.key
.