K
K
Klaytn Docs
Search…
caver.wallet.keyring
caver.wallet.keyring is a package that provides functionality related to Keyring which includes address and private key(s).

Class

Keyring is a structure that contains the address of the account and the private key(s). This is a class in caver-js that allows users to sign on using their own Klaytn's account.
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

1
const keyring = new caver.wallet.keyring.singleKeyring(address, key)
Copied!
SingleKeyring is a class that stores the address of the account and a private key. To create a SingleKeyring instance with a private key string, please refer to caver.wallet.keyring.create.
SingleKeyring uses a private key with which no roles assigned.
properties
Name
Type
Description
address
string
The address of the account.
key
An instance of PrivateKey containing one private key inside.

MultipleKeyring

1
const keyring = new caver.wallet.keyring.multipleKeyring(address, keys)
Copied!
MultipleKeyring is a class that stores the address of the account and the multiple private keys. To create a MultipleKeyring instance with private key strings, please refer to caver.wallet.keyring.create.
MultipleKeyring uses private keys with which no roles assigned.
properties
Name
Type
Description
address
string
The address of the account.
keys
Array
An array of PrivateKey instances containing one private key inside.

RoleBasedKeyring

1
const keyring = new caver.wallet.keyring.roleBasedKeyring(address, keys)
Copied!
RoleBasedKeyring is a class that stores the address of the account and the private keys to be used for each role in the form of an array.
RoleBasedKeyring defines keys which is implemented as a two-dimensional array (empty keys looks like [ [], [], [] ]) that can include multiple keys for each role. The first array element defines the private key(s) for roleTransactionKey, the second defines private key(s) for roleAccountUpdateKey, and the third defines the private key(s) for roleFeePayerKey.
properties
Name
Type
Description
address
string
The address of the account.
keys
Array
A two-dimensional array that defines the keys used for each role. Each role includes PrivateKey instance(s). The first element in this is roleTransactionKey. The second element is roleAccountUpdateKey. The last element is roleFeePayerKey.
Below is a getter defined in keyring to intuitively use the key defined for each role. The key used for each role can be accessed more easily through the getter below.
Name
Type
Description
roleTransactionKey
Array
The roleTransactionKey used to sign transactions (except for transactions for the account update). keyring.roleTransactionkey will return the first element of keys property.
roleAccountUpdateKey
Array
The roleAccountUpdateKey used to sign account update transactions. keyring.roleAccountUpdateKey will return the second element of keys property.
roleFeePayerKey
Array
The roleFeePayerKey used to sign transactions as a fee payer. keyring.roleFeePayerKey will return the thrid element of keys property.

PrivateKey

1
const privateKey = new caver.wallet.keyring.privateKey('0x{private key}')
Copied!
PrivateKey is a class that contains a private key string. The private key to be used for each role in Keyring is defined as this PrivateKey instance.
properties
Name
Type
Description
privateKey
string
The private key string.

SignatureData

SignatureData is a class that contains signature data inside. The signature which is the result of sign or signMessage will be returned as a signatureData. You can see how signatureData contains signature(s) inside like below.
1
const signature = new caver.wallet.keyring.signatureData(['0x1b', '0x2dfc6...', '0x15038...'])
Copied!
properties
Name
Type
Description
v
String
ECDSA recovery id.
r
String
ECDSA signature r.
s
String
ECDSA signature s.

caver.wallet.keyring.generate

1
caver.wallet.keyring.generate([entropy])
Copied!
Generates a SingleKeyring instance with a randomly generated private key.
Parameters
Name
Type
Description
entropy
string
(optional) A random string to increase entropy.
Return Value
Type
Description
A randomly generated single keyring instance is returned.
Example
1
> caver.wallet.keyring.generate()
2
SingleKeyring {
3
_address: '0x8ecdfda0281f0d36518f89e0e2444c4f98b2e718',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!

caver.wallet.keyring.generateSingleKey

1
caver.wallet.keyring.generateSingleKey([entropy])
Copied!
Generates a private key string.
Parameters
Name
Type
Description
entropy
string
(optional) A random string to increase entropy.
Return Value
Type
Description
string
The private key string is returned.
Example
1
> caver.wallet.keyring.generateSingleKey()
2
'0x{private key}'
Copied!

caver.wallet.keyring.generateMultipleKeys

1
caver.wallet.keyring.generateMultipleKeys(num [, entropy])
Copied!
Generates private key strings.
Parameters
Name
Type
Description
num
number
The number of private key strings.
entropy
string
(optional) A random string to increase entropy.
Return Value
Type
Description
Array
An array that includes private key strings is returned.
Example
1
> caver.wallet.keyring.generateMultipleKeys(3)
2
[
3
'0x{private key1}',
4
'0x{private key2}',
5
'0x{private key3}'
6
]
Copied!

caver.wallet.keyring.generateRoleBasedKeys

1
caver.wallet.keyring.generateRoleBasedKeys(numArray [, entropy])
Copied!
Generates a 2D array of which each array element contains keys defined for each role.
Parameters
Name
Type
Description
numArray
Array
An array containing the number of keys for each role.
entropy
string
(optional) A random string to increase entropy.
Return Value
Type
Description
Array
A 2D array of which each array element contains keys defined for each role is returned.
Example
1
> caver.wallet.keyring.generateRoleBasedKeys([2, 1, 3])
2
[
3
[
4
'0x{private key1}',
5
'0x{private key2}'
6
],
7
[
8
'0x{private key3}'
9
],
10
[
11
'0x{private key4}',
12
'0x{private key5}',
13
'0x{private key6}'
14
]
15
]
Copied!

caver.wallet.keyring.create

1
caver.wallet.keyring.create(address, key)
Copied!
Creates a Keyring instance with parameters.
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.
Parameters
Name
Type
Description
address
string
An address of keyring.
key
string | Array
The private key string, an array of private keys, or a 2D array of which each element contains key(s) to be used for each role.
Return Value
Type
Description
Keyring
The keyring instance is returned. Depending on the key parameter, it can be SingleKeyring, MultipleKeyring or RoleBasedKeyring.
Example
1
// Create singleKeyring which uses one private key
2
> caver.wallet.keyring.create('0x{address in hex}', '0x{private key}')
3
SingleKeyring {
4
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
5
_key: PrivateKey { _privateKey: '0x{private key}' }
6
}
7
8
// Create multipleKeyring which uses multiple private keys
9
> caver.wallet.keyring.create('0x{address in hex}', ['0x{private key1}', '0x{private key2}'])
10
MultipleKeyring {
11
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
12
_keys: [
13
PrivateKey { _privateKey: '0x{private key1}' },
14
PrivateKey { _privateKey: '0x{private key2}' }
15
]
16
}
17
18
// Create roleBasedKeyring which uses different private key(s) by roles
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.keyring.create('0x{address in hex}', roleBasedKeys)
25
RoleBasedKeyring {
26
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
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.keyring.createFromPrivateKey

1
caver.wallet.keyring.createFromPrivateKey(key)
Copied!
Creates a SingleKeyring instance from a private key string or a KlaytnWalletKey.
Parameters
Name
Type
Description
key
string
This parameter can be either a private key or KlaytnWalletKey.
Return Value
Type
Description
The SingleKeyring instance is returned.
Example
1
// Create singleKeyring from private key string
2
> caver.wallet.keyring.createFromPrivateKey('0x{private key}')
3
SingleKeyring {
4
_address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0',
5
_key: PrivateKey { _privateKey: '0x{private key}' }
6
}
7
8
// Create singleKeyring from KlaytnWalletKey
9
> caver.wallet.keyring.createFromPrivateKey('0x{private key}0x{type}0x{address in hex}')
10
SingleKeyring {
11
_address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0',
12
_key: PrivateKey { _privateKey: '0x{private key}' }
13
}
Copied!

caver.wallet.keyring.createFromKlaytnWalletKey

1
caver.wallet.keyring.createFromKlaytnWalletKey(klaytnWalletKey)
Copied!
Creates a SingleKeyring instance from a KlaytnWalletKey string.
Parameters
Name
Type
Description
klaytnWalletKey
string
The KlaytnWalletKey string.
Return Value
Type
Description
The SingleKeyring instance is returned.
Example
1
> caver.wallet.keyring.createFromKlaytnWalletKey('0x{private key}0x{type}0x{address in hex}')
2
SingleKeyring {
3
_address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!

caver.wallet.keyring.createWithSingleKey

1
caver.wallet.keyring.createWithSingleKey(address, key)
Copied!
Creates a SingleKeyring instance from an address and a private key string.
Parameters
Name
Type
Description
address
string
An address to be used for creating a keyring.
key
string
A private key string.
Return Value
Type
Description
The SingleKeyring instance is returned.
Example
1
> caver.wallet.keyring.createWithSingleKey('0x{address in hex}', '0x{private key}')
2
SingleKeyring {
3
_address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0',
4
_key: PrivateKey { _privateKey: '0x{private key}' }
5
}
Copied!

caver.wallet.keyring.createWithMultipleKey

1
caver.wallet.keyring.createWithMultipleKey(address, key)
Copied!
Creates a MultipleKeyring instance from an address and private key strings.
Parameters
Name
Type
Description
address
string
An address of keyring.
keyArray
Array
An array of private key strings.
Return Value
Type
Description
The MultipleKeyring instance is returned.
Example
1
> caver.wallet.keyring.createWithMultipleKey('0x{address in hex}', ['0x{private key1}', '0x{private key2}' ])
2
MultipleKeyring {
3
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
4
_keys: [
5
PrivateKey { _privateKey: '0x{private key1}' },
6
PrivateKey { _privateKey: '0x{private key2}' }
7
]
8
}
Copied!

caver.wallet.keyring.createWithRoleBasedKey

1
caver.wallet.keyring.createWithRoleBasedKey(address, roledBasedKeyArray)
Copied!
Creates a RoleBasedKeyring instance from an address and a 2D array of which each array element contains keys defined for each role.
Parameters
Name
Type
Description
address
string
An address of keyring.
roledBasedKeyArray
Array
A two-dimensional array containing arrays of private key strings for each role.
Return Value
Type
Description
The RoleBasedKeyring instance is returned.
Example
1
> const roleBasedKeys = [
2
['0x{private key1}', '0x{private key2}'],
3
['0x{private key3}', '0x{private key4}'],
4
['0x{private key5}', '0x{private key6}'],
5
]
6
> caver.wallet.keyring.createWithRoleBasedKey('0x{address in hex}', roleBasedKeys)
7
RoleBasedKeyring {
8
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
9
_keys: [
10
[
11
PrivateKey { _privateKey: '0x{private key1}' },
12
PrivateKey { _privateKey: '0x{private key2}' }
13
],
14
[
15
PrivateKey { _privateKey: '0x{private key3}' },
16
PrivateKey { _privateKey: '0x{private key4}' }
17
],
18
[
19
PrivateKey { _privateKey: '0x{private key5}' },
20
PrivateKey { _privateKey: '0x{private key6}' }
21
]
22
]
23
}
Copied!

caver.wallet.keyring.decrypt

1
caver.wallet.keyring.decrypt(keystore, password)
Copied!
Decrypts a keystore v3 or v4 JSON and returns the decrypted Keyring instance.
Parameters
Name
Type
Description
keystore
object
The keystore v3 or v4 to decrypt.
password
string
The password used for encryption.
Return Value
Type
Description
Keyring
The decrypted keyring instance (SingleKeyring, MultipleKeyring or RoleBasedKeyring).
Example
1
// Decrypt keystroe v4 (encrypted single keyring)
2
> caver.wallet.keyring.decrypt({
3
version: 4,
4
id: '9c12de05-0153-41c7-a8b7-849472eb5de7',
5
address: '0xc02cec4d0346bf4124deeb55c5216a4138a40a8c',
6
keyring: [
7
{
8
ciphertext: 'eacf496cea5e80eca291251b3743bf93cdbcf7072efc3a74efeaf518e2796b15',
9
cipherparams: { iv: 'd688a4319342e872cefcf51aef3ec2da' },
10
cipher: 'aes-128-ctr',
11
kdf: 'scrypt',
12
kdfparams: {
13
dklen: 32,
14
salt: 'c3cee502c7157e0faa42386c6d666116ffcdf093c345166c502e23bc34e6ba40',
15
n: 4096,
16
r: 8,
17
p: 1
18
},
19
mac: '4b49574f3d3356fa0d04f73e07d5a2a6bbfdd185bedfa31f37f347bc98f2ef26'
20
}
21
]
22
}, 'password')
23
SingleKeyring {
24
_address: '0xc02cec4d0346bf4124deeb55c5216a4138a40a8c',
25
_key: PrivateKey { _privateKey: '0x{private key}' }
26
}
27
28
// Decrypt keystroe v4 (encrypted multiple keyring)
29
> caver.wallet.keyring.decrypt({
30
version: 4,
31
id: '55da3f9c-6444-4fc1-abfa-f2eabfc57501',
32
address: '0x86bce8c859f5f304aa30adb89f2f7b6ee5a0d6e2',
33
keyring: [
34
{
35
ciphertext: '93dd2c777abd9b80a0be8e1eb9739cbf27c127621a5d3f81e7779e47d3bb22f6',
36
cipherparams: { iv: '84f90907f3f54f53d19cbd6ae1496b86' },
37
cipher: 'aes-128-ctr',
38
kdf: 'scrypt',
39
kdfparams: {
40
dklen: 32,
41
salt: '69bf176a136c67a39d131912fb1e0ada4be0ed9f882448e1557b5c4233006e10',
42
n: 4096,
43
r: 8,
44
p: 1,
45
},
46
mac: '8f6d1d234f4a87162cf3de0c7fb1d4a8421cd8f5a97b86b1a8e576ffc1eb52d2',
47
},
48
{
49
ciphertext: '53d50b4e86b550b26919d9b8cea762cd3c637dfe4f2a0f18995d3401ead839a6',
50
cipherparams: { iv: 'd7a6f63558996a9f99e7daabd289aa2c' },
51
cipher: 'aes-128-ctr',
52
kdf: 'scrypt',
53
kdfparams: {
54
dklen: 32,
55
salt: '966116898d90c3e53ea09e4850a71e16df9533c1f9e1b2e1a9edec781e1ad44f',
56
n: 4096,
57
r: 8,
58
p: 1,
59
},
60
mac: 'bca7125e17565c672a110ace9a25755847d42b81aa7df4bb8f5ce01ef7213295',
61
},
62
],
63
}, 'password')
64
MultipleKeyring {
65
_address: '0x86bce8c859f5f304aa30adb89f2f7b6ee5a0d6e2',
66
_keys: [
67
PrivateKey { _privateKey: '0x{private key1}' },
68
PrivateKey { _privateKey: '0x{private key2}' }
69
]
70
}
71
72
// Decrypt keystroe v4 (encrypted role-based keyring)
73
> caver.wallet.keyring.decrypt({
74
version: 4,
75
id: '55da3f9c-6444-4fc1-abfa-f2eabfc57501',
76
address: '0x86bce8c859f5f304aa30adb89f2f7b6ee5a0d6e2',
77
keyring: [
78
[
79
{
80
ciphertext: '93dd2c777abd9b80a0be8e1eb9739cbf27c127621a5d3f81e7779e47d3bb22f6',
81
cipherparams: { iv: '84f90907f3f54f53d19cbd6ae1496b86' },
82
cipher: 'aes-128-ctr',
83
kdf: 'scrypt',
84
kdfparams: {
85
dklen: 32,
86
salt: '69bf176a136c67a39d131912fb1e0ada4be0ed9f882448e1557b5c4233006e10',
87
n: 4096,
88
r: 8,
89
p: 1,
90
},
91
mac: '8f6d1d234f4a87162cf3de0c7fb1d4a8421cd8f5a97b86b1a8e576ffc1eb52d2',
92
},
93
{
94
ciphertext: '53d50b4e86b550b26919d9b8cea762cd3c637dfe4f2a0f18995d3401ead839a6',
95
cipherparams: { iv: 'd7a6f63558996a9f99e7daabd289aa2c' },
96
cipher: 'aes-128-ctr',
97
kdf: 'scrypt',
98
kdfparams: {
99
dklen: 32,
100
salt: '966116898d90c3e53ea09e4850a71e16df9533c1f9e1b2e1a9edec781e1ad44f',
101
n: 4096,
102
r: 8,
103
p: 1,
104
},
105
mac: 'bca7125e17565c672a110ace9a25755847d42b81aa7df4bb8f5ce01ef7213295',
106
},
107
],
108
[
109
{
110
ciphertext: 'f16def98a70bb2dae053f791882f3254c66d63416633b8d91c2848893e7876ce',
111
cipherparams: { iv: 'f5006128a4c53bc02cada64d095c15cf' },
112
cipher: 'aes-128-ctr',
113
kdf: 'scrypt',
114
kdfparams: {
115
dklen: 32,
116
salt: '0d8a2f71f79c4880e43ff0795f6841a24cb18838b3ca8ecaeb0cda72da9a72ce',
117
n: 4096,
118
r: 8,
119
p: 1,
120
},
121
mac: '38b79276c3805b9d2ff5fbabf1b9d4ead295151b95401c1e54aed782502fc90a',
122
},
123
],
124
[
125
{
126
ciphertext: '544dbcc327942a6a52ad6a7d537e4459506afc700a6da4e8edebd62fb3dd55ee',
127
cipherparams: { iv: '05dd5d25ad6426e026818b6fa9b25818' },
128
cipher: 'aes-128-ctr',
129
kdf: 'scrypt',
130
kdfparams: {
131
dklen: 32,
132
salt: '3a9003c1527f65c772c54c6056a38b0048c2e2d58dc0e584a1d867f2039a25aa',
133
n: 4096,
134
r: 8,
135
p: 1,
136
},
137
mac: '19a698b51409cc9ac22d63d329b1201af3c89a04a1faea3111eec4ca97f2e00f',
138
},
139
{
140
ciphertext: 'dd6b920f02cbcf5998ed205f8867ddbd9b6b088add8dfe1774a9fda29ff3920b',
141
cipherparams: { iv: 'ac04c0f4559dad80dc86c975d1ef7067' },
142
cipher: 'aes-128-ctr',
143
kdf: 'scrypt',
144
kdfparams: {
145
dklen: 32,
146
salt: '22279c6dbcc706d7daa120022a236cfe149496dca8232b0f8159d1df999569d6',
147
n: 4096,
148
r: 8,
149
p: 1,
150
},
151
mac: '1c54f7378fa279a49a2f790a0adb683defad8535a21bdf2f3dadc48a7bddf517',
152
},
153
],
154
],
155
}, 'password')
156
RoleBasedKeyring {
157
_address: '0x86bce8c859f5f304aa30adb89f2f7b6ee5a0d6e2',
158
_keys: [
159
[
160
PrivateKey { _privateKey: '0x{private key1}' },
161
PrivateKey { _privateKey: '0x{private key2}' }
162
],
163
[
164
PrivateKey { _privateKey: '0x{private key3}' }
165
],
166
[
167
PrivateKey { _privateKey: '0x{private key4}' },
168
PrivateKey { _privateKey: '0x{private key5}' }
169
]
170
]
171
}
172
173
// Decrypt keystroe v3 JSON
174
> caver.wallet.keyring.decrypt({
175
version: 3,
176
id: '43f99d36-3905-40e6-bff8-ff0dfc380037',
177
address: '0xc02cec4d0346bf4124deeb55c5216a4138a40a8c',
178
crypto: {
179
ciphertext: 'f7296e68807837a5318502c097276a89d58d91b85e45e692aee284a27bcd0955',
180
cipherparams: { iv: '03fd985d07777601078840c73cc6f7f3' },
181
cipher: 'aes-128-ctr',
182
kdf: 'scrypt',
183
kdfparams: {
184
dklen: 32,
185
salt: '46f85271c43fa64ab3338c5235f1d5073bc9379d9b7ba6065c89afb816d83a8a',
186
n: 4096,
187
r: 8,
188
p: 1
189
},
190
mac: '947f13cd1481fa5ba186e59418ef7600fa69e9830054d59e4d5dc67176e1f967'
191
}
192
}, 'password')
193
SingleKeyring {
194
_address: '0xc02cec4d0346bf4124deeb55c5216a4138a40a8c',
195
_key: PrivateKey { _privateKey: '0x{private key}' }
196
}
Copied!

keyring.getPublicKey

1
keyring.getPublicKey()
Copied!
Returns the public key string(s). If keyring is an instance of SingleKeyring, getPublicKey returns a public key string. If keyring is an instance of MultipleKeyring, getPublicKey returns an array of public key strings. If keyring is an instance of RoleBasedKeyring, getPublicKey returns a two-dimensional array in which the public key(s) used for each role is defined as an array.
Parameters
Name
Type
Description
compressed
boolean
(optional) Whether in compressed format or not (default: false).
Return Value
Type
Description
string | Array
The public key of the keyring.
Example
1
// Get public key with singleKeyring
2
> keyring.getPublicKey()
3
'0x49b2a...'
4
5
// Get public key with multipleKeyring
6
> keyring.getPublicKey()
7
[ '0x65b51...', '0x8d85c...' ]
8
9
// Get public key with roleBasedKeyring
10
> keyring.getPublicKey()
11
[
12
[ '0x2d939...', '0x6beb4...', '0xd8f2f...' ],
13
[ '0xf09cd...', '0x96a63...', '0x02000...' ],
14
[ '0xc2d33...', '0x3088f...', '0xab193...' ]
15
]
Copied!

keyring.copy

1
keyring.copy()
Copied!
Returns a copied keyring instance.
Return Value
Type
Description
Keyring
A copied keyring instance (SingleKeyring, MultipleKeyring or RoleBasedKeyring).
Example
1
// When keyring is an instance of SingleKeyring
2
> keyring.copy()
3
SingleKeyring {
4
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
5
_key: PrivateKey { _privateKey: '0x{private key}' }
6
}
7
8
// When keyring is an instance of MultipleKeyring
9
> keyring.copy()
10
MultipleKeyring {
11
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
12
_keys: [
13
PrivateKey { _privateKey: '0x{private key1}' },
14
PrivateKey { _privateKey: '0x{private key2}' }
15
]
16
}
17
18
// When keyring is an instance of RoleBasedKeyring
19
> keyring.copy()
20
RoleBasedKeyring {
21
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
22
_keys: [
23
[
24
PrivateKey { _privateKey: '0x{private key1}' },
25
PrivateKey { _privateKey: '0x{private key2}' }
26
],
27
[
28
PrivateKey { _privateKey: '0x{private key3}' },
29
PrivateKey { _privateKey: '0x{private key4}' }
30
],
31
[
32
PrivateKey { _privateKey: '0x{private key5}' },
33
PrivateKey { _privateKey: '0x{private key6}' }
34
]
35
]
36
}
Copied!

keyring.sign

1
keyring.sign(transactionHash, chainId, role [, index])
Copied!
Signs with transactionHash with the private key(s) and returns signature(s). If the user has not defined an index parameter, keyring.sign signs transaction using all the private keys used by the role. If index is defined, the keyring.sign signs transaction using only one private key at the index. The role used in caver-js can be checked through caver.wallet.keyring.role.
When signing transactions, it is recommended to use caver.wallet.sign or transaction.sign.
Parameters
Name
Type
Description
transactionHash
string
The hash string of a transaction to sign.
chainId
string | number
The chain id of the Klaytn blockchain platform.
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
Array
An array of SignatureData.
Example
1
// Using roleBasedKeyring which has two private key in roleTransactionKey
2
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleTransactionKey)
3
[
4
SignatureData { _v: '0x5044', _r: '0x7a8b6...', _s: '0x17139...' },
5
SignatureData { _v: '0x5043', _r: '0x7f978...', _s: '0x1a532...' }
6
]
7
8
// Using roleBasedKeyring which has two private key in roleTransactionKey with index
9
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleTransactionKey, 1)
10
[
11
SignatureData { _v: '0x5043', _r: '0x7f978...', _s: '0x1a532...' }
12
]
13
14
// Using roleBasedKeyring which has two private key in roleAccountUpdateKey
15
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleAccountUpdateKey)
16
[
17
SignatureData { _v: '0x5044', _r: '0xdbce8...', _s: '0x039a6...' },
18
SignatureData { _v: '0x5044', _r: '0xf69b7...', _s: '0x71dc9...' }
19
]
20
21
// Using roleBasedKeyring which has two private key in roleAccountUpdateKey with index
22
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleAccountUpdateKey, 1)
23
[
24
SignatureData { _v: '0x5044', _r: '0xf69b7...', _s: '0x71dc9...' }
25
]
26
27
// Using roleBasedKeyring which has two private key in roleFeePayerKey
28
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleFeePayerKey)
29
[
30
SignatureData { _v: '0x5043', _r: '0xe48bf...', _s: '0x1cf36...' },
31
SignatureData { _v: '0x5043', _r: '0x82976...', _s: '0x3c5e0...' }
32
]
33
34
// Using roleBasedKeyring which has two private key in roleFeePayerKey with index
35
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleFeePayerKey, 1)
36
[
37
SignatureData { _v: '0x5043', _r: '0x82976...', _s: '0x3c5e0...' }
38
]
Copied!

keyring.signMessage

1
keyring.signMessage(message, role [, index])
Copied!
Signs message with Klaytn-specific prefix. This calculates a Klaytn-specific signature with:
1
sign(keccak256("\x19Klaytn Signed Message:\n" + len(message) + message)))
Copied!
If the user has not defined the index parameter, keyring.signMessage signs message with all the private keys used by the role. If the index parameter is given, keyring.signMessage signs message using only one private key at the given index. The role used in caver-js can be found through caver.wallet.keyring.role.
Parameters
Name
Type
Description
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 with roleTransactionKey
2
> keyring.signMessage('message to sign', caver.wallet.keyring.role.roleTransactionKey)
3
{
4
messageHash: '0x9c4c1ae0aa1faf7e59eaf6fcf36a34542698197b379a9949b58c92925e74c069',
5
signatures: [
6
SignatureData { _v: '0x1b', _r: '0x2dfc6...', _s: '0x15038...' }
7
],
8
message: 'message to sign'
9
}
10
11
// Sign with roleFeePayerKey and index
12
> keyring.signMessage('message to sign', caver.wallet.keyring.role.roleFeePayerKey, 1)
13
{
14
messageHash: '0x9c4c1ae0aa1faf7e59eaf6fcf36a34542698197b379a9949b58c92925e74c069',
15
signatures: [
16
SignatureData { _v: '0x1b', _r: '0x2dfc6...', _s: '0x15038...' }
17
],
18
message: 'message to sign'
19
}
Copied!

keyring.getKeyByRole

1
keyring.getKeyByRole(role)
Copied!
Returns the private key(s) used by the role entered as a parameter.
Parameters
Name
Type
Description
role
number
A number indicating the role of the key. You can use caver.wallet.keyring.role.
Return Value
Type
Description
PrivateKey | Array
An instance of PrivateKey or an array containing the PrivateKey instances used by the role.
Example
1
// getKeyByRole with singleKeyring.
2
// The singleKeyring will return the single same PrivateKey intance regardless of role.
3
> keyring.getKeyByRole(caver.wallet.keyring.role.roleTransactionKey)
4
PrivateKey { _privateKey: '0x{private key}' }
5
6
> keyring.getKeyByRole(caver.wallet.keyring.role.roleAccountUpdateKey)
7
PrivateKey { _privateKey: '0x{private key}' }
8
9
> keyring.getKeyByRole(caver.wallet.keyring.role.roleFeePayerKey)
10
PrivateKey { _privateKey: '0x{private key}' }
11
12
// getKeyByRole with multipleKeyring.
13
// The multipleKeyring will also return the single same array of PrivateKey intances regardless of role
14
> keyring.getKeyByRole(caver.wallet.keyring.role.roleTransactionKey)
15
[
16
PrivateKey { _privateKey: '0x{private key1}' },
17
PrivateKey { _privateKey: '0x{private key2}' }
18
]
19
20
> keyring.getKeyByRole(caver.wallet.keyring.role.roleAccountUpdateKey)
21
[
22
PrivateKey { _privateKey: '0x{private key1}' },
23
PrivateKey { _privateKey: '0x{private key2}' }
24
]
25
26
> keyring.getKeyByRole(caver.wallet.keyring.role.roleFeePayerKey)
27
[
28
PrivateKey { _privateKey: '0x{private key1}' },
29
PrivateKey { _privateKey: '0x{private key2}' }
30
]
31
32
// getKeyByRole with roleBasedKeyring.
33
// The roleBasedKeyring will return different array of PrivateKey intances depends on role
34
> keyring.getKeyByRole(caver.wallet.keyring.role.roleTransactionKey)
35
[
36
PrivateKey { _privateKey: '0x{private key1}' }
37
]
38
39
> keyring.getKeyByRole(caver.wallet.keyring.role.roleAccountUpdateKey)
40
[
41
PrivateKey { _privateKey: '0x{private key2}' },
42
PrivateKey { _privateKey: '0x{private key3}' }
43
]
44
45
> keyring.getKeyByRole(caver.wallet.keyring.role.roleFeePayerKey)
46
[
47
PrivateKey { _privateKey: '0x{private key4}' },
48
PrivateKey { _privateKey: '0x{private key5}' },
49
PrivateKey { _privateKey: '0x{private key6}' }
50
]
Copied!

keyring.getKlaytnWalletKey

1
keyring.getKlaytnWalletKey()
Copied!
Returns the KlaytnWalletKey string for the keyring. With MultipleKeyring or RoleBasedKeyring, KlaytnWalletKey cannot be used. In this case, use keyring.encrypt.
Return Value
Type
Description
string
The KlaytnWalletKey of the keyring.
Example
1
> keyring.getKlaytnWalletKey()
2
'0x{private key}0x{type}0x{address in hex}'
Copied!

keyring.toAccount

1
keyring.toAccount([options])
Copied!
Returns the Account instance for updating the AccountKey of the Klaytn accounts. The Account instance has an AccountKey instance that can contain public key(s) inside, which