Class Bech32
java.lang.Object
com.codename1.crypto.encoding.Bech32
Bech32 (BIP-173) and Bech32m (BIP-350) encoding, bit regrouping and the
SegWit address helpers built on them.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe result ofdecode(String).static final classThe result ofdecodeSegwitAddress(String, String). -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]convertBits(byte[] data, int fromBits, int toBits, boolean pad) Regroups bits, for example 8-bit bytes to 5-bit values and back.static Bech32.DecodedDecodes and validates a Bech32 or Bech32m string.static Bech32.SegwitAddressdecodeSegwitAddress(String hrp, String addr) Decodes and validates a SegWit address.static StringEncodes an HRP and 5-bit data into a Bech32 or Bech32m string.static StringencodeSegwitAddress(String hrp, int witnessVersion, byte[] program) Encodes a SegWit address, using Bech32 for witness version 0 and Bech32m otherwise.
-
Field Details
-
BECH32
public static final int BECH32Checksum variant constant for original Bech32 (BIP-173).- See Also:
-
BECH32M
public static final int BECH32MChecksum variant constant for Bech32m (BIP-350).- See Also:
-
-
Method Details
-
encode
Encodes an HRP and 5-bit data into a Bech32 or Bech32m string.
Parameters
hrp: the human readable part, 1 to 83 printable ASCII characters (33 to 126); uppercase is foldeddata: values in the range 0 to 31variant:BECH32orBECH32M
Returns
the lowercase encoded string
Throws
IllegalArgumentException: for an invalid HRP, data value, variant or total length over 90
-
decode
Decodes and validates a Bech32 or Bech32m string.
Parameters
str: 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: input values, each fitting infromBitsbitsfromBits: input group size, 1 to 8toBits: output group size, 1 to 8pad: 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
Encodes a SegWit address, using Bech32 for witness version 0 and Bech32m otherwise.
Parameters
hrp: the network prefix such asbcortbwitnessVersion: 0 to 16program: 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
Decodes and validates a SegWit address.
Parameters
hrp: the expected network prefix, compared without regard to caseaddr: the address
Returns
the witness version and program
Throws
IllegalArgumentException: if the address is invalid or has another HRP
-