Skip to content

Add ScriptContext to Miniscript #97

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 3 commits into from
Jun 29, 2020
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ matrix:
env: DO_FUZZ=true DO_LINT=true
- rust: beta
- rust: nightly
env: DO_BENCH=true
env: DO_BENCH=true DO_MIRI=true
- rust: 1.22.0

script:
Expand Down
17 changes: 17 additions & 0 deletions contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ then
)
fi

# Miri Checks if told to
# Only supported in nightly
if [ "$DO_MIRI" = true ]
then
(
MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri)
echo "Installing latest nightly with Miri: $MIRI_NIGHTLY"
rustup set profile minimal
rustup default "$MIRI_NIGHTLY"
rustup component add miri
cargo miri test -- -- miri_

# Change back to latest nightly possibly without Miri
rustup default nightly
)
fi

# Bench if told to
if [ "$DO_BENCH" = true ]
then
Expand Down
3 changes: 0 additions & 3 deletions examples/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ fn main() {
0,
0,
);

println!("\nExample one");
for elem in iter {
match elem.expect("no evaluation error") {
Expand All @@ -129,7 +128,6 @@ fn main() {
0,
0,
);

println!("\nExample two");
for elem in iter {
match elem.expect("no evaluation error") {
Expand All @@ -154,7 +152,6 @@ fn main() {
0,
0,
);

println!("\nExample three");
for elem in iter {
let error = elem.expect_err("evaluation error");
Expand Down
8 changes: 5 additions & 3 deletions fuzz/fuzz_targets/compile_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
extern crate miniscript;

use miniscript::Segwitv0;
use miniscript::{policy, DummyKey, Miniscript};
use policy::Liftable;

use std::str::FromStr;

type DummyScript = Miniscript<DummyKey>;
type DummyScript = Miniscript<DummyKey, Segwitv0>;
type DummyPolicy = policy::Concrete<DummyKey>;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
// Compile
if let Ok(desc) = pol.compile() {
if let Ok(desc) = pol.compile::<Segwitv0>() {
// Lift
assert_eq!(desc.clone().lift(), pol.clone().lift());
// Try to roundtrip the output of the compiler
Expand All @@ -37,7 +38,8 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/roundtrip_concrete.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

extern crate miniscript;
extern crate regex;
use std::str::FromStr;
use miniscript::{policy, DummyKey};
use regex::Regex;
use std::str::FromStr;

type DummyPolicy = policy::Concrete<DummyKey>;

Expand All @@ -29,7 +28,8 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down
10 changes: 6 additions & 4 deletions fuzz/fuzz_targets/roundtrip_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

extern crate miniscript;
extern crate regex;

Expand All @@ -10,13 +9,15 @@ fn do_test(data: &[u8]) {
let s = String::from_utf8_lossy(data);
if let Ok(desc) = Descriptor::<DummyKey>::from_str(&s) {
let output = desc.to_string();

let multi_wrap_pk_re = Regex::new("([a-z]+)c:pk_k\\(").unwrap();
let multi_wrap_pkh_re = Regex::new("([a-z]+)c:pk_h\\(").unwrap();

let normalize_aliases = multi_wrap_pk_re.replace_all(&s, "$1:pk(");
let normalize_aliases = multi_wrap_pkh_re.replace_all(&normalize_aliases, "$1:pkh(");
let normalize_aliases = normalize_aliases.replace("c:pk_k(", "pk(").replace("c:pk_h(", "pkh(");
let normalize_aliases = normalize_aliases
.replace("c:pk_k(", "pk(")
.replace("c:pk_h(", "pkh(");

assert_eq!(normalize_aliases.to_lowercase(), output.to_lowercase());
}
Expand All @@ -32,7 +33,8 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down
9 changes: 5 additions & 4 deletions fuzz/fuzz_targets/roundtrip_miniscript_script.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

extern crate miniscript;

use miniscript::Miniscript;
use miniscript::bitcoin::blockdata::script;
use miniscript::Miniscript;
use miniscript::Segwitv0;

fn do_test(data: &[u8]) {
// Try round-tripping as a script
let script = script::Script::from(data.to_owned());

if let Ok(pt) = Miniscript::parse(&script) {
if let Ok(pt) = Miniscript::<_, Segwitv0>::parse(&script) {
let output = pt.encode();
assert_eq!(pt.script_size(), output.len());
assert_eq!(output, script);
Expand All @@ -25,7 +25,8 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down
18 changes: 10 additions & 8 deletions fuzz/fuzz_targets/roundtrip_miniscript_str.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@

extern crate miniscript;
extern crate regex;

use std::str::FromStr;
use regex::Regex;
use std::str::FromStr;

use miniscript::{DummyKey};
use miniscript::DummyKey;
use miniscript::Miniscript;
use miniscript::Segwitv0;

fn do_test(data: &[u8]) {
let s = String::from_utf8_lossy(data);
if let Ok(desc) = Miniscript::<DummyKey>::from_str(&s) {
if let Ok(desc) = Miniscript::<DummyKey, Segwitv0>::from_str(&s) {
let output = desc.to_string();

let multi_wrap_pk_re = Regex::new("([a-z]+)c:pk_k\\(").unwrap();
let multi_wrap_pkh_re = Regex::new("([a-z]+)c:pk_h\\(").unwrap();

let normalize_aliases = multi_wrap_pk_re.replace_all(&s, "$1:pk(");
let normalize_aliases = multi_wrap_pkh_re.replace_all(&normalize_aliases, "$1:pkh(");
let normalize_aliases = normalize_aliases.replace("c:pk_k(", "pk(").replace("c:pk_h(", "pkh(");
let normalize_aliases = normalize_aliases
.replace("c:pk_k(", "pk(")
.replace("c:pk_h(", "pkh(");

assert_eq!(normalize_aliases.to_lowercase(), output.to_lowercase());

}
}

Expand All @@ -35,7 +36,8 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down
8 changes: 4 additions & 4 deletions fuzz/fuzz_targets/roundtrip_semantic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

extern crate miniscript;

use std::str::FromStr;
use miniscript::{policy, DummyKey};
use std::str::FromStr;

type DummyPolicy = policy::Semantic<DummyKey>;

Expand All @@ -24,12 +23,13 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
do_test(data);
});
}
}
}
10 changes: 5 additions & 5 deletions src/descriptor/create_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bitcoin::blockdata::script::Instruction;
use descriptor::satisfied_constraints::Error as IntError;
use descriptor::satisfied_constraints::{Stack, StackElement};
use descriptor::Descriptor;
use miniscript::Miniscript;
use miniscript::{Legacy, Miniscript, Segwitv0};
use Error;
use ToPublicKey;

Expand Down Expand Up @@ -112,7 +112,7 @@ fn verify_wsh<'txin>(
script_pubkey: &bitcoin::Script,
script_sig: &bitcoin::Script,
witness: &'txin [Vec<u8>],
) -> Result<(Miniscript<bitcoin::PublicKey>, Stack<'txin>), Error> {
) -> Result<(Miniscript<bitcoin::PublicKey, Segwitv0>, Stack<'txin>), Error> {
if !script_sig.is_empty() {
return Err(Error::NonEmptyScriptSig);
}
Expand All @@ -121,7 +121,7 @@ fn verify_wsh<'txin>(
if witness_script.to_v0_p2wsh() != *script_pubkey {
return Err(Error::IncorrectScriptHash);
}
let ms = Miniscript::parse(&witness_script)?;
let ms = Miniscript::<bitcoin::PublicKey, Segwitv0>::parse(&witness_script)?;
//only iter till len -1 to not include the witness script
let stack: Vec<StackElement> = witness
.iter()
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn from_txin_with_witness_stack<'txin>(
if !witness.is_empty() {
return Err(Error::NonEmptyWitness);
}
let ms = Miniscript::parse(&redeem_script)?;
let ms = Miniscript::<bitcoin::PublicKey, Legacy>::parse(&redeem_script)?;
Ok((Descriptor::Sh(ms), stack))
}
} else {
Expand All @@ -230,7 +230,7 @@ pub fn from_txin_with_witness_stack<'txin>(
if !witness.is_empty() {
return Err(Error::NonEmptyWitness);
}
let ms = Miniscript::parse(script_pubkey)?;
let ms = Miniscript::<bitcoin::PublicKey, Legacy>::parse(script_pubkey)?;
Ok((Descriptor::Bare(ms), Stack(stack?)))
}
}
Expand Down
Loading