caver.utils

caver-js utility APIs.

caver.utils provides utility functions.

randomHex

caver.utils.randomHex(size)

The randomHexarrow-up-right 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., 32 will result in a 32-byte HEX string with 64 characters prefixed with "0x".

Return Value

Type

Description

string

The generated random HEX string.

Example

> caver.utils.randomHex(32)
'0x861b56754dba7769f9740c3ad70b4694aa24d604c1dba3bac7ec45978927b8de'

> caver.utils.randomHex(4)
'0x5641d6ce'

> caver.utils.randomHex(2)
'0xf058'

> caver.utils.randomHex(1)
'0x7c'

> caver.utils.randomHex(0)
'0x'

_

The underscorearrow-up-right library for many convenient JavaScript functions.

See the underscore API referencearrow-up-right for details.

Example

toBN

Safely converts any given value (including BigNumber.jsarrow-up-right instances) into a BN.jsarrow-up-right instance, for handling big numbers in JavaScript.

Parameters

Name

Type

Description

number

string | number

number to convert to a big number.

Return Value

Type

Description

Object

The BN.jsarrow-up-right instance.

Examples

isBN

Checks if a given value is a BN.jsarrow-up-right instance.

Parameters

Name

Type

Description

bn

object

Return Value

Type

Description

boolean

true if a given value is a BN.jsarrow-up-right instance.

Example

isBigNumber

Checks if a given value is a BigNumber.jsarrow-up-right instance.

Parameters

Name

Type

Description

bignumber

object

Return Value

Type

Description

boolean

true if a given value is a BigNumber.js instance.

Example

sha3

Calculates the sha3 of the input.

NOTE: To mimic the sha3 behavior of Solidity use caver.utils.soliditySha3.

Parameters

Name

Type

Description

str

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 {type: 'uint', value: '123456'} or {t: 'bytes', v: '0xfff456'}. Basic types are autodetected as follows: - string non numerical UTF-8 string is interpreted as string. - `string

number

BN

HEXpositive number is interpreted asuint256.<br> -string

number

BNnegative number is interpreted asint256.<br> -booleanasbool.<br> -stringHEX string with leading0xis interpreted asbytes.<br> -HEXHEX number representation is interpreted asuint256`.

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

The given HEX string.

Return Value

Type

Description

boolean

true if a given parameter is a HEX string.

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

The given HEX string.

Return Value

Type

Description

boolean

true if a given string is a HEX string.

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

true if a given string is a valid Klaytn address.

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

true when the checksum of the address is valid, false if it is not a checksum address, or the checksum is invalid.

Examples

toHex

Converts any given value to HEX. The numeric strings will be 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

hexToNumberString

Returns the number representation of a given HEX value as a string.

Parameters

Name

Type

Description

hexstring

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

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 an 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

str

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

str

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

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

convertToPeb

Converts any KLAY value into peb.

NOTE: "peb" is the smallest KLAY unit, and you should always use "peb" as the unit of KLAY. Convert to "KLAY" only for display reasons.

Parameters

Name

Type

Description

number

string | number | BN

The value.

unit

string

(optional, defaults to "KLAY") The unit of KLAY to convert from. number will be multiplied by one of the following multipliers for the unit provided: - peb: '1' - kpeb: '1000' - Mpeb: '1000000' - Gpeb: '1000000000' - Ston: '1000000000' - uKLAY: '1000000000000' - mKLAY: '1000000000000000' - KLAY: '1000000000000000000' - kKLAY: '1000000000000000000000' - MKLAY: '1000000000000000000000000' - GKLAY: '1000000000000000000000000000'

Return Value

Type

Description

string | BN

If the number parameter is an instance of BNarrow-up-right, it returns a BN instance, otherwise a string.

Examples

convertFromPeb

NOTE: "peb" is the smallest KLAY unit, and you should always use "peb" as the unit of KLAY. Convert to "KLAY" only for display reasons.

Parameters

Name

Type

Description

number

string | number | BN | BigNumber

The value in peb.

unit

string

(optional, defaults to "KLAY") The unit of KLAY to convert your "peb" into. number will be divided by one of the following denominators for the unit provided: - peb: '1' - kpeb: '1000' - Mpeb: '1000000' - Gpeb: '1000000000' - Ston: '1000000000' - uKLAY: '1000000000000' - mKLAY: '1000000000000000' - KLAY: '1000000000000000000' - kKLAY: '1000000000000000000000' - MKLAY: '1000000000000000000000000' - GKLAY: '1000000000000000000000000000'

Return Value

Type

Description

string

The string number.

Examples

unitMap

Shows all possible KLAY values and their amount in peb.

Return Value

Type

Description

Object

With the following properties: - peb: '1' - kpeb: '1000' - Mpeb: '1000000' - Gpeb: '1000000000' - Ston: '1000000000' - uKLAY: '1000000000000' - mKLAY: '1000000000000000' - KLAY: '1000000000000000000' - kKLAY: '1000000000000000000000' - MKLAY: '1000000000000000000000000' - GKLAY: '1000000000000000000000000000' - TKLAY: '1000000000000000000000000000000'

Examples

klayUnit

Shows all KLAY units.

Return Value

Type

Description

Object

An object in which the units of KLAY used in Klaytn are defined. Each unit has its name and pebFactor. pebFactor is used when converting KLAY currently translated in each unit to 'peb'.

Examples

padLeft

Adds 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 0.

Return Value

Type

Description

string

The padded string.

Examples

padRight

Adds padding on the right of a string, Useful for adding paddings to HEX strings.

Parameters

Name

Type

Description

str

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 0.

Return Value

Type

Description

string

The padded string.

Examples

trimLeadingZero

Removes leading zero from 0x-prefixed hex string.

Parameters

Name

Type

Description

hexString

string

A hex string to trim.

Return Value

Type

Description

string

A hex string without leading zero.

Examples

makeEven

Returns a string to an even length.

Parameters

Name

Type

Description

hexString

string

A hex string to make even.

Return Value

Type

Description

string

A string with even length.

Examples

toTwosComplement

Converts a negative number into a two's complement.

Parameters

Name

Type

Description

num

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.

Parameters

Name

Type

Description

transactionObject

object

An instance of Transaction to check contract deploy transaction or not.

Return Value

Type

Description

boolean

true means the transaction object is for smart contract deploy.

Examples

xyPointFromPublicKey

Returns the x and y coordinates of the given publicKey. For more information on key cryptography, see Elliptic-curve cryptographyarrow-up-right.

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. To validate public key, please use isValidPublicKey.

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 the input is a 0x-prefixed hex string, otherwise it returns false.

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

true means the input is 0x-prefixed hex string.

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.

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.1arrow-up-right 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 Bufferarrow-up-right. 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.

Parameters

Name

Type

Description

input

Buffer | string | number | Array | BN | BigNumber | object

The value to be converted to a Buffer.

NOTE BigNumber type is supported since caver-js v1.6.4arrow-up-right.

Return Value

Type

Description

Buffer

The value converted to Buffer type is returned.

Examples

numberToBuffer

This function converts a number to a Bufferarrow-up-right. The caver.utils.toBuffer has the same behavior as this function when the input is a number.

Parameters

Name

Type

Description

input

string | number | BN | BigNumber

A number to be converted to a Buffer.

Return Value

Type

Description

Buffer

The value converted to Buffer type is returned.

Examples

isValidHash

Returns true if the input is in 32-bytes hash format, otherwise it returns false.

Parameters

Name

Type

Description

input

string

The value to be examined that if it is in 32-bytes hash format or not.

Return Value

Type

Description

boolean

true means the input is in the format of 32-bytes hash.

Examples

isValidHashStrict

Returns true if the input is in 0x-prefixed 32-bytes hash format, otherwise it returns false. This function only looks at the input and determines if it is in the format of 0x-prefixed 32-bytes hash. Difference to caver.utils.isValidHash is that it expects HEX to be prefixed with 0x.

Parameters

Name

Type

Description

input

string

The value to be examined that if it is in the format of 0x-prefixed 32-bytes hash or not.

Return Value

Type

Description

boolean

true means the input is in the format of 0x-prefixed 32-bytes hash.

Examples

isTxHash

Returns true if the 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 This function has been deprecated. Use isValidHash to determine if a valid hash is 32 bytes long.

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

true means the input is in the format of transaction hash.

Examples

isTxHashStrict

Returns true if the 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 This function has been deprecated. Use isValidHashStrict to determine if a valid hash is 32 bytes long.

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

true means the input is in the format of transaction hash.

Examples

isValidPrivateKey

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

true means the privateKey is valid.

Examples

isValidPublicKey

Returns true if publicKey is valid, otherwise it returns false.

Parameters

Name

Type

Description

publicKey

string

A public key string to validate.

Return Value

Type

Description

boolean

true means the publicKey is valid.

Examples

isValidRole

Returns true if a role is valid, otherwise it returns false. You can check roles supported by caver-js through caver.wallet.keyring.role.

Parameters

Name

Type

Description

role

string

A role string to validate.

Return Value

Type

Description

boolean

true means the role is valid.

Examples

isValidBlockNumberCandidate

Validates the block number (or block tag string).

The block number should be one of the types below:

  • predefined block number ex:) 'latest', 'earliest', 'pending', 'genesis'

  • hex

  • finite number

Parameters

Name

Type

Description

blockNumber

string | number

The block number to validate. This can be block number in number type or block tag(latest, pending, earliest, genesis) string.

Return Value

Type

Description

boolean

true means blockNumber is valid.

Examples

isPredefinedBlockNumber

Returns true if the parameter is predefined block tag.

Parameters

Name

Type

Description

predefinedBlock

string

The predefined block.

Return Value

Type

Description

boolean

true means predefinedBlock is valid predefined block tag.

Examples

isEmptySig

Returns true if sig is in the format of empty signature (SignatureData { _v: '0x01', _r: '0x', _s: '0x' } or [SignatureData { _v: '0x01', _r: '0x', _s: '0x' }]), otherwise it returns false.

In caver-js, if signatures or feePayerSignatures is empty, the value representing an empty signature, [SignatureData { _v: '0x01', _r: '0x', _s: '0x' }], is returned for the property. This function is used to check whether the given signature is [SignatureData { _v: '0x01', _r: '0x', _s: '0x' }] (or SignatureData { _v: '0x01', _r: '0x', _s: '0x' } in the 'LEGACY' transaction).

Parameters

Name

Type

Description

sig

object | Array

An instance of SignatureData or array of SignatureData to check empty or not.

Return Value

Type

Description

boolean

true means the sig is empty.

Examples

isKlaytnWalletKey

Returns true if key is in KlaytnWalletKey format, otherwise it returns false.

Parameters

Name

Type

Description

key

string

A key string to check in the format of KlaytnWalletKey or not.

Return Value

Type

Description

boolean

true means the key is 0x{private key}0x{type}0x{address in hex} or {private key}0x{type}0x{address in hex}.

Examples

bufferToHex

Converts buffer to 0x-prefixed hex string.

Parameters

Name

Type

Description

buffer

Buffer

A buffer to convert to hex string.

Return Value

Type

Description

string

The 0x-prefixed hex string.

Examples

parseKlaytnWalletKey

Parses KlaytnWalletKey string to an array which includes "private key", "type", "address".

Parameters

Name

Type

Description

key

string

A KlaytnWalletKey string.

Return Value

Type

Description

Array

The parsed KlaytnWalletKey.

Examples

hashMessage

Hashes message with Klaytn specific prefix: keccak256("\x19Klaytn Signed Message:\n" + len(message) + message))

Parameters

Name

Type

Description

message

string

A message to hash. If it is a HEX string, it will be UTF-8 decoded first.

Return Value

Type

Description

string

The hashed message with Klaytn specific prefix.

Examples

recover

Recovers the Klaytn address that was used to sign the given data.

Parameters

Name

Type

Description

message

string

Either message or hashed message.

signature

object | Array

An instance of SignatureData.

isHashed

boolean

(optional, default: false) If the last parameter is true, the given message will NOT automatically be prefixed with "\x19Klaytn Signed Message:\n" + message.length + message, and will be assumed to be already prefixed.

Return Value

Type

Description

string

The Klaytn address used to sign this data.

Examples

recoverPublicKey

Recovers the public key that was used to sign the given data.

NOTE caver.utils.recoverPublicKey is supported since caver-js v1.6.3arrow-up-right.

Parameters

Name

Type

Description

message

string

Either message or hashed message.

signature

object | Array

An instance of SignatureData.

isHashed

boolean

(optional, default: false) Whether the message passed as a parameter is hashed with the prefix "\x19Klaytn Signed Message:\n" + message.length + message.

Return Value

Type

Description

string

The public key used to sign this data.

Examples

publicKeyToAddress

Returns an address derived from a public key. This function simply converts the public key string into an address form by hashing it. It has nothing to do with an actual account on Klaytn.

NOTE caver.utils.publicKeyToAddress is supported since caver-js v1.6.3arrow-up-right.

Parameters

Name

Type

Description

publicKey

string

The public key string to get the address.

Return Value

Type

Description

string

The address string derived from a public key.

Examples

compressPublicKey

Compresses the uncompressed public key.

Parameters

Name

Type

Description

uncompressedPublicKey

string

An uncompressed public key.

Return Value

Type

Description

string

A compressed public key.

Examples

decompressPublicKey

Decompresses the compressed public key.

Parameters

Name

Type

Description

compressedPublicKey

string

A compressed public key.

Return Value

Type

Description

string

An uncompressed public key.

Examples

isCompressedPublicKey

Returns true if public key is compressed, otherwise false.

Parameters

Name

Type

Description

publicKey

string

A public key.

Return Value

Type

Description

boolean

true means compressed.

Examples

decodeSignature

Decodes a raw signature data composed of 'R(32 byte) + S(32 byte) + V(1byte)'.

NOTE caver.utils.decodeSignature is supported since caver-js v1.6.3arrow-up-right.

Parameters

Name

Type

Description

signature

string

The signature string to decode. It is composed of R(32bytes) + S(32bytes) + V(1byte).

Return Value

Type

Description

object

A SignatureData instance that includes v, r and s.

Examples

Last updated

Was this helpful?