Package org.bitcoinj.base
Class SegwitAddress
- java.lang.Object
- 
- org.bitcoinj.base.SegwitAddress
 
- 
 public class SegwitAddress extends java.lang.Object implements 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[])orECKey.toAddress(ScriptType, Network)to construct a native segwit address.
- A human-readable part (HRP) which is a string the specifies the network. See
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classSegwitAddress.SegwitHrpHuman-readable part (HRP) of Segwit addresses for standard Bitcoin networks.
 - 
Field SummaryFields Modifier and Type Field Description protected Networknetworkstatic intWITNESS_PROGRAM_LENGTH_PKHstatic intWITNESS_PROGRAM_LENGTH_SHstatic intWITNESS_PROGRAM_LENGTH_TRstatic intWITNESS_PROGRAM_MAX_LENGTHstatic intWITNESS_PROGRAM_MIN_LENGTHprotected byte[]witnessProgramprotected shortwitnessVersion- 
Fields inherited from interface org.bitcoinj.base.AddressPARTIAL_ADDRESS_COMPARATOR
 
- 
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intcompareTo(Address o)Comparison field order for addresses is:Network.id()Legacy vs.booleanequals(java.lang.Object o)static SegwitAddressfromBech32(java.lang.String bech32, Network network)Construct aSegwitAddressfrom its textual form.static SegwitAddressfromBech32(NetworkParameters params, java.lang.String bech32)Deprecated.UseAddressParserstatic SegwitAddressfromHash(Network network, byte[] hash)Construct aSegwitAddressthat represents the given hash, which is either a pubkey hash or a script hash.static SegwitAddressfromHash(NetworkParameters params, byte[] hash)Deprecated.static SegwitAddressfromKey(NetworkParameters params, ECKey key)Deprecated.static SegwitAddressfromProgram(Network network, int witnessVersion, byte[] witnessProgram)Construct aSegwitAddressthat represents the given program, which is either a pubkey, a pubkey hash or a script hash – depending on the script version.static SegwitAddressfromProgram(NetworkParameters params, int witnessVersion, byte[] witnessProgram)Deprecated.byte[]getHash()Get either the public key hash or script hash that is encoded in the address.ScriptTypegetOutputScriptType()Get the type of output script that will be used for sending to the address.byte[]getWitnessProgram()Returns the witness program in decoded form.intgetWitnessVersion()Returns the witness version in decoded form.inthashCode()Networknetwork()Get the network this address works on.java.lang.StringtoBech32()Returns the textual form of the address.java.lang.StringtoString()- 
Methods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface org.bitcoinj.base.AddressgetParameters
 
- 
 
- 
- 
- 
Field Detail- 
WITNESS_PROGRAM_LENGTH_PKHpublic static final int WITNESS_PROGRAM_LENGTH_PKH - See Also:
- Constant Field Values
 
 - 
WITNESS_PROGRAM_LENGTH_SHpublic static final int WITNESS_PROGRAM_LENGTH_SH - See Also:
- Constant Field Values
 
 - 
WITNESS_PROGRAM_LENGTH_TRpublic static final int WITNESS_PROGRAM_LENGTH_TR - See Also:
- Constant Field Values
 
 - 
WITNESS_PROGRAM_MIN_LENGTHpublic static final int WITNESS_PROGRAM_MIN_LENGTH - See Also:
- Constant Field Values
 
 - 
WITNESS_PROGRAM_MAX_LENGTHpublic static final int WITNESS_PROGRAM_MAX_LENGTH - See Also:
- Constant Field Values
 
 - 
networkprotected final Network network 
 - 
witnessVersionprotected final short witnessVersion 
 - 
witnessProgramprotected final byte[] witnessProgram 
 
- 
 - 
Method Detail- 
getWitnessVersionpublic 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
 
 - 
getWitnessProgrampublic byte[] getWitnessProgram() Returns the witness program in decoded form.- Returns:
- witness program
 
 - 
getHashpublic byte[] getHash() Description copied from interface:AddressGet either the public key hash or script hash that is encoded in the address.
 - 
getOutputScriptTypepublic ScriptType getOutputScriptType() Get the type of output script that will be used for sending to the address. This is eitherScriptType.P2WPKHorScriptType.P2WSH.- Specified by:
- getOutputScriptTypein interface- Address
- Returns:
- type of output script
 
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 - 
fromBech32@Deprecated public static SegwitAddress fromBech32(@Nullable NetworkParameters params, java.lang.String bech32) throws AddressFormatException Deprecated.UseAddressParserConstruct aSegwitAddressfrom its textual form.- Parameters:
- params- expected network this address is valid for, or null if the network should be derived from the bech32
- bech32- bech32-encoded textual form of the address
- Returns:
- constructed address
- Throws:
- AddressFormatException- if something about the given bech32 address isn't right
 
 - 
fromBech32public static SegwitAddress fromBech32(java.lang.String bech32, @Nonnull Network network) throws AddressFormatException Construct aSegwitAddressfrom its textual form.- Parameters:
- bech32- bech32-encoded textual form of the address
- network- expected network this address is valid for
- Returns:
- constructed address
- Throws:
- AddressFormatException- if something about the given bech32 address isn't right
 
 - 
fromHash@Deprecated public static SegwitAddress fromHash(NetworkParameters params, byte[] hash) Deprecated.Construct aSegwitAddressthat 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 for
- hash- 20-byte pubkey hash or 32-byte script hash
- Returns:
- constructed address
 
 - 
fromHashpublic static SegwitAddress fromHash(Network network, byte[] hash) Construct aSegwitAddressthat 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 for
- hash- 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 aSegwitAddressthat 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 for
- witnessVersion- version number between 0 and 16
- witnessProgram- version dependent witness program
- Returns:
- constructed address
 
 - 
fromProgrampublic static SegwitAddress fromProgram(Network network, int witnessVersion, byte[] witnessProgram) Construct aSegwitAddressthat 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 for
- witnessVersion- version number between 0 and 16
- witnessProgram- version dependent witness program
- Returns:
- constructed address
 
 - 
fromKey@Deprecated public static SegwitAddress fromKey(NetworkParameters params, ECKey key) Deprecated.Construct aSegwitAddressthat 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 for
- key- only the public part is used
- Returns:
- constructed address
 
 - 
networkpublic Network network() Get the network this address works on. Use ofBitcoinNetworkis preferred to use ofNetworkParameterswhen you need to know what network an address is for.
 - 
hashCodepublic int hashCode() - Overrides:
- hashCodein class- java.lang.Object
 
 - 
equalspublic boolean equals(java.lang.Object o) - Overrides:
- equalsin class- java.lang.Object
 
 - 
toBech32public java.lang.String toBech32() Returns the textual form of the address.- Returns:
- textual form encoded in bech32
 
 - 
compareTopublic int compareTo(Address o) Comparison field order for addresses is:- Network.id()
- Legacy vs. Segwit
- (Legacy only) Version byte
- remaining bytes
 Implementations use Address.PARTIAL_ADDRESS_COMPARATORfor tests 1 and 2.
 
- 
 
-