caver.wallet.keyring은 주소와 개인키를 포함하는 키링 관련 기능을 제공하는 패키지입니다.
Class
Keyring은 계정 주소와 개인키를 포함하는 구조입니다. 사용자들이 클레이튼 계정을 사용해 서명을 할 수 있게 해주는 caver-js 클래스입니다.
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은 계정의 address와 private key를 저장하는 클래스입니다. To create a SingleKeyring instance with a private key string, please refer to caver.wallet.keyring.create.
MultipleKeyring은 계정의 address와 private keys를 저장하는 클래스입니다. To create a MultipleKeyring instance with private key strings, please refer to caver.wallet.keyring.create.
RoleBasedKeyring는 계정의 address와 각 역할에 사용될 private keys를 배열 형식으로 저장하는 클래스입니다.
RoleBasedKeyring defines keys which is implemented as a two-dimensional array (empty keys looks like [ [], [], [] ]) that can include multiple keys for each role. 2차원 배열의 첫 번째 원소에는 roleTransactionKey로 사용될 개인키(들), 두 번째 원소에는 roleAccountUpdateKey로 사용될 개인키(들), 세 번째 원소에는 roleFeePayerKey로 사용될 개인키(들)이 저장됩니다.
properties
이하는 각 역할에 정의된 키를 직관적으로 사용하기 위해 키링에 정의된 게터(getter)입니다. 각 역할에 사용되는 키에 게터를 통해 더 쉽게 접근할 수 있습니다.
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.
공개키 문자열(들)을 반환합니다. 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
Return Value
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
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.
// 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).
This function is only used for certain transaction types. Therefore, it is recommended to use caver.wallet.sign or transaction.sign when signing a transaction.
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
Return Value
The returned object contains the following:
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
Return Value
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()'0x{private key}0x{type}0x{address in hex}'
keyring.toAccount
keyring.toAccount([options])
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 will be sent to Klaytn Network and used for validating transactions. For more details about Account, see Account Update.
Note that if you update the AccountKey of the Account stored in the Klaytn, the old private key(s) cannot be used anymore. See Getting started on how to use the returned Account instance to update information in your Klaytn account on Klaytn.
Depending on the type of the private key(s) in the keyring, the returned Account instances can be classified as follows.
When the keyring contains a private key string: Return an Account instance that includes the address in the keyring and an instance of AccountKeyPublic
When the keyring contains private key strings: Return an Account instance that includes the address in the keyring and an instance of AccountKeyWeigthedMultiSig
When the keyring contains the different private key strings by role: Return an Account instance that includes the address in the keyring and an instance of AccountKeyRoleBased