From 1b40550ce870850fd1aef906b155bf259ba50a17 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 27 May 2024 14:42:15 +1000 Subject: [PATCH] Add an AddressData type In the 0.32.0 release we removed the `address::Payload` struct because it was deemed an implementation detail. As a byproduct of doing so we made it impossible for users to match on an enum and get the address payload (or data). - Add a public `AddressData` enum that holds an address' encoded data. - Add a conversion function to `Address` that returns the data enum. This patch is additive and is expected to be backported and release as a `0.32` point release. --- bitcoin/src/address/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 89658144b..49f9d6736 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -235,6 +235,30 @@ impl From for KnownHrp { fn from(n: Network) -> Self { Self::from_network(n) } } +/// 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 +/// excluding the network information. +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[non_exhaustive] +pub enum AddressData { + /// Data encoded by a P2PKH address. + P2pkh { + /// The pubkey hash used to encumber outputs to this address. + pubkey_hash: PubkeyHash + }, + /// Data encoded by a P2SH address. + P2sh { + /// The script hash used to encumber outputs to this address. + script_hash: ScriptHash + }, + /// Data encoded by a Segwit address. + Segwit { + /// The witness program used to encumber outputs to this address. + witness_program: WitnessProgram + }, +} + /// A Bitcoin address. /// /// ### Parsing addresses @@ -470,6 +494,17 @@ impl Address { } } + /// Gets the address data from this address. + pub fn to_address_data(&self) -> AddressData { + use AddressData::*; + + match self.0 { + AddressInner::P2pkh { hash, network: _ } => P2pkh { pubkey_hash: hash }, + AddressInner::P2sh { hash, network: _ } => P2sh { script_hash: hash }, + AddressInner::Segwit { program, hrp: _ } => Segwit { witness_program: program }, + } + } + /// Gets the pubkey hash for this address if this is a P2PKH address. pub fn pubkey_hash(&self) -> Option { use AddressInner::*;