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 Kaia'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: User signs with a private key
- MultipleKeyring: User signs with private keys
- RoleBasedKeyring: 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 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 | PrivateKey | An instance of PrivateKey containing one private key inside. |
MultipleKeyring
const keyring = new caver.wallet.keyring.multipleKeyring(address, keys)
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
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.
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
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 |
|---|---|
| SingleKeyring | A randomly generated single keyring instance is returned. |
Example
> caver.wallet.keyring.generate()SingleKeyring { _address: '0x8ecdfda0281f0d36518f89e0e2444c4f98b2e718', _key: PrivateKey { _privateKey: '0x{private key}' }}
caver.wallet.keyring.generateSingleKey
caver.wallet.keyring.generateSingleKey([entropy])
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
> caver.wallet.keyring.generateSingleKey()'0x{private key}'
caver.wallet.keyring.generateMultipleKeys
caver.wallet.keyring.generateMultipleKeys(num [, entropy])
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
> caver.wallet.keyring.generateMultipleKeys(3)[ '0x{private key1}', '0x{private key2}', '0x{private key3}']
caver.wallet.keyring.generateRoleBasedKeys
caver.wallet.keyring.generateRoleBasedKeys(numArray [, entropy])
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
> caver.wallet.keyring.generateRoleBasedKeys([2, 1, 3])[ [ '0x{private key1}', '0x{private key2}' ], [ '0x{private key3}' ], [ '0x{private key4}', '0x{private key5}', '0x{private key6}' ]]
caver.wallet.keyring.create
caver.wallet.keyring.create(address, key)
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 \ | 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
// Create singleKeyring which uses one private key> caver.wallet.keyring.create('0x{address in hex}', '0x{private key}')SingleKeyring { _address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90', _key: PrivateKey { _privateKey: '0x{private key}' }}// Create multipleKeyring which uses multiple private keys> caver.wallet.keyring.create('0x{address in hex}', ['0x{private key1}', '0x{private key2}'])MultipleKeyring { _address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90', _keys: [ PrivateKey { _privateKey: '0x{private key1}' }, PrivateKey { _privateKey: '0x{private key2}' } ]}// Create roleBasedKeyring which uses different private key(s) by roles> const roleBasedKeys = [ ['0x{private key1}', '0x{private key2}'], ['0x{private key3}', '0x{private key4}'], ['0x{private key5}', '0x{private key6}'],]> caver.wallet.keyring.create('0x{address in hex}', roleBasedKeys)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}' } ] ]}
caver.wallet.keyring.createFromPrivateKey
caver.wallet.keyring.createFromPrivateKey(key)
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 |
|---|---|
| SingleKeyring | The SingleKeyring instance is returned. |
Example
// Create singleKeyring from private key string> caver.wallet.keyring.createFromPrivateKey('0x{private key}')SingleKeyring { _address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0', _key: PrivateKey { _privateKey: '0x{private key}' }}// Create singleKeyring from KlaytnWalletKey> caver.wallet.keyring.createFromPrivateKey('0x{private key}0x{type}0x{address in hex}')SingleKeyring { _address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0', _key: PrivateKey { _privateKey: '0x{private key}' }}
caver.wallet.keyring.createFromKlaytnWalletKey
caver.wallet.keyring.createFromKlaytnWalletKey(klaytnWalletKey)
Creates a SingleKeyring instance from a KlaytnWalletKey string.
Parameters
| Name | Type | Description |
|---|---|---|
| klaytnWalletKey | string | The KlaytnWalletKey string. |
Return Value
| Type | Description |
|---|---|
| SingleKeyring | The SingleKeyring instance is returned. |
Example
> caver.wallet.keyring.createFromKlaytnWalletKey('0x{private key}0x{type}0x{address in hex}')SingleKeyring { _address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0', _key: PrivateKey { _privateKey: '0x{private key}' }}
caver.wallet.keyring.createWithSingleKey
caver.wallet.keyring.createWithSingleKey(address, key)
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 |
|---|---|
| SingleKeyring | The SingleKeyring instance is returned. |
Example
> caver.wallet.keyring.createWithSingleKey('0x{address in hex}', '0x{private key}')SingleKeyring { _address: '0xaa7b43f2eab01cfd787b07ce2f2fb5d6d20a8aa0', _key: PrivateKey { _privateKey: '0x{private key}' }}
caver.wallet.keyring.createWithMultipleKey
caver.wallet.keyring.createWithMultipleKey(address, key)
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 |
|---|---|
| MultipleKeyring | The MultipleKeyring instance is returned. |
Example
> caver.wallet.keyring.createWithMultipleKey('0x{address in hex}', ['0x{private key1}', '0x{private key2}' ])MultipleKeyring { _address: '0x30fcfa9679c7141a234c1324c7e0a8b715bdfc90', _keys: [ PrivateKey { _privateKey: '0x{private key1}' }, PrivateKey { _privateKey: '0x{private key2}' } ]}