>caver.utils.sha3('234') // taken as string'0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79'>caver.utils.sha3(newcaver.utils.BN('234')) // utils.sha3 stringify bignumber instance.'0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79'>caver.utils.sha3(234)null// can't calculate the has of a number>caver.utils.sha3(0xea) // same as above, just the HEX representation of the numbernull>caver.utils.sha3('0xea') // will be converted to a byte array first, and then hashed'0x2f20677459120677484f7104c76deb6846a2c071f9b3152c103bb12cd54d1a4a'
soliditySha3
caver.utils.soliditySha3(param1 [, param2,...])
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
>caver.utils.soliditySha3('234564535','0xfff23243',true,-10)// auto detects: uint256, bytes, bool, int256'0x3e27a893dc40ef8a7f0841d96639de2f58a132be5ae466d40087a2cfa83b7179'>caver.utils.soliditySha3('Hello!%') // auto detects: string'0x661136a4267dba9ccdf6bfddb7c00e714de936674c4bdb065a531cf1cb15c7fc'>caver.utils.soliditySha3('234') // auto detects: uint256'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'>caver.utils.soliditySha3(0xea) // same as above'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'>caver.utils.soliditySha3(newcaver.utils.BN('234')) // same as above'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'>caver.utils.soliditySha3({type:'uint256', value:'234'})) // same as above'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'>caver.utils.soliditySha3({t:'uint', v:newcaver.utils.BN('234')})) // same as above'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'>caver.utils.soliditySha3('0x407D73d8a49eeb85D32Cf465507dd71d507100c1')'0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b'>caver.utils.soliditySha3({t:'bytes', v:'0x407D73d8a49eeb85D32Cf465507dd71d507100c1'})'0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b'// same result as above>caver.utils.soliditySha3({t:'address', v:'0x407D73d8a49eeb85D32Cf465507dd71d507100c1'})'0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b' // same as above, but will do a checksum check, if its multi case
>caver.utils.soliditySha3({t:'bytes32', v:'0x407D73d8a49eeb85D32Cf465507dd71d507100c1'})'0x3c69a194aaf415ba5d6afca734660d0a3d45acdc05d54cd1ca89a8988e7625b4'// different result as above> caver.utils.soliditySha3({t: 'string', v: 'Hello!%'}, {t: 'int8', v:-23}, {t: 'address', v: '0x85F43D8a49eeB85d32Cf465507DD71d507100C1d'})
'0xa13b31627c1ed7aaded5aecec71baf02fe123797fffd45e662eac8e06fbe4955'
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
>caver.utils.isAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d')true>caver.utils.isAddress('c1912fee45d61c87cc5ea59dae31190fffff232d')true>caver.utils.isAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D')true// as all is uppercase, no checksum will be checked>caver.utils.isAddress('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d')true>caver.utils.isAddress('0xC1912fEE45d61C87Cc5EA59DaE31190FFFFf232d')false// wrong checksum
toChecksumAddress
caver.utils.toChecksumAddress(address)
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
>caver.utils.toChecksumAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d')'0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d'>caver.utils.toChecksumAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D')'0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d'// same as above
checkAddressChecksum
caver.utils.checkAddressChecksum(address)
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.
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
>caver.utils.toHex('234')'0xea'>caver.utils.toHex(234)'0xea'>caver.utils.toHex(newcaver.utils.BN('234'))'0xea'>caver.utils.toHex(newcaver.utils.BigNumber('234'))'0xea'>caver.utils.toHex('I have 100€')'0x49206861766520313030e282ac'
hexToNumberString
caver.utils.hexToNumberString(hex)
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
>caver.utils.hexToNumberString('0xea')"234"
hexToNumber
caver.utils.hexToNumber(hex)
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
>caver.utils.hexToNumber('0xea')234
numberToHex
caver.utils.numberToHex(number)
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
>caver.utils.numberToHex('234')'0xea'
hexToUtf8
caver.utils.hexToUtf8(hex)caver.utils.hexToString(hex) // ALIAS
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
>caver.utils.hexToUtf8('0x49206861766520313030e282ac')'I have 100€'
hexToAscii
caver.utils.hexToAscii(hex)
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
>caver.utils.hexToAscii('0x4920686176652031303021')'I have 100!'
utf8ToHex
caver.utils.utf8ToHex(str)caver.utils.stringToHex(str) // ALIAS
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
>caver.utils.utf8ToHex('I have 100€')'0x49206861766520313030e282ac'
asciiToHex
caver.utils.asciiToHex(str)
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
>caver.utils.asciiToHex('I have 100!')'0x4920686176652031303021'
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 BN, it returns a BN instance, otherwise a string.
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'
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'.
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.
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. 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.
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.
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
// with '0x' hex prefix
> caver.utils.isValidHash('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
true
// without '0x' hex prefix
> caver.utils.isValidHash('e9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
true
> caver.utils.isValidHash('0x1')
false
isValidHashStrict
caver.utils.isValidHashStrict(input)
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
// with '0x' hex prefix
> caver.utils.isValidHashStrict('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
true
// without '0x' hex prefix
> caver.utils.isValidHashStrict('e9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
false
> caver.utils.isValidHashStrict('0x1')
false
isTxHash
caver.utils.isTxHash(input)
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
// with '0x' hex prefix
> caver.utils.isTxHash('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
true
// without '0x' hex prefix
> caver.utils.isTxHash('e9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
true
> caver.utils.isTxHash('0x1')
false
isTxHashStrict
caver.utils.isTxHashStrict(input)
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
// with '0x' hex prefix
> caver.utils.isTxHashStrict('0xe9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
true
// without '0x' hex prefix
> caver.utils.isTxHashStrict('e9a11d9ef95fb437f75d07ce768d43e74f158dd54b106e7d3746ce29d545b550')
false
> caver.utils.isTxHashStrict('0x1')
false
isValidPrivateKey
caver.utils.isValidPrivateKey(privateKey)
Returns true if privateKey is valid, otherwise it returns false.