Merge rust-bitcoin/rust-bitcoin#4251: Enable getting the network kind from an address
f4f79f88eb
Enable getting the network kind from an address (Tobin C. Harding) Pull request description: Users may wish to ask of an address 'what kind of address is this?' We have the `NetworkKind` struct that abstracts over the answer but currently no API to ask the question. The address may have been parsed or constructed and weather the network has been checked already is immaterial. Hence we add the function for both `NetworkChecked` and `NetworkUnchecked` addresses. Fix: #4247 ACKs for top commit: apoelstra: ACK f4f79f88eb2c6c80c46c95c69fcc43b17d306be2; successfully ran local tests Kixunil: ACKf4f79f88eb
Tree-SHA512: 57bdf7a0f2ae8bf599b3830d10201af3f6312a802ab72c0d86e346af660cbc4f430954e46d6698032a062514ec3ee1ee7edc732beff79af99a84ce718a519afa
This commit is contained in:
commit
78538bc8a6
|
@ -273,6 +273,16 @@ impl From<Network> for KnownHrp {
|
|||
fn from(n: Network) -> Self { Self::from_network(n) }
|
||||
}
|
||||
|
||||
impl From<KnownHrp> for NetworkKind {
|
||||
fn from(hrp: KnownHrp) -> Self {
|
||||
match hrp {
|
||||
KnownHrp::Mainnet => NetworkKind::Main,
|
||||
KnownHrp::Testnets => NetworkKind::Test,
|
||||
KnownHrp::Regtest => NetworkKind::Test,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The data encoded by an `Address`.
|
||||
///
|
||||
/// This is the data used to encumber an output that pays to this address i.e., it is the address
|
||||
|
@ -450,6 +460,16 @@ impl<V: NetworkValidation> Address<V> {
|
|||
|
||||
/// Marks the network of this address as unchecked.
|
||||
pub fn into_unchecked(self) -> Address<NetworkUnchecked> { Address(self.0, PhantomData) }
|
||||
|
||||
/// Returns the [`NetworkKind`] of this address.
|
||||
pub fn network_kind(&self) -> NetworkKind {
|
||||
use AddressInner::*;
|
||||
match self.0 {
|
||||
P2pkh { hash: _, ref network } => *network,
|
||||
P2sh { hash: _, ref network } => *network,
|
||||
Segwit { program: _, ref hrp } => NetworkKind::from(*hrp),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Methods and functions that can be called only on `Address<NetworkChecked>`.
|
||||
|
|
Loading…
Reference in New Issue