K
K
Klaytn Docs
Search…
caver.wallet
caver.wallet is a package that manages Keyring instances in in-memory wallet. caver.wallet accepts all SingleKeyring, MultipleKeyring, and RoleBasedKeyring, and manages them by address.

Class

KeyringContainer

1
caver.wallet
Copied!
KeyringContainer is a class that manages SingleKeyring, MultipleKeyring, and RoleBasedKeyring instances. When Caver is instantiated, it creates a KeyringContainer instance in the caver.wallet. You can store and manage keyring instances in the in-memory wallet through caver.wallet.
properties
Name
Type
Description
length
number
The number of keyrings in keyringContainer.

caver.wallet.generate

1
caver.wallet.generate(numberOfKeyrings [, entropy])
Copied!
Generates instances of SingleKeyring in the keyringContainer with randomly generated private keys.
Parameters
Name
Type
Description
numberOfKeyrings
number
The number of SingleKeyring instances to create.
entropy
string
(optional) A random string to increase entropy.
Return Value
Type
Description
Array
An array containing the addresses of the generated.
Example
1
// generate without entropy
2
> caver.wallet.generate(3)
3
[
4
'0xb4b0c3781082cf818bfaf5adfc73fdf59d92c1cd',
5
'0x9957dfd92e4b70f91131c573293343bc5f21f215',
6
'0xed2fe179c18fa528da2392532998560bd1008511'
7
]
8
9
// generate with entropy
10
> caver.wallet.generate(3, caver.utils.randomHex(32))
11
[
12
'0xb4b0c3781082cf818bfaf5adfc73fdf59d92c1cd',
13
'0x9957dfd92e4b70f91131c573293343bc5f21f215',
14
'0xed2fe179c18fa528da2392532998560bd1008511'
15
]
Copied!

caver.wallet.newKeyring

1
caver.wallet.newKeyring(address, key)
Copied!
Creates a keyring instance with given parameters and adds it to the caver.wallet.
If key is a private key string, a SingleKeyring instance that uses a single private key is created. If key is an array containing private key strings, a MultipleKeyring instance that use multiple private keys is created. If key is a 2D array of which each element contains the private key(s) to be used for each role, a RoleBasedKeyring instance is created. The keyring created is added to caver.wallet.
Parameters
Name
Type
Description
address
string
The address string.
key
string | Array
The private key string, an array of private keys, or a 2D array of which each array element contains keys defined for each role.
Return Value
Type
Description
object
The keyring instance (SingleKeyring, MultipleKeyring or RoleBasedKeyring) added to caver.wallet is returned.
Example
1
// Create a instance of SingleKeyring and add to caver.wallet
2
> caver.wallet.newKeyring('0x{address in hex}', '0x{private key}')
3
SingleKeyring {
4
_address: '0x386a4bb40abbfaa59cecdc3ced202475895fd569',
5
_key: PrivateKey { _privateKey: '0x{private key}' }
6
}
7
8
// Create a instance of MultipleKeyring and add to caver.wallet
9
> caver.wallet.newKeyring('0x{address in hex}', ['0x{private key1}', '0x{private key2}'])
10
MultipleKeyring {
11
_address: '0x17e7531b40ad5d7b5fa7b4ec78df64ce1cb36d24',
12
_keys: [
13
PrivateKey { _privateKey: '0x{private key1}' },
14
PrivateKey { _privateKey: '0x{private key2}' }
15
]
16
}
17
18
// Create a instance of RoleBasedKeyring and add to caver.wallet
19
> const roleBasedKeys = [
20
['0x{private key1}', '0x{private key2}'],
21
['0x{private key3}', '0x{private key4}'],
22
['0x{private key5}', '0x{private key6}'],
23
]
24
> caver.wallet.newKeyring('0x{address in hex}', roleBasedKeys)
25
RoleBasedKeyring {
26
_address: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
27
_keys: [
28
[
29
PrivateKey { _privateKey: '0x{private key1}' },
30
PrivateKey { _privateKey: '0x{private key2}' }
31
],
32
[
33
PrivateKey { _privateKey: '0x{private key3}' },
34
PrivateKey { _privateKey: '0x{private key4}' }
35
],
36
[
37
PrivateKey { _privateKey: '0x{private key5}' },
38
PrivateKey { _privateKey: '0x{private key6}' }
39
]
40
]
41
}
Copied!

caver.wallet.updateKeyring

1
caver.wallet.updateKeyring(keyring)
Copied!
Updates the keyring inside the caver.wallet. When a new keyring instance (SingleKeyring, MultipleKeyring or RoleBasedKeyring) is passed as a parameter, the existing keyring stored in the caver.wallet that matches the address property of the given keyring instance is found and replaced with the given one. An error occurs when the matching keyring is not found.
Parameters
Name
Type
Description
keyring
object
The new keyring (SingleKeyring, MultipleKeyring or RoleBasedKeyring) to be stored in caver.wallet.
Return Value
Type
Description
object
The updated keyring (SingleKeyring, MultipleKeyring or RoleBasedKeyring) stored in caver.wallet.
Example
1
> caver.wallet.updateKeyring(newKeyring)
2
SingleKeyring {
3
_address: '0x386a4bb40abbfaa59cecdc3ced202475895fd569',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!

caver.wallet.getKeyring

1
caver.wallet.getKeyring(address)
Copied!
Returns the keyring instance corresponding to the address in caver.wallet.
Parameters
Name
Type
Description
address
string
The address of keyring to query.
Return Value
Type
Description
object
The found keyring instance (SingleKeyring, MultipleKeyring or RoleBasedKeyring) stored in caver.wallet.
Example
1
> caver.wallet.getKeyring('0x386a4bb40abbfaa59cecdc3ced202475895fd569')
2
SingleKeyring {
3
_address: '0x386a4bb40abbfaa59cecdc3ced202475895fd569',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!

caver.wallet.isExisted

1
caver.wallet.isExisted(address)
Copied!
Returns true if there is a keyring matching the address.
Parameters
Name
Type
Description
address
string
The address of keyring to check existence.
Return Value
Type
Description
boolean
true means a keyring matching with the address is existed in the caver.wallet.
Example
1
> caver.wallet.isExisted('0x386a4bb40abbfaa59cecdc3ced202475895fd569')
2
true
Copied!

caver.wallet.add

1
caver.wallet.add(keyring)
Copied!
Adds an instance of keyring to the caver.wallet. If the newly given keyring has the same address with one of the keyrings that already exist in caver.wallet, an error is returned. In this case, use updateKeyring to update the existing keyring in caver.wallet.
Parameters
Name
Type
Description
keyring
object
A keyring instance (SingleKeyring, MultipleKeyring or RoleBasedKeyring) to add to caver.wallet.
Return Value
Type
Description
object
The added keyring (SingleKeyring, MultipleKeyring or RoleBasedKeyring) in caver.wallet.
Example
1
> caver.wallet.add(keyring)
2
SingleKeyring {
3
_address: '0x386a4bb40abbfaa59cecdc3ced202475895fd569',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!

caver.wallet.remove

1
caver.wallet.remove(address)
Copied!
Deletes the keyring from caver.wallet whose address matches the address of the given keyring.
Parameters
Name
Type
Description
address
string
An address of the keyring to be deleted in caver.wallet.
Return Value
Type
Description
boolean
true if keyring is removed from caver.wallet.
Example
1
> caver.wallet.remove('0x6a3edfad6d1126020d5369e9097db39281876c5d')
2
true
Copied!

caver.wallet.signMessage

1
caver.wallet.signMessage(address, message, role [, index])
Copied!
Signs the message with Klaytn-specific prefix using keyring stored in caver.wallet. This calculates a Klaytn-specific signature with:
1
sign(keccak256("\x19Klaytn Signed Message:\n" + len(message) + message)))
Copied!
If the user has not provided the index parameter, caver.wallet.signMessage signs message using all the private keys used by the role. If the index parameter is given, caver.wallet.signMessage signs message using only one private key at the given index. The role used in caver-js can be found from caver.wallet.keyring.role.
Parameters
Name
Type
Description
address
string
An address of the keyring to be used.
message
string
The message to sign.
role
number
A number indicating the role of the key. You can use caver.wallet.keyring.role.
index
number
(optional) The index of the private key you want to use. The index must be less than the length of the array of the private keys defined for each role. If an index is not defined, this method will use all the private keys.
Return Value
Type
Description
object
An object that includes the result of signing.
The returned object contains the following:
Name
Type
Description
messageHash
string
The hash of message with Klaytn-specific prefix.
signatures
Array
An array of SignatureData.
message
string
The message to sign.
Example
1
// Sign message with roleTransactionKey which uses two private keys
2
> caver.wallet.signMessage('0x386a4bb40abbfaa59cecdc3ced202475895fd569', 'message to sign', caver.wallet.keyring.role.roleTransactionKey)
3
{
4
messageHash: '0x9c4c1ae0aa1faf7e59eaf6fcf36a34542698197b379a9949b58c92925e74c069',
5
signatures: [
6
SignatureData { _v: '0x1c', _r: '0xb3239...', _s: '0x584d2...' },
7
SignatureData { _v: '0x1b', _r: '0x13c64...', _s: '0x60c61...' }
8
],
9
message: 'message to sign'
10
}
11
12
// Sign message with roleTransactionKey and index
13
> caver.wallet.signMessage('0x386a4bb40abbfaa59cecdc3ced202475895fd569', 'message to sign', caver.wallet.keyring.role.roleTransactionKey, 1)
14
{
15
messageHash: '0x9c4c1ae0aa1faf7e59eaf6fcf36a34542698197b379a9949b58c92925e74c069',
16
signatures: [
17
SignatureData { _v: '0x1b', _r: '0x13c64...', _s: '0x60c61...' }
18
],
19
message: 'message to sign'
20
}
Copied!

caver.wallet.sign

1
caver.wallet.sign(address, transaction [, index] [, hasher])
Copied!
Signs the transaction as a sender of the transaction and appends signatures in the transaction object using the keyring in caver.wallet.
For Account Update transaction, use roleTransactionKey, otherwise, use roleTransactionKey. If the user has not defined an index, caver.wallet.sign signs the transaction using all the private keys used by the role. If index is defined, the caver.wallet.sign signs the transaction using only one private key at the given index.
Parameters
Name
Type
Description
address
string
An address of the keyring to be used.
transaction
object
An instance of Transaction.
index
number
(optional) The index of the private key you want to use. The index must be less than the length of the array of the private keys defined for each role. If an index is not defined, this method will use all the private keys.
hasher
function
(optional) A hash function to get the transaction hash. If hasher is given as a parameter, it calculates the transaction hash instead of the default method for calculating transaction hash implemented in caver-js. See Basic for details about the default method for transaction hash generation.
Return Value
Promise returning object: The signed transaction.
Type
Description
object
A signed transaction instance. The sign(s) is added to the transaction.signatures.
For more information about fields by transaction type, see caver.transaction.
Example
1
// This example uses the ValueTransfer transaction.
2
// Please refer to [caver.transaction] for how to use various transaction types.
3
> const transaction = caver.transaction.valueTransfer.create({
4
from: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
5
to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
6
value: 1,
7
gas: 30000,
8
})
9
10
> const customHasher = () => { ... }
11
12
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleTransactionKey
13
> caver.wallet.sign('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction).then(console.log)
14
ValueTransfer {
15
_type: 'TxTypeValueTransfer',
16
_from: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
17
_gas: '0x7530',
18
_signatures: [
19
SignatureData { _v: '0x4e43', _r: '0xd78a2...', _s: '0x379e9...' },
20
SignatureData { _v: '0x4e43', _r: '0x70a58...', _s: '0x2ab28...' }
21
],
22
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
23
_value: '0x1',
24
_chainId: '0x2710',
25
_gasPrice: '0x5d21dba00',
26
_nonce: '0x0'
27
}
28
29
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleTransactionKey and index
30
> caver.wallet.sign('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction, 1).then(console.log)
31
ValueTransfer {
32
_type: 'TxTypeValueTransfer',
33
_from: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
34
_gas: '0x7530',
35
_signatures: [
36
SignatureData { _v: '0x4e43', _r: '0x70a58...', _s: '0x2ab28...' }
37
],
38
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
39
_value: '0x1',
40
_chainId: '0x2710',
41
_gasPrice: '0x5d21dba00',
42
_nonce: '0x0'
43
}
44
45
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleTransactionKey and hasher
46
> caver.wallet.sign('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction, customHasher).then(console.log)
47
ValueTransfer {
48
_type: 'TxTypeValueTransfer',
49
_from: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
50
_gas: '0x7530',
51
_signatures: [
52
SignatureData { _v: '0x4e44', _r: '0x7a8b6...', _s: '0x17139...' },
53
SignatureData { _v: '0x4e43', _r: '0x7f978...', _s: '0x1a532...' }
54
],
55
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
56
_value: '0x1',
57
_chainId: '0x2710',
58
_gasPrice: '0x5d21dba00',
59
_nonce: '0x0'
60
}
61
62
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleTransactionKey, index and hasher
63
> caver.wallet.sign('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction, 0, customHasher).then(console.log)
64
ValueTransfer {
65
_type: 'TxTypeValueTransfer',
66
_from: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
67
_gas: '0x7530',
68
_signatures: [
69
SignatureData { _v: '0x4e44', _r: '0x7a8b6...', _s: '0x17139...' }
70
],
71
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
72
_value: '0x1',
73
_chainId: '0x2710',
74
_gasPrice: '0x5d21dba00',
75
_nonce: '0x0'
76
}
Copied!

caver.wallet.signAsFeePayer

1
caver.wallet.signAsFeePayer(address, transaction [, index] [, hasher])
Copied!
Signs the transaction as fee payer of the transaction and appends feePayerSignatures in the transaction object using the keyring in caver.wallet.
For signing a transaction as a fee payer, use roleFeePayerKey. If the user has not defined an index, caver.wallet.signAsFeePayer signs the transaction using all the private keys used by the role. If index is defined, the caver.wallet.signAsFeePayer signs the transaction using only one private key at the given index.
If the transaction.feePayer is not defined, the address of keyring which is founded from caver.wallet is assigned.
Parameters
Name
Type
Description
address
string
An address of the keyring to be used.
transaction
object
An instance of FeeDelegatedTransaction.
index
number
(optional) The index of the private key you want to use. The index must be less than the length of the array of the private keys defined for each role. If an index is not defined, this method will use all the private keys.
hasher
function
(optional) A function to get the transaction hash. If hasher is defined as a parameter, this is used to get the transaction hash instead of a default implementation in caver-js.
Return Value
Promise returning object: The signed transaction.
Type
Description
object
A signed transaction instance. The signing result is appended to the transaction.feePayerSignatures.
For more information about fields by transaction type, see caver.transaction.
Example
1
// This example uses the FeeDelegatedValueTransfer transaction.
2
// Please refer to [caver.transaction] for how to use various transaction types.
3
> const transaction = caver.transaction.feeDelegatedValueTransfer.create({
4
from: '0x6fddbcb99d31b8755c2b840a367f53eea4b4f45c',
5
to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
6
value: 1,
7
gas: 30000,
8
})
9
10
> const customHasher = () => { ... }
11
12
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleFeePayerKey
13
> caver.wallet.signAsFeePayer('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction).then(console.log)
14
FeeDelegatedValueTransfer {
15
_type: 'TxTypeFeeDelegatedValueTransfer',
16
_from: '0x6fddbcb99d31b8755c2b840a367f53eea4b4f45c',
17
_gas: '0x7530',
18
_signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ],
19
_feePayer: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
20
_feePayerSignatures: [
21
SignatureData { _v: '0x4e44', _r: '0x7010e...', _s: '0x65d6b...' },
22
SignatureData { _v: '0x4e43', _r: '0x96ef2...', _s: '0x77f34...' }
23
],
24
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
25
_value: '0x1',
26
_chainId: '0x2710',
27
_gasPrice: '0x5d21dba00',
28
_nonce: '0x0'
29
}
30
31
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleFeePayerKey, index
32
> caver.wallet.signAsFeePayer('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction, 0).then(console.log)
33
FeeDelegatedValueTransfer {
34
_type: 'TxTypeFeeDelegatedValueTransfer',
35
_from: '0x6fddbcb99d31b8755c2b840a367f53eea4b4f45c',
36
_gas: '0x7530',
37
_signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ],
38
_feePayer: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
39
_feePayerSignatures: [
40
SignatureData { _v: '0x4e44', _r: '0x7010e...', _s: '0x65d6b...' }
41
],
42
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
43
_value: '0x1',
44
_chainId: '0x2710',
45
_gasPrice: '0x5d21dba00',
46
_nonce: '0x0'
47
}
48
49
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleFeePayerKey and hasher
50
> caver.wallet.signAsFeePayer('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction, customHasher).then(console.log)
51
FeeDelegatedValueTransfer {
52
_type: 'TxTypeFeeDelegatedValueTransfer',
53
_from: '0x6fddbcb99d31b8755c2b840a367f53eea4b4f45c',
54
_gas: '0x7530',
55
_signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ],
56
_feePayer: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
57
_feePayerSignatures: [
58
SignatureData { _v: '0x4e43', _r: '0xe48bf...', _s: '0x1cf36...' },
59
SignatureData { _v: '0x4e43', _r: '0x82976...', _s: '0x3c5e0...' }
60
],
61
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
62
_value: '0x1',
63
_chainId: '0x2710',
64
_gasPrice: '0x5d21dba00',
65
_nonce: '0x0'
66
}
67
68
// Sign a transaction with the address of RoleBasedKeyring which use two private keys for roleFeePayerKey, index and hasher
69
> caver.wallet.signAsFeePayer('0xe7e9184c125020af5d34eab7848bab799a1dcba9', transaction, 0, customHasher).then(console.log)
70
FeeDelegatedValueTransfer {
71
_type: 'TxTypeFeeDelegatedValueTransfer',
72
_from: '0x6fddbcb99d31b8755c2b840a367f53eea4b4f45c',
73
_gas: '0x7530',
74
_signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ],
75
_feePayer: '0xe7e9184c125020af5d34eab7848bab799a1dcba9',
76
_feePayerSignatures: [
77
SignatureData { _v: '0x4e43', _r: '0x82976...', _s: '0x3c5e0...' }
78
],
79
_to: '0x3424b91026bdc5ec55df4548e6ebf0f28b60abd7',
80
_value: '0x1',
81
_chainId: '0x2710',
82
_gasPrice: '0x5d21dba00',
83
_nonce: '0x0'
84
}
Copied!
Last modified 1mo ago