Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Bech32

  1. Object
  2. Bech32
Bech32 (BIP-173) and Bech32m (BIP-350) encoding, bit regrouping and the SegWit address helpers built on them.

Nested types

class Bech32.DecodedThe result of decode(String).
class Bech32.SegwitAddressThe result of decodeSegwitAddress(String, String).

Fields

public static final int BECH32 = 1Checksum variant constant for original Bech32 (BIP-173).
public static final int BECH32M = 2Checksum variant constant for Bech32m (BIP-350).

Methods

public static String encode(String hrp, byte[] data, int variant)Encodes an HRP and 5-bit data into a Bech32 or Bech32m string.
public static Bech32.Decoded decode(String str)Decodes and validates a Bech32 or Bech32m string.
public static byte[] convertBits(byte[] data, int fromBits, int toBits, boolean pad)Regroups bits, for example 8-bit bytes to 5-bit values and back.
public static String encodeSegwitAddress(String hrp, int witnessVersion, byte[] program)Encodes a SegWit address, using Bech32 for witness version 0 and Bech32m otherwise.
public static Bech32.SegwitAddress decodeSegwitAddress(String hrp, String addr)Decodes and validates a SegWit address.

Inherited methods

Field details

BECH32

public static final int BECH32 = 1
Checksum variant constant for original Bech32 (BIP-173).

BECH32M

public static final int BECH32M = 2
Checksum variant constant for Bech32m (BIP-350).

Method details

encode

public static String encode(String hrp, byte[] data, int variant)
Encodes an HRP and 5-bit data into a Bech32 or Bech32m string.

Parameters

hrp String
the human readable part, 1 to 83 printable ASCII characters (33 to 126); uppercase is folded
data byte[]
values in the range 0 to 31
variant int
BECH32 or BECH32M

Returns

the lowercase encoded string

Throws

IllegalArgumentException
for an invalid HRP, data value, variant or total length over 90

decode

public static Bech32.Decoded decode(String str)
Decodes and validates a Bech32 or Bech32m string.

Parameters

str String
the encoded text

Returns

the HRP, data and checksum variant

Throws

IllegalArgumentException
for any violation of the BIP-173/BIP-350 rules

convertBits

public static byte[] convertBits(byte[] data, int fromBits, int toBits, boolean pad)
Regroups bits, for example 8-bit bytes to 5-bit values and back.

Parameters

data byte[]
input values, each fitting in fromBits bits
fromBits int
input group size, 1 to 8
toBits int
output group size, 1 to 8
pad boolean
whether leftover bits are zero padded (encoding) or must be zero and shorter than a group (decoding)

Returns

the regrouped values

Throws

IllegalArgumentException
for an out-of-range value or invalid padding

encodeSegwitAddress

public static String encodeSegwitAddress(String hrp, int witnessVersion, byte[] program)
Encodes a SegWit address, using Bech32 for witness version 0 and Bech32m otherwise.

Parameters

hrp String
the network prefix such as bc or tb
witnessVersion int
0 to 16
program byte[]
the witness program, 2 to 40 bytes (20 or 32 for version 0)

Returns

the address

Throws

IllegalArgumentException
if the version or program length is invalid

decodeSegwitAddress

public static Bech32.SegwitAddress decodeSegwitAddress(String hrp, String addr)
Decodes and validates a SegWit address.

Parameters

hrp String
the expected network prefix, compared without regard to case
addr String
the address

Returns

the witness version and program

Throws

IllegalArgumentException
if the address is invalid or has another HRP