Skip to content

Commit

Permalink
create an address matching the fed desc
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Dec 4, 2024
1 parent 59362c8 commit 624e3f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/descriptor/pegin/dynafed_pegin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::fmt;

use bitcoin::blockdata::script::{self, PushBytes};
use bitcoin::{self, PublicKey, ScriptBuf as BtcScript, Weight};
use bitcoin_miniscript::descriptor::DescriptorType;
use elements::secp256k1_zkp;

use crate::descriptor::checksum::{self, verify_checksum};
Expand Down Expand Up @@ -158,14 +159,21 @@ impl<Pk: MiniscriptKey> Pegin<Pk> {
where
Pk: ToPublicKey,
{
// TODO
Ok(bitcoin::Address::p2shwsh(
// Should the address type taken from the top level user desc?
&self
.bitcoin_witness_script(secp)
.expect("DO this cleanly after TR. Pay to taproot pegins unspecified till now"),
network,
))
match self.fed_desc.desc_type() {
DescriptorType::Wsh => Ok(bitcoin::Address::p2wsh(
&self
.bitcoin_witness_script(secp)
.expect("DO this cleanly after TR. Pay to taproot pegins unspecified till now"),
network,
)),
DescriptorType::ShWsh => Ok(bitcoin::Address::p2shwsh(
&self
.bitcoin_witness_script(secp)
.expect("DO this cleanly after TR. Pay to taproot pegins unspecified till now"),
network,
)),
_ => return Err(Error::UnsupportedAddressForPegin),
}
}

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

let witness_script_0 = pegin
let address_0 = pegin
.derived_descriptor(0, &secp)
.unwrap()
.bitcoin_witness_script(&secp)
.bitcoin_address(bitcoin::Network::Bitcoin, &secp)
.unwrap();
let address_0 = bitcoin::Address::p2wsh(&witness_script_0, bitcoin::Network::Bitcoin);
assert_eq!(
address_0.to_string(),
"bc1qqkq6czql4zqwsylgrfzttjrn5wjeqmwfq5yn80p39amxtnkng9lsn6c5qr"
);

let witness_script_1 = pegin
let address_1 = pegin
.derived_descriptor(1, &secp)
.unwrap()
.bitcoin_witness_script(&secp)
.bitcoin_address(bitcoin::Network::Bitcoin, &secp)
.unwrap();
let address_1 = bitcoin::Address::p2wsh(&witness_script_1, bitcoin::Network::Bitcoin);
assert_ne!(address_0, address_1);
assert_eq!(
address_1.to_string(),
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ pub enum Error {
/// derivation indexes of different lengths.
MultipathDescLenMismatch,

/// The federation descriptor has a type not supported
UnsupportedAddressForPegin,

/// Conversion error in descriptor
Conversion(descriptor::ConversionError),
}
Expand Down Expand Up @@ -540,6 +543,7 @@ impl fmt::Display for Error {
Error::TrNoExplicitScript => write!(f, "No script code for Tr descriptors"),
Error::MultipathDescLenMismatch => write!(f, "At least two BIP389 key expressions in the descriptor contain tuples of derivation indexes of different lengths"),
Error::Conversion(ref e) => e.fmt(f),
Error::UnsupportedAddressForPegin => write!(f, "Cannot create the address from the pegin descriptor, the federation descriptor is of an unsuppported type"),

}
}
Expand Down Expand Up @@ -579,6 +583,7 @@ impl error::Error for Error {
| BareDescriptorAddr
| TaprootSpendInfoUnavialable
| TrNoScriptCode
| UnsupportedAddressForPegin
| TrNoExplicitScript => None,
MultipathDescLenMismatch => None,
BtcError(e) => Some(e),
Expand Down

0 comments on commit 624e3f4

Please sign in to comment.