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:
1
$ npm install caver-js
Copied!
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:
1
$ npm install [email protected]
Copied!

Sending KLAY at a glance

This section describes a simple example of using a keystore file to send KLAY with a value transfer transaction. The keystore file can be created in Klaytn Wallet. If you need KLAY for testing, you can get Baobab testnet KLAY from the Klaytn Wallet.
1
const fs = require('fs')
2
const Caver = require('./index')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Read keystore json file
7
const keystore = fs.readFileSync('./keystore.json', 'utf8')
8
9
// Decrypt keystore
10
const keyring = caver.wallet.keyring.decrypt(keystore, 'password')
11
console.log(keyring)
12
13
// Add to caver.wallet
14
caver.wallet.add(keyring)
15
16
// Create value transfer transaction
17
const vt = caver.transaction.valueTransfer.create({
18
from: keyring.address,
19
to: '0x8084fed6b1847448c24692470fc3b2ed87f9eb47',
20
value: caver.utils.toPeb(1, 'KLAY'),
21
gas: 25000,
22
})
23
24
// Sign to the transaction
25
const signed = await caver.wallet.sign(keyring.address, vt)
26
27
// Send transaction to the Klaytn blockchain platform (Klaytn)
28
const receipt = await caver.rpc.klay.sendRawTransaction(signed)
29
console.log(receipt)
30
}
31
32
testFunction()
Copied!

Starting with caver-js

Once you have finished installing caver-js, you can now connect to a Klaytn Node using caver-js.
To practice the examples below, first create a test file in the working directory.
1
$ touch test.js
Copied!
You can see the test.js file created in the working directory.
Write the following code in test.js.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const version = await caver.rpc.klay.getClientVersion()
7
console.log(version)
8
}
9
10
testFunction()
Copied!
Running the above code gives you the following result.
1
$ node ./test.js
2
Klaytn/v1.4.0/linux-amd64/go1.14.1
Copied!
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 Klaytn node.

Connecting to a Klaytn Node

You can import the caver-js module and connect it to a Klaytn Node in the Baobab testnet as shown in the example below:
1
const Caver = require('caver-js')
2
const caver = new Caver('https://your.en.url:8651/')
Copied!
If you are running an EN, you can connect it to your own node by changing the host and port like below:
1
const Caver = require('caver-js')
2
const caver = new Caver('http://localhost:8551/')
Copied!

Managing Keyrings

Keyring is a structure that contains the address of the Klaytn 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.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const keyring = caver.wallet.keyring.generate()
7
console.log(keyring)
8
}
9
10
testFunction()
Copied!
Running the above code gives you the following result.
1
$ node ./test.js
2
SingleKeyring {
3
_address: '0x3d263c3c0df60c5516f932d244531742f45eed5c',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!
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.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Create a keyring from a private key
7
const keyringFromPrivateKey = caver.wallet.keyring.createFromPrivateKey('0x{private key}')
8
console.log(keyringFromPrivateKey)
9
}
10
11
testFunction()
Copied!
Running the above code gives you the following result.
1
$ node ./test.js
2
SingleKeyring {
3
_address: '0xf5a9079f311f9ec55170af351627aff0c5d2e287',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!
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.

Creating a SingleKeyring with a private key and an address

If your private key for your Klaytn account is decoupled from the address, you can create a keyring using the given address and the given private key like below.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Create a keyring with an address and a private key
7
const keyring = caver.wallet.keyring.createWithSingleKey('0x{address in hex}', '0x{private key}')
8
console.log(keyring)
9
10
// Create a keyring from a KlaytnWalletKey
11
const keyringFromKlaytnWalletKey = caver.wallet.keyring.createFromKlaytnWalletKey('0x{private key}0x{type}0x{address in hex}')
12
console.log(keyringFromKlaytnWalletKey)
13
}
14
15
testFunction()
Copied!
Run the code in your console like below.
1
$ node ./test.js
2
SingleKeyring {
3
_address: '0x17e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
6
SingleKeyring {
7
_address: '0x17e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24',
8
_key: PrivateKey { _privateKey: '0x{private key}' }
9
}
Copied!

Creating a MultipleKeyring with multiple private keys

If you want to use multiple private keys, you can create a MultipleKeyring using an address and multiple private keys. The below examples show how to create a MultipleKeyring with multiple private keys.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Create a keyring with an address and private keys
7
const keyring = caver.wallet.keyring.createWithMultipleKey('0x{address in hex}', [ '0x{private key1}', '0x{private key2}' ])
8
console.log(keyring)
9
}
10
11
testFunction()
Copied!
Running the above code gives you the following result.
1
$ node ./test.js
2
MultipleKeyring {
3
_address: '0x17e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24',
4
_keys: [
5
PrivateKey { _privateKey: '0x{private key1}' },
6
PrivateKey { _privateKey: '0x{private key2}' }
7
]
8
}
Copied!
As you can see, _keys has multiple PrivateKey instances in the array. Member variables defined inside the instance can be accessed through keyring.address and keyring.keys.

Creating a RoleBasedKeyring with private keys

To use different private key(s) for each role, caver.wallet.keyring.createWithRoleBasedKey is used instead. Each array element represents a role described in RoleBasedKeyring. The example below shows how to create a RoleBasedKeyring instance from different keys for each role.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Create a keyring with an address and private keys defined by each roles
7
const keyring = caver.wallet.keyring.createWithRoleBasedKey('0x{address in hex}', [
8
[ '0x{private key1}', '0x{private key2}', '0x{private key3}' ],
9
[ '0x{private key4}'],
10
[ '0x{private key5}', '0x{private key6}' ],
11
])
12
console.log(keyring)
13
}
14
15
testFunction()
Copied!
Running the above code gives you the following result.
1
$ node ./test.js
2
RoleBasedKeyring {
3
_address: '0x17e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24',
4
_keys: [
5
[
6
PrivateKey { _privateKey: '0x{private key1}' },
7
PrivateKey { _privateKey: '0x{private key2}' },
8
PrivateKey { _privateKey: '0x{private key3}' }
9
],
10
[ PrivateKey { _privateKey: '0x{private key4}' } ],
11
[
12
PrivateKey { _privateKey: '0x{private key5}' },
13
PrivateKey { _privateKey: '0x{private key6}' }
14
]
15
]
16
}
Copied!
Looking at the output above, the first element of the keys array, roleTransactionKey, has three PrivateKey instances, and the second element, roleAccountUpdateKey, has one PrivateKey instance. And the last element of the array, roleFeePayerKey, has two PrivateKey instances.
Note: Calling functions related to keyring (caver.wallet.keyring) or wallet (caver.wallet) do not affect the actual Klaytn blockchain platform (Klaytn).

Adding Keyrings to caver-js

You can use a keyring easily by using the in-memory wallet provided by caver-js. The following examples illustrate how to add a keyring to a wallet using a keyring instance and a keystore file generated by Klaytn Wallet.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Using a keyring instance
7
const keyring = caver.wallet.keyring.generate()
8
caver.wallet.add(keyring)
9
console.log(caver.wallet.getKeyring(keyring.address))
10
11
// Using a keystore file
12
const decrypted = caver.wallet.keyring.decrypt({
13
version: 4,
14
id: '9c12de05-0153-41c7-a8b7-849472eb5de7',
15
address: '0xc02cec4d0346bf4124deeb55c5216a4138a40a8c',
16
keyring: [
17
{
18
ciphertext: 'eacf496cea5e80eca291251b3743bf93cdbcf7072efc3a74efeaf518e2796b15',
19
cipherparams: { iv: 'd688a4319342e872cefcf51aef3ec2da' },
20
cipher: 'aes-128-ctr',
21
kdf: 'scrypt',
22
kdfparams: {
23
dklen: 32,
24
salt: 'c3cee502c7157e0faa42386c6d666116ffcdf093c345166c502e23bc34e6ba40',
25
n: 4096,
26
r: 8,
27
p: 1
28
},
29
mac: '4b49574f3d3356fa0d04f73e07d5a2a6bbfdd185bedfa31f37f347bc98f2ef26'
30
}
31
]
32
}, 'password')
33
34
caver.wallet.add(decrypted)
35
console.log(caver.wallet.getKeyring(decrypted.address))
36
}
37
38
testFunction()
Copied!
Run the in your console.
1
$ node ./test.js
2
SingleKeyring {
3
_address: '0x66391720b488a3fb2c7c69d99cd4cd6e23ca18e3',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
6
SingleKeyring {
7
_address: '0xc02cec4d0346bf4124deeb55c5216a4138a40a8c',
8
_key: PrivateKey { _privateKey: '0x{private key}' }
9
}
Copied!
Looking at the output above, you can query your keyring from caver.wallet after adding it to caver.wallet.
If you have an address and private key(s) to use, you can easily create a keyring and add it directly to caver.wallet via caver.wallet.newKeyring.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Add to wallet with an address and a private key
7
const addedSingle = caver.wallet.newKeyring('0x{address in hex}', '0x{private key1}')
8
console.log(caver.wallet.getKeyring(addedSingle.address))
9
10
// Add to wallet with an address and private keys
11
const addedMultiple = caver.wallet.newKeyring('0x{address in hex}', ['0x{private key2}', '0x{private key3}', '0x{private key4}'])
12
console.log(caver.wallet.getKeyring(addedMultiple.address))
13
14
// Add to wallet with an address and private keys defined by each roles
15
const addedRoleBased = caver.wallet.newKeyring('0x{address in hex}', [
16
['0x{private key5}', '0x{private key6}', '0x{private key7}'],
17
['0x{private key8}', '0x{private key9}'],
18
['0x{private key10}', '0x{private key11}']
19
])
20
console.log(caver.wallet.getKeyring(addedRoleBased.address))
21
}
22
23
testFunction()
Copied!
Running the above code gives you the following result. The result of the above code execution is shown below. When caver.wallet.newKeyring is executed with a private key, a Keyring instance with one private key is created and added to caver.wallet. For multiple private keys, a Keyring instance with multiple private keys is created. When passing one or more private keys for each role as arguments, a Keyring instance with a different private key(s) for each role is created and also added to the caver.wallet.
1
$ node ./test.js
2
SingleKeyring {
3
_address: '0x651f6ae6b45750082b22805583acc989399c6552',
4
_key: PrivateKey { _privateKey: '0x{private key1}' }
5
}
6
MultipleKeyring {
7
_address: '0xce3ee92aeb4d600a41c98bdf92e8b337e186bf58',
8
_keys: [
9
PrivateKey { _privateKey: '0x{private key2}' },
10
PrivateKey { _privateKey: '0x{private key3}' },
11
PrivateKey { _privateKey: '0x{private key4}' }
12
]
13
}
14
RoleBasedKeyring {
15
_address: '0x626d5b94ec76a105c5afa370bb7e59050a22b8b5',
16
_keys: [
17
[
18
PrivateKey { _privateKey: '0x{private key5}' },
19
PrivateKey { _privateKey: '0x{private key6}' },
20
PrivateKey { _privateKey: '0x{private key7}' }
21
],
22
[
23
PrivateKey { _privateKey: '0x{private key8}' },
24
PrivateKey { _privateKey: '0x{private key9}' }
25
],
26
[
27
PrivateKey { _privateKey: '0x{private key10}' },
28
PrivateKey { _privateKey: '0x{private key11}' }
29
]
30
]
31
}
Copied!
caver.wallet.add or caver.wallet.newKeyring returns a Keyring instance after adding it to caver.wallet.

Sending a Transaction

This section will show you how to send KLAY using caver-js on the Baobab network.

Getting KLAY via Baobab Faucet

If you need KLAY for testing, you can get Baobab testnet KLAY from the Klaytn Wallet. Log in to the Klaytn Wallet using the private key or the keystore file and receive Baobab testnet KLAY via the faucet for testing.

Sending a Value Transfer Transaction

You can use a caver-js wallet to generate a signature of a transaction. You have to go through two steps below to send the transaction to the network.
    1.
    Sign a transaction
      If the keyring you want to use is added to caver.wallet, you can use caver.wallet.sign function to sign.
      If you manage the keyring separately without adding it to caver.wallet, you can sign the transaction through transaction.sign function.
    2.
    Send the RLP-encoded string of the signed transaction to the Klaytn via caver.rpc.klay.sendRawTransaction.
Note: The sender should have enough number of KLAY.

Sign a transaction

Before sending a transaction to Klaytn, you should sign a transaction first.
Below is an example of how to sign a transaction if a keyring is added to the caver.wallet.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Add a keyring to caver.wallet
7
const keyring = caver.wallet.keyring.createFromPrivateKey('0x{private key}')
8
caver.wallet.add(keyring)
9
10
// Create a value transfer transaction
11
const valueTransfer = caver.transaction.valueTransfer.create({
12
from: keyring.address,
13
to: '0x176ff0344de49c04be577a3512b6991507647f72',
14
value: 1,
15
gas: 30000,
16
})
17
18
// Sign the transaction via caver.wallet.sign
19
await caver.wallet.sign(keyring.address, valueTransfer)
20
21
const rlpEncoded = valueTransfer.getRLPEncoding()
22
console.log(`RLP-encoded string: ${rlpEncoded}`)
23
}
24
25
testFunction()
Copied!
The above code adds a keyring to caver.wallet, creates a transaction, and signs the transaction through caver.wallet.sign.
Running the above code gives you the following result. When the above code is executed, the RLP-encoded string of the transaction is shown below. (The RLP-encoded string output you got could be different from the string output shown below.)
1
RLP-encoded string: 0x08f87e808505d21dba0082753094176ff0344de49c04be577a3512b6991507647f720194ade4883d092e2a972d70637ca7de9ab5166894a2f847f845824e44a0e1ec99789157e5cb6bc691935c204a23aaa3dc049efafca106992a5d5db2d179a0511c421d5e508fdb335b6048ca7aa84560a53a5881d531644ff178b6aa4c0a41
Copied!

Send the RLP-encoded string of the signed transaction to the Klaytn

Now you can send a signed transaction to the network like below. If you want to run the below example, replace 0x{RLP-encoded string} with the value of rlpEncoded above.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const rlpEncoding = `0x{RLP-encoded string}`
7
8
// Send the transaction using `caver.rpc.klay.sendRawTransaction`.
9
const receipt = await caver.rpc.klay.sendRawTransaction(rlpEncoding)
10
console.log(receipt)
11
}
12
13
testFunction()
Copied!
Running the above code gives you the following result. When the above code is executed, the receipt of the transaction is shown below.
1
$ node ./test.js
2
{
3
blockHash: '0xd20066b448da77a41a46fbf0856792b85b60c42213126f661f6434b5b1263072',
4
blockNumber: '0x1efb',
5
contractAddress: null,
6
from: '0x09a08f2289d3eb3499868908f1c84fd9523fe11b',
7
gas: '0x7530',
8
...
9
signatures: [
10
{
11
V: '0x4e43',
12
R: '0x5737aa8c88f019a3ee184faed6d34d103f77773bd5434cb0328c11738c8d9755',
13
S: '0x578b118f4400999e5232bd0860cfbdbf89622f6e11cc6bd9722a86767d2723b7'
14
}
15
],
16
status: '0x1',
17
to: '0x176ff0344de49c04be577a3512b6991507647f72',
18
transactionHash: '0x43e8ab1a2365ad598448b4402c1cfce6a71b3a103fce3a69905613e50b978113',
19
transactionIndex: 0,
20
type: 'TxTypeValueTransfer',
21
typeInt: 8,
22
value: '0x1'
23
}
Copied!
If you want to sign a transaction and send it to the network without caver.wallet, see the example below.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
// Create a value transfer transaction
7
const keyring = caver.wallet.keyring.createFromPrivateKey('0x{private key}')
8
const valueTransfer = caver.transaction.valueTransfer.create({
9
from: keyring.address,
10
to: '0x176ff0344de49c04be577a3512b6991507647f72',
11
value: 1,
12
gas: 30000,
13
})
14
15
// Sign the transaction via transaction.sign
16
await valueTransfer.sign(keyring)
17
18
// Send the transaction to the Klaytn using `caver.rpc.klay.sendRawTransaction`.
19
const receipt = await caver.rpc.klay.sendRawTransaction(valueTransfer)
20
console.log(receipt)
21
}
22
23
testFunction()
Copied!
When the above code is executed, the receipt of the transaction is printed like the previous example.

Checking Receipts

You can use the promise or event emitter to get the receipt of the transaction when you transfer the transaction to the Klaytn by caver.rpc.klay.sendRawTransaction.
The following example shows how to get a receipt using promises and event emitters.
1
// Using a promise - async/await
2
const receipt = await caver.rpc.klay.sendRawTransaction(rawTransaction)
3
console.log(receipt)
4
5
// Using a promise
6
caver.rpc.klay.sendRawTransaction(rawTransaction).then(console.log)
7
8
// Using an event emitter
9
caver.rpc.klay.sendRawTransaction(rawTransaction).on('receipt', console.log)
Copied!
As described in the example above, you can get the result of sending a transaction through the promise and event emitter. The transactionHash field is defined inside the receipt object. You can use caver.rpc.klay.getTransactionReceipt RPC call with receipt.transactionHash to query the receipt of a transaction at any time from the network after the transaction is included in a block. The example below shows how to get a receipt using the caver.rpc.klay.getTransactionReceipt RPC call.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const receipt = await caver.rpc.klay.getTransactionReceipt('0x40552efbba23347d36f6f5aaba6b9aeb6602e004df62c1988d9b7b1f036e676a')
7
console.log(receipt)
8
}
9
10
testFunction()
Copied!
Running the above code gives you the following result. When the above code is executed, the receipt of the transaction is shown below.
1
$ node ./test.js
2
{
3
blockHash: '0x65d041011440e04643c546eb8bbb1dcabb659c3b3216e01473cb0712e47b5f69',
4
blockNumber: '0x20db',
5
contractAddress: null,
6
from: '0x09a08f2289d3eb3499868908f1c84fd9523fe11b',
7
gas: '0x7530',
8
...
9
signatures: [
10
{
11
V: '0x4e43',
12
R: '0xfabe48071a8b72f0c340b2ee9d948a496cce467aebe027159d66a175e6b4b5b4',
13
S: '0x1d4e503f1b084cda15edeba6b7b8eba15057b9d2484f7f3d095c980c2d98f13'
14
}
15
],
16
status: '0x1',
17
to: '0x176ff0344de49c04be577a3512b6991507647f72',
18
transactionHash: '0x40552efbba23347d36f6f5aaba6b9aeb6602e004df62c1988d9b7b1f036e676a',
19
transactionIndex: 0,
20
type: 'TxTypeValueTransfer',
21
typeInt: 8,
22
value: '0x1'
23
}
Copied!
The result of the transaction can be found through the status of the receipt. For the details of the return values, see caver.rpc.klay.getTransactionReceipt. If a transaction is failed, you can check more about the error in txError of the receipt. For more information about txError, see txError: Detailed Information of Transaction Failures.

Executing Other Transaction Types

Klaytn provides various transaction types for extensibility and performance. For more information, see Transactions. This section describes some examples that can be used with caver-js.

Fee Delegation

Klaytn provides Fee Delegation feature. Here's an example of making a RLP-encoded transaction when you are a sender of this kind of transaction:
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const sender = caver.wallet.keyring.createFromPrivateKey('0x{private key}')
7
caver.wallet.add(sender)
8
9
const feeDelegatedTx = caver.transaction.feeDelegatedValueTransfer.create({
10
from: sender.address,
11
to: '0x176ff0344de49c04be577a3512b6991507647f72',
12
value: 5,
13
gas: 50000,
14
})
15
16
await caver.wallet.sign(sender.address, feeDelegatedTx)
17
18
const rlpEncoded = feeDelegatedTx.getRLPEncoding()
19
console.log(rlpEncoded)
20
}
21
22
testFunction()
Copied!
When the above code is executed, the RLP-encoded string will be printed. (The RLP-encoded string output you got could be different from the string output shown below.)
1
$ node ./test.js
2
0x09f884028505d21dba0082c35094176ff0344de49c04be577a3512b6991507647f720594f5a9079f311f9ec55170af351627aff0c5d2e287f847f845824e43a0f4b53dbd4c915cb73b9c7fa17e22106ee9640155a06ab4a7ed8661f846d2a5cca035b5bba6a26d4ccd20c65e8f31cce265c193f1c874806f9fae6b0ee9df0addf080c4c3018080
Copied!
The fee payer can send the transaction to the Klaytn after attaching the feePayerSignatures to the RLP-encoded string (rawTransaction) signed by the transaction sender. If caver.wallet also has the fee payer's key, the fee payer's signature can be injected into feeDelegatedTx by calling caver.wallet.signAsFeePayer(feePayer.address, feeDelegatedTx). Otherwise, the fee payer has to create a feeDelegatedTx from the RLP-encoded string signed by the sender and add the fee payer's sign onto it, as illustrated below. If you want to run the below example, replace 0x{RLP-encoded string} with the value of rlpEncoded above.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const feePayer = caver.wallet.keyring.createFromPrivateKey('0x{private key}')
7
caver.wallet.add(feePayer)
8
9
const rlpEncoded = '0x{RLP-encoded string}'
10
11
const feeDelegateTxFromRLPEncoding = caver.transaction.feeDelegatedValueTransfer.create(rlpEncoded)
12
13
// Set the fee payer address.
14
feeDelegateTxFromRLPEncoding.feePayer = feePayer.address
15
await caver.wallet.signAsFeePayer(feePayer.address, feeDelegateTxFromRLPEncoding)
16
17
console.log(feeDelegateTxFromRLPEncoding.getRLPEncoding())
18
}
19
20
testFunction()
Copied!
When the above code is executed, the RLP-encoded string including the sender's signatures and fee payer's signatures is printed like below. (The output you got could be different from the string output shown below.)
1
$ node ./test.js
2
0x09f8dc028505d21dba0082c35094176ff0344de49c04be577a3512b6991507647f720594f5a9079f311f9ec55170af351627aff0c5d2e287f847f845824e43a0f4b53dbd4c915cb73b9c7fa17e22106ee9640155a06ab4a7ed8661f846d2a5cca035b5bba6a26d4ccd20c65e8f31cce265c193f1c874806f9fae6b0ee9df0addf09417e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24f847f845824e44a0921b7c3be69db96ce14134b306c2ada423613cb66ecc6697ee8067983c268b6ea07b86b255d1c781781315d85d7904226fb2101eb9498c4a03f3fbd30ba3ec5b79
Copied!
The transaction is now signed by both the sender and the fee payer, and it can now be sent over the network. Replace 0x{RLP-encoded string} with the RLP-encoded string output of the example code above.
1
// test.js
2
const Caver = require('caver-js')
3
const caver = new Caver('https://your.en.url:8651/')
4
5
async function testFunction() {
6
const rlpEncoded = '0x{RLP-encoded string}'
7
const receipt = await caver.rpc.klay.sendRawTransaction(rlpEncoded)
8
console.log(receipt)
9
}
10
11
testFunction()
Copied!
Running the above code gives you the following result. Through the execution result of the above code, you can check the FeeDelegatedValueTransfer transaction result.
1
$ node ./test.js
2
{
3
blockHash: '0xb6a76163c4c558f50bdae77968a0f35dcfececf78b5cb780c3514a30a1c0a864',
4
blockNumber: '0xede',
5
contractAddress: null,
6
feePayer: '0x17e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24',
7
feePayerSignatures: [
8
{
9
V: '0x4e44',
10
R: '0x921b7c3be69db96ce14134b306c2ada423613cb66ecc6697ee8067983c268b6e',
11
S: '0x7b86b255d1c781781315d85d7904226fb2101eb9498c4a03f3fbd30ba3ec5b79'
12
}
13
],
14
from: '0xf5a9079f311f9ec55170af351627aff0c5d2e287',
15
gas: '0xc350',
16
...
17
signatures: [
18
{
19
V: '0x4e43',
20
R: '0xf4b53dbd4c915cb73b9c7fa17e22106ee9640155a06ab4a7ed8661f846d2a5cc',
21
S: '0x35b5bba6a26d4ccd20c65e8f31cce265c193f1c874806f9fae6b0ee9df0addf0'
22
}
23
]