A utility class for encoding and decoding data using the Bech32 encoding format.

This class provides methods for encoding binary data with a human-readable prefix (HRP) into a Bech32 string and for decoding Bech32 strings back into their HRP and binary data components.

Methods

  • Decodes a Bech32 string into its human-readable prefix (HRP) and binary data.

    Extracts the HRP and binary data from the Bech32 string. Additionally, it converts the binary data into a hexadecimal string for convenience.

    Parameters

    • encodedString: string

      The Bech32-encoded string to decode.

    Returns Bech32DecodeResult

    • An object containing:
      • hrp: The decoded human-readable prefix.
      • data: The decoded binary data as a Uint8Array.
      • hex: The hexadecimal string representation of the binary data.
    • If the decoding fails due to an invalid Bech32 string.
  • Encodes binary data into a Bech32 string with a specified human-readable prefix (HRP).

    The HRP provides context about the encoded data (e.g., address type). The binary data is converted into a Bech32-compliant string, which includes the HRP and checksum.

    Parameters

    • hrp: string

      The human-readable prefix for the Bech32 string (e.g., 'addr').

    • data: Uint8Array

      The binary data to encode.

    Returns string

    • The Bech32-encoded string.
    • If the encoding fails due to invalid inputs or insufficient memory.
  • Encodes a hexadecimal string into a Bech32 string with a specified HRP.

    Converts the hexadecimal string into binary data, then encodes it using the Bech32 format.

    Parameters

    • hrp: string

      The human-readable prefix for the Bech32 string.

    • data: string

      The hexadecimal string to encode.

    Returns string

    • The Bech32-encoded string.
    • If the input is not a valid hexadecimal string or the encoding fails.