Package org.bitcoinj.base
Class Bech32
- java.lang.Object
-
- org.bitcoinj.base.Bech32
-
public class Bech32 extends java.lang.Object
Implementation of the Bech32 encoding. Used in the implementation ofSegwitAddress
and also provides an API for encoding/decoding arbitrary Bech32 data. To parse Bech32 Bitcoin addresses, useAddressParser
. To encode arbitrary Bech32 data, seeencodeBytes(Encoding, String, byte[])
. To decode arbitrary Bech32 strings, seedecodeBytes(String, String, Encoding)
ordecode(String)
.Based on the original Coinomi implementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Bech32.Bech32Bytes
Binary data in 5-bits-per-byte format as used in Bech32 encoding/decoding.static class
Bech32.Bech32Data
Bech32 data in 5-bit byte format withBech32.Encoding
and human-readable part (HRP) information.static class
Bech32.Encoding
Enumeration of known Bech32 encoding format types: Bech32 and Bech32m.
-
Constructor Summary
Constructors Constructor Description Bech32()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Bech32.Bech32Data
decode(java.lang.String str)
Decode a Bech32 string.static byte[]
decodeBytes(java.lang.String bech32, java.lang.String expectedHrp, Bech32.Encoding expectedEncoding)
Decode a Bech32 string to a byte array.static java.lang.String
encode(Bech32.Bech32Data bech32)
Encode a Bech32 string.static java.lang.String
encode(Bech32.Encoding encoding, java.lang.String hrp, Bech32.Bech32Bytes values)
Encode a Bech32 string.static java.lang.String
encodeBytes(Bech32.Encoding encoding, java.lang.String hrp, byte[] bytes)
Encode a byte array to a Bech32 string
-
-
-
Method Detail
-
encodeBytes
public static java.lang.String encodeBytes(Bech32.Encoding encoding, java.lang.String hrp, byte[] bytes)
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(java.lang.String bech32, java.lang.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
public static java.lang.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 java.lang.String encode(Bech32.Encoding encoding, java.lang.String hrp, Bech32.Bech32Bytes values)
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
public static Bech32.Bech32Data decode(java.lang.String str) throws AddressFormatException
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
-
-