Expand description
A Bitcoin address.
Fields
payload: Payload
The type of the address.
network: Network
The network on which this address is usable.
Implementations
sourceimpl Address
impl Address
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub fn p2wsh(script: &Script, network: Network) -> Address
pub fn p2wsh(script: &Script, network: Network) -> Address
Creates a witness pay to script hash address.
sourcepub 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.
sourcepub fn p2tr<C: Verification>(
secp: &Secp256k1<C>,
internal_key: UntweakedPublicKey,
merkle_root: Option<TapBranchHash>,
network: Network
) -> Address
pub fn p2tr<C: Verification>(
secp: &Secp256k1<C>,
internal_key: UntweakedPublicKey,
merkle_root: Option<TapBranchHash>,
network: Network
) -> Address
Creates a pay to taproot address from an untweaked key.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub fn from_script(script: &Script, network: Network) -> Option<Address>
pub fn from_script(script: &Script, network: Network) -> Option<Address>
Constructs an Address
from an output script (scriptPubkey
).
sourcepub fn script_pubkey(&self) -> Script
pub fn script_pubkey(&self) -> Script
Generates a script pubkey spending to this address.
sourcepub 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.”
sourcepub 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
sourceimpl<'de> Deserialize<'de> for Address
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for Address
serde
only.sourcefn deserialize<D>(deserializer: D) -> Result<Address, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Address, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Ord for Address
impl Ord for Address
sourceimpl PartialOrd<Address> for Address
impl PartialOrd<Address> for Address
sourcefn partial_cmp(&self, other: &Address) -> Option<Ordering>
fn partial_cmp(&self, other: &Address) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for Address
impl StructuralEq for Address
impl StructuralPartialEq for Address
Auto Trait Implementations
impl RefUnwindSafe for Address
impl Send for Address
impl Sync for Address
impl Unpin for Address
impl UnwindSafe for Address
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more