K
K
Klaytn Docs
Search…
caver.utils
caver-js utility APIs.
caver.utils provides utility functions.

randomHex

1
caver.utils.randomHex(size)
Copied!
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
1
> caver.utils.randomHex(32)
2
'0x861b56754dba7769f9740c3ad70b4694aa24d604c1dba3bac7ec45978927b8de'
3
4
> caver.utils.randomHex(4)
5
'0x5641d6ce'
6
7
> caver.utils.randomHex(2)
8
'0xf058'
9
10
> caver.utils.randomHex(1)
11
'0x7c'
12
13
> caver.utils.randomHex(0)
14
'0x'
Copied!

_

1
caver.utils._()
Copied!
The underscore library for many convenient JavaScript functions.
See the underscore API reference for details.
Example
1
> var _ = caver.utils._
2
3
> _.union([1,2],[3])
4
[1,2,3]
5
6
> _.each({my: 'object'}, function(value, key){ ... })
7
...
Copied!

toBN

1
caver.utils.toBN(number)
Copied!
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
number to convert to a big number.
Return Value
Type
Description
Object
The BN.js instance.
Examples
1
> caver.utils.toBN(1234).toString()
2
'1234'
3
4
> caver.utils.toBN('1234').add(caver.utils.toBN('1')).toString()
5
'1235'
6
7
> caver.utils.toBN('0xea').toString()
8
'234'
Copied!

isBN

1
caver.utils.isBN(bn)
Copied!
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
1
> var number = new caver.utils.BN(10)
2
> caver.utils.isBN(number)
3
true
Copied!

isBigNumber

1
caver.utils.isBigNumber(bignumber)
Copied!
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
1
> var number = new caver.utils.BigNumber(10)
2
> caver.utils.isBigNumber(number)
3
true
Copied!

sha3

1
caver.utils.sha3(str)
2
caver.utils.keccak256(str) // ALIAS
Copied!
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
1
> caver.utils.sha3('234') // taken as string
2
'0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79'
3
4
> caver.utils.sha3(new caver.utils.BN('234')) // utils.sha3 stringify bignumber instance.
5
'0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79'
6
7
> caver.utils.sha3(234)
8
null // can't calculate the has of a number
9
10
> caver.utils.sha3(0xea) // same as above, just the HEX representation of the number
11
null
12
13
> caver.utils.sha3('0xea') // will be converted to a byte array first, and then hashed
14
'0x2f20677459120677484f7104c76deb6846a2c071f9b3152c103bb12cd54d1a4a'
Copied!

soliditySha3

1
caver.utils.soliditySha3(param1 [, param2, ...])
Copied!
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
1
> caver.utils.soliditySha3('234564535', '0xfff23243', true, -10)
2
// auto detects: uint256, bytes, bool, int256
3
'0x3e27a893dc40ef8a7f0841d96639de2f58a132be5ae466d40087a2cfa83b7179'
4
5
> caver.utils.soliditySha3('Hello!%') // auto detects: string
6
'0x661136a4267dba9ccdf6bfddb7c00e714de936674c4bdb065a531cf1cb15c7fc'
7
8
> caver.utils.soliditySha3('234') // auto detects: uint256
9
'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'
10
11
> caver.utils.soliditySha3(0xea) // same as above
12
'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'
13
14
> caver.utils.soliditySha3(new caver.utils.BN('234')) // same as above
15
'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'
16
17
> caver.utils.soliditySha3({type: 'uint256', value: '234'})) // same as above
18
'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'
19
20
> caver.utils.soliditySha3({t: 'uint', v: new caver.utils.BN('234')})) // same as above
21
'0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2'
22
23
> caver.utils.soliditySha3('0x407D73d8a49eeb85D32Cf465507dd71d507100c1')
24
'0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b'
25
26
> caver.utils.soliditySha3({t: 'bytes', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'})
27
'0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b' // same result as above
28
29
> caver.utils.soliditySha3({t: 'address', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'})
30
'0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b' // same as above, but will do a checksum check, if its multi case
31
32
> caver.utils.soliditySha3({t: 'bytes32', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'})
33
'0x3c69a194aaf415ba5d6afca734660d0a3d45acdc05d54cd1ca89a8988e7625b4' // different result as above
34
35
> caver.utils.soliditySha3({t: 'string', v: 'Hello!%'}, {t: 'int8', v:-23}, {t: 'address', v: '0x85F43D8a49eeB85d32Cf465507DD71d507100C1d'})
36
'0xa13b31627c1ed7aaded5aecec71baf02fe123797fffd45e662eac8e06fbe4955'
Copied!

isHex

1
caver.utils.isHex(hex)
Copied!
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
1
> caver.utils.isHex('0xc1912')
2
true
3
4
> caver.utils.isHex('c1912')
5
true
6
7
> caver.utils.isHex('0xZ1912')
8
false
9
10
> caver.utils.isHex('Hello')
11
false
Copied!

isHexStrict

1
caver.utils.isHexStrict(hex)
Copied!
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
1
> caver.utils.isHexStrict('0xc1912')
2
true
3
4
> caver.utils.isHexStrict('c1912')
5
false
6
7
> caver.utils.isHexStrict('0xZ1912')
8
false
9
10
> caver.utils.isHex('Hello')
11
false
Copied!

isAddress

1
caver.utils.isAddress(address)
Copied!
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
1
> caver.utils.isAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d')
2
true
3
4
> caver.utils.isAddress('c1912fee45d61c87cc5ea59dae31190fffff232d')
5
true
6
7
> caver.utils.isAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D')
8
true // as all is uppercase, no checksum will be checked
9
10
> caver.utils.isAddress('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d')
11
true
12
13
> caver.utils.isAddress('0xC1912fEE45d61C87Cc5EA59DaE31190FFFFf232d')
14
false // wrong checksum
Copied!

toChecksumAddress

1
caver.utils.toChecksumAddress(address)
Copied!
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
1
> caver.utils.toChecksumAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d')
2
'0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d'
3
4
> caver.utils.toChecksumAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D')
5
'0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d' // same as above
Copied!

checkAddressChecksum

1
caver.utils.checkAddressChecksum(address)
Copied!
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
1
> caver.utils.checkAddressChecksum('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d')
2
true
Copied!

toHex

1
caver.utils.toHex(mixed)
Copied!
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
1
> caver.utils.toHex('234')
2
'0xea'
3
4
> caver.utils.toHex(234)
5
'0xea'
6
7
> caver.utils.toHex(new caver.utils.BN('234'))
8
'0xea'
9
10
> caver.utils.toHex(new caver.utils.BigNumber('234'))
11
'0xea'
12
13
> caver.utils.toHex('I have 100€')
14
'0x49206861766520313030e282ac'
Copied!

hexToNumberString

1
caver.utils.hexToNumberString(hex)
Copied!
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
1
> caver.utils.hexToNumberString('0xea')
2
"234"
Copied!

hexToNumber

1
caver.utils.hexToNumber(hex)
Copied!
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
1
> caver.utils.hexToNumber('0xea')
2
234
Copied!

numberToHex

1
caver.utils.numberToHex(number)
Copied!
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
1
> caver.utils.numberToHex('234')
2
'0xea'
Copied!

hexToUtf8

1
caver.utils.hexToUtf8(hex)
2
caver.utils.hexToString(hex) // ALIAS
Copied!
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
1
> caver.utils.hexToUtf8('0x49206861766520313030e282ac')
2
'I have 100€'
Copied!

hexToAscii

1
caver.utils.hexToAscii(hex)
Copied!
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
1
> caver.utils.hexToAscii('0x4920686176652031303021')
2
'I have 100!'
Copied!

utf8ToHex

1
caver.utils.utf8ToHex(str)
2
caver.utils.stringToHex(str) // ALIAS
Copied!
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
1
> caver.utils.utf8ToHex('I have 100€')
2
'0x49206861766520313030e282ac'
Copied!

asciiToHex

1
caver.utils.asciiToHex(str)
Copied!
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
1
> caver.utils.asciiToHex('I have 100!')
2
'0x4920686176652031303021'
Copied!

hexToBytes

1
caver.utils.hexToBytes(hex)
Copied!
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
1
> caver.utils.hexToBytes('0x000000ea')
2
[ 0, 0, 0, 234 ]
Copied!

bytesToHex

1
caver.utils.bytesToHex(byteArray)
Copied!
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
1
> caver.utils.bytesToHex([ 72, 101, 108, 108, 111, 33, 36 ])
2
'0x48656c6c6f2124'
Copied!

convertToPeb

1
caver.utils.convertToPeb(number [, unit])
Copied!
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 BN, it returns a BN instance, otherwise a string.
Examples
1
> caver.utils.convertToPeb('1', 'KLAY')
2
'1000000000000000000'
3
4
> caver.utils.convertToPeb(caver.utils.toBN(1), 'KLAY')
5
<BN: de0b6b3a7640000>
Copied!

convertFromPeb

1
caver.utils.convertFromPeb(number [, unit])
Copied!
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
1
> caver.utils.convertFromPeb('1', 'KLAY')
2
'0.000000000000000001'
Copied!

unitMap

1
caver.utils.unitMap
Copied!
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
1
> caver.utils.unitMap
2
{
3
peb: '1',
4
kpeb: '1000',
5
Mpeb: '1000000',
6
Gpeb: '1000000000',
7
Ston: '1000000000',
8
uKLAY: '1000000000000',
9
mKLAY: '1000000000000000',
10
KLAY: '1000000000000000000',
11
kKLAY: '1000000000000000000000',
12
MKLAY: '1000000000000000000000000',
13
GKLAY: '1000000000000000000000000000',
14
TKLAY: '1000000000000000000000000000000'
15
}
Copied!

klayUnit

1
caver.utils.klayUnit
Copied!
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
1
> caver.utils.klayUnit
2
{
3
peb: { unit: 'peb', pebFactor: 0 },
4
kpeb: { unit: 'kpeb', pebFactor: 3 },
5
Mpeb: { unit: 'Mpeb', pebFactor: 6 },
6
Gpeb: { unit: 'Gpeb', pebFactor: 9 },
7
ston: { unit: 'ston', pebFactor: 9 },
8
uKLAY: { unit: 'uKLAY', pebFactor: 12 },
9
mKLAY: { unit: 'mKLAY', pebFactor: 15 },
10
KLAY: { unit: 'KLAY', pebFactor: 18 },
11
kKLAY: { unit: 'kKLAY', pebFactor: 21 },
12
MKLAY: { unit: 'MKLAY', pebFactor: 24 },
13
GKLAY: { unit: 'GKLAY', pebFactor: 27 },
14
TKLAY: { unit: 'TKLAY', pebFactor: 30 }
15
}
Copied!

padLeft

1
caver.utils.padLeft(string, characterAmount [, sign])
2
caver.utils.leftPad(string, characterAmount [, sign]) // ALIAS
Copied!
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
1
> caver.utils.padLeft('0x3456ff', 20)
2
'0x000000000000003456ff'
3
4
> caver.utils.padLeft(0x3456ff, 20)
5
'0x000000000000003456ff'
6
7
> caver.utils.padLeft('Hello', 20, 'x')
8
'xxxxxxxxxxxxxxxHello'
Copied!

padRight

1
caver.utils.padRight(str, characterAmount [, sign])
2
caver.utils.rightPad(str, characterAmount [, sign]) // ALIAS
Copied!
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
1
> caver.utils.padRight('0x3456ff', 20)
2
'0x3456ff00000000000000'
3
4
> caver.utils.padRight(0x3456ff, 20)
5
'0x3456ff00000000000000'
6
7
> caver.utils.padRight('Hello', 20, 'x')
8
'Helloxxxxxxxxxxxxxxx'
Copied!

trimLeadingZero

1
caver.utils.trimLeadingZero(hexString)
Copied!
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
1
> caver.utils.trimLeadingZero('0x000011')
2
0x11
Copied!

makeEven

1
caver.utils.makeEven(hexString)
Copied!
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
1
> caver.utils.makeEven('0x011')
2
0x0011
Copied!

toTwosComplement

1
caver.utils.toTwosComplement(num)
Copied!
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
1
> caver.utils.toTwosComplement('-1')
2
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
3
4
> caver.utils.toTwosComplement(-1)
5
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
6
7
> caver.utils.toTwosComplement('0x1')
8
'0x0000000000000000000000000000000000000000000000000000000000000001'
9
10
> caver.utils.toTwosComplement(-15)
11
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1'
12
13
> caver.utils.toTwosComplement('-0x1')
14
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
Copied!

isContractDeployment

1
caver.utils.isContractDeployment(transactionObject)
Copied!
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
1
> caver.utils.isContractDeployment(caver.transaction.legacyTransaction.create({
2
to: '0x9957dfd92e4b70f91131c573293343bc5f21f215',
3
value: caver.utils.toPeb(1, 'KLAY'),
4
gas: 25000,
5
}))
6
false
7
8
> caver.utils.isContractDeployment(caver.transaction.legacyTransaction.create({
9
input: '0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029',
10
gas: 200000,
11
}))
12
true
13
14
> caver.utils.isContractDeployment(caver.transaction.smartContractDeploy.create({
15
from: '0x88e245dec96830f012f8fc1806bc623b3774560d',
16
input: '0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029',
17
gas: 100000,
18
}))
19
true
20
21
> caver.utils.isContractDeployment(caver.transaction.feeDelegatedSmartContractDeploy.create({
22
from: '0x88e245dec96830f012f8fc1806bc623b3774560d',
23
input: '0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029',
24
gas: 100000,
25
}))
26
true
27
28
> caver.utils.isContractDeployment(caver.transaction.feeDelegatedSmartContractDeployWithRatio.create({
29
from: '0x88e245dec96830f012f8fc1806bc623b3774560d',
30
input: '0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029',
31
gas: 100000,
32
feeRatio: 30,
33
}))
34
true
Copied!

xyPointFromPublicKey

1
caver.utils.xyPointFromPublicKey(publicKey)
Copied!
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.
Examples
1
> caver.utils.xyPointFromPublicKey('0xa5862ded55cd9c7e9ff246dbc264ca5d5c605308f59b74e581b4f089d4c8c88cb9f00df6a56493f6029af215d266c907660ea0f7a4111ea025ea9d9be418fa55')
2
[
3
'0xa5862ded55cd9c7e9ff246dbc264ca5d5c605308f59b74e581b4f089d4c8c88c',
4
'0xb9f00df6a56493f6029af215d266c907660ea0f7a4111ea025ea9d9be418fa55'
5
]
Copied!

isHexPrefixed

1
caver.utils.isHexPrefixed(input)
Copied!
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
1
> caver.utils.isHexPrefixed('0xa5b0cd8c87e77879d64cc064ee239ed6f71cacf9')
2
true
3
4
> caver.utils.isHexPrefixed('0x1')
5
true
6
7
> caver.utils.isHexPrefixed('0xqwer')
8
false
9
10
> caver.utils.isHexPrefixed('1')
11
false
Copied!

addHexPrefix

1
caver.utils.addHexPrefix(input)
Copied!
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
1
> caver.utils.addHexPrefix('a5b0cd8c87e77879d64cc064ee239ed6f71cacf9')
2
'0xa5b0cd8c87e77879d64cc064ee239ed6f71cacf9'
3
4
> caver.utils.addHexPrefix('0xa5b0cd8c87e77879d64cc064ee239ed6f71cacf9')
5
'0xa5b0cd8c87e77879d64cc064ee239ed6f71cacf9'
Copied!

stripHexPrefix

1
caver.utils.stripHexPrefix(input)
Copied!
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
1
> caver.utils.stripHexPrefix('a5b0cd8c87e77879d64cc064ee239ed6f71cacf9')
2
'a5b0cd8c87e77879d64cc064ee239ed6f71cacf9'
3
4
> caver.utils.stripHexPrefix('0xa5b0cd8c87e77879d64cc064ee239ed6f71cacf9')
5
'a5b0cd8c87e77879d64cc064ee239ed6f71cacf9'