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 .
Keyring can be classified into three types depending on the type of key being stored: to store one address and one private key, to store one address and multiple private keys, and to store one address and one or more private keys for each role.
: User signs with a private key
: User signs with private keys
: User signs with the private key(s) by role
SingleKeyring
const keyring = new caver.wallet.keyring.singleKeyring(address, key)
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 .
SingleKeyring uses a private key with which no roles assigned.
properties
Name
Type
Description
address
string
The address of the account.
key
MultipleKeyring
const keyring = new caver.wallet.keyring.multipleKeyring(address, keys)
MultipleKeyring uses private keys with which no roles assigned.
properties
Name
Type
Description
address
string
The address of the account.
keys
Array
RoleBasedKeyring
const keyring = new caver.wallet.keyring.roleBasedKeyring(address, keys)
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.
properties
Name
Type
Description
address
string
The address of the account.
keys
Array
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
const privateKey = new caver.wallet.keyring.privateKey('0x{private key}')
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.
const signature = new caver.wallet.keyring.signatureData(['0x1b', '0x2dfc6...', '0x15038...'])
properties
Name
Type
Description
v
String
ECDSA recovery id.
r
String
ECDSA signature r.
s
String
ECDSA signature s.
caver.wallet.keyring.generate
caver.wallet.keyring.generate([entropy])
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.
(optional) Whether in compressed format or not (default: false).
Return Value
Type
Description
string | Array
The public key of the keyring.
Example
// Get public key with singleKeyring
> keyring.getPublicKey()
'0x49b2a...'
// Get public key with multipleKeyring
> keyring.getPublicKey()
[ '0x65b51...', '0x8d85c...' ]
// Get public key with roleBasedKeyring
> keyring.getPublicKey()
[
[ '0x2d939...', '0x6beb4...', '0xd8f2f...' ],
[ '0xf09cd...', '0x96a63...', '0x02000...' ],
[ '0xc2d33...', '0x3088f...', '0xab193...' ]
]
keyring.copy
keyring.copy()
Returns a copied keyring instance.
Return Value
Type
Description
Keyring
Example
// When keyring is an instance of SingleKeyring
> keyring.copy()
SingleKeyring {
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
_key: PrivateKey { _privateKey: '0x{private key}' }
}
// When keyring is an instance of MultipleKeyring
> keyring.copy()
MultipleKeyring {
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
_keys: [
PrivateKey { _privateKey: '0x{private key1}' },
PrivateKey { _privateKey: '0x{private key2}' }
]
}
// When keyring is an instance of RoleBasedKeyring
> keyring.copy()
RoleBasedKeyring {
_address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90',
_keys: [
[
PrivateKey { _privateKey: '0x{private key1}' },
PrivateKey { _privateKey: '0x{private key2}' }
],
[
PrivateKey { _privateKey: '0x{private key3}' },
PrivateKey { _privateKey: '0x{private key4}' }
],
[
PrivateKey { _privateKey: '0x{private key5}' },
PrivateKey { _privateKey: '0x{private key6}' }
]
]
}
keyring.sign
keyring.sign(transactionHash, chainId, role [, index])
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.
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
Example
// Using roleBasedKeyring which has two private key in roleTransactionKey
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleTransactionKey)
[
SignatureData { _v: '0x5044', _r: '0x7a8b6...', _s: '0x17139...' },
SignatureData { _v: '0x5043', _r: '0x7f978...', _s: '0x1a532...' }
]
// Using roleBasedKeyring which has two private key in roleTransactionKey with index
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleTransactionKey, 1)
[
SignatureData { _v: '0x5043', _r: '0x7f978...', _s: '0x1a532...' }
]
// Using roleBasedKeyring which has two private key in roleAccountUpdateKey
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleAccountUpdateKey)
[
SignatureData { _v: '0x5044', _r: '0xdbce8...', _s: '0x039a6...' },
SignatureData { _v: '0x5044', _r: '0xf69b7...', _s: '0x71dc9...' }
]
// Using roleBasedKeyring which has two private key in roleAccountUpdateKey with index
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleAccountUpdateKey, 1)
[
SignatureData { _v: '0x5044', _r: '0xf69b7...', _s: '0x71dc9...' }
]
// Using roleBasedKeyring which has two private key in roleFeePayerKey
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleFeePayerKey)
[
SignatureData { _v: '0x5043', _r: '0xe48bf...', _s: '0x1cf36...' },
SignatureData { _v: '0x5043', _r: '0x82976...', _s: '0x3c5e0...' }
]
// Using roleBasedKeyring which has two private key in roleFeePayerKey with index
> keyring.sign('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550', '0x2810', caver.wallet.keyring.role.roleFeePayerKey, 1)
[
SignatureData { _v: '0x5043', _r: '0x82976...', _s: '0x3c5e0...' }
]
keyring.ecsign
keyring.ecsign(hash, role [, index])
Signs with hashed data using the private key and returns a signature where V is 0 or 1 (parity of the y-value of a the secp256k1 curve).
Parameters
Name
Type
Description
hash
string
The hash string 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.
Signs message with Klaytn-specific prefix. This calculates a Klaytn-specific signature with:
sign(keccak256("\x19Klaytn Signed Message:\n" + len(message) + message)))
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
message
string
The message to sign.
Example
// Sign with roleTransactionKey
> keyring.signMessage('message to sign', caver.wallet.keyring.role.roleTransactionKey)
{
messageHash: '0x9c4c1ae0aa1faf7e59eaf6fcf36a34542698197b379a9949b58c92925e74c069',
signatures: [
SignatureData { _v: '0x1b', _r: '0x2dfc6...', _s: '0x15038...' }
],
message: 'message to sign'
}
// Sign with roleFeePayerKey and index
> keyring.signMessage('message to sign', caver.wallet.keyring.role.roleFeePayerKey, 1)
{
messageHash: '0x9c4c1ae0aa1faf7e59eaf6fcf36a34542698197b379a9949b58c92925e74c069',
signatures: [
SignatureData { _v: '0x1b', _r: '0x2dfc6...', _s: '0x15038...' }
],
message: 'message to sign'
}
keyring.getKeyByRole
keyring.getKeyByRole(role)
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
Example
// getKeyByRole with singleKeyring.
// The singleKeyring will return the single same PrivateKey intance regardless of role.
> keyring.getKeyByRole(caver.wallet.keyring.role.roleTransactionKey)
PrivateKey { _privateKey: '0x{private key}' }
> keyring.getKeyByRole(caver.wallet.keyring.role.roleAccountUpdateKey)
PrivateKey { _privateKey: '0x{private key}' }
> keyring.getKeyByRole(caver.wallet.keyring.role.roleFeePayerKey)
PrivateKey { _privateKey: '0x{private key}' }
// getKeyByRole with multipleKeyring.
// The multipleKeyring will also return the single same array of PrivateKey intances regardless of role
> keyring.getKeyByRole(caver.wallet.keyring.role.roleTransactionKey)
[
PrivateKey { _privateKey: '0x{private key1}' },
PrivateKey { _privateKey: '0x{private key2}' }
]
> keyring.getKeyByRole(caver.wallet.keyring.role.roleAccountUpdateKey)
[
PrivateKey { _privateKey: '0x{private key1}' },
PrivateKey { _privateKey: '0x{private key2}' }
]
> keyring.getKeyByRole(caver.wallet.keyring.role.roleFeePayerKey)
[
PrivateKey { _privateKey: '0x{private key1}' },
PrivateKey { _privateKey: '0x{private key2}' }
]
// getKeyByRole with roleBasedKeyring.
// The roleBasedKeyring will return different array of PrivateKey intances depends on role
> keyring.getKeyByRole(caver.wallet.keyring.role.roleTransactionKey)
[
PrivateKey { _privateKey: '0x{private key1}' }
]
> keyring.getKeyByRole(caver.wallet.keyring.role.roleAccountUpdateKey)
[
PrivateKey { _privateKey: '0x{private key2}' },
PrivateKey { _privateKey: '0x{private key3}' }
]
> keyring.getKeyByRole(caver.wallet.keyring.role.roleFeePayerKey)
[
PrivateKey { _privateKey: '0x{private key4}' },
PrivateKey { _privateKey: '0x{private key5}' },
PrivateKey { _privateKey: '0x{private key6}' }
]
keyring.getKlaytnWalletKey
keyring.getKlaytnWalletKey()
Return Value
Type
Description
string
Example
> keyring.getKlaytnWalletKey()
'0x{private key}0x{type}0x{address in hex}'
keyring.toAccount
keyring.toAccount([options])
Parameters
Name
Type
Description
options
Return Value
Type
Description
An Account instance to be used when a user updates AccountKey for their account in the Klaytn. Note that if you want to replace the existing keyring (or the existing private key(s)) with a new keyring (or a new private key(s)) for your account, you must update your AccountKey by sending an Account Update transaction to Klaytn beforehand.
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 .
An array of instances containing one private key inside.
RoleBasedKeyring defines keys which is implemented as a two-dimensional array (empty keys looks like [ [], [], [] ]) that can include multiple keys for each . 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.
A two-dimensional array that defines the keys used for each . Each includes instance(s). The first element in this is roleTransactionKey. The second element is roleAccountUpdateKey. The last element is roleFeePayerKey.
Generates a 2D array of which each array element contains keys defined for each .
An array containing the number of keys for each .
A 2D array of which each array element contains keys defined for each is returned.
If key is a private key string, a instance that uses a single private key is created. If key is an array containing private key strings, a 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 instance is created.
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 .
The keyring instance is returned. Depending on the key parameter, it can be , or .
Creates a SingleKeyring instance from a private key string or a .
This parameter can be either a private key or .
Creates a SingleKeyring instance from a string.
The string.
Creates a RoleBasedKeyring instance from an address and a 2D array of which each array element contains keys defined for each .
The decrypted keyring instance (, or ).
Returns the public key string(s). If keyring is an instance of , getPublicKey returns a public key string. If keyring is an instance of , getPublicKey returns an array of public key strings. If keyring is an instance of , getPublicKey returns a two-dimensional array in which the public key(s) used for each role is defined as an array.
A copied keyring instance (, or ).
When signing transactions, it is recommended to use or .
An array of .
This function is only used for certain transaction types. Therefore, it is recommended to use or when signing a transaction.
An array of .
An array of .
| Array
An instance of or an array containing the instances used by the role.
Returns the string for the keyring. With or , cannot be used. In this case, use .
The of the keyring.
Returns the instance for updating the of the . The instance has an instance that can contain public key(s) inside, which will be sent to Klaytn Network and used for validating transactions. For more details about , see .
Note that if you update the of the stored in the Klaytn, the old private key(s) cannot be used anymore. See on how to use the returned instance to update information in your on Klaytn.
Depending on the type of the private key(s) in the keyring, the returned instances can be classified as follows.
When the keyring contains a private key string: Return an instance that includes the address in the keyring and an instance of
When the keyring contains private key strings: Return an instance that includes the address in the keyring and an instance of
When the keyring contains the different private key strings by role: Return an instance that includes the address in the keyring and an instance of
| Array
(optional) instance containing information that should be defined when updating your existing account to the one with a number of private keys. If keyring uses different private keys for each role, a instance must be defined for each role in an array. If keyring uses more than one private key and options parameter is not defined, the default with the threshold of 1 and the weight of 1 for each key will be used.
Encrypts a keyring and returns a keystore v4 standard. For more information, please refer to .
The address in the encrypted .
For more information, please refer to .
Encrypts an instance of and returns a keystore v3 standard.
Note that and cannot use encryptV3. In this case, please use with a keystore V4 standard.