Package org.bitcoinj.core
Class SegwitAddress
- java.lang.Object
-
- org.bitcoinj.core.PrefixedChecksummedBytes
-
- org.bitcoinj.core.Address
-
- org.bitcoinj.core.SegwitAddress
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Comparable<Address>
public class SegwitAddress extends 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
NetworkParameters.getSegwitAddressHrp(). - 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(NetworkParameters, String),fromHash(NetworkParameters, byte[])orfromKey(NetworkParameters, ECKey)to construct a native segwit address.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intWITNESS_PROGRAM_LENGTH_PKHstatic intWITNESS_PROGRAM_LENGTH_SHstatic intWITNESS_PROGRAM_LENGTH_TRstatic intWITNESS_PROGRAM_MAX_LENGTHstatic intWITNESS_PROGRAM_MIN_LENGTH-
Fields inherited from class org.bitcoinj.core.PrefixedChecksummedBytes
bytes, params
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Address o)Comparison field order for addresses is:NetworkParameters.getId()Legacy vs.static SegwitAddressfromBech32(NetworkParameters params, java.lang.String bech32)Construct aSegwitAddressfrom its textual form.static SegwitAddressfromHash(NetworkParameters params, byte[] hash)Construct aSegwitAddressthat represents the given hash, which is either a pubkey hash or a script hash.static SegwitAddressfromKey(NetworkParameters params, ECKey key)Construct aSegwitAddressthat represents the public part of the givenECKey.static SegwitAddressfromProgram(NetworkParameters params, 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.byte[]getHash()Get either the public key hash or script hash that is encoded in the address.Script.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.java.lang.StringtoBech32()Returns the textual form of the address.java.lang.StringtoString()-
Methods inherited from class org.bitcoinj.core.Address
compareAddressPartial, fromKey, fromString
-
Methods inherited from class org.bitcoinj.core.PrefixedChecksummedBytes
clone, equals, getParameters, hashCode
-
-
-
-
Field Detail
-
WITNESS_PROGRAM_LENGTH_PKH
public static final int WITNESS_PROGRAM_LENGTH_PKH
- See Also:
- Constant Field Values
-
WITNESS_PROGRAM_LENGTH_SH
public static final int WITNESS_PROGRAM_LENGTH_SH
- See Also:
- Constant Field Values
-
WITNESS_PROGRAM_LENGTH_TR
public static final int WITNESS_PROGRAM_LENGTH_TR
- See Also:
- Constant Field Values
-
WITNESS_PROGRAM_MIN_LENGTH
public static final int WITNESS_PROGRAM_MIN_LENGTH
- See Also:
- Constant Field Values
-
WITNESS_PROGRAM_MAX_LENGTH
public static final int WITNESS_PROGRAM_MAX_LENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
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 class:AddressGet either the public key hash or script hash that is encoded in the address.
-
getOutputScriptType
public Script.ScriptType getOutputScriptType()
Get the type of output script that will be used for sending to the address. This is eitherScript.ScriptType.P2WPKHorScript.ScriptType.P2WSH.- Specified by:
getOutputScriptTypein classAddress- Returns:
- type of output script
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fromBech32
public static SegwitAddress fromBech32(@Nullable NetworkParameters params, java.lang.String bech32) throws AddressFormatException
Construct aSegwitAddressfrom 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
-
fromHash
public static SegwitAddress fromHash(NetworkParameters params, 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:
params- network this address is valid forhash- 20-byte pubkey hash or 32-byte script hash- Returns:
- constructed address
-
fromProgram
public static SegwitAddress fromProgram(NetworkParameters params, 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:
params- network this address is valid forwitnessVersion- version number between 0 and 16witnessProgram- version dependent witness program- Returns:
- constructed address
-
fromKey
public static SegwitAddress fromKey(NetworkParameters params, ECKey key)
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 forkey- only the public part is used- Returns:
- constructed address
-
toBech32
public java.lang.String toBech32()
Returns the textual form of the address.- Returns:
- textual form encoded in bech32
-
compareTo
public int compareTo(Address o)
Comparison field order for addresses is:NetworkParameters.getId()- Legacy vs. Segwit
- (Legacy only) Version byte
- remaining
bytes
Implementations may use
compareAddressPartialfor tests 1 and 2.
-
-