|
1 | 1 | use crate::{miniscript, Error, Policy};
|
2 | 2 | use bitcoin_hashes::{hash160, ripemd160, sha256, Hash};
|
3 | 3 | use elements::schnorr::{TapTweak, XOnlyPublicKey};
|
4 |
| -use elements::secp256k1_zkp; |
5 | 4 | use elements::taproot::{
|
6 | 5 | ControlBlock, LeafVersion, TapBranchHash, TapLeafHash, TaprootBuilder, TaprootMerkleBranch,
|
7 | 6 | TaprootSpendInfo,
|
8 | 7 | };
|
| 8 | +use elements::{bitcoin, secp256k1_zkp}; |
9 | 9 | use miniscript::descriptor::{ConversionError, DescriptorSecretKey, KeyMap};
|
10 | 10 | use miniscript::Error as MSError;
|
11 | 11 | use miniscript::{
|
@@ -395,6 +395,39 @@ impl Descriptor<DescriptorPublicKey> {
|
395 | 395 | }
|
396 | 396 | }
|
397 | 397 |
|
| 398 | +impl Descriptor<DefiniteDescriptorKey> { |
| 399 | + /// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or |
| 400 | + /// otherwise converting them. All [`secp256k1_zkp::XOnlyPublicKey`]s are converted to by adding a |
| 401 | + /// default(0x02) y-coordinate. |
| 402 | + /// |
| 403 | + /// # Errors |
| 404 | + /// |
| 405 | + /// This function will return an error if hardened derivation is attempted. |
| 406 | + pub fn derived_descriptor<C: secp256k1_zkp::Verification>( |
| 407 | + &self, |
| 408 | + secp: &secp256k1_zkp::Secp256k1<C>, |
| 409 | + ) -> Result<Descriptor<bitcoin::PublicKey>, ConversionError> { |
| 410 | + struct Derivator<'a, C: secp256k1_zkp::Verification>(&'a secp256k1_zkp::Secp256k1<C>); |
| 411 | + |
| 412 | + impl<'a, C: secp256k1_zkp::Verification> |
| 413 | + Translator<DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError> |
| 414 | + for Derivator<'a, C> |
| 415 | + { |
| 416 | + fn pk( |
| 417 | + &mut self, |
| 418 | + pk: &DefiniteDescriptorKey, |
| 419 | + ) -> Result<bitcoin::PublicKey, ConversionError> { |
| 420 | + pk.derive_public_key(self.0) |
| 421 | + } |
| 422 | + |
| 423 | + translate_hash_clone!(DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError); |
| 424 | + } |
| 425 | + |
| 426 | + let derived = self.translate_pk(&mut Derivator(secp))?; |
| 427 | + Ok(derived) |
| 428 | + } |
| 429 | +} |
| 430 | + |
398 | 431 | impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {
|
399 | 432 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
400 | 433 | write!(f, "sim({},{})", self.internal_key, self.policy)
|
|
0 commit comments