Skip to content

Backport of #818 to 12.3 #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: release-12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo-minimal.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ dependencies = [

[[package]]
name = "miniscript"
version = "12.3.2"
version = "12.3.3"
dependencies = [
"bech32",
"bitcoin",
Expand Down
2 changes: 1 addition & 1 deletion Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ dependencies = [

[[package]]
name = "miniscript"
version = "12.3.2"
version = "12.3.3"
dependencies = [
"bech32",
"bitcoin",
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniscript"
version = "12.3.2"
version = "12.3.3"
authors = ["Andrew Poelstra <[email protected]>, Sanket Kanjalkar <[email protected]>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-miniscript/"
Expand Down
16 changes: 14 additions & 2 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::error;

use bitcoin::bip32::{self, XKeyIdentifier};
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash, HashEngine};
use bitcoin::key::XOnlyPublicKey;
use bitcoin::key::{PublicKey, XOnlyPublicKey};
use bitcoin::secp256k1::{Secp256k1, Signing, Verification};

use crate::prelude::*;
Expand Down Expand Up @@ -473,6 +473,18 @@ impl FromStr for DescriptorPublicKey {
}
}

impl From<XOnlyPublicKey> for DescriptorPublicKey {
fn from(key: XOnlyPublicKey) -> Self {
DescriptorPublicKey::Single(SinglePub { origin: None, key: SinglePubKey::XOnly(key) })
}
}

impl From<PublicKey> for DescriptorPublicKey {
fn from(key: PublicKey) -> Self {
DescriptorPublicKey::Single(SinglePub { origin: None, key: SinglePubKey::FullKey(key) })
}
}

/// Descriptor key conversion error
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum ConversionError {
Expand Down Expand Up @@ -1036,7 +1048,7 @@ impl DefiniteDescriptorKey {
/// Construct an instance from a descriptor key and a derivation index
///
/// Returns `None` if the key contains a wildcard
fn new(key: DescriptorPublicKey) -> Option<Self> {
pub fn new(key: DescriptorPublicKey) -> Option<Self> {
if key.has_wildcard() {
None
} else {
Expand Down
47 changes: 46 additions & 1 deletion src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ mod tests {
use bitcoin::hashes::Hash;
use bitcoin::script::PushBytes;
use bitcoin::sighash::EcdsaSighashType;
use bitcoin::{bip32, PublicKey, Sequence};
use bitcoin::{bip32, PublicKey, Sequence, XOnlyPublicKey};

use super::checksum::desc_checksum;
use super::*;
Expand Down Expand Up @@ -2064,4 +2064,49 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
)
.unwrap_err();
}

#[test]
fn convert_public_key_descriptor_to_definite_key() {
let descriptor_str = "wsh(or_d(pk(021d4ea7132d4e1a362ee5efd8d0b59dd4d1fe8906eefa7dd812b05a46b73d829b),pk(0302c8bbbb393f32c843149ce36d56405595aaabab2d0e1f4ca5f9de67dd7419f6)))";
let full_pk_descriptor: Descriptor<PublicKey> =
Descriptor::from_str(descriptor_str).unwrap();

struct TranslateFullPk;

impl Translator<bitcoin::PublicKey, DefiniteDescriptorKey, ()> for TranslateFullPk {
fn pk(&mut self, pk: &bitcoin::PublicKey) -> Result<DefiniteDescriptorKey, ()> {
Ok(DefiniteDescriptorKey::new(DescriptorPublicKey::from(*pk))
.expect("DescriptorPublicKey from PublicKey has no wildcards"))
}

translate_hash_clone!(bitcoin::PublicKey, DefiniteDescriptorKey, ());
}

let converted_descriptor = full_pk_descriptor
.translate_pk(&mut TranslateFullPk)
.expect("infallible");

assert_eq!(full_pk_descriptor.to_string(), converted_descriptor.to_string());

let xonly_descriptor_str = "tr(1d4ea7132d4e1a362ee5efd8d0b59dd4d1fe8906eefa7dd812b05a46b73d829b,pk(02c8bbbb393f32c843149ce36d56405595aaabab2d0e1f4ca5f9de67dd7419f6))";
let xonly_pk_descriptor: Descriptor<XOnlyPublicKey> =
Descriptor::from_str(xonly_descriptor_str).unwrap();

struct TranslateXOnlyPk;

impl Translator<XOnlyPublicKey, DefiniteDescriptorKey, ()> for TranslateXOnlyPk {
fn pk(&mut self, pk: &XOnlyPublicKey) -> Result<DefiniteDescriptorKey, ()> {
Ok(DefiniteDescriptorKey::new(DescriptorPublicKey::from(*pk))
.expect("DescriptorPublicKey from XOnlyPublicKey has no wildcards"))
}

translate_hash_clone!(XOnlyPublicKey, DefiniteDescriptorKey, ());
}

let xonly_converted_descriptor = xonly_pk_descriptor
.translate_pk(&mut TranslateXOnlyPk)
.expect("infallible");

assert_eq!(xonly_pk_descriptor.to_string(), xonly_converted_descriptor.to_string());
}
}
Loading