Skip to content

Commit

Permalink
Merge commit 'cddc22df7f678563d0314437c2ab88acfa154200' into update_2…
Browse files Browse the repository at this point in the history
…022_10

cddc22d Merge rust-bitcoin/rust-miniscript#450: Upgrade bitcoin dependency to v0.29.0 (Change satisfier locktime)
  • Loading branch information
sanket1729 committed Oct 20, 2022
2 parents cb90aa7 + cddc22d commit 6f19281
Show file tree
Hide file tree
Showing 35 changed files with 519 additions and 370 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ edition = "2018"
[features]
compiler = []
trace = []

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

[dependencies]
bitcoin = "0.28.1"
elements = "0.19.0"
bitcoin-miniscript = {package = "miniscript", git = "https://github.com/rust-bitcoin/rust-miniscript", rev = "c7c39f1e9d1b8da9e2c9318a61fb508553619e6c"}
bitcoin = "0.29.1"
elements = "0.21.0"
bitcoin-miniscript = {package = "miniscript", git = "https://github.com/rust-bitcoin/rust-miniscript", rev = "cddc22df7f678563d0314437c2ab88acfa154200"}

# Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1.0", optional = true }


[dev-dependencies]
serde_json = "1.0"
elementsd = {version = "0.5.0", features=["0_21_0","bitcoind_22_0"]}
elementsd = {version = "0.6.0", features=["0_21_0","bitcoind_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"]}

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

use elements::secp256k1_zkp;
use elements::{secp256k1_zkp, PackedLockTime, Sequence};

fn main() {
// Avoid repeatedly typing a pretty-common descriptor type
Expand All @@ -28,13 +28,12 @@ fn main() {
// Transaction which spends some output
let mut tx = elements::Transaction {
version: 2,
lock_time: 0,
lock_time: PackedLockTime::ZERO,
input: vec![elements::TxIn {
previous_output: elements::OutPoint::default(),
script_sig: elements::Script::new(),
sequence: 0xffffffff,
sequence: Sequence::MAX,
is_pegin: false,
has_issuance: false,
asset_issuance: elements::AssetIssuance::default(),
witness: elements::TxInWitness::default(),
}],
Expand Down
2 changes: 1 addition & 1 deletion examples/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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
17 changes: 9 additions & 8 deletions examples/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ extern crate elements_miniscript as miniscript;
use std::str::FromStr;

use elements::encode::Decodable;
use elements::{confidential, secp256k1_zkp};
use elements::hashes::Hash;
use elements::{confidential, secp256k1_zkp, LockTime, Sequence};
use miniscript::TxEnv;

use crate::miniscript::interpreter::KeySigPair; // secp256k1 re-exported from rust-bitcoin
Expand All @@ -40,8 +41,8 @@ fn main() {
&spk_input_1,
&transaction.input[0].script_sig,
&transaction.input[0].witness.script_witness,
0,
0,
Sequence::ZERO,
LockTime::ZERO,
)
.unwrap();

Expand Down Expand Up @@ -79,8 +80,8 @@ fn main() {
&spk_input_1,
&transaction.input[0].script_sig,
&transaction.input[0].witness.script_witness,
0,
0,
Sequence::ZERO,
LockTime::ZERO,
)
.unwrap();

Expand All @@ -99,7 +100,7 @@ fn main() {
let utxos = [spent_utxo, elements::TxOut::default()];
let env = TxEnv::new(&transaction, &utxos, 0).expect("Input len == witness utxo len");
// segwit spends don't require genesis hash
let genesis_hash = elements::BlockHash::default();
let genesis_hash = elements::BlockHash::all_zeros();

println!("\nExample two");
for elem in interpreter.iter(&secp, &env, genesis_hash) {
Expand All @@ -120,8 +121,8 @@ fn main() {
&spk_input_1,
&transaction.input[0].script_sig,
&transaction.input[0].witness.script_witness,
0,
0,
Sequence::ZERO,
LockTime::ZERO,
)
.unwrap();

Expand Down
30 changes: 22 additions & 8 deletions src/descriptor/csfs_cov/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ mod tests {
use elements::secp256k1_zkp::ZERO_TWEAK;
use elements::{
self, confidential, opcodes, script, secp256k1_zkp, AssetId, AssetIssuance,
EcdsaSigHashType, OutPoint, Script, Transaction, TxIn, TxInWitness, TxOut, Txid,
EcdsaSigHashType, LockTime, OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn,
TxInWitness, TxOut, Txid,
};

use super::cov::*;
Expand Down Expand Up @@ -190,7 +191,7 @@ mod tests {
// Now create a transaction spending this.
let mut spend_tx = Transaction {
version: 2,
lock_time: 0,
lock_time: PackedLockTime::ZERO,
input: vec![txin_from_txid_vout(
"141f79c7c254ee3a9a9bc76b4f60564385b784bdfc1882b25154617801fe2237",
1,
Expand Down Expand Up @@ -260,7 +261,14 @@ mod tests {

// A pair of satisfiers is also a satisfier
let (wit, ss) = desc.get_satisfaction((cov_sat, pk_sat))?;
let interpreter = Interpreter::from_txdata(&desc.script_pubkey(), &ss, &wit, 0, 0).unwrap();
let interpreter = Interpreter::from_txdata(
&desc.script_pubkey(),
&ss,
&wit,
Sequence::ZERO,
LockTime::ZERO,
)
.unwrap();

assert!(wit[0].len() <= 73);
assert!(wit[1].len() == 4); // version
Expand Down Expand Up @@ -385,7 +393,7 @@ mod tests {
// Now create a transaction spending this.
let mut spend_tx = Transaction {
version: 2,
lock_time: 0,
lock_time: PackedLockTime::ZERO,
input: vec![txin_from_txid_vout(
"7c8e615c8da947fefd2d9b6f83f313a9b59d249c93a5f232287633195b461cb7",
0,
Expand Down Expand Up @@ -450,15 +458,22 @@ mod tests {

// A pair of satisfiers is also a satisfier
let (wit, ss) = desc.get_satisfaction((cov_sat, pk_sat)).unwrap();
let interpreter = Interpreter::from_txdata(&desc.script_pubkey(), &ss, &wit, 0, 0).unwrap();
let interpreter = Interpreter::from_txdata(
&desc.script_pubkey(),
&ss,
&wit,
Sequence::ZERO,
LockTime::ZERO,
)
.unwrap();
// Check that everything is executed correctly with dummysigs
let constraints: Result<Vec<_>, _> = interpreter.iter_assume_sigs().collect();
constraints.expect("Covenant incorrect satisfaction");
// Commented Demo test code:
// 1) Send 0.002 btc to above address
// 2) Create a tx by filling up txid
// 3) Send the tx
assert_eq!(witness_size(&wit), 384);
assert_eq!(witness_size(&wit), 385);
assert_eq!(wit.len(), 13);
// spend_tx.input[0].witness.script_witness = wit;
// use elements::encode::serialize_hex;
Expand All @@ -472,9 +487,8 @@ mod tests {
txid: Txid::from_str(txid).unwrap(),
vout: vout,
},
sequence: 0xfffffffe,
sequence: Sequence::MAX,
is_pegin: false,
has_issuance: false,
// perhaps make this an option in elements upstream?
asset_issuance: AssetIssuance {
asset_blinding_nonce: secp256k1_zkp::ZERO_TWEAK,
Expand Down
4 changes: 2 additions & 2 deletions src/descriptor/csfs_cov/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ impl<'tx, 'ptx, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for LegacyCovSati
}

fn lookup_nsequence(&self) -> Option<u32> {
Some(self.tx.input[self.idx as usize].sequence)
Some(self.tx.input[self.idx as usize].sequence.to_consensus_u32())
}

fn lookup_outputs(&self) -> Option<&[elements::TxOut]> {
Some(&self.tx.output)
}

fn lookup_nlocktime(&self) -> Option<u32> {
Some(self.tx.lock_time)
Some(self.tx.lock_time.to_u32())
}

fn lookup_sighashu32(&self) -> Option<u32> {
Expand Down
8 changes: 3 additions & 5 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ mod tests {
use elements::hashes::{hash160, sha256};
use elements::opcodes::all::{OP_CLTV, OP_CSV};
use elements::script::Instruction;
use elements::{opcodes, script};
use elements::{opcodes, script, Sequence};

use super::checksum::desc_checksum;
use super::tr::Tr;
Expand Down Expand Up @@ -1239,9 +1239,8 @@ mod tests {
elements::TxIn {
previous_output: elements::OutPoint::default(),
script_sig: script_sig,
sequence: 100,
sequence: Sequence::from_height(100),
is_pegin: false,
has_issuance: false,
asset_issuance: elements::AssetIssuance::default(),
witness: txin_witness,
}
Expand Down Expand Up @@ -1514,9 +1513,8 @@ mod tests {
let mut txin = elements::TxIn {
previous_output: elements::OutPoint::default(),
script_sig: Script::new(),
sequence: 100,
sequence: Sequence::from_height(100),
is_pegin: false,
has_issuance: false,
asset_issuance: elements::AssetIssuance::default(),
witness: elements::TxInWitness::default(),
};
Expand Down
17 changes: 12 additions & 5 deletions src/descriptor/pegin/legacy_pegin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub struct LegacyPegin<Pk: MiniscriptKey> {
/// The emergency threshold
pub emer_k: usize,
/// csv timelock
pub timelock: u32,
pub timelock: bitcoin::Sequence,
/// The elements descriptor required to redeem
///
/// TODO: Allow extension user descriptors when claiming pegins
Expand All @@ -143,7 +143,7 @@ impl<Pk: MiniscriptKey> LegacyPegin<Pk> {
fed_k: usize,
emer_pks: Vec<LegacyPeginKey>,
emer_k: usize,
timelock: u32,
timelock: bitcoin::Sequence,
desc: Descriptor<Pk, CovenantExt<CovExtArgs>>,
) -> Self {
let fed_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(fed_k, fed_pks.clone()))
Expand Down Expand Up @@ -268,8 +268,8 @@ impl<Pk: MiniscriptKey> LegacyPegin<Pk> {
let mut rser = right.encode().into_bytes();
// ...and we have an OP_VERIFY style checksequenceverify, which in
// Liquid production was encoded with OP_DROP instead...
assert_eq!(rser[4], opcodes::all::OP_VERIFY.into_u8());
rser[4] = opcodes::all::OP_DROP.into_u8();
assert_eq!(rser[4], opcodes::all::OP_VERIFY.to_u8());
rser[4] = opcodes::all::OP_DROP.to_u8();
// ...then we should serialize it by sharing the OP_CMS across
// both branches, and add an OP_DEPTH check to distinguish the
// branches rather than doing the normal cascade construction
Expand Down Expand Up @@ -315,7 +315,14 @@ impl<Pk: MiniscriptKey> LegacyPegin<Pk> {
.map(|pk| LegacyPeginKey::Functionary(bitcoin::PublicKey::from_str(pk).unwrap()))
.collect();

Self::new(fed_pks, 11, emer_pks, 2, 4032, user_desc)
Self::new(
fed_pks,
11,
emer_pks,
2,
bitcoin::Sequence::from_consensus(4032),
user_desc,
)
}
}

Expand Down
23 changes: 2 additions & 21 deletions src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::{Arc, Mutex};
use std::{fmt, hash};

use elements::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 elements::{self, opcodes, secp256k1_zkp, Script};
Expand Down Expand Up @@ -259,26 +259,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> Tr<Pk, Ext> {
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use crate::extensions::EvalError;
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 @@ -142,6 +144,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 @@ -224,6 +231,7 @@ impl error::Error for Error {

match self {
AbsoluteLocktimeNotMet(_)
| AbsoluteLocktimeComparisonInvalid(_, _)
| CannotInferTrDescriptors
| ControlBlockVerificationError
| CouldNotEvaluate
Expand Down

0 comments on commit 6f19281

Please sign in to comment.