Hethers
  • Documentation
  • Getting Started
  • Application Programming Interface
    • Providers
      • Provider
        • Accounts Methods
        • Logs Methods
        • Network Status Methods
        • Transactions Methods
        • Event Emitter Methods
        • Base Provider
        • HederaProvider
      • Types
    • Contract Interaction
      • Contract
      • ContractFactory
      • Example: ERC-20 Contract
    • Utilities
      • Accounts
      • Addresses
      • Application Binary Interface
        • AbiCoder
        • ABI Formats
        • Fragments
        • Interface
      • BigNumber
      • Byte Manipulation
      • Constants
      • Display Logic and Input
      • Encoding Utilities
      • FixedNumber
      • Hashing Algorithms
      • HD Wallet
      • Logging
      • Property Utilities
      • Signing Key
      • Strings
      • Transactions
      • Web Utilities
      • Wordlists
    • Signers
  • Contributing
  • Other Resources
Powered by GitBook
On this page
  • Types
  • Inspection
  • Converting between Arrays and Hexstrings
  • Array Manipulation
  • Hexstring Manipulation
  • Signature Conversion
  • Random Bytes
  1. Application Programming Interface
  2. Utilities

Byte Manipulation

PreviousBigNumberNextConstants

Last updated 3 years ago

While there are many high-level APIs for interacting with Hedera, such as and , a lot of the low level access requires byte manipulation operations.

Many of these operations are used internally, but can also be used to help normalize binary data representations from the output of various functions and methods.

The Bytes package is directly imported from the . The complete documentation can be found in the official .

Types

Bytes

A Bytes is any object which is an or with each value in the valid byte range (i.e. between 0 and 255 inclusive), or is an Object with a length property where each indexed property is in the valid byte range.

BytesLike

A BytesLike can be either a or a .

DataHexString

A DataHexstring is identical to a except that it has an even number of nibbles, and therefore is a valid representation of binary data as a string.

HexString

A Hexstring is a string which has a 0x prefix followed by any number of nibbles (i.e. case-insensitive hexadecimal characters, 0-9 and a-f).

Signature

  • r and s --- The x co-ordinate of r and the s value of the signature

  • v --- The parity of the y co-ordinate of r

  • _vs --- The of the s and v

  • recoveryParam --- The normalized (i.e. 0 or 1) value of v

SignatureLike

For example, if _vs is specified, s and v may be omitted. Likewise, if recoveryParam is provided, v may be omitted (as in these cases the missing values can be computed).

Inspection

hethers.utils.isBytes( object ) ⇒ boolean

hethers.utils.isBytesLike( object ) ⇒ boolean

hethers.utils.isHexString( object , [ length ] ) ⇒ boolean

Converting between Arrays and Hexstrings

hethers.utils.arrayify( DataHexStringOrArrayish [ , options ] ) ⇒ Uint8Array

Converts DataHexStringOrArrayish to a Uint8Array.

// Convert a hexstring to a Uint8Array
arrayify("0x1234")
// Uint8Array [ 18, 52 ]

// Convert an Array to a hexstring
hexlify([1, 2, 3, 4])
// '0x01020304'

// Convert an Object to a hexstring
hexlify({ length: 2, "0": 1, "1": 2 })
// '0x0102'

// Convert an Array to a hexstring
hexlify([ 1 ])
// '0x01'

// Convert a number to a stripped hex value
hexValue(1)
// '0x1'

// Convert an Array to a stripped hex value
hexValue([ 1, 2 ])
// '0x102'

Array Manipulation

hethers.utils.concat( arrayOfBytesLike ) ⇒ Uint8Array

hethers.utils.stripZeros( aBytesLike ) ⇒ Uint8Array

Returns a Uint8Array with all leading 0 bytes of aBtyesLike removed.

hethers.utils.zeroPad( aBytesLike, length ) ⇒ Uint8Array

Returns a Uint8Array of the data in aBytesLike with 0 bytes prepended to length bytes long.

If aBytesLike is already longer than length bytes long, an InvalidArgument error will be thrown.

Hexstring Manipulation

Returns the length (in bytes) of aBytesLike.

If aBytesLike is already longer than length bytes long, an InvalidArgument error will be thrown.

Signature Conversion

Return the raw-format of aSignaturelike, which is 65 bytes (130 nibbles) long, concatenating the r, s and (normalized) v of a Signature.

Random Bytes

hethers.utils.randomBytes( length ) ⇒ Uint8Array

Return a new Uint8Array of length random bytes.

hethers.utils.shuffled( array ) ⇒ Array<any>

utils.randomBytes(8)
// Uint8Array [ 238, 239, 73, 38, 25, 219, 132, 179 ]

const data = [ 1, 2, 3, 4, 5, 6, 7 ];

// Returns a new Array
utils.shuffled(data);
// [
//   4,
//   6,
//   5,
//   2,
//   1,
//   7,
//   3
// ]

// The Original is unscathed...
data
// [
//   1,
//   2,
//   3,
//   4,
//   5,
//   6,
//   7
// ]

Raw Signature (inherits )

A Raw Signature is a common Signature format where the r, s and v are concatenated into a 65 byte (130 nibble) .

A SignatureLike is similar to a , except redundant properties may be omitted or it may be a .

Returns true if and only if object is a valid .

Returns true if and only if object is a Bytes or .

Returns true if and only if object is a valid hex string. If length is specified and object is not a valid of length bytes, an InvalidArgument error is thrown.

hethers.utils.hexlify( hexstringOrArrayish ) ⇒ string<>

Converts hexstringOrArrayish to a .

hethers.utils.hexValue( aBigNumberish ) ⇒ string<>

Converts aBigNumberish to a , with no unnecessary leading zeros.

Concatenates all the in arrayOfBytesLike into a single Uint8Array.

hethers.utils.hexConcat( arrayOfBytesLike ) ⇒ string<>

Concatenates all the in arrayOfBytesLike into a single

hethers.utils.hexDataLength( aBytesLike ) ⇒ string<>

hethers.utils.hexDataSlice( aBytesLike, offset [ , endOffset ] ) ⇒ string<>

Returns a representation of a slice of aBytesLike, from offset (in bytes) to endOffset (in bytes). If endOffset is omitted, the length of aBytesLike is used.

hethers.utils.hexStripZeros( aBytesLike ) ⇒ string<>

Returns a representation of aBytesLike with all leading zeros removed.

hethers.utils.hexZeroPad( aBytesLike, length ) ⇒ string<>

Returns a representation of aBytesLike padded to length bytes.

hethers.utils.joinSignature( aSignatureLike ) ⇒ string<>

hethers.utils.splitSignature( aSignatureLikeOrBytesLike ) ⇒

Return the full expanded-format of aSignaturelike or a raw-format . Any missing properties will be computed.

Return a copy of array shuffled using .

Fisher-Yates Shuffle
Contracts
Providers
The Ethers Project
ethers docs
Array
TypedArray
compact representation
Bytes
DataHexString
HexString
DataHexString
DataHexString
Signature
Raw Signature
Bytes
DataHexString
DataHexString
DataHexString
DataHexString
HexString
HexString
BytesLike
DataHexString
BytesLike
DataHexString
DataHexString
DataHexString
DataHexString
HexString
HexString
DataHexString
DataHexString
RawSignature
Signature
DataHexString