caver.utils (~v1.4.1)

caver-js utility APIs.

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

HEX 문자열의 바이트 크기, 가령, 32는 접두어 "0x"와 64개의 문자로 구성된 32바이트의 HEX 문자열이 결과가 될 것입니다.

Return Value

Type
Description

String

The generated random HEX string.

Example

> caver.utils.randomHex(32);
"0xa5b9d60f32436310afebcfda832817a68921beb782fabf7915cc0460b443116a"

> caver.utils.randomHex(4);
"0x6892ffc6"

> caver.utils.randomHex(2);
"0x99d6"

> caver.utils.randomHex(1);
"0x9a"

> caver.utils.randomHex(0);
"0x"

_

많은 편리한 자바스크립트 함수를 위한 underscore 라이브러리.

See the underscore API reference for details.

Example

BN

자바스크립트에서 큰 수로 계산하기 위한 BN.js 라이브러리. 자세한 내용은 BN.js documentation을 참조하세요.

Parameters

Name
Type
Description

mixed

String | Number

BN 객체로 변환할 숫자, 숫자 문자열 또는 HEX 문자열입니다.

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

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

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

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

Example

sha3

Calculates the sha3 of the input.

참고: 솔리디티의 sha3를 모방하려면 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 {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

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

true if a given string is a HEX string.

Example

isHexStrict

Checks if a given string is a HEX string. caver.utils.isHex와의 차이점은 HEX 앞에 0x이 붙는다는 것입니다.

Parameters

Name
Type
Description

hex

String | HEX

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. 주소에 대문자와 소문자가 있으면 체크섬(checksum)도 검사합니다.

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. 숫자 문자열은 숫자로 해석됩니다. Text strings will be interpreted as UTF-8 strings.

Parameters

Name
Type
Description

mixed

String | Number | BN | BigNumber

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.

참고: BN.js 클래스의 경우 caver.utils.BN를 사용합니다.

Parameters

Name
Type
Description

number

String | Number | HEX

큰 숫자로 변환할 숫자.

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.

참고: 큰 숫자에는 유용하지 않으며, 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

문자열 또는 숫자로 된 숫자.

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

ASCII 문자열로 변환할 HEX 문자열.

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.

참고: "peb"는 가장 작은 KLAY 단위이며, 항상 peb로 계산해야 하며 표시할 용도로만 변환해야 합니다.

Parameters

Name
Type
Description

number

String | Number | BN

값.

unit

String

(선택 사항, 기본값은 "KLAY") 변환하려고 하는 KLAY. Possible units are: - 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

숫자 또는 문자열이 주어지면 숫자 문자열을 반환하고, 그렇지 않으면 BN.js 인스턴스를 반환합니다.

Examples

fromPeb

참고: "peb"는 가장 작은 KLAY 단위이며, 항상 peb로 계산해야 하며 표시할 용도로만 변환해야 합니다.

Parameters

Name
Type
Description

number

String | Number | BN

peb 단위의 값.

unit

String

(선택 사항, 기본값은 "KLAY") 변환하고자 하는 KLAY. Possible units are: - 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 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: - peb: '1' - kpeb: '1000' - Mpeb: '1000000' - Gpeb: '1000000000' - Ston: '1000000000' - uKLAY: '1000000000000' - mKLAY: '1000000000000000' - KLAY: '1000000000000000000' - kKLAY: '1000000000000000000000' - MKLAY: '1000000000000000000000000' - GKLAY: '1000000000000000000000000000'

Examples

padLeft

문자열 왼쪽에 패딩(padding)을 추가합니다. 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

(선택 사항) 사용할 문자 부호, 기본값은 "0".

Return Value

Type
Description

String

The padded string.

Examples

padRight

문자열 오른쪽에 패딩을 추가합니다. HEX 문자열에 패딩을 추가하는 데 유용합니다.

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

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

변환할 숫자.

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.

참고 caver.klay.isContractDeployment는 v1.0.1-rc.8에서 지원됩니다. To use this feature, please install v1.0.1-rc.8 or higher.

Parameters

Name
Type
Description

transactionObject

Object

트랜잭션이 스마트 컨트랙트 배포용인지 사용자가 확인하고자 하는 트랜잭션 객체.

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. 키 암호화에 대한 자세한 내용은 타원 곡선 암호화를 참조하세요.

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

입력이 0x 접두사가 있는 16진수 문자열이면 true, 그렇지 않으면 false를 반환합니다.

참고 caver.klay.isHexPrefixed는 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

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.

참고 caver.klay.addHexPrefix는 v1.0.1에서 지원됩니다. To use this feature, please install v1.0.1 or higher.

Parameters

Name
Type
Description

input

String

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

접두사 0x를 제거할 문자열.

Return Value

Type
Description

String

A string stripped of 0x is returned.

Examples

toBuffer

This function converts the input to a Buffer. toBuffer을 사용하여 객체를 버퍼로 변환하려면, 객체는 반드시 toArray 함수를 구현해야 합니다. 문자열 유형 입력의 경우, 이 함수는 0x 접두사가 있는 16진수 문자열에서만 작동합니다.

참고 caver.klay.toBuffer는 v1.1.0에서 지원됩니다. 이 기능을 사용하려면 v1.1.0 또는 그 이상을 설치하세요.

Parameters

Name
Type
Description

input

Buffer | String | Number | Array | BN | Object

버퍼로 변환할 값.

Return Value

Type
Description

Buffer

The value converted to Buffer type is returned.

Examples

numberToBuffer

This function converts a number to a Buffer. 입력이 숫자인 경우 caver.utils.toBuffer는 이 함수와 동일하게 동작합니다.

참고 caver.klay.numberToBuffer는 v1.1.0에서 지원됩니다. To use this feature, please install v1.1.0 or higher.

Parameters

Name
Type
Description

input

String | Number | BN

버퍼로 변환할 숫자.

Return Value

Type
Description

Buffer

The value converted to Buffer type is returned.

Examples

isTxHash

입력이 트랜잭션 해시 형식이면 true, 그렇지 않으면 false를 반환합니다. This function only looks at the input and determines if it is in the format of a transaction hash. 참고 caver.klay.isTxHash는 v1.2.0-rc.1에서 지원됩니다. 이 기능을 사용하려면 v1.2.0-rc.1 또는 그 이상을 설치하세요. 매개변수

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는 입력값이 트랜잭션 해시 형식임을 의미합니다.

Examples

isTxHashStrict

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. caver.utils.isTxHash와의 차이점은 HEX 앞에 0x이 붙는다는 것입니다. 참고 caver.klay.isTxHashStrict는 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

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

Examples

isValidPrivateKey

privateKey가 유효하면 true, 그렇지 않으면 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.

참고 caver.utils.isValidPublicKey는 caver-js v1.2.0부터 지원됩니다.

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

role이 유효하면 true, 그렇지 않으면 false를 반환합니다. AccountKeyRoleBased에서 사용되는 현재 정의된 role은 transactionKey,updateKeyfeePayerKey입니다.

참고 caver.utils.isValidRole는 caver-js v1.2.0부터 지원됩니다.

Parameters

Name
Type
Description

role

String

A role string to validate.

Return Value

Type
Description

Boolean

true means the role is valid.

Examples

isEmptySig

sig가 빈 서명 형식인 경우 ([[ '0x01', '0x', '0x']] 또는 [ '0x01', '0x', '0x']) true, 그렇지 않은 경우 false를 반환합니다.

caver-js에서 트랜잭션을 RLP 인코딩할 때 signatures 또는 feePayerSignatures가 비어있는 경우, 해당 프로퍼티에는 빈 서명을 나타내는 [[ '0x01', '0x', '0x']] 값이 반환됩니다. 이 함수는 주어진 서명이 [[ '0x01', '0x', '0x']]인지 ('legacy' 트랜잭션인 경우는 [ '0x01', '0x', '0x']) 확인하고자 할 때 사용합니다.

참고 caver.utils.isEmptySig는 caver-js v1.2.0부터 지원됩니다.

Parameters

Name
Type
Description

sig

Array

비어있는지 확인할 서명의 배열.

Return Value

Type
Description

Boolean

true는 sig가 [[ '0x01', '0x', '0x']] 또는 [ '0x01', '0x', '0x']임을 의미합니다.

Examples

isKlaytnWalletKey

키가 KlaytnWalletKey 포맷이면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

참고caver.utils.isKlaytnWalletKey는 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

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.

참고 caver.utils.bufferToHex는 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

Was this helpful?