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
- Object
- Bech32
Bech32 (BIP-173) and Bech32m (BIP-350) encoding, bit regrouping and the
SegWit address helpers built on them.
Nested types
class Bech32.Decoded | The result of decode(String). |
class Bech32.SegwitAddress | The result of decodeSegwitAddress(String, String). |
Fields
public static final int BECH32 = 1 | Checksum variant constant for original Bech32 (BIP-173). |
public static final int BECH32M = 2 | Checksum 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 = 1Checksum variant constant for original Bech32 (BIP-173).
BECH32M
public static final int BECH32M = 2Checksum 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
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
strString- 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
databyte[]- input values, each fitting in
fromBitsbits fromBitsint- input group size, 1 to 8
toBitsint- output group size, 1 to 8
padboolean- 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
hrpString- the network prefix such as
bcortb witnessVersionint- 0 to 16
programbyte[]- 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
hrpString- the expected network prefix, compared without regard to case
addrString- the address
Returns
the witness version and program
Throws
IllegalArgumentException- if the address is invalid or has another HRP