caver.utils (~v1.4.1)
caver-js utility APIs.
caver.utils
provides utility functions.
randomHex
The randomHex library to generate cryptographically strong pseudo-random HEX strings from a given byte size.
Parameters
Name | Type | Description |
---|---|---|
size | Number | The byte size for the HEX string, e.g., |
Return Value
Type | Description |
---|---|
String | The generated random HEX string. |
Example
_
The underscore library for many convenience JavaScript functions.
See the underscore API reference for details.
Example
BN
The BN.js library for calculating with big numbers in JavaScript. See the BN.js documentation for details.
Parameters
Name | Type | Description |
---|---|---|
mixed | String | Number | A number, number string or HEX string to convert to a BN object. |
Return Value
Type | Description |
---|---|
Object | The BN.js instance. |
Example
isBN
Checks if a given value is a BN.js instance.
Parameters
Name | Type | Description |
---|---|---|
bn | Object | A BN.js instance. |
Return Value
Type | Description |
---|---|
Boolean |
|
Example
isBigNumber
Checks if a given value is a BigNumber.js instance.
Parameters
Name | Type | Description |
---|---|---|
bignumber | Object | A BigNumber.js instance. |
Return Value
Type | Description |
---|---|
Boolean |
|
Example
sha3
Calculates the sha3 of the input.
NOTE: To mimic the sha3 behavior of Solidity use caver.utils.soliditySha3.
Parameters
Name | Type | Description |
---|---|---|
string | String | A string to hash. |
Return Value
Type | Description |
---|---|
String | The result hash. |
Example
soliditySha3
Calculates the sha3 of given input parameters in the same way solidity would. This means arguments will be ABI converted and tightly packed before being hashed.
Parameters
Name | Type | Description |
---|---|---|
paramX | Mixed | Any type, or an object with |
Return Value
Type | Description |
---|---|
String | The result hash. |
Example
isHex
Checks if a given string is a HEX string.
Parameters
Name | Type | Description |
---|---|---|
hex | String | HEX | The given HEX string. |
Return Value
Type | Description |
---|---|
Boolean |
|
Example
isHexStrict
Checks if a given string is a HEX string. Difference to caver.utils.isHex is that it expects HEX to be prefixed with 0x
.
Parameters
Name | Type | Description |
---|---|---|
hex | String | HEX | The given HEX string. |
Return Value
Type | Description |
---|---|
Boolean |
|
Example
isAddress
Checks if a given string is a valid Klaytn address. It will also check the checksum, if the address has upper and lowercase letters.
Parameters
Name | Type | Description |
---|---|---|
address | String | An address string. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
toChecksumAddress
Converts an upper or lowercase Klaytn address to a checksum address.
Parameters
Name | Type | Description |
---|---|---|
address | String | An address string. |
Return Value
Type | Description |
---|---|
String | The checksum address. |
Examples
checkAddressChecksum
Checks the checksum of a given address. Will also return false
on non-checksum addresses.
Parameters
Name | Type | Description |
---|---|---|
address | String | An address string. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
toHex
Converts any given value to HEX. Number strings will interpreted as numbers. Text strings will be interpreted as UTF-8 strings.
Parameters
Name | Type | Description |
---|---|---|
mixed | String | Number | BN | BigNumber | The input to convert to HEX. |
Return Value
Type | Description |
---|---|
String | The resulting HEX string. |
Examples
toBN
Safely converts any given value (including BigNumber.js instances) into a BN.js instance, for handling big numbers in JavaScript.
NOTE: For just the BN.js class, use caver.utils.BN.
Parameters
Name | Type | Description |
---|---|---|
number | String | Number | HEX | Number to convert to a big number. |
Return Value
Type | Description |
---|---|
Object | The BN.js instance. |
Examples
hexToNumberString
Returns the number representation of a given HEX value as a string.
Parameters
Name | Type | Description |
---|---|---|
hexString | HEX String | A HEX string to be converted. |
Return Value
Type | Description |
---|---|
String | The number as a string. |
Examples
hexToNumber
Returns the number representation of a given HEX value.
NOTE: This is not useful for big numbers, rather use caver.utils.toBN.
Parameters
Name | Type | Description |
---|---|---|
hexString | HEX String | A HEX string to be converted. |
Return Value
Type | Description |
---|---|
Number | The number representation of a given HEX value. |
Examples
numberToHex
Returns the HEX representation of a given number value.
Parameters
Name | Type | Description |
---|---|---|
number | String | Number | BN | BigNumber | A number as string or number. |
Return Value
Type | Description |
---|---|
String | The HEX value of the given number. |
Examples
hexToUtf8
Returns the UTF-8 string representation of a given HEX value.
Parameters
Name | Type | Description |
---|---|---|
hex | String | A HEX string to convert to a UTF-8 string. |
Return Value
Type | Description |
---|---|
String | The UTF-8 string. |
Examples
hexToAscii
Returns the ASCII string representation of a given HEX value.
Parameters
Name | Type | Description |
---|---|---|
hex | String | A HEX string to convert to a ASCII string. |
Return Value
Type | Description |
---|---|
String | The ASCII string. |
Examples
utf8ToHex
Returns the HEX representation of a given UTF-8 string.
Parameters
Name | Type | Description |
---|---|---|
string | String | A UTF-8 string to convert to a HEX string. |
Return Value
Type | Description |
---|---|
String | The HEX string. |
Examples
asciiToHex
Returns the HEX representation of a given ASCII string.
Parameters
Name | Type | Description |
---|---|---|
string | String | An ASCII string to convert to a HEX string. |
Return Value
Type | Description |
---|---|
String | The HEX string. |
Examples
hexToBytes
Returns a byte array from the given HEX string.
Parameters
Name | Type | Description |
---|---|---|
hex | HEX String | A HEX string to be converted. |
Return Value
Type | Description |
---|---|
Array | The byte array. |
Examples
bytesToHex
Returns a HEX string from a byte array.
Parameters
Name | Type | Description |
---|---|---|
byteArray | Array | A byte array to convert. |
Return Value
Type | Description |
---|---|
String | The HEX string. |
Examples
toPeb
Converts any KLAY value into peb.
NOTE: "peb" is the smallest KLAY unit, and you should always make calculations in peb and convert only for display reasons.
Parameters
Name | Type | Description |
---|---|---|
number | String | Number | BN | The value. |
unit | String | (optional, defaults to |
Return Value
Type | Description |
---|---|
String | BN | If a number or a string is given, it returns a number string, otherwise a BN.js instance. |
Examples
fromPeb
NOTE: "peb" is the smallest KLAY unit, and you should always make calculations in KLAY and convert only for display reasons.
Parameters
Name | Type | Description |
---|---|---|
number | String | Number | BN | The value in peb. |
unit | String | (optional, defaults to |
Return Value
Type | Description |
---|---|
String | BN | If a number or a string is given, it returns a number string, otherwise a BN.js instance. |
Examples
unitMap
Shows all possible KLAY values and their amount in peb.
Return Value
Type | Description |
---|---|
Object | With the following properties:
- |
Examples
padLeft
Adds a padding on the left of a string. Useful for adding paddings to HEX strings.
Parameters
Name | Type | Description |
---|---|---|
string | String | The string to add padding on the left. |
characterAmount | Number | The number of characters the total string should have. |
sign | String | (optional) The character sign to use, defaults to |
Return Value
Type | Description |
---|---|
String | The padded string. |
Examples
padRight
Adds a padding on the right of a string, Useful for adding paddings to HEX strings.
Parameters
Name | Type | Description |
---|---|---|
string | String | The string to add padding on the right. |
characterAmount | Number | The number of characters the total string should have. |
sign | String | (optional) The character sign to use, defaults to |
Return Value
Type | Description |
---|---|
String | The padded string. |
Examples
toTwosComplement
Converts a negative number into a two's complement.
Parameters
Name | Type | Description |
---|---|---|
number | Number | String | BigNumber | The number to convert. |
Return Value
Type | Description |
---|---|
String | The converted hex string. |
Examples
isContractDeployment
Returns true
if the given transaction is a smart contract deploy transaction. It returns false
if the transaction is not a smart contract deploy transaction. The result is determined by the values of the parameters in the transactionObject
. Make sure all the mandatory parameters are set correctly.
NOTE caver.klay.isContractDeployment is supported from v1.0.1-rc.8. To use this feature, please install v1.0.1-rc.8 or higher.
Parameters
Name | Type | Description |
---|---|---|
transactionObject | Object | Transaction object that you want to determine if the transaction is for smart contract deploy. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
xyPointFromPublicKey
Returns the x and y coordinates of the given publicKey. For more information on key cryptography, see Elliptic-curve cryptography.
NOTE This function does not contain any logic to check whether the public key is valid. The function only split the input publicKey into x and y points by length.
Parameters
Name | Type | Description |
---|---|---|
publicKey | String | The publicKey to get x and y points. |
Return Value
Type | Description |
---|---|
Array | An array storing x and y points. Index 0 has x point, and index 1 has y point. |
Examples
isHexPrefixed
Returns true
if input is a 0x-prefixed hex string, otherwise it returns false
.
NOTE caver.klay.isHexPrefixed is supported from v1.0.1. To use this feature, please install v1.0.1 or higher.
Parameters
Name | Type | Description |
---|---|---|
input | String | The value to be determined if the parameter is 0x-prefixed hex string or not. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
addHexPrefix
Returns a 0x-prefixed hex string. If the input is already 0x-prefixed or a non-hex string, the input value is returned as-is.
NOTE caver.klay.addHexPrefix is supported from v1.0.1. To use this feature, please install v1.0.1 or higher.
Parameters
Name | Type | Description |
---|---|---|
input | String | String value to be prefixed with 0x. |
Return Value
Type | Description |
---|---|
String | 0x-prefixed hex string is returned. |
Examples
stripHexPrefix
Returns the result with 0x prefix stripped from input.
NOTE caver.klay.stripHexPrefix is supported from v1.0.1. To use this feature, please install v1.0.1 or higher.
Parameters
Name | Type | Description |
---|---|---|
input | String | String to remove 0x prefix. |
Return Value
Type | Description |
---|---|
String | A string stripped of 0x is returned. |
Examples
toBuffer
This function converts the input to a Buffer. To convert an Object into a Buffer using toBuffer
, the Object must implement toArray function. For String type input, this function only works with a 0x-prefixed hex string.
NOTE caver.klay.toBuffer is supported from v1.1.0. To use this feature, please install v1.1.0 or higher.
Parameters
Name | Type | Description |
---|---|---|
input | Buffer | String | Number | Array | BN | Object | The value to be converted to a Buffer. |
Return Value
Type | Description |
---|---|
Buffer | The value converted to Buffer type is returned. |
Examples
numberToBuffer
This function converts a number to a Buffer. The caver.utils.toBuffer has the same behavior as this function when the input is a number.
NOTE caver.klay.numberToBuffer is supported from v1.1.0. To use this feature, please install v1.1.0 or higher.
Parameters
Name | Type | Description |
---|---|---|
input | String | Number | BN | A number to be converted to a Buffer. |
Return Value | ||
Type | Description | |
--- | --- | |
Buffer | The value converted to Buffer type is returned. | |
Examples | ||
```javascript |
caver.utils.numberToBuffer(1) <Buffer 01> caver.utils.numberToBuffer('2') <Buffer 02> caver.utils.numberToBuffer('0x3') <Buffer 03> caver.utils.numberToBuffer(new caver.utils.BN(4)) <Buffer 04>
Returns true
if input is in transaction hash format, otherwise it returns false
. This function only looks at the input and determines if it is in the format of a transaction hash. NOTE caver.klay.isTxHash is supported from v1.2.0-rc.1. To use this feature, please install v1.2.0-rc.1 or higher. Parameters
Name | Type | Description |
---|---|---|
input | String | The value to be determined if the parameter is in the format of transaction hash or not. |
Return Value | ||
Type | Description | |
--- | --- | |
Boolean |
| |
Examples | ||
```javascript | ||
// with '0x' hex prefix |
caver.utils.isTxHash('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550') true // without '0x' hex prefix caver.utils.isTxHash('e9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550') true caver.utils.isTxHash('0x1') false
Returns true
if input is in transaction hash format, otherwise it returns false
. This function only looks at the input and determines if it is in the format of a transaction hash. Difference to caver.utils.isTxHash is that it expects HEX to be prefixed with 0x
. NOTE caver.klay.isTxHashStrict is supported from v1.2.0-rc.1. To use this feature, please install v1.2.0-rc.1 or higher. Parameters
Name | Type | Description |
---|---|---|
input | String | The value to be determined if the parameter is in the format of transaction hash or not. |
Return Value | ||
Type | Description | |
--- | --- | |
Boolean |
| |
Examples | ||
```javascript | ||
// with '0x' hex prefix |
caver.utils.isTxHashStrict('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550') true // without '0x' hex prefix caver.utils.isTxHashStrict('e9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550') false caver.utils.isTxHashStrict('0x1') false
Returns true
if privateKey is valid, otherwise it returns false
.
Parameters
Name | Type | Description |
---|---|---|
privateKey | String | A private key string to validate. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
isValidPublicKey
Returns true
if publicKey is valid, otherwise it returns false
.
NOTE caver.utils.isValidPublicKey
is supported since caver-js v1.2.0.
Parameters
Name | Type | Description |
---|---|---|
publicKey | String | A public key string to validate. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
isValidRole
Returns true
if role is valid, otherwise it returns false
. Roles that are currently defined for AccountKeyRoleBased are transactionKey
, updateKey
, and feePayerKey
.
NOTE caver.utils.isValidRole
is supported since caver-js v1.2.0.
Parameters
Name | Type | Description |
---|---|---|
role | String | A role string to validate. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
isEmptySig
Returns true
if sig is in the format of empty signature ([['0x01', '0x', '0x']]
or ['0x01', '0x', '0x']
), otherwise it returns false
.
When RLP-encoding a transaction in caver-js, if signatures or feePayerSignatures is empty, the value representing an empty signature, [['0x01', '0x', '0x']]
, is returned for the property. This function is used to check whether the given signature is [['0x01', '0x', '0x']]
(or ['0x01', '0x', '0x']
in the 'LEGACY' transaction).
NOTE caver.utils.isEmptySig
is supported since caver-js v1.2.0.
Parameters
Name | Type | Description |
---|---|---|
sig | Array | An array of signatures to check empty or not. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
isKlaytnWalletKey
Returns true
if key is in KlaytnWalletKey format, otherwise it returns false
.
NOTE caver.utils.isKlaytnWalletKey
has been updated with the additional logic for a more accurate result since caver-js v1.3.2-rc.3.
Parameters
Name | Type | Description |
---|---|---|
key | String | A key string to check in the format of KlaytnWalletKey or not. |
Return Value
Type | Description |
---|---|
Boolean |
|
Examples
bufferToHex
Converts buffer to 0x-prefixed hex string.
NOTE caver.utils.bufferToHex
is supported since caver-js v1.3.2-rc.1.
Parameters
Name | Type | Description |
---|---|---|
buffer | Buffer | A buffer to convert to hex string. |
Return Value
Type | Description |
---|---|
String | The 0x-prefixed hex string. |
Examples
Last updated