Skip to content

1inch/ts-byte-utils-lib

Repository files navigation

@1inch/byte-utils

Contains helpers to work with bytes

Installation

npm install @1inch/byte-utils

Docs

🧰 Functions

⚙️ isHexString

Check that string starts with 0x and has only valid hex symbols

Function Type
isHexString (val: string) => boolean

⚙️ isHexBytes

Check that string is valid hex with 0x prefix and length is even

Function Type
isHexBytes (val: string) => boolean

🔧 Constants

⚙️ UINT_8_MAX

Constant Type
UINT_8_MAX bigint

⚙️ UINT_24_MAX

Constant Type
UINT_24_MAX bigint

⚙️ UINT_32_MAX

Constant Type
UINT_32_MAX bigint

⚙️ UINT_40_MAX

Constant Type
UINT_40_MAX bigint

⚙️ UINT_48_MAX

Constant Type
UINT_48_MAX bigint

⚙️ UINT_80_MAX

Constant Type
UINT_80_MAX bigint

⚙️ UINT_160_MAX

Constant Type
UINT_160_MAX bigint

⚙️ UINT_128_MAX

Constant Type
UINT_128_MAX bigint

⚙️ UINT_256_MAX

Constant Type
UINT_256_MAX bigint

🏭 BitMask

Class to define bit mask: new BitMask(16, 32) is for bits from [16, 32) => 0xffff0000

Methods

⚙️ toString

Method Type
toString () => string

⚙️ toBigInt

Method Type
toBigInt () => bigint

🏭 BN

Class to work with bits in bignumber Immutable, all methods return new value

Methods

⚙️ fromNumber

Method Type
fromNumber (n: number) => BN

⚙️ fromHex

Method Type
fromHex (hex: string) => BN

⚙️ add

Add value

Method Type
add (other: BN) => BN

⚙️ sub

Subtract value

Method Type
sub (other: BN) => BN

⚙️ setBit

Method Type
setBit (n: bigint, value: 0 or 1) => BN

⚙️ getBit

Method Type
getBit (n: bigint) => 0 or 1

⚙️ shiftLeft

Method Type
shiftLeft (n: bigint) => BN

⚙️ shiftRight

Method Type
shiftRight (n: bigint) => BN

⚙️ and

Method Type
and (other: bigint or BN) => BN

⚙️ or

Method Type
or (other: bigint or BN) => BN

⚙️ xor

Method Type
xor (other: bigint or BN) => BN

⚙️ isZero

Method Type
isZero () => boolean

⚙️ isOne

Method Type
isOne () => boolean

⚙️ getMask

Return bits defined in mask as BN

Method Type
getMask (mask: BitMask) => BN

⚙️ setMask

Set bits defined in mask to value if value is bigger than mask then error will be thrown

Method Type
setMask (mask: BitMask, value: bigint or BN) => BN

⚙️ clearMask

Set bits defined in mask to 0s

Method Type
clearMask (mask: BitMask) => BN

⚙️ toHex

Return 0x prefixed string with hex representation of BN, padded with '0s' if padNum specified

Method Type
toHex (padNum?: number) => string

⚙️ toNumber

Convert BN to Number

Caution: value will be rounded for numbers > Number.MAX_SAFE_INTEGER

Method Type
toNumber () => number

🏭 BytesIter

Class to iterate through bytes string by parsing individual bytes

Methods

⚙️ BigInt

Method Type
BigInt (bytes: string) => BytesIter<bigint>

⚙️ String

Method Type
String (bytes: string) => BytesIter<string>

⚙️ rest

Returns all not consumed bytes

Method Type
rest () => T

⚙️ isEmpty

Method Type
isEmpty () => boolean

⚙️ nextByte

Method Type
nextByte (side?: Side) => T

⚙️ nextBytes

Method Type
nextBytes (n: number, side?: Side) => T

⚙️ nextAddress

Method Type
nextAddress (side?: Side) => T

⚙️ nextUint8

Method Type
nextUint8 (side?: Side) => T

⚙️ nextUint16

Method Type
nextUint16 (side?: Side) => T

⚙️ nextUint24

Method Type
nextUint24 (side?: Side) => T

⚙️ nextUint32

Method Type
nextUint32 (side?: Side) => T

⚙️ nextUint128

Method Type
nextUint128 (side?: Side) => T

⚙️ nextUint160

Method Type
nextUint160 (side?: Side) => T

⚙️ nextUint256

Method Type
nextUint256 (side?: Side) => T

🏭 BytesBuilder

Helper class to build an arbitrary bytes sequence

Methods

⚙️ addAddress

Method Type
addAddress (address: string or bigint or BN) => this

⚙️ addBytes

Method Type
addBytes (bytes: string) => this

⚙️ addByte

Method Type
addByte (byte: string or bigint or BN) => this

⚙️ addUint8

Method Type
addUint8 (val: string or bigint or BN) => this

⚙️ addUint16

Method Type
addUint16 (val: string or bigint or BN) => this

⚙️ addUint24

Method Type
addUint24 (val: string or bigint or BN) => this

⚙️ addUint32

Method Type
addUint32 (val: string or bigint or BN) => this

⚙️ addUint64

Method Type
addUint64 (val: string or bigint or BN) => this

⚙️ addUint128

Method Type
addUint128 (val: string or bigint or BN) => this

⚙️ addUint160

Method Type
addUint160 (val: string or bigint or BN) => this

⚙️ addUint256

Method Type
addUint256 (val: string or bigint or BN) => this

⚙️ asBigInt

Returns bytes as single bigint value

Method Type
asBigInt () => bigint

⚙️ asHex

Returns hex string

Method Type
asHex (prefixed?: boolean) => string

Parameters:

  • prefixed: should be string prefixed with 0x or not, true by default