Skip to content

various improvements/refactors of the decoder and its errors #845

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 5 commits into
base: master
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 fuzz/fuzz_targets/roundtrip_miniscript_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn do_test(data: &[u8]) {
// Try round-tripping as a script
let script = script::Script::from_bytes(data);

if let Ok(pt) = Miniscript::<miniscript::bitcoin::PublicKey, Segwitv0>::parse(script) {
if let Ok(pt) = Miniscript::<miniscript::bitcoin::PublicKey, Segwitv0>::decode(script) {
let output = pt.encode();
assert_eq!(pt.script_size(), output.len());
assert_eq!(&output, script);
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/roundtrip_miniscript_script_tap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn do_test(data: &[u8]) {
// Try round-tripping as a script
let script = script::Script::from_bytes(data);

if let Ok(pt) = Miniscript::<miniscript::bitcoin::key::XOnlyPublicKey, Tap>::parse(script) {
if let Ok(pt) = Miniscript::<miniscript::bitcoin::key::XOnlyPublicKey, Tap>::decode(script) {
let output = pt.encode();
assert_eq!(pt.script_size(), output.len());
assert_eq!(&output, script);
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn script_from_stack_elem<Ctx: ScriptContext>(
) -> Result<Miniscript<Ctx::Key, Ctx>, Error> {
match *elem {
stack::Element::Push(sl) => {
Miniscript::parse_with_ext(bitcoin::Script::from_bytes(sl), &ExtParams::allow_all())
Miniscript::decode_with_ext(bitcoin::Script::from_bytes(sl), &ExtParams::allow_all())
.map_err(Error::from)
}
stack::Element::Satisfied => Ok(Miniscript::TRUE),
Expand Down Expand Up @@ -327,7 +327,7 @@ pub(super) fn from_txdata<'txin>(
} else {
if wit_stack.is_empty() {
// Bare script parsed in BareCtx
let miniscript = Miniscript::<bitcoin::PublicKey, BareCtx>::parse_with_ext(
let miniscript = Miniscript::<bitcoin::PublicKey, BareCtx>::decode_with_ext(
spk,
&ExtParams::allow_all(),
)?;
Expand Down
34 changes: 11 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ mod util;
use core::{fmt, hash, str};

use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
use bitcoin::hex::DisplayHex;
use bitcoin::{script, Opcode};

pub use crate::blanket_traits::FromStrKey;
pub use crate::descriptor::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey};
Expand Down Expand Up @@ -436,15 +434,8 @@ pub trait ForEachKey<Pk: MiniscriptKey> {

#[derive(Debug)]
pub enum Error {
/// Opcode appeared which is not part of the script subset
InvalidOpcode(Opcode),
/// Some opcode occurred followed by `OP_VERIFY` when it had
/// a `VERIFY` version that should have been used instead
NonMinimalVerify(String),
/// Push was illegal in some context
InvalidPush(Vec<u8>),
/// rust-bitcoin script error
Script(script::Error),
/// Error when lexing a bitcoin Script.
ScriptLexer(crate::miniscript::lex::Error),
/// rust-bitcoin address error
AddrError(bitcoin::address::ParseError),
/// rust-bitcoin p2sh address error
Expand Down Expand Up @@ -490,7 +481,7 @@ pub enum Error {
/// Bare descriptors don't have any addresses
BareDescriptorAddr,
/// PubKey invalid under current context
PubKeyCtxError(miniscript::decode::KeyParseError, &'static str),
PubKeyCtxError(miniscript::decode::KeyError, &'static str),
/// No script code for Tr descriptors
TrNoScriptCode,
/// At least two BIP389 key expressions in the descriptor contain tuples of
Expand Down Expand Up @@ -519,12 +510,7 @@ const MAX_RECURSION_DEPTH: u32 = 402;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::InvalidOpcode(op) => write!(f, "invalid opcode {}", op),
Error::NonMinimalVerify(ref tok) => write!(f, "{} VERIFY", tok),
Error::InvalidPush(ref push) => {
write!(f, "invalid push {:x}", push.as_hex())
},
Error::Script(ref e) => fmt::Display::fmt(e, f),
Error::ScriptLexer(ref e) => e.fmt(f),
Error::AddrError(ref e) => fmt::Display::fmt(e, f),
Error::AddrP2shError(ref e) => fmt::Display::fmt(e, f),
Error::UnexpectedStart => f.write_str("unexpected start of script"),
Expand Down Expand Up @@ -576,10 +562,7 @@ impl std::error::Error for Error {
use self::Error::*;

match self {
InvalidOpcode(_)
| NonMinimalVerify(_)
| InvalidPush(_)
| UnexpectedStart
UnexpectedStart
| Unexpected(_)
| UnknownWrapper(_)
| NonTopLevel(_)
Expand All @@ -593,7 +576,7 @@ impl std::error::Error for Error {
| BareDescriptorAddr
| TrNoScriptCode
| MultipathDescLenMismatch => None,
Script(e) => Some(e),
ScriptLexer(e) => Some(e),
AddrError(e) => Some(e),
AddrP2shError(e) => Some(e),
Secp(e) => Some(e),
Expand All @@ -614,6 +597,11 @@ impl std::error::Error for Error {
}
}

#[doc(hidden)]
impl From<miniscript::lex::Error> for Error {
fn from(e: miniscript::lex::Error) -> Error { Error::ScriptLexer(e) }
}

#[doc(hidden)]
impl From<miniscript::types::Error> for Error {
fn from(e: miniscript::types::Error) -> Error { Error::TypeCheck(e.to_string()) }
Expand Down
2 changes: 1 addition & 1 deletion src/miniscript/analyzable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::prelude::*;
use crate::{Miniscript, MiniscriptKey, ScriptContext, Terminal};

/// Params for parsing miniscripts that either non-sane or non-specified(experimental) in the spec.
/// Used as a parameter [`Miniscript::from_str_ext`] and [`Miniscript::parse_with_ext`].
/// Used as a parameter [`Miniscript::from_str_ext`] and [`Miniscript::decode_with_ext`].
///
/// This allows parsing miniscripts if
/// 1. It is unsafe(does not require a digital signature to spend it)
Expand Down
Loading
Loading