Package org.bitcoinj.base
Class SegwitAddress
java.lang.Object
org.bitcoinj.base.SegwitAddress
- All Implemented Interfaces:
Comparable<Address>
,Address
Implementation of native segwit addresses. They are composed of two parts:
- A human-readable part (HRP) which is a string the specifies the network. See
SegwitAddress.SegwitHrp
. - A data part, containing the witness version (encoded as an OP_N operator) and program (encoded by re-arranging bits into groups of 5).
See BIP350 and BIP173 for details.
However, you don't need to care about the internals. Use fromBech32(String, Network)
,
fromHash(org.bitcoinj.base.Network, byte[])
or ECKey.toAddress(ScriptType, Network)
to construct a native segwit address.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Human-readable part (HRP) of Segwit addresses for standard Bitcoin networks. -
Field Summary
Modifier and TypeFieldDescriptionprotected final Network
static final int
static final int
static final int
static final int
static final int
protected final byte[]
protected final short
Fields inherited from interface org.bitcoinj.base.Address
PARTIAL_ADDRESS_COMPARATOR
-
Method Summary
Modifier and TypeMethodDescriptionint
Comparison field order for addresses is:Network.id()
Legacy vs.boolean
static SegwitAddress
fromBech32
(String bech32, Network network) Construct aSegwitAddress
from its textual form.static SegwitAddress
fromBech32
(NetworkParameters params, String bech32) Deprecated.static SegwitAddress
Construct aSegwitAddress
that represents the given hash, which is either a pubkey hash or a script hash.static SegwitAddress
fromHash
(NetworkParameters params, byte[] hash) Deprecated.static SegwitAddress
fromKey
(NetworkParameters params, ECKey key) Deprecated.static SegwitAddress
fromProgram
(Network network, int witnessVersion, byte[] witnessProgram) Construct aSegwitAddress
that represents the given program, which is either a pubkey, a pubkey hash or a script hash – depending on the script version.static SegwitAddress
fromProgram
(NetworkParameters params, int witnessVersion, byte[] witnessProgram) Deprecated.byte[]
getHash()
Get either the public key hash or script hash that is encoded in the address.Get the type of output script that will be used for sending to the address.byte[]
Returns the witness program in decoded form.int
Returns the witness version in decoded form.int
hashCode()
network()
Get the network this address works on.toBech32()
Returns the textual form of the address.toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.bitcoinj.base.Address
getParameters
-
Field Details
-
WITNESS_PROGRAM_LENGTH_PKH
public static final int WITNESS_PROGRAM_LENGTH_PKH- See Also:
-
WITNESS_PROGRAM_LENGTH_SH
public static final int WITNESS_PROGRAM_LENGTH_SH- See Also:
-
WITNESS_PROGRAM_LENGTH_TR
public static final int WITNESS_PROGRAM_LENGTH_TR- See Also:
-
WITNESS_PROGRAM_MIN_LENGTH
public static final int WITNESS_PROGRAM_MIN_LENGTH- See Also:
-
WITNESS_PROGRAM_MAX_LENGTH
public static final int WITNESS_PROGRAM_MAX_LENGTH- See Also:
-
network
-
witnessVersion
protected final short witnessVersion -
witnessProgram
protected final byte[] witnessProgram
-
-
Method Details
-
getWitnessVersion
public int getWitnessVersion()Returns the witness version in decoded form. Only versions 0 and 1 are in use right now.- Returns:
- witness version, between 0 and 16
-
getWitnessProgram
public byte[] getWitnessProgram()Returns the witness program in decoded form.- Returns:
- witness program
-
getHash
public byte[] getHash()Description copied from interface:Address
Get either the public key hash or script hash that is encoded in the address. -
getOutputScriptType
Get the type of output script that will be used for sending to the address. This is eitherScriptType.P2WPKH
orScriptType.P2WSH
.- Specified by:
getOutputScriptType
in interfaceAddress
- Returns:
- type of output script
-
toString
-
fromBech32
@Deprecated public static SegwitAddress fromBech32(@Nullable NetworkParameters params, String bech32) throws AddressFormatException Deprecated.UseAddressParser
Construct aSegwitAddress
from its textual form.- Parameters:
params
- expected network this address is valid for, or null if the network should be derived from the bech32bech32
- bech32-encoded textual form of the address- Returns:
- constructed address
- Throws:
AddressFormatException
- if something about the given bech32 address isn't right
-
fromBech32
public static SegwitAddress fromBech32(String bech32, @Nonnull Network network) throws AddressFormatException Construct aSegwitAddress
from its textual form.- Parameters:
bech32
- bech32-encoded textual form of the addressnetwork
- expected network this address is valid for- Returns:
- constructed address
- Throws:
AddressFormatException
- if something about the given bech32 address isn't right
-
fromHash
Deprecated.Construct aSegwitAddress
that represents the given hash, which is either a pubkey hash or a script hash. The resulting address will be either a P2WPKH or a P2WSH type of address.- Parameters:
params
- network this address is valid forhash
- 20-byte pubkey hash or 32-byte script hash- Returns:
- constructed address
-
fromHash
Construct aSegwitAddress
that represents the given hash, which is either a pubkey hash or a script hash. The resulting address will be either a P2WPKH or a P2WSH type of address.- Parameters:
network
- network this address is valid forhash
- 20-byte pubkey hash or 32-byte script hash- Returns:
- constructed address
-
fromProgram
@Deprecated public static SegwitAddress fromProgram(NetworkParameters params, int witnessVersion, byte[] witnessProgram) Deprecated.Construct aSegwitAddress
that represents the given program, which is either a pubkey, a pubkey hash or a script hash – depending on the script version. The resulting address will be either a P2WPKH, a P2WSH or a P2TR type of address.- Parameters:
params
- network this address is valid forwitnessVersion
- version number between 0 and 16witnessProgram
- version dependent witness program- Returns:
- constructed address
-
fromProgram
Construct aSegwitAddress
that represents the given program, which is either a pubkey, a pubkey hash or a script hash – depending on the script version. The resulting address will be either a P2WPKH, a P2WSH or a P2TR type of address.- Parameters:
network
- network this address is valid forwitnessVersion
- version number between 0 and 16witnessProgram
- version dependent witness program- Returns:
- constructed address
-
fromKey
Deprecated.Construct aSegwitAddress
that represents the public part of the givenECKey
. Note that an address is derived from a hash of the public key and is not the public key itself.- Parameters:
params
- network this address is valid forkey
- only the public part is used- Returns:
- constructed address
-
network
Get the network this address works on. Use ofBitcoinNetwork
is preferred to use ofNetworkParameters
when you need to know what network an address is for. -
hashCode
public int hashCode() -
equals
-
toBech32
Returns the textual form of the address.- Returns:
- textual form encoded in bech32
-
compareTo
Comparison field order for addresses is:Network.id()
- Legacy vs. Segwit
- (Legacy only) Version byte
- remaining
bytes
Implementations use
Address.PARTIAL_ADDRESS_COMPARATOR
for tests 1 and 2.- Specified by:
compareTo
in interfaceAddress
- Specified by:
compareTo
in interfaceComparable<Address>
- Parameters:
o
- otherAddress
object- Returns:
- comparison result
-
AddressParser