caver.utils
caver.utils
provides utility functions.
randomHex
caver.utils.randomHex(size)
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., 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'
_ (underscore)
caver.utils._()
The underscore library for many convenient JavaScript functions.
See the underscore API reference for details.
Example
> var _ = caver.utils._> _.union([1,2],[3])[1,2,3]> _.each({my: 'object'}, function(value, key){ ... })...
toBN
caver.utils.toBN(number)
Safely converts any given value (including BigNumber.js instances) into a BN.js instance, for handling big numbers in JavaScript.
Parameters
Name | Type | Description |
---|---|---|
number | string \ | number |
Return Value
Type | Description |
---|---|
Object | The BN.js instance. |
Examples
> caver.utils.toBN(1234).toString()'1234'> caver.utils.toBN('1234').add(caver.utils.toBN('1')).toString()'1235'> caver.utils.toBN('0xea').toString()'234'
isBN
caver.utils.isBN(bn)
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 | true if a given value is a BN.js instance. |
Example
> var number = new caver.utils.BN(10)> caver.utils.isBN(number)true
isBigNumber
caver.utils.isBigNumber(bignumber)
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 | true if a given value is a BigNumber.js instance. |
Example
> var number = new caver.utils.BigNumber(10)> caver.utils.isBigNumber(number)true
sha3
caver.utils.sha3(str)caver.utils.keccak256(str) // ALIAS
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
> caver.utils.sha3('234') // taken as string'0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79'> caver.utils.sha3(new caver.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 |
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(new caver.utils.BN('234')) // same as above'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'> caver.utils.soliditySha3({type: 'uint256', value: '234'})) // same as above'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'> caver.utils.soliditySha3({t: 'uint', v: new caver.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'
isHex
caver.utils.isHex(hex)
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
> caver.utils.isHex('0xc1912')true> caver.utils.isHex('c1912')true> caver.utils.isHex('0xZ1912')false> caver.utils.isHex('Hello')false
isHexStrict
caver.utils.isHexStrict(hex)
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
> caver.utils.isHexStrict('0xc1912')true> caver.utils.isHexStrict('c1912')false> caver.utils.isHexStrict('0xZ1912')false> caver.utils.isHex('Hello')false
isAddress
caver.utils.isAddress(address)
Checks if a given string is a valid kaia 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 kaia 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 kaia 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. |
Examples
> caver.utils.checkAddressChecksum('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d')true