Skip to content

Commit

Permalink
Depend on "possible" bitcoin 0.29.0
Browse files Browse the repository at this point in the history
Depend on possible code in the upcoming bitcoin 0.29.0 release.

"possible" because the current "dev-branch" of bitcoin that is used
contains two PRs that have not yet merged.
  • Loading branch information
tcharding committed Jul 27, 2022
1 parent c7c39f1 commit c525f53
Show file tree
Hide file tree
Showing 29 changed files with 418 additions and 291 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ std = ["bitcoin/std", "bitcoin/secp-recovery"]
no-std = ["hashbrown", "bitcoin/no-std"]
compiler = []
trace = []

unstable = []
use-serde = ["serde", "bitcoin/use-serde"]
use-serde = ["serde", "bitcoin/serde"]
rand = ["bitcoin/rand"]

[dependencies]
bitcoin = { version = "0.28.1", default-features = false }
bitcoin = { git = "https://github.com/tcharding/rust-bitcoin", branch = "07-20-release-0.29.0", default-features = false }
serde = { version = "1.0", optional = true }
hashbrown = { version = "0.11", optional = true }

[dev-dependencies]
bitcoind = {version = "0.26.1", features=["22_0"]}
bitcoind = { git = "https://github.com/tcharding/bitcoind", branch = "dev-build", features=["22_0"] }
actual-rand = { package = "rand", version = "0.8.4"}
secp256k1 = {version = "0.22.1", features = ["rand-std"]}
secp256k1 = {version = "0.24.0", features = ["rand-std"]}
bitcoin = { git = "https://github.com/tcharding/rust-bitcoin", branch = "07-20-release-0.29.0", features = ["rand"] }

[[example]]
name = "htlc"
Expand Down
6 changes: 3 additions & 3 deletions examples/sign_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::collections::HashMap;
use std::str::FromStr;

use bitcoin::blockdata::witness::Witness;
use bitcoin::secp256k1;
use bitcoin::{secp256k1, PackedLockTime, Sequence};

fn main() {
let mut tx = spending_transaction();
Expand Down Expand Up @@ -91,11 +91,11 @@ fn main() {
fn spending_transaction() -> bitcoin::Transaction {
bitcoin::Transaction {
version: 2,
lock_time: 0,
lock_time: PackedLockTime::ZERO,
input: vec![bitcoin::TxIn {
previous_output: Default::default(),
script_sig: bitcoin::Script::new(),
sequence: 0xffffffff,
sequence: Sequence::MAX,
witness: Witness::default(),
}],
output: vec![bitcoin::TxOut {
Expand Down
2 changes: 1 addition & 1 deletion examples/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn main() {
let secp = secp256k1::Secp256k1::new();
let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
// Random unspendable XOnlyPublicKey provided for compilation to Taproot Descriptor
let unspendable_pubkey = bitcoin::XOnlyPublicKey::from_keypair(&key_pair);
let (unspendable_pubkey, _parity) = bitcoin::XOnlyPublicKey::from_keypair(&key_pair);

pk_map.insert("UNSPENDABLE_KEY".to_string(), unspendable_pubkey);
let pubkeys = hardcoded_xonlypubkeys();
Expand Down
5 changes: 3 additions & 2 deletions examples/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::str::FromStr;
use bitcoin::consensus::Decodable;
use bitcoin::secp256k1::{self, Secp256k1};
use bitcoin::util::sighash;
use bitcoin::{LockTime, Sequence};
use miniscript::interpreter::KeySigPair;

fn main() {
Expand All @@ -33,8 +34,8 @@ fn main() {
&spk_input_1,
&tx.input[0].script_sig,
&tx.input[0].witness,
0,
0,
Sequence::ZERO,
LockTime::ZERO,
)
.unwrap();

Expand Down
20 changes: 10 additions & 10 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ mod tests {
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{hash160, sha256};
use bitcoin::util::bip32;
use bitcoin::{self, secp256k1, EcdsaSighashType, PublicKey};
use bitcoin::{self, secp256k1, EcdsaSighashType, PublicKey, Sequence};

use super::checksum::desc_checksum;
use super::tr::Tr;
Expand Down Expand Up @@ -1155,7 +1155,7 @@ mod tests {
let mut txin = bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
script_sig: bitcoin::Script::new(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::default(),
};
let bare = Descriptor::new_bare(ms.clone()).unwrap();
Expand All @@ -1166,7 +1166,7 @@ mod tests {
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
script_sig: script::Builder::new().push_slice(&sigser[..]).into_script(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::default(),
}
);
Expand All @@ -1182,7 +1182,7 @@ mod tests {
.push_slice(&sigser[..])
.push_key(&pk)
.into_script(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::default(),
}
);
Expand All @@ -1195,7 +1195,7 @@ mod tests {
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
script_sig: bitcoin::Script::new(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::from_vec(vec![sigser.clone(), pk.to_bytes(),]),
}
);
Expand All @@ -1216,7 +1216,7 @@ mod tests {
script_sig: script::Builder::new()
.push_slice(&redeem_script[..])
.into_script(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::from_vec(vec![sigser.clone(), pk.to_bytes(),]),
}
);
Expand All @@ -1238,7 +1238,7 @@ mod tests {
.push_slice(&sigser[..])
.push_slice(&ms.encode()[..])
.into_script(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::default(),
}
);
Expand All @@ -1253,7 +1253,7 @@ mod tests {
bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
script_sig: bitcoin::Script::new(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::from_vec(vec![sigser.clone(), ms.encode().into_bytes(),]),
}
);
Expand All @@ -1268,7 +1268,7 @@ mod tests {
script_sig: script::Builder::new()
.push_slice(&ms.encode().to_v0_p2wsh()[..])
.into_script(),
sequence: 100,
sequence: Sequence::from_height(100),
witness: Witness::from_vec(vec![sigser.clone(), ms.encode().into_bytes(),]),
}
);
Expand Down Expand Up @@ -1401,7 +1401,7 @@ mod tests {
let mut txin = bitcoin::TxIn {
previous_output: bitcoin::OutPoint::default(),
script_sig: bitcoin::Script::new(),
sequence: 0,
sequence: Sequence::ZERO,
witness: Witness::default(),
};
let satisfier = {
Expand Down
1 change: 1 addition & 0 deletions src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
TaprootBuilderError::EmptyTree => {
unreachable!("Taptree is a well formed tree with atleast 1 element")
}
_ => unreachable!("non_exhaustive catchall"),
},
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/interpreter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use crate::prelude::*;
pub enum Error {
/// Could not satisfy, absolute locktime not met
AbsoluteLocktimeNotMet(u32),
/// Could not satisfy, lock time values are different units
AbsoluteLocktimeComparisonInvalid(u32, u32),
/// Cannot Infer a taproot descriptor
/// Key spends cannot infer the internal key of the descriptor
/// Inferring script spends is possible, but is hidden nodes are currently
Expand Down Expand Up @@ -187,6 +189,7 @@ impl fmt::Display for Error {
Error::VerifyFailed => {
f.write_str("Expected Satisfied Boolean at stack top for VERIFY")
}
_ => f.write_str("Unknown error, non_exhaustive catch all"),
}
}
}
Expand Down Expand Up @@ -234,6 +237,7 @@ impl error::Error for Error {
Secp(e) => Some(e),
SchnorrSig(e) => Some(e),
SighashError(e) => Some(e),
_ => None, // non_exhaustive catch all.
}
}
}
Expand Down
37 changes: 21 additions & 16 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use core::str::FromStr;
use bitcoin::blockdata::witness::Witness;
use bitcoin::hashes::{hash160, ripemd160, sha256};
use bitcoin::util::{sighash, taproot};
use bitcoin::{self, secp256k1, TxOut};
use bitcoin::{self, secp256k1, LockTime, Sequence, TxOut};

use crate::miniscript::context::NoChecks;
use crate::miniscript::ScriptContext;
Expand All @@ -48,8 +48,8 @@ pub struct Interpreter<'txin> {
/// For non-Taproot spends, the scriptCode; for Taproot script-spends, this
/// is the leaf script; for key-spends it is `None`.
script_code: Option<bitcoin::Script>,
age: u32,
lock_time: u32,
age: Sequence,
lock_time: LockTime,
}

// A type representing functions for checking signatures that accept both
Expand Down Expand Up @@ -173,8 +173,8 @@ impl<'txin> Interpreter<'txin> {
spk: &bitcoin::Script,
script_sig: &'txin bitcoin::Script,
witness: &'txin Witness,
age: u32, // CSV, relative lock time.
lock_time: u32, // CLTV, absolute lock time.
age: Sequence, // CSV, relative lock time.
lock_time: LockTime, // CLTV, absolute lock time.
) -> Result<Self, Error> {
let (inner, stack, script_code) = inner::from_txdata(spk, script_sig, witness)?;
Ok(Interpreter {
Expand Down Expand Up @@ -491,12 +491,12 @@ pub enum SatisfiedConstraint {
///Relative Timelock for CSV.
RelativeTimelock {
/// The value of RelativeTimelock
time: u32,
n: Sequence,
},
///Absolute Timelock for CLTV.
AbsoluteTimelock {
/// The value of Absolute timelock
time: u32,
n: LockTime,
},
}

Expand Down Expand Up @@ -531,8 +531,8 @@ pub struct Iter<'intp, 'txin: 'intp> {
public_key: Option<&'intp BitcoinKey>,
state: Vec<NodeEvaluationState<'intp>>,
stack: Stack<'txin>,
age: u32,
lock_time: u32,
age: Sequence,
lock_time: LockTime,
has_errored: bool,
}

Expand Down Expand Up @@ -619,7 +619,7 @@ where
Terminal::After(ref n) => {
debug_assert_eq!(node_state.n_evaluated, 0);
debug_assert_eq!(node_state.n_satisfied, 0);
let res = self.stack.evaluate_after(n, self.lock_time);
let res = self.stack.evaluate_after(&n.into(), self.lock_time);
if res.is_some() {
return res;
}
Expand Down Expand Up @@ -1094,8 +1094,9 @@ mod tests {
pks.push(pk);
der_sigs.push(sigser);

let keypair = bitcoin::KeyPair::from_secret_key(&secp, sk);
x_only_pks.push(bitcoin::XOnlyPublicKey::from_keypair(&keypair));
let keypair = bitcoin::KeyPair::from_secret_key(&secp, &sk);
let (x_only_pk, _parity) = bitcoin::XOnlyPublicKey::from_keypair(&keypair);
x_only_pks.push(x_only_pk);
let schnorr_sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &[0u8; 32]);
let schnorr_sig = bitcoin::SchnorrSig {
sig: schnorr_sig,
Expand Down Expand Up @@ -1144,8 +1145,8 @@ mod tests {
n_evaluated: 0,
n_satisfied: 0,
}],
age: 1002,
lock_time: 1002,
age: Sequence::from_height(1002),
lock_time: LockTime::from_height(1002).unwrap(),
has_errored: false,
}
}
Expand Down Expand Up @@ -1208,7 +1209,9 @@ mod tests {
let after_satisfied: Result<Vec<SatisfiedConstraint>, Error> = constraints.collect();
assert_eq!(
after_satisfied.unwrap(),
vec![SatisfiedConstraint::AbsoluteTimelock { time: 1000 }]
vec![SatisfiedConstraint::AbsoluteTimelock {
n: LockTime::from_height(1000).unwrap()
}]
);

//Check Older
Expand All @@ -1218,7 +1221,9 @@ mod tests {
let older_satisfied: Result<Vec<SatisfiedConstraint>, Error> = constraints.collect();
assert_eq!(
older_satisfied.unwrap(),
vec![SatisfiedConstraint::RelativeTimelock { time: 1000 }]
vec![SatisfiedConstraint::RelativeTimelock {
n: Sequence::from_height(1000)
}]
);

//Check Sha256
Expand Down
32 changes: 23 additions & 9 deletions src/interpreter/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use bitcoin;
use bitcoin::blockdata::{opcodes, script};
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
use bitcoin::{LockTime, Sequence};

use super::error::PkEvalErrInner;
use super::{
Expand Down Expand Up @@ -230,14 +231,27 @@ impl<'txin> Stack<'txin> {
/// booleans
pub(super) fn evaluate_after(
&mut self,
n: &u32,
lock_time: u32,
n: &LockTime,
lock_time: LockTime,
) -> Option<Result<SatisfiedConstraint, Error>> {
if lock_time >= *n {
use LockTime::*;

let is_satisfied = match (*n, lock_time) {
(Blocks(n), Blocks(lock_time)) => n <= lock_time,
(Seconds(n), Seconds(lock_time)) => n <= lock_time,
_ => {
return Some(Err(Error::AbsoluteLocktimeComparisonInvalid(
n.to_consensus_u32(),
lock_time.to_consensus_u32(),
)))
}
};

if is_satisfied {
self.push(Element::Satisfied);
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { time: *n }))
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { n: *n }))
} else {
Some(Err(Error::AbsoluteLocktimeNotMet(*n)))
Some(Err(Error::AbsoluteLocktimeNotMet(n.to_consensus_u32())))
}
}

Expand All @@ -249,14 +263,14 @@ impl<'txin> Stack<'txin> {
/// booleans
pub(super) fn evaluate_older(
&mut self,
n: &u32,
age: u32,
n: &Sequence,
age: Sequence,
) -> Option<Result<SatisfiedConstraint, Error>> {
if age >= *n {
self.push(Element::Satisfied);
Some(Ok(SatisfiedConstraint::RelativeTimelock { time: *n }))
Some(Ok(SatisfiedConstraint::RelativeTimelock { n: *n }))
} else {
Some(Err(Error::RelativeLocktimeNotMet(*n)))
Some(Err(Error::RelativeLocktimeNotMet(n.to_consensus_u32())))
}
}

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ pub mod interpreter;
pub mod miniscript;
pub mod policy;
pub mod psbt;
pub mod timelock;

#[cfg(test)]
mod test_utils;
Expand Down

0 comments on commit c525f53

Please sign in to comment.