Skip to content

Commit

Permalink
Upgrade bitcoin dependency
Browse files Browse the repository at this point in the history
Upgrade to the latest versions the dependencies required to use `bitcoin
v0.31.0-rc1`:

- bitcoin: to v0.31.0-rc1
- secp256k1: to v0.28.0
- internals: to v0.2.0
- bitcoind: git branch with similar upgrade (includes bitcoincore-rpc)
  • Loading branch information
tcharding committed Nov 15, 2023
1 parent c94deab commit ad2524b
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 183 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ base64 = ["bitcoin/base64"]

[dependencies]
bech32 = { version = "0.10.0-beta", default-features = false }
bitcoin = { version = "0.30.0", default-features = false }
internals = { package = "bitcoin-private", version = "0.1.0", default_features = false }
bitcoin = { version = "0.31.0", default-features = false }
internals = { package = "bitcoin-internals", version = "0.2.0", default_features = false }

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

[dev-dependencies]
serde_test = "1.0.147"
bitcoin = { version = "0.30.0", features = ["base64"] }
secp256k1 = {version = "0.27.0", features = ["rand-std"]}
bitcoin = { version = "0.31.0", features = ["base64"] }
secp256k1 = {version = "0.28.0", features = ["rand-std"]}

[[example]]
name = "htlc"
Expand Down
6 changes: 3 additions & 3 deletions bitcoind-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false

[dependencies]
miniscript = {path = "../"}
bitcoind = { version = "0.32.0" }
bitcoind = { git = "https://github.com/tcharding/bitcoind", branch = "10-16-upgrade-bitcoin" }
actual-rand = { package = "rand", version = "0.8.4"}
secp256k1 = {version = "0.27.0", features = ["rand-std"]}
internals = { package = "bitcoin-private", version = "0.1.0", default_features = false }
secp256k1 = {version = "0.28.0", features = ["rand-std"]}
internals = { package = "bitcoin-internals", version = "0.2.0", default_features = false }
6 changes: 3 additions & 3 deletions bitcoind-tests/tests/setup/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct PubData {
#[derive(Debug, Clone)]
pub struct SecretData {
pub sks: Vec<bitcoin::secp256k1::SecretKey>,
pub x_only_keypairs: Vec<bitcoin::secp256k1::KeyPair>,
pub x_only_keypairs: Vec<bitcoin::secp256k1::Keypair>,
pub sha256_pre: [u8; 32],
pub hash256_pre: [u8; 32],
pub ripemd160_pre: [u8; 32],
Expand All @@ -62,7 +62,7 @@ fn setup_keys(
) -> (
Vec<bitcoin::secp256k1::SecretKey>,
Vec<miniscript::bitcoin::PublicKey>,
Vec<bitcoin::secp256k1::KeyPair>,
Vec<bitcoin::secp256k1::Keypair>,
Vec<XOnlyPublicKey>,
) {
let secp_sign = secp256k1::Secp256k1::signing_only();
Expand All @@ -87,7 +87,7 @@ fn setup_keys(
let mut x_only_pks = vec![];

for i in 0..n {
let keypair = bitcoin::secp256k1::KeyPair::from_secret_key(&secp_sign, &sks[i]);
let keypair = bitcoin::secp256k1::Keypair::from_secret_key(&secp_sign, &sks[i]);
let (xpk, _parity) = XOnlyPublicKey::from_keypair(&keypair);
x_only_keypairs.push(keypair);
x_only_pks.push(xpk);
Expand Down
23 changes: 13 additions & 10 deletions bitcoind-tests/tests/test_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use std::path::Path;

use bitcoin::hashes::{sha256d, Hash};
use bitcoin::psbt::Psbt;
use bitcoin::{psbt, secp256k1, Amount, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid};
use bitcoin::{
psbt, secp256k1, transaction, Amount, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid,
};
use bitcoind::bitcoincore_rpc::{json, Client, RpcApi};
use miniscript::bitcoin::absolute;
use miniscript::psbt::PsbtExt;
Expand Down Expand Up @@ -49,7 +51,7 @@ fn btc<F: Into<f64>>(btc: F) -> Amount { Amount::from_btc(btc.into()).unwrap() }
// Find the Outpoint by value.
// Ideally, we should find by scriptPubkey, but this
// works for temp test case
fn get_vout(cl: &Client, txid: Txid, value: u64) -> (OutPoint, TxOut) {
fn get_vout(cl: &Client, txid: Txid, value: Amount) -> (OutPoint, TxOut) {
let tx = cl
.get_transaction(&txid, None)
.unwrap()
Expand Down Expand Up @@ -102,7 +104,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
for (desc, txid) in desc_vec.iter().zip(txids) {
let mut psbt = Psbt {
unsigned_tx: Transaction {
version: 2,
version: transaction::Version::TWO,
lock_time: absolute::LockTime::from_time(1_603_866_330)
.expect("valid timestamp")
.into(), // 10/28/2020 @ 6:25am (UTC)
Expand All @@ -117,7 +119,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
outputs: vec![],
};
// figure out the outpoint from the txid
let (outpoint, witness_utxo) = get_vout(&cl, txid, btc(1.0).to_sat());
let (outpoint, witness_utxo) = get_vout(&cl, txid, btc(1.0));
let mut txin = TxIn::default();
txin.previous_output = outpoint;
// set the sequence to a non-final number for the locktime transactions to be
Expand All @@ -132,9 +134,10 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
.get_new_address(None, Some(json::AddressType::Bech32))
.unwrap()
.assume_checked();
psbt.unsigned_tx
.output
.push(TxOut { value: 99_999_000, script_pubkey: addr.script_pubkey() });
psbt.unsigned_tx.output.push(TxOut {
value: Amount::from_sat(99_999_000),
script_pubkey: addr.script_pubkey(),
});
let mut input = psbt::Input::default();
input.witness_utxo = Some(witness_utxo);
input.witness_script = Some(desc.explicit_script().unwrap());
Expand Down Expand Up @@ -163,16 +166,16 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
.map(|pk| sks[pks.iter().position(|&x| x == pk).unwrap()])
.collect();
// Get the required sighash message
let amt = btc(1).to_sat();
let amt = btc(1);
let mut sighash_cache = bitcoin::sighash::SighashCache::new(&psbts[i].unsigned_tx);
let sighash_ty = bitcoin::sighash::EcdsaSighashType::All;
let sighash = sighash_cache
.segwit_signature_hash(0, &ms.encode(), amt, sighash_ty)
.p2wsh_signature_hash(0, &ms.encode(), amt, sighash_ty)
.unwrap();

// requires both signing and verification because we check the tx
// after we psbt extract it
let msg = secp256k1::Message::from_slice(&sighash[..]).unwrap();
let msg = secp256k1::Message::from_digest(sighash.to_byte_array());

// Finally construct the signature and add to psbt
for sk in sks_reqd {
Expand Down
18 changes: 9 additions & 9 deletions bitcoind-tests/tests/test_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use bitcoin::psbt::Psbt;
use bitcoin::sighash::SighashCache;
use bitcoin::taproot::{LeafVersion, TapLeafHash};
use bitcoin::{
absolute, psbt, secp256k1, sighash, Amount, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid,
absolute, psbt, secp256k1, sighash, transaction, Amount, OutPoint, Sequence, Transaction, TxIn,
TxOut, Txid,
};
use bitcoind::bitcoincore_rpc::{json, Client, RpcApi};
use miniscript::bitcoin::{self, ecdsa, taproot, ScriptBuf};
Expand All @@ -28,7 +29,7 @@ use setup::test_util::{self, TestData};
fn btc<F: Into<f64>>(btc: F) -> Amount { Amount::from_btc(btc.into()).unwrap() }

// Find the Outpoint by spk
fn get_vout(cl: &Client, txid: Txid, value: u64, spk: ScriptBuf) -> (OutPoint, TxOut) {
fn get_vout(cl: &Client, txid: Txid, value: Amount, spk: ScriptBuf) -> (OutPoint, TxOut) {
let tx = cl
.get_transaction(&txid, None)
.unwrap()
Expand Down Expand Up @@ -102,7 +103,7 @@ pub fn test_desc_satisfy(
// Spend one input and spend one output for simplicity.
let mut psbt = Psbt {
unsigned_tx: Transaction {
version: 2,
version: transaction::Version::TWO,
lock_time: absolute::LockTime::from_time(1_603_866_330)
.expect("valid timestamp")
.into(), // 10/28/2020 @ 6:25am (UTC)
Expand All @@ -117,8 +118,7 @@ pub fn test_desc_satisfy(
outputs: vec![],
};
// figure out the outpoint from the txid
let (outpoint, witness_utxo) =
get_vout(&cl, txid, btc(1.0).to_sat(), derived_desc.script_pubkey());
let (outpoint, witness_utxo) = get_vout(&cl, txid, btc(1.0), derived_desc.script_pubkey());
let mut txin = TxIn::default();
txin.previous_output = outpoint;
// set the sequence to a non-final number for the locktime transactions to be
Expand All @@ -137,7 +137,7 @@ pub fn test_desc_satisfy(
// (Was getting insufficient fees error, for deep script trees)
psbt.unsigned_tx
.output
.push(TxOut { value: 99_997_000, script_pubkey: addr.script_pubkey() });
.push(TxOut { value: Amount::from_sat(99_997_000), script_pubkey: addr.script_pubkey() });
let mut input = psbt::Input::default();
input
.update_with_descriptor_unchecked(&definite_desc)
Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn test_desc_satisfy(
let sighash_msg = sighash_cache
.taproot_key_spend_signature_hash(0, &prevouts, hash_ty)
.unwrap();
let msg = secp256k1::Message::from_slice(&sighash_msg[..]).unwrap();
let msg = secp256k1::Message::from_digest(sighash_msg.to_byte_array());
let mut aux_rand = [0u8; 32];
rand::thread_rng().fill_bytes(&mut aux_rand);
let schnorr_sig =
Expand All @@ -183,7 +183,7 @@ pub fn test_desc_satisfy(
// No internal key
}
// ------------------ script spend -------------
let x_only_keypairs_reqd: Vec<(secp256k1::KeyPair, TapLeafHash)> = tr
let x_only_keypairs_reqd: Vec<(secp256k1::Keypair, TapLeafHash)> = tr
.iter_scripts()
.flat_map(|(_depth, ms)| {
let leaf_hash = TapLeafHash::from_script(&ms.encode(), LeafVersion::TapScript);
Expand All @@ -197,7 +197,7 @@ pub fn test_desc_satisfy(
let sighash_msg = sighash_cache
.taproot_script_spend_signature_hash(0, &prevouts, leaf_hash, hash_ty)
.unwrap();
let msg = secp256k1::Message::from_slice(&sighash_msg[..]).unwrap();
let msg = secp256k1::Message::from_digest(sighash_msg.to_byte_array());
let mut aux_rand = [0u8; 32];
rand::thread_rng().fill_bytes(&mut aux_rand);
let sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &aux_rand);
Expand Down
26 changes: 13 additions & 13 deletions examples/psbt_sign_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use miniscript::bitcoin::hashes::hex::FromHex;
use miniscript::bitcoin::psbt::{self, Psbt};
use miniscript::bitcoin::sighash::SighashCache;
use miniscript::bitcoin::{
self, base64, secp256k1, Address, Network, OutPoint, PrivateKey, Script, Sequence, Transaction,
TxIn, TxOut,
self, secp256k1, transaction, Address, Amount, Network, OutPoint, PrivateKey, Script, Sequence,
Transaction, TxIn, TxOut,
};
use miniscript::psbt::{PsbtExt, PsbtInputExt};
use miniscript::Descriptor;
Expand Down Expand Up @@ -52,7 +52,7 @@ fn main() {
println!("Backup3 public key: {}", _backup3_private.public_key(&secp256k1));

let spend_tx = Transaction {
version: 2,
version: transaction::Version::TWO,
lock_time: bitcoin::absolute::LockTime::from_consensus(5000),
input: vec![],
output: vec![],
Expand Down Expand Up @@ -86,13 +86,15 @@ fn main() {
txin.sequence = Sequence::from_height(26); //Sequence::MAX; //
psbt.unsigned_tx.input.push(txin);

psbt.unsigned_tx
.output
.push(TxOut { script_pubkey: receiver.script_pubkey(), value: amount / 5 - 500 });
psbt.unsigned_tx.output.push(TxOut {
script_pubkey: receiver.script_pubkey(),
value: Amount::from_sat(amount / 5 - 500),
});

psbt.unsigned_tx
.output
.push(TxOut { script_pubkey: bridge_descriptor.script_pubkey(), value: amount * 4 / 5 });
psbt.unsigned_tx.output.push(TxOut {
script_pubkey: bridge_descriptor.script_pubkey(),
value: Amount::from_sat(amount * 4 / 5),
});

// Generating signatures & witness data

Expand Down Expand Up @@ -133,14 +135,12 @@ fn main() {
.insert(pk1, bitcoin::ecdsa::Signature { sig: sig1, hash_ty: hash_ty });

println!("{:#?}", psbt);

let serialized = psbt.serialize();
println!("{}", base64::encode(&serialized));
println!("{}", psbt);

psbt.finalize_mut(&secp256k1).unwrap();
println!("{:#?}", psbt);

let tx = psbt.extract_tx();
let tx = psbt.extract_tx().expect("failed to extract tx");
println!("{}", bitcoin::consensus::encode::serialize_hex(&tx));
}

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

use bitcoin::blockdata::witness::Witness;
use bitcoin::{absolute, ecdsa, secp256k1, Sequence};
use bitcoin::{absolute, ecdsa, secp256k1, transaction, Amount, Sequence};

fn main() {
let mut tx = spending_transaction();
Expand Down Expand Up @@ -78,7 +78,7 @@ fn main() {
// Transaction which spends some output.
fn spending_transaction() -> bitcoin::Transaction {
bitcoin::Transaction {
version: 2,
version: transaction::Version::TWO,
lock_time: absolute::LockTime::ZERO,
input: vec![bitcoin::TxIn {
previous_output: Default::default(),
Expand All @@ -88,7 +88,7 @@ fn spending_transaction() -> bitcoin::Transaction {
}],
output: vec![bitcoin::TxOut {
script_pubkey: bitcoin::ScriptBuf::new(),
value: 100_000_000,
value: Amount::from_sat(100_000_000),
}],
}
}
Expand Down
7 changes: 3 additions & 4 deletions examples/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use std::collections::HashMap;
use std::str::FromStr;

use miniscript::bitcoin::address::WitnessVersion;
use miniscript::bitcoin::key::{KeyPair, XOnlyPublicKey};
use miniscript::bitcoin::key::{Keypair, XOnlyPublicKey};
use miniscript::bitcoin::secp256k1::rand;
use miniscript::bitcoin::Network;
use miniscript::bitcoin::{Network, WitnessVersion};
use miniscript::descriptor::DescriptorType;
use miniscript::policy::Concrete;
use miniscript::{translate_hash_fail, Descriptor, Miniscript, Tap, TranslatePk, Translator};
Expand Down Expand Up @@ -83,7 +82,7 @@ fn main() {

// We require secp for generating a random XOnlyPublicKey
let secp = secp256k1::Secp256k1::new();
let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
let key_pair = Keypair::new(&secp, &mut rand::thread_rng());
// Random unspendable XOnlyPublicKey provided for compilation to Taproot Descriptor
let (unspendable_pubkey, _parity) = XOnlyPublicKey::from_keypair(&key_pair);

Expand Down
2 changes: 1 addition & 1 deletion examples/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn main() {
// Same, but with the wrong signature hash, to demonstrate what happens
// given an apparently invalid script.
let secp = Secp256k1::new();
let message = secp256k1::Message::from_slice(&[0x01; 32][..]).expect("32-byte hash");
let message = secp256k1::Message::from_digest([0x01; 32]);

let iter = interpreter.iter_custom(Box::new(|key_sig: &KeySigPair| {
let (pk, ecdsa_sig) = key_sig.as_ecdsa().expect("Ecdsa Sig");
Expand Down
Loading

0 comments on commit ad2524b

Please sign in to comment.