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 Aug 9, 2022
1 parent c7c39f1 commit 05c5637
Show file tree
Hide file tree
Showing 29 changed files with 423 additions and 312 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
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 = { version = "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 = { version = "0.29.0", features = ["rand"] }

[[example]]
name = "htlc"
Expand Down
6 changes: 3 additions & 3 deletions examples/sign_multisig.rs
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
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
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
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
23 changes: 2 additions & 21 deletions src/descriptor/tr.rs
Expand Up @@ -5,7 +5,7 @@ use core::{fmt, hash};

use bitcoin::blockdata::opcodes;
use bitcoin::util::taproot::{
LeafVersion, TaprootBuilder, TaprootBuilderError, TaprootSpendInfo, TAPROOT_CONTROL_BASE_SIZE,
LeafVersion, TaprootBuilder, TaprootSpendInfo, TAPROOT_CONTROL_BASE_SIZE,
TAPROOT_CONTROL_MAX_NODE_COUNT, TAPROOT_CONTROL_NODE_SIZE,
};
use bitcoin::{secp256k1, Address, Network, Script};
Expand Down Expand Up @@ -238,26 +238,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
// Assert builder cannot error here because we have a well formed descriptor
match builder.finalize(&secp, self.internal_key.to_x_only_pubkey()) {
Ok(data) => data,
Err(e) => match e {
TaprootBuilderError::InvalidMerkleTreeDepth(_) => {
unreachable!("Depth checked in struct construction")
}
TaprootBuilderError::NodeNotInDfsOrder => {
unreachable!("Insertion is called in DFS order")
}
TaprootBuilderError::OverCompleteTree => {
unreachable!("Taptree is a well formed tree")
}
TaprootBuilderError::InvalidInternalKey(_) => {
unreachable!("Internal key checked for validity")
}
TaprootBuilderError::IncompleteTree => {
unreachable!("Taptree is a well formed tree")
}
TaprootBuilderError::EmptyTree => {
unreachable!("Taptree is a well formed tree with atleast 1 element")
}
},
Err(_) => unreachable!("We know the builder can be finalized"),
}
};
let spend_info = Arc::new(data);
Expand Down
8 changes: 8 additions & 0 deletions src/interpreter/error.rs
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 @@ -129,6 +131,11 @@ impl fmt::Display for Error {
"required absolute locktime CLTV of {} blocks, not met",
n
),
Error::AbsoluteLocktimeComparisonInvalid(n, lock_time) => write!(
f,
"could not satisfy, lock time values are different units n: {} lock_time: {}",
n, lock_time
),
Error::CannotInferTrDescriptors => write!(f, "Cannot infer taproot descriptors"),
Error::ControlBlockParse(ref e) => write!(f, "Control block parse error {}", e),
Error::ControlBlockVerificationError => {
Expand Down Expand Up @@ -198,6 +205,7 @@ impl error::Error for Error {

match self {
AbsoluteLocktimeNotMet(_)
| AbsoluteLocktimeComparisonInvalid(_, _)
| CannotInferTrDescriptors
| ControlBlockVerificationError
| CouldNotEvaluate
Expand Down
37 changes: 21 additions & 16 deletions src/interpreter/mod.rs
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

0 comments on commit 05c5637

Please sign in to comment.