Contains helpers to work with bytes
npm install @1inch/byte-utils
Check that string starts with 0x and has only valid hex symbols
Function |
Type |
isHexString |
(val: string) => boolean |
Check that string is valid hex with 0x prefix and length is even
Function |
Type |
isHexBytes |
(val: string) => boolean |
Constant |
Type |
UINT_8_MAX |
bigint |
Constant |
Type |
UINT_24_MAX |
bigint |
Constant |
Type |
UINT_32_MAX |
bigint |
Constant |
Type |
UINT_40_MAX |
bigint |
Constant |
Type |
UINT_48_MAX |
bigint |
Constant |
Type |
UINT_80_MAX |
bigint |
Constant |
Type |
UINT_160_MAX |
bigint |
Constant |
Type |
UINT_128_MAX |
bigint |
Constant |
Type |
UINT_256_MAX |
bigint |
Class to define bit mask: new BitMask(16, 32) is for bits from [16, 32) => 0xffff0000
Method |
Type |
toString |
() => string |
Method |
Type |
toBigInt |
() => bigint |
Class to work with bits in bignumber
Immutable, all methods return new value
Method |
Type |
fromNumber |
(n: number) => BN |
Method |
Type |
fromHex |
(hex: string) => BN |
Add value
Method |
Type |
add |
(other: BN) => BN |
Subtract value
Method |
Type |
sub |
(other: BN) => BN |
Method |
Type |
setBit |
(n: bigint, value: 0 or 1) => BN |
Method |
Type |
getBit |
(n: bigint) => 0 or 1 |
Method |
Type |
shiftLeft |
(n: bigint) => BN |
Method |
Type |
shiftRight |
(n: bigint) => BN |
Method |
Type |
and |
(other: bigint or BN) => BN |
Method |
Type |
or |
(other: bigint or BN) => BN |
Method |
Type |
xor |
(other: bigint or BN) => BN |
Method |
Type |
isZero |
() => boolean |
Method |
Type |
isOne |
() => boolean |
Return bits defined in mask
as BN
Method |
Type |
getMask |
(mask: BitMask) => BN |
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 |
Set bits defined in mask
to 0s
Method |
Type |
clearMask |
(mask: BitMask) => BN |
Return 0x prefixed string with hex representation of BN, padded with '0s' if padNum
specified
Method |
Type |
toHex |
(padNum?: number) => string |
Convert BN to Number
Caution: value will be rounded for numbers > Number.MAX_SAFE_INTEGER
Method |
Type |
toNumber |
() => number |
Class to iterate through bytes string by parsing individual bytes
Method |
Type |
BigInt |
(bytes: string) => BytesIter<bigint> |
Method |
Type |
String |
(bytes: string) => BytesIter<string> |
Returns all not consumed bytes
Method |
Type |
isEmpty |
() => boolean |
Method |
Type |
nextByte |
(side?: Side) => T |
Method |
Type |
nextBytes |
(n: number, side?: Side) => T |
Method |
Type |
nextAddress |
(side?: Side) => T |
Method |
Type |
nextUint8 |
(side?: Side) => T |
Method |
Type |
nextUint16 |
(side?: Side) => T |
Method |
Type |
nextUint24 |
(side?: Side) => T |
Method |
Type |
nextUint32 |
(side?: Side) => T |
Method |
Type |
nextUint128 |
(side?: Side) => T |
Method |
Type |
nextUint160 |
(side?: Side) => T |
Method |
Type |
nextUint256 |
(side?: Side) => T |
Helper class to build an arbitrary bytes sequence
Method |
Type |
addAddress |
(address: string or bigint or BN) => this |
Method |
Type |
addBytes |
(bytes: string) => this |
Method |
Type |
addByte |
(byte: string or bigint or BN) => this |
Method |
Type |
addUint8 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint16 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint24 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint32 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint64 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint128 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint160 |
(val: string or bigint or BN) => this |
Method |
Type |
addUint256 |
(val: string or bigint or BN) => this |
Returns bytes as single bigint value
Method |
Type |
asBigInt |
() => bigint |
Returns hex string
Method |
Type |
asHex |
(prefixed?: boolean) => string |
Parameters:
prefixed
: should be string prefixed with 0x or not, true by default