@aptos-labs/ts-sdk - v5.1.4
    Preparing search index...

    Class Hex

    NOTE: Do not use this class when working with account addresses; use AccountAddress instead. When accepting hex data as input to a function, prefer to accept HexInput and

    A helper class for working with hex data. Hex data, when represented as a string, generally looks like this, for example: 0xaabbcc, 45cd32, etc.

    then use the static helper methods of this class to convert it into the desired format. This enables the greatest flexibility for the developer.

    Example usage:

    getTransactionByHash(txnHash: HexInput): Promise<Transaction> {
    const txnHashString = Hex.fromHexInput(txnHash).toString();
    return await getTransactionByHashInner(txnHashString);
    }

    This call to Hex.fromHexInput().toString() converts the HexInput to a hex string with a leading 0x prefix, regardless of what the input format was.

    Other ways to chain the functions together:

    • Hex.fromHexString({ hexInput: "0x1f" }).toUint8Array()
    • new Hex([1, 3]).toStringWithoutPrefix()
    Index

    Implementation - Serialization

    • Create a new Hex instance from a Uint8Array.

      Parameters

      • data: Uint8Array

        The Uint8Array containing the data to initialize the Hex instance.

      Returns Hex

    • Determine if two Hex instances are equal by comparing their underlying byte data.

      Parameters

      • other: Hex

        The Hex instance to compare to.

      Returns boolean

      true if the Hex instances are equal, false if not.

    • Get the hex data as a string with the 0x prefix.

      Returns string

      Hex string with 0x prefix

    • Get the hex data as a string without the 0x prefix.

      Returns string

      Hex string without 0x prefix

    • Get the inner hex data as a Uint8Array. The inner data is already a Uint8Array, so no conversion takes place.

      Returns Uint8Array

      Hex data as Uint8Array

    • Converts an instance of HexInput, which can be a string or a Uint8Array, into a Hex instance. This function is useful for transforming hexadecimal representations into a structured Hex object for further manipulation.

      Parameters

      • hexInput: HexInput

        A HexInput which can be a string or Uint8Array.

      Returns Hex

      A Hex instance created from the provided hexInput.

    • Converts a hex string into a Hex instance, allowing for both prefixed and non-prefixed formats.

      Parameters

      • str: string

        A hex string, with or without the 0x prefix.

      Returns Hex

      Hex - The resulting Hex instance created from the provided string.

      ParsingError - If the hex string is too short, has an odd number of characters, or contains invalid hex characters.

    • Check if the provided string is a valid hexadecimal representation.

      Parameters

      • str: string

        A hex string representing byte data.

      Returns ParsingResult<HexInvalidReason>

      An object containing:

      • valid: A boolean indicating whether the string is valid.
      • invalidReason: The reason for invalidity if the string is not valid.
      • invalidReasonMessage: A message explaining why the string is invalid.

    Methods

    • Converts a HexInput (string or Uint8Array) to a hex string with '0x' prefix.

      Parameters

      • hexInput: HexInput

        The input to convert, either a hex string (with/without '0x' prefix) or Uint8Array

      Returns string

      A hex string with '0x' prefix (e.g., "0x1234")

      Hex.hexInputToString("1234")        // returns "0x1234"
      Hex.hexInputToString("0x1234") // returns "0x1234"
      Hex.hexInputToString(new Uint8Array([0x12, 0x34])) // returns "0x1234"
    • Converts a HexInput (string or Uint8Array) to a hex string without '0x' prefix.

      Parameters

      • hexInput: HexInput

        The input to convert, either a hex string (with/without '0x' prefix) or Uint8Array

      Returns string

      A hex string without '0x' prefix (e.g., "1234")

      Hex.hexInputToStringWithoutPrefix("1234")        // returns "1234"
      Hex.hexInputToStringWithoutPrefix("0x1234") // returns "1234"
      Hex.hexInputToStringWithoutPrefix(new Uint8Array([0x12, 0x34])) // returns "1234"
    • Converts an instance of HexInput, which can be a string or a Uint8Array, into a Uint8Array.

      Parameters

      • hexInput: HexInput

        A HexInput which can be a string or Uint8Array.

      Returns Uint8Array

      A Uint8Array created from the provided hexInput.