Skip to content
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

Add example using implementing Extension for APO emulation #32

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,8 @@ impl_from_str!(
Ok(tr) => Ok(Descriptor::Tr(tr)),
Err(_) => {
// Try parsing with extensions
let tr = Tr::<Pk, T>::from_str(s)?;
// descriptors are always parsed insane. This will improve once we use upstream ExtParams Api.
let tr = Tr::<Pk, T>::from_str_insane(s)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In acced27:

This is a bit eyebrow-raising. This means that Descriptor::from_str will no longer enforce any sanity rules when parsing tapscripts?

This is fine if it's temporary and we're going to fix it when we bring ExtParams down, but the comment should have the text FIXME in it so that we have a reminder to deal with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this comment was wrong/incomplete. This was meant to covenant descriptors should be parsed as insane because they are not safe(in the sense there is no signature guarding it).

Parsing regular descriptors should work as expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, perfect. Can you update the comment?

Ok(Descriptor::TrExt(tr))
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/descriptor/tr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Tapscript

use std::cmp::{self, max};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::{fmt, hash};

Expand Down Expand Up @@ -417,7 +416,7 @@ impl_block_str!(
fn parse_tr_script_spend(tree: &expression::Tree,) -> Result<TapTree<Pk, Ext>, Error> {
match tree {
expression::Tree { name, args } if !name.is_empty() && args.is_empty() => {
let script = Miniscript::<Pk, Tap, Ext>::from_str(name)?;
let script = Miniscript::<Pk, Tap, Ext>::from_str_insane(name)?;
Ok(TapTree::Leaf(Arc::new(script)))
}
expression::Tree { name, args } if name.is_empty() && args.len() == 2 => {
Expand Down Expand Up @@ -484,6 +483,20 @@ impl_from_str!(
=> Ext; Extension,
type Err = Error;,
fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = Self::from_str_insane(s)?;
if res.iter_scripts().any(|(_, ms)| ms.sanity_check().is_err()) {
return Err(Error::BadDescriptor("Sanity check failed".to_string()));
}
Ok(res)
}
);

#[rustfmt::skip]
impl_block_str!(
Tr<Pk, Ext>,
=> Ext; Extension,
/// Parse taproot descriptors without any sanity checks
pub fn from_str_insane(s: &str,) -> Result<Tr<Pk, Ext>, Error> {
let desc_str = verify_checksum(s)?;
let top = parse_tr_tree(desc_str)?;
Self::from_tree(&top)
Expand Down Expand Up @@ -742,6 +755,7 @@ where
mod tests {
use super::*;
use crate::{ForEachKey, NoExt};
use core::str::FromStr;

#[test]
fn test_for_each() {
Expand Down