Skip to content

Commit 624e3f4

Browse files
committed
create an address matching the fed desc
1 parent 59362c8 commit 624e3f4

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/descriptor/pegin/dynafed_pegin.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::fmt;
2525

2626
use bitcoin::blockdata::script::{self, PushBytes};
2727
use bitcoin::{self, PublicKey, ScriptBuf as BtcScript, Weight};
28+
use bitcoin_miniscript::descriptor::DescriptorType;
2829
use elements::secp256k1_zkp;
2930

3031
use crate::descriptor::checksum::{self, verify_checksum};
@@ -158,14 +159,21 @@ impl<Pk: MiniscriptKey> Pegin<Pk> {
158159
where
159160
Pk: ToPublicKey,
160161
{
161-
// TODO
162-
Ok(bitcoin::Address::p2shwsh(
163-
// Should the address type taken from the top level user desc?
164-
&self
165-
.bitcoin_witness_script(secp)
166-
.expect("DO this cleanly after TR. Pay to taproot pegins unspecified till now"),
167-
network,
168-
))
162+
match self.fed_desc.desc_type() {
163+
DescriptorType::Wsh => Ok(bitcoin::Address::p2wsh(
164+
&self
165+
.bitcoin_witness_script(secp)
166+
.expect("DO this cleanly after TR. Pay to taproot pegins unspecified till now"),
167+
network,
168+
)),
169+
DescriptorType::ShWsh => Ok(bitcoin::Address::p2shwsh(
170+
&self
171+
.bitcoin_witness_script(secp)
172+
.expect("DO this cleanly after TR. Pay to taproot pegins unspecified till now"),
173+
network,
174+
)),
175+
_ => return Err(Error::UnsupportedAddressForPegin),
176+
}
169177
}
170178

171179
/// Computes the bitcoin scriptpubkey of the descriptor.
@@ -387,23 +395,21 @@ mod tests {
387395
let pegin = Pegin::new(fed_peg_desc, elem_desc.descriptor);
388396
let secp = secp256k1::Secp256k1::new();
389397

390-
let witness_script_0 = pegin
398+
let address_0 = pegin
391399
.derived_descriptor(0, &secp)
392400
.unwrap()
393-
.bitcoin_witness_script(&secp)
401+
.bitcoin_address(bitcoin::Network::Bitcoin, &secp)
394402
.unwrap();
395-
let address_0 = bitcoin::Address::p2wsh(&witness_script_0, bitcoin::Network::Bitcoin);
396403
assert_eq!(
397404
address_0.to_string(),
398405
"bc1qqkq6czql4zqwsylgrfzttjrn5wjeqmwfq5yn80p39amxtnkng9lsn6c5qr"
399406
);
400407

401-
let witness_script_1 = pegin
408+
let address_1 = pegin
402409
.derived_descriptor(1, &secp)
403410
.unwrap()
404-
.bitcoin_witness_script(&secp)
411+
.bitcoin_address(bitcoin::Network::Bitcoin, &secp)
405412
.unwrap();
406-
let address_1 = bitcoin::Address::p2wsh(&witness_script_1, bitcoin::Network::Bitcoin);
407413
assert_ne!(address_0, address_1);
408414
assert_eq!(
409415
address_1.to_string(),

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ pub enum Error {
374374
/// derivation indexes of different lengths.
375375
MultipathDescLenMismatch,
376376

377+
/// The federation descriptor has a type not supported
378+
UnsupportedAddressForPegin,
379+
377380
/// Conversion error in descriptor
378381
Conversion(descriptor::ConversionError),
379382
}
@@ -540,6 +543,7 @@ impl fmt::Display for Error {
540543
Error::TrNoExplicitScript => write!(f, "No script code for Tr descriptors"),
541544
Error::MultipathDescLenMismatch => write!(f, "At least two BIP389 key expressions in the descriptor contain tuples of derivation indexes of different lengths"),
542545
Error::Conversion(ref e) => e.fmt(f),
546+
Error::UnsupportedAddressForPegin => write!(f, "Cannot create the address from the pegin descriptor, the federation descriptor is of an unsuppported type"),
543547

544548
}
545549
}
@@ -579,6 +583,7 @@ impl error::Error for Error {
579583
| BareDescriptorAddr
580584
| TaprootSpendInfoUnavialable
581585
| TrNoScriptCode
586+
| UnsupportedAddressForPegin
582587
| TrNoExplicitScript => None,
583588
MultipathDescLenMismatch => None,
584589
BtcError(e) => Some(e),

0 commit comments

Comments
 (0)