Skip to content

Latest commit

 

History

History
496 lines (418 loc) · 13.7 KB

format.md

File metadata and controls

496 lines (418 loc) · 13.7 KB

Functions

any(arg)any

Do nothing for the given value.

uInt(arg)number

format input to number

bigInt(arg)BigInt
bigIntFromBuffer(arg)BigInt
bigUInt(arg)BigInt
bigUIntHex(arg)string

When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0")

big(arg)Big
fixed64(arg)Number
epochNumber(arg)string
hex(arg)string

When encoding UNFORMATTED DATA (byte arrays, account addresses, hashes, bytecode arrays): encode as hex, prefix with "0x", two hex digits per byte.

hex40(arg)string
address(address, networkId, [verbose])string

Checks if a given string is a valid address.

hexAddress(address)string

Checks if a given string is a valid hex address. It will also check the checksum, if the address has upper and lowercase letters.

checksumAddress(arg)string

Will convert an upper or lowercase address to a checksum address.

blockHash(arg)string
transactionHash(arg)string
privateKey(arg)string
publicKey(arg)string
hexBuffer(arg)Buffer
bytes(arg)Buffer

It can only be in hex format If want to pass a string

boolean(arg)boolean
keccak256(arg)string

Compute the keccak256 cryptographic hash of a value, returned as a hex string.

any(arg) ⇒ any

Do nothing for the given value.

Kind: global function
Returns: any - arg

Param Type
arg any

Example

> format.any(1)
 1

uInt(arg) ⇒ number

format input to number

Kind: global function

Param Type
arg number | BigInt | string | boolean

Example

> format.uInt(-3.14)
 Error("not match uint")
> format.uInt(null)
 Error("not match number")
> format.uInt('0')
 0
> format.uInt(1)
 1
> format.uInt(BigInt(100))
 100
> format.uInt('0x10')
 16
> format.uInt('')
 0
> format.uInt(true)
 1
> format.uInt(false)
 0
> format.uInt(Number.MAX_SAFE_INTEGER + 1) // unsafe integer
 Error("not match uint")

bigInt(arg) ⇒ BigInt

Kind: global function

Param Type
arg number | string | BigInt

Example

> format.bigInt(-3.14)
 Error("Cannot convert -3.14 to a BigInt")
> format.bigInt('0.0')
 0n
> format.bigInt('-1')
 -1n
> format.bigInt(1)
 1n
> format.bigInt(BigInt(100))
 100n
> format.bigInt('0x10')
 16n
> format.bigInt(Number.MAX_SAFE_INTEGER + 1) // unsafe integer
 9007199254740992n

bigIntFromBuffer(arg) ⇒ BigInt

Kind: global function

Param Type
arg Buffer

bigUInt(arg) ⇒ BigInt

Kind: global function

Param Type
arg number | string | BigInt

Example

> format.bigUInt('0.0')
 0n
> format.bigUInt('-1')
 Error("not match bigUInt")

bigUIntHex(arg) ⇒ string

When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0")

Kind: global function
Returns: string - Hex string

Param Type
arg number | string | BigInt

Example

> format.bigUIntHex(100)
 "0x64"
> format.bigUIntHex('0x0a')
 "0xa"
> format.bigUIntHex(-1))
 Error("not match uintHex")

big(arg) ⇒ Big

Kind: global function
Returns: Big - Big instance

Param Type
arg number | string | BigInt

Example

> format.big('0b10').toString()
 '2'
> format.big('0O10').toString()
 '8'
> format.big('010').toString()
 '10'
> format.big('0x10').toString()
 '16'
> format.big(3.14).toString()
 '3.14'
> format.big('-03.140').toString()
 '-3.14'
> format.big(null)
 Error('Invalid number')

fixed64(arg) ⇒ Number

Kind: global function

Param Type
arg string | number | BigInt | Big

Example

> format.fixed64('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
 1
> format.fixed64('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
 0.5

epochNumber(arg) ⇒ string

Kind: global function

Param Type Description
arg number | string number or label, See EPOCH_NUMBER

Example

> format.epochNumber(10)
 "0xa"
> format.epochNumber(EPOCH_NUMBER.LATEST_STATE)
 "latest_state"
> format.epochNumber('latest_mined')
 "latest_mined"

hex(arg) ⇒ string

When encoding UNFORMATTED DATA (byte arrays, account addresses, hashes, bytecode arrays): encode as hex, prefix with "0x", two hex digits per byte.

Kind: global function
Returns: string - Hex string

Param Type
arg number | BigInt | string | Buffer | boolean | null

Example

> format.hex(null)
 '0x'
> format.hex(1)
 "0x01"
> format.hex(256)
 "0x0100"
> format.hex(true)
 "0x01"
> format.hex(Buffer.from([1,10,255]))
 "0x010aff"
> format.hex("0x0a")
 "0x0a"

hex40(arg) ⇒ string

Kind: global function
Returns: string - hex40 address

Param Type
arg string | Buffer

address(address, networkId, [verbose]) ⇒ string

Checks if a given string is a valid address.

Kind: global function
Returns: string - Hex string

Param Type Default Description
address string | Buffer
networkId number
[verbose] boolean false if you want a address with type info, pass true

Example

> format.address('0x0123456789012345678901234567890123456789', 1)
 "cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp"
> format.address('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 Error("not match address")

hexAddress(address) ⇒ string

Checks if a given string is a valid hex address. It will also check the checksum, if the address has upper and lowercase letters.

Kind: global function
Returns: string - Hex string

Param Type
address string | Buffer

Example

> format.hexAddress('0x0123456789012345678901234567890123456789')
 "0x0123456789012345678901234567890123456789"
> format.hexAddress('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 Error("not match address")
> format.hexAddress('cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp')
 0x0123456789012345678901234567890123456789

checksumAddress(arg) ⇒ string

Deprecated

Will convert an upper or lowercase address to a checksum address.

Kind: global function
Returns: string - Checksum address hex string

Param Type
arg string | Buffer

Example

> format.checksumAddress('0x1b716c51381e76900ebaa7999a488511a4e1fd0a')
 "0x1B716c51381e76900EBAA7999A488511A4E1fD0a"
> format.checksumAddress('0X1B716C51381E76900EBAA7999A488511A4E1FD0A')
 "0x1B716c51381e76900EBAA7999A488511A4E1fD0a"
> format.checksumAddress('0x1B716c51381e76900EBAA7999A488511A4E1fD0A')
 "0x1B716c51381e76900EBAA7999A488511A4E1fD0a"

blockHash(arg) ⇒ string

Kind: global function
Returns: string - Hex string

Param Type
arg string | Buffer

Example

> format.privateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
> format.privateKey('0x0123456789012345678901234567890123456789')
 Error("not match hex64")

transactionHash(arg) ⇒ string

Kind: global function
Returns: string - Hex string

Param Type
arg string | Buffer

Example

> format.privateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
> format.privateKey('0x0123456789012345678901234567890123456789')
 Error("not match hex64")

privateKey(arg) ⇒ string

Kind: global function
Returns: string - Hex string

Param Type
arg string | Buffer

Example

> format.privateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
> format.privateKey('0x0123456789012345678901234567890123456789')
 Error("not match hex64")

publicKey(arg) ⇒ string

Kind: global function
Returns: string - Hex string

Param Type
arg string | Buffer

Example

> format.publicKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
> format.publicKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
 Error("not match publicKey")

hexBuffer(arg) ⇒ Buffer

Kind: global function

Param Type
arg number | string | BigInt | Buffer | boolean | null

Example

> format.hexBuffer(Buffer.from([0, 1]))
 <Buffer 00 01>
> format.hexBuffer(null)
 <Buffer >
> format.hexBuffer(1024)
 <Buffer 04 00>
> format.hexBuffer('0x0a')
 <Buffer 0a>
> format.hexBuffer(true)
 <Buffer 01>
> format.hexBuffer(3.14)
 Error("not match hex")

bytes(arg) ⇒ Buffer

It can only be in hex format If want to pass a string

Kind: global function

Param Type
arg string | Buffer | array

Example

> format.bytes('0xabcd')
 <Buffer ab cd>
> format.bytes([0, 1])
 <Buffer 00 01>
> format.bytes(Buffer.from([0, 1]))
 <Buffer 00 01>

boolean(arg) ⇒ boolean

Kind: global function

Param Type
arg boolean

Example

> format.boolean(true)
 true
> format.boolean(false)
 false

keccak256(arg) ⇒ string

Compute the keccak256 cryptographic hash of a value, returned as a hex string.

Kind: global function

Param Type
arg string | Buffer

Example

> format.keccak256('Transfer(address,address,uint256)')
 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
> format.keccak256(Buffer.from([0x42]))
 "0x1f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a69111"
> format.keccak256(format.hexBuffer('0x42'))
 "0x1f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a69111"
> format.keccak256('0x42') // "0x42" as string and transfer to <Buffer 30 78 34 32> by ascii
 "0x3c1b2d38851281e9a7b59d10973b0c87c340ff1e76bde7d06bf6b9f28df2b8c0"