Struct Address
pub struct Address {
pub payload: Payload,
pub network: Network,
}
Expand description
Fields§
§payload: Payload
The type of the address.
network: Network
The network on which this address is usable.
Implementations§
§impl Address
impl Address
pub fn p2pkh(pk: &PublicKey, network: Network) -> Address
pub fn p2pkh(pk: &PublicKey, network: Network) -> Address
Creates a pay to (compressed) public key hash address from a public key.
This is the preferred non-witness type address.
pub fn p2sh(script: &Script, network: Network) -> Result<Address, Error>
pub fn p2sh(script: &Script, network: Network) -> Result<Address, Error>
Creates a pay to script hash P2SH address from a script.
This address type was introduced with BIP16 and is the popular type to implement multi-sig these days.
pub fn p2wpkh(pk: &PublicKey, network: Network) -> Result<Address, Error>
pub fn p2wpkh(pk: &PublicKey, network: Network) -> Result<Address, Error>
Creates a witness pay to public key address from a public key.
This is the native segwit address type for an output redeemable with a single signature.
§Errors
Will only return an error if an uncompressed public key is provided.
pub fn p2shwpkh(pk: &PublicKey, network: Network) -> Result<Address, Error>
pub fn p2shwpkh(pk: &PublicKey, network: Network) -> Result<Address, Error>
Creates a pay to script address that embeds a witness pay to public key.
This is a segwit address type that looks familiar (as p2sh) to legacy clients.
§Errors
Will only return an Error if an uncompressed public key is provided.
pub fn p2wsh(script: &Script, network: Network) -> Address
pub fn p2wsh(script: &Script, network: Network) -> Address
Creates a witness pay to script hash address.
pub fn p2shwsh(script: &Script, network: Network) -> Address
pub fn p2shwsh(script: &Script, network: Network) -> Address
Creates a pay to script address that embeds a witness pay to script hash address.
This is a segwit address type that looks familiar (as p2sh) to legacy clients.
pub fn p2tr<C>(
secp: &Secp256k1<C>,
internal_key: XOnlyPublicKey,
merkle_root: Option<TapBranchHash>,
network: Network,
) -> Addresswhere
C: Verification,
pub fn p2tr<C>(
secp: &Secp256k1<C>,
internal_key: XOnlyPublicKey,
merkle_root: Option<TapBranchHash>,
network: Network,
) -> Addresswhere
C: Verification,
Creates a pay to taproot address from an untweaked key.
pub fn p2tr_tweaked(output_key: TweakedPublicKey, network: Network) -> Address
pub fn p2tr_tweaked(output_key: TweakedPublicKey, network: Network) -> Address
Creates a pay to taproot address from a pre-tweaked output key.
This method is not recommended for use, Address::p2tr()
should be used where possible.
pub fn address_type(&self) -> Option<AddressType>
pub fn address_type(&self) -> Option<AddressType>
Gets the address type of the address.
§Returns
None if unknown, non-standard or related to the future witness version.
pub fn is_standard(&self) -> bool
pub fn is_standard(&self) -> bool
Checks whether or not the address is following Bitcoin standardness rules.
SegWit addresses with unassigned witness versions or non-standard program sizes are considered non-standard.
pub fn from_script(script: &Script, network: Network) -> Result<Address, Error>
pub fn from_script(script: &Script, network: Network) -> Result<Address, Error>
Constructs an Address
from an output script (scriptPubkey
).
pub fn script_pubkey(&self) -> Script
pub fn script_pubkey(&self) -> Script
Generates a script pubkey spending to this address.
pub fn to_qr_uri(&self) -> String
pub fn to_qr_uri(&self) -> String
Creates a URI string bitcoin:address optimized to be encoded in QR codes.
If the address is bech32, both the schema and the address become uppercase. If the address is base58, the schema is lowercase and the address is left mixed case.
Quoting BIP 173 “inside QR codes uppercase SHOULD be used, as those permit the use of alphanumeric mode, which is 45% more compact than the normal byte mode.”
pub fn is_valid_for_network(&self, network: Network) -> bool
pub fn is_valid_for_network(&self, network: Network) -> bool
Parsed addresses do not always have one network. The problem is that legacy testnet, regtest and signet addresse use the same prefix instead of multiple different ones. When parsing, such addresses are always assumed to be testnet addresses (the same is true for bech32 signet addresses). So if one wants to check if an address belongs to a certain network a simple comparison is not enough anymore. Instead this function can be used.
use bitcoin::{Address, Network};
let address: Address = "2N83imGV3gPwBzKJQvWJ7cRUY2SpUyU6A5e".parse().unwrap();
assert!(address.is_valid_for_network(Network::Testnet));
assert!(address.is_valid_for_network(Network::Regtest));
assert!(address.is_valid_for_network(Network::Signet));
assert_eq!(address.is_valid_for_network(Network::Bitcoin), false);
let address: Address = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse().unwrap();
assert!(address.is_valid_for_network(Network::Bitcoin));
assert_eq!(address.is_valid_for_network(Network::Testnet), false);
Returns true if the given pubkey is directly related to the address payload.
This is determined by directly comparing the address payload with either the hash of the given public key or the segwit redeem hash generated from the given key. For taproot addresses, the supplied key is assumed to be tweaked
Returns true if the supplied xonly public key can be used to derive the address.
This will only work for Taproot addresses. The Public Key is assumed to have already been tweaked.
Trait Implementations§
§impl Ord for Address
impl Ord for Address
§impl PartialOrd for Address
impl PartialOrd for Address
impl Eq for Address
impl StructuralPartialEq for Address
Auto Trait Implementations§
impl Freeze for Address
impl RefUnwindSafe for Address
impl Send for Address
impl Sync for Address
impl Unpin for Address
impl UnwindSafe for Address
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request