Skip to content

Commit 9754bae

Browse files
committed
Merge rust-bitcoin#2810: Add an AddressData type
ea61e9f Add an AddressData type (Tobin C. Harding) Pull request description: This is a backport of the first patch (excluding api file changes) of rust-bitcoin#2808. ---- 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. ACKs for top commit: apoelstra: ACK ea61e9f Tree-SHA512: 13ffd4dac6a6f58cd99f280301fed9188d70a411ada6eb3e3dff25570d66a047568a6035a5e02c2b8e9d59cf4f4deae531b521e99a6bdf3fcad325febc280e2f
2 parents 0b59160 + ea61e9f commit 9754bae

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

bitcoin/src/address/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,30 @@ impl From<Network> for KnownHrp {
235235
fn from(n: Network) -> Self { Self::from_network(n) }
236236
}
237237

238+
/// The data encoded by an `Address`.
239+
///
240+
/// This is the data used to encumber an output that pays to this address i.e., it is the address
241+
/// excluding the network information.
242+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
243+
#[non_exhaustive]
244+
pub enum AddressData {
245+
/// Data encoded by a P2PKH address.
246+
P2pkh {
247+
/// The pubkey hash used to encumber outputs to this address.
248+
pubkey_hash: PubkeyHash
249+
},
250+
/// Data encoded by a P2SH address.
251+
P2sh {
252+
/// The script hash used to encumber outputs to this address.
253+
script_hash: ScriptHash
254+
},
255+
/// Data encoded by a Segwit address.
256+
Segwit {
257+
/// The witness program used to encumber outputs to this address.
258+
witness_program: WitnessProgram
259+
},
260+
}
261+
238262
/// A Bitcoin address.
239263
///
240264
/// ### Parsing addresses
@@ -475,6 +499,17 @@ impl Address {
475499
}
476500
}
477501

502+
/// Gets the address data from this address.
503+
pub fn to_address_data(&self) -> AddressData {
504+
use AddressData::*;
505+
506+
match self.0 {
507+
AddressInner::P2pkh { hash, network: _ } => P2pkh { pubkey_hash: hash },
508+
AddressInner::P2sh { hash, network: _ } => P2sh { script_hash: hash },
509+
AddressInner::Segwit { program, hrp: _ } => Segwit { witness_program: program },
510+
}
511+
}
512+
478513
/// Gets the pubkey hash for this address if this is a P2PKH address.
479514
pub fn pubkey_hash(&self) -> Option<PubkeyHash> {
480515
use AddressInner::*;

0 commit comments

Comments
 (0)