Package org.bitcoinj.base
Class Bech32
java.lang.Object
org.bitcoinj.base.Bech32
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBinary data in 5-bits-per-byte format as used in Bech32 encoding/decoding.static classBech32 data in 5-bit byte format withBech32.Encodingand human-readable part (HRP) information.static enumEnumeration of known Bech32 encoding format types: Bech32 and Bech32m. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Bech32.Bech32DataDecode a Bech32 string.static byte[]decodeBytes(String bech32, String expectedHrp, Bech32.Encoding expectedEncoding) Decode a Bech32 string to a byte array.static Stringencode(Bech32.Bech32Data bech32) Encode a Bech32 string.static Stringencode(Bech32.Encoding encoding, String hrp, Bech32.Bech32Bytes values) Encode a Bech32 string.static StringencodeBytes(Bech32.Encoding encoding, String hrp, byte[] bytes) Encode a byte array to a Bech32 string
-
Constructor Details
-
Bech32
public Bech32()
-
-
Method Details
-
encodeBytes
Encode a byte array to a Bech32 string- Parameters:
encoding- Desired encoding Bech32 or Bech32mhrp- human-readable part to use for encodingbytes- 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 stringexpectedHrp- Expected value for the human-readable partexpectedEncoding- Expected encoding- Returns:
- Decoded value as byte array (8-bits per byte)
- Throws:
AddressFormatException- if unexpected hrp or encoding
-
encode
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
Encode a Bech32 string.- Parameters:
encoding- The requested encodinghrp- The requested human-readable partvalues- Binary data in 5-bit per byte format- Returns:
- A string containing the Bech32-encoded data
-
decode
Decode a Bech32 string.To get the fully-decoded data, call
Bech32.Bech32Bytes.decode5to8()on the returnedBech32Data.- 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
-