Skip to content

Expose sh/wsh Inners #232

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

Merged
merged 2 commits into from
Jan 29, 2021
Merged
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.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniscript"
version = "5.0.2"
version = "5.1.0"
authors = ["Andrew Poelstra <[email protected]>, Sanket Kanjalkar <[email protected]>"]
repository = "https://github.com/apoelstra/miniscript"
description = "Miniscript: a subset of Bitcoin Script designed for analysis"
Expand Down
9 changes: 8 additions & 1 deletion examples/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
extern crate bitcoin;
extern crate miniscript;

use miniscript::DescriptorTrait;
use miniscript::{descriptor::DescriptorType, DescriptorTrait};
use std::str::FromStr;

fn main() {
Expand Down Expand Up @@ -45,4 +45,11 @@ fn main() {
format!("{:x}", my_descriptor.explicit_script()),
"21020202020202020202020202020202020202020202020202020202020202020202ac"
);

let desc = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
"sh(wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202)))",
)
.unwrap();

assert!(desc.desc_type() == DescriptorType::ShWsh);
}
51 changes: 49 additions & 2 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ mod sh;
mod sortedmulti;
// Descriptor Exports
pub use self::bare::{Bare, Pkh};
pub use self::segwitv0::{Wpkh, Wsh};
pub use self::sh::Sh;
pub use self::segwitv0::{Wpkh, Wsh, WshInner};
pub use self::sh::{Sh, ShInner};
pub use self::sortedmulti::SortedMultiVec;

mod checksum;
Expand Down Expand Up @@ -168,6 +168,31 @@ pub enum Descriptor<Pk: MiniscriptKey> {
Wsh(Wsh<Pk>),
}

/// Descriptor Type of the descriptor
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum DescriptorType {
/// Bare descriptor(Contains the native P2pk)
Bare,
/// Pure Sh Descriptor. Does not contain nested Wsh/Wpkh
Sh,
/// Pkh Descriptor
Pkh,
/// Wpkh Descriptor
Wpkh,
/// Wsh
Wsh,
/// Sh Wrapped Wsh
ShWsh,
/// Sh wrapped Wpkh
ShWpkh,
/// Sh Sorted Multi
ShSortedMulti,
/// Wsh Sorted Multi
WshSortedMulti,
/// Sh Wsh Sorted Multi
ShWshSortedMulti,
}

impl<Pk: MiniscriptKey> Descriptor<Pk> {
// Keys

Expand Down Expand Up @@ -251,6 +276,28 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
pub fn new_wsh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error> {
Ok(Descriptor::Wsh(Wsh::new_sortedmulti(k, pks)?))
}

/// Get the [DescriptorType] of [Descriptor]
pub fn desc_type(&self) -> DescriptorType {
match *self {
Descriptor::Bare(ref _bare) => DescriptorType::Bare,
Descriptor::Pkh(ref _pkh) => DescriptorType::Pkh,
Descriptor::Wpkh(ref _wpkh) => DescriptorType::Wpkh,
Descriptor::Sh(ref sh) => match sh.as_inner() {
ShInner::Wsh(ref wsh) => match wsh.as_inner() {
WshInner::SortedMulti(ref _smv) => DescriptorType::ShWshSortedMulti,
WshInner::Ms(ref _ms) => DescriptorType::ShWsh,
},
ShInner::Wpkh(ref _wpkh) => DescriptorType::ShWpkh,
ShInner::SortedMulti(ref _smv) => DescriptorType::ShSortedMulti,
ShInner::Ms(ref _ms) => DescriptorType::Sh,
},
Descriptor::Wsh(ref wsh) => match wsh.as_inner() {
WshInner::SortedMulti(ref _smv) => DescriptorType::WshSortedMulti,
WshInner::Ms(ref _ms) => DescriptorType::Wsh,
},
}
}
}

impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
Expand Down