Struct bitcoincash::util::address::Address
source · Expand description
Fields§
§payload: Payload
The type of the address.
network: Network
The network on which this address is usable.
Implementations§
source§impl 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.
Examples found in repository?
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
pub fn is_signed_by_address<C: secp256k1::Verification>(
&self,
secp_ctx: &secp256k1::Secp256k1<C>,
address: &Address,
msg_hash: sha256d::Hash
) -> Result<bool, MessageSignatureError> {
match address.address_type() {
Some(AddressType::P2pkh) => {
let pubkey = self.recover_pubkey(secp_ctx, msg_hash)?;
Ok(*address == Address::p2pkh(&pubkey, address.network))
}
Some(address_type) => Err(MessageSignatureError::UnsupportedAddressType(address_type)),
None => Ok(false),
}
}
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.
Examples found in repository?
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
pub fn is_standard(&self) -> bool {
self.address_type().is_some()
}
/// Constructs an [`Address`] from an output script (`scriptPubkey`).
pub fn from_script(script: &script::Script, network: Network) -> Result<Address, Error> {
Ok(Address {
payload: Payload::from_script(script)?,
network,
})
}
/// Generates a script pubkey spending to this address.
pub fn script_pubkey(&self) -> script::Script {
self.payload.script_pubkey()
}
/// 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 to_qr_uri(&self) -> String {
let schema = match self.payload {
Payload::WitnessProgram { .. } => "BITCOIN",
_ => "bitcoin",
};
format!("{}:{:#}", schema, self)
}
/// 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.
///
/// ```rust
/// use bitcoincash::{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::Scalenet));
///
/// 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);
/// ```
pub fn is_valid_for_network(&self, network: Network) -> bool {
let is_legacy = match self.address_type() {
Some(AddressType::P2pkh) | Some(AddressType::P2sh) => true,
_ => false
};
match (self.network, network) {
(a, b) if a == b => true,
(Network::Bitcoin, _) | (_, Network::Bitcoin) => false,
(Network::Regtest, _) | (_, Network::Regtest) if !is_legacy => false,
(Network::Testnet, _) | (Network::Testnet4, _) | (Network::Regtest, _) | (Network::Scalenet, _) | (Network::Chipnet, _) => true
}
}
More examples
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
pub fn is_signed_by_address<C: secp256k1::Verification>(
&self,
secp_ctx: &secp256k1::Secp256k1<C>,
address: &Address,
msg_hash: sha256d::Hash
) -> Result<bool, MessageSignatureError> {
match address.address_type() {
Some(AddressType::P2pkh) => {
let pubkey = self.recover_pubkey(secp_ctx, msg_hash)?;
Ok(*address == Address::p2pkh(&pubkey, address.network))
}
Some(address_type) => Err(MessageSignatureError::UnsupportedAddressType(address_type)),
None => Ok(false),
}
}
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) -> Result<Address, Error>
pub fn from_script(script: &Script, network: Network) -> Result<Address, Error>
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 bitcoincash::{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::Scalenet));
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§
source§impl<'de> Deserialize<'de> for Address
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for Address
serde
only.source§fn 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>,
source§impl Ord for Address
impl Ord for Address
source§impl PartialEq<Address> for Address
impl PartialEq<Address> for Address
source§impl PartialOrd<Address> for Address
impl PartialOrd<Address> for Address
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more