Class Bech32

java.lang.Object
org.bitcoinj.base.Bech32

public class Bech32 extends Object
Implementation of the Bech32 encoding. Used in the implementation of SegwitAddress and also provides an API for encoding/decoding arbitrary Bech32 data. To parse Bech32 Bitcoin addresses, use AddressParser. To encode arbitrary Bech32 data, see encodeBytes(Encoding, String, byte[]). To decode arbitrary Bech32 strings, see decodeBytes(String, String, Encoding) or decode(String).

Based on the original Coinomi implementation.

See Also:
  • Constructor Details

    • Bech32

      public Bech32()
  • Method Details

    • encodeBytes

      public static String encodeBytes(Bech32.Encoding encoding, String hrp, byte[] bytes)
      Encode a byte array to a Bech32 string
      Parameters:
      encoding - Desired encoding Bech32 or Bech32m
      hrp - human-readable part to use for encoding
      bytes - Arbitrary binary data (8-bits per byte)
      Returns:
      A Bech32 string
    • decodeBytes

      public static byte[] decodeBytes(String bech32, String expectedHrp, Bech32.Encoding expectedEncoding)
      Decode a Bech32 string to a byte array.
      Parameters:
      bech32 - A Bech32 format string
      expectedHrp - Expected value for the human-readable part
      expectedEncoding - Expected encoding
      Returns:
      Decoded value as byte array (8-bits per byte)
      Throws:
      AddressFormatException - if unexpected hrp or encoding
    • encode

      public static String encode(Bech32.Bech32Data bech32)
      Encode a Bech32 string.
      Parameters:
      bech32 - Contains 5-bits/byte data, desired encoding and human-readable part
      Returns:
      A string containing the Bech32-encoded data
    • encode

      public static String encode(Bech32.Encoding encoding, String hrp, Bech32.Bech32Bytes values)
      Encode a Bech32 string.
      Parameters:
      encoding - The requested encoding
      hrp - The requested human-readable part
      values - Binary data in 5-bit per byte format
      Returns:
      A string containing the Bech32-encoded data
    • decode

      public static Bech32.Bech32Data decode(String str) throws AddressFormatException
      Decode a Bech32 string.

      To get the fully-decoded data, call Bech32.Bech32Bytes.decode5to8() on the returned Bech32Data.

      Parameters:
      str - A string containing Bech32-encoded data
      Returns:
      An object with the detected encoding, hrp, and decoded data (in 5-bit per byte format)
      Throws:
      AddressFormatException - if the string is invalid