Skip to content

update to bitcoin 0.30.0 #537

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 1 commit into from
May 4, 2023
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
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ rand = ["bitcoin/rand"]
base64 = ["bitcoin/base64"]

[dependencies]
bitcoin = { version = "0.29.1", default-features = false }
bitcoin = { version = "0.30.0", default-features = false }
hashbrown = { version = "0.11", optional = true }
internals = { package = "bitcoin-private", version = "0.1.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.29.2", features = ["base64"] }
secp256k1 = {version = "0.24.0", features = ["rand-std"]}
bitcoin = { version = "0.30.0", features = ["base64"] }
secp256k1 = {version = "0.27.0", features = ["rand-std"]}
actual-base64 = { package = "base64", version = "0.13.0" }

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

[dependencies]
miniscript = {path = "../"}
bitcoind = { version = "0.29.3" }
bitcoind = { version = "0.30.0" }
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 }
2 changes: 1 addition & 1 deletion bitcoind-tests/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn setup() -> BitcoinD {
let bitcoind = bitcoind::BitcoinD::new(exe_path).unwrap();
let cl = &bitcoind.client;
// generate to an address by the wallet. And wait for funds to mature
let addr = cl.get_new_address(None, None).unwrap();
let addr = cl.get_new_address(None, None).unwrap().assume_checked();
let blks = cl.generate_to_address(101, &addr).unwrap();
assert_eq!(blks.len(), 101);

Expand Down
40 changes: 25 additions & 15 deletions bitcoind-tests/tests/setup/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@
use std::str::FromStr;

use actual_rand as rand;
use bitcoin::hashes::hex::ToHex;
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
use bitcoin::secp256k1;
use internals::hex::exts::DisplayHex;
use miniscript::descriptor::{SinglePub, SinglePubKey};
use miniscript::{
bitcoin, hash256, Descriptor, DescriptorPublicKey, Error, Miniscript, ScriptContext,
TranslatePk, Translator,
};
use rand::RngCore;
use secp256k1::XOnlyPublicKey;

#[derive(Clone, Debug)]
pub struct PubData {
pub pks: Vec<bitcoin::PublicKey>,
pub x_only_pks: Vec<bitcoin::XOnlyPublicKey>,
pub x_only_pks: Vec<XOnlyPublicKey>,
pub sha256: sha256::Hash,
pub hash256: hash256::Hash,
pub ripemd160: ripemd160::Hash,
Expand All @@ -43,7 +44,7 @@ pub struct PubData {
#[derive(Debug, Clone)]
pub struct SecretData {
pub sks: Vec<bitcoin::secp256k1::SecretKey>,
pub x_only_keypairs: Vec<bitcoin::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 @@ -61,8 +62,8 @@ fn setup_keys(
) -> (
Vec<bitcoin::secp256k1::SecretKey>,
Vec<miniscript::bitcoin::PublicKey>,
Vec<bitcoin::KeyPair>,
Vec<bitcoin::XOnlyPublicKey>,
Vec<bitcoin::secp256k1::KeyPair>,
Vec<XOnlyPublicKey>,
) {
let secp_sign = secp256k1::Secp256k1::signing_only();
let mut sk = [0; 32];
Expand All @@ -86,8 +87,8 @@ fn setup_keys(
let mut x_only_pks = vec![];

for i in 0..n {
let keypair = bitcoin::KeyPair::from_secret_key(&secp_sign, &sks[i]);
let (xpk, _parity) = bitcoin::XOnlyPublicKey::from_keypair(&keypair);
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 Expand Up @@ -286,32 +287,41 @@ pub fn parse_test_desc(
fn subs_hash_frag(ms: &str, pubdata: &PubData) -> String {
let ms = ms.replace(
"sha256(H)",
&format!("sha256({})", &pubdata.sha256.to_hex()),
&format!("sha256({})", &pubdata.sha256.to_string()),
);
let ms = ms.replace(
"hash256(H)",
&format!("hash256({})", &pubdata.hash256.into_inner().to_hex()),
&format!("hash256({})", &pubdata.hash256.to_string()),
);
let ms = ms.replace(
"ripemd160(H)",
&format!("ripemd160({})", &pubdata.ripemd160.to_hex()),
&format!("ripemd160({})", &pubdata.ripemd160.to_string()),
);
let ms = ms.replace(
"hash160(H)",
&format!("hash160({})", &pubdata.hash160.to_hex()),
&format!("hash160({})", &pubdata.hash160.to_string()),
);

let mut rand_hash32 = [0u8; 32];
rand::thread_rng().fill_bytes(&mut rand_hash32);

let mut rand_hash20 = [0u8; 20];
rand::thread_rng().fill_bytes(&mut rand_hash20);
let ms = ms.replace("sha256(H!)", &format!("sha256({})", rand_hash32.to_hex()));
let ms = ms.replace("hash256(H!)", &format!("hash256({})", rand_hash32.to_hex()));
let ms = ms.replace(
"sha256(H!)",
&format!("sha256({})", rand_hash32.to_lower_hex_string()),
);
let ms = ms.replace(
"hash256(H!)",
&format!("hash256({})", rand_hash32.to_lower_hex_string()),
);
let ms = ms.replace(
"ripemd160(H!)",
&format!("ripemd160({})", rand_hash20.to_hex()),
&format!("ripemd160({})", rand_hash20.to_lower_hex_string()),
);
let ms = ms.replace(
"hash160(H!)",
&format!("hash160({})", rand_hash20.to_lower_hex_string()),
);
let ms = ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_hex()));
ms
}
34 changes: 22 additions & 12 deletions bitcoind-tests/tests/test_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use std::io::{self, BufRead};
use std::path::Path;

use bitcoin::hashes::{sha256d, Hash};
use bitcoin::psbt::Psbt;
use bitcoin::secp256k1::{self, Secp256k1};
use bitcoin::util::psbt;
use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;
use bitcoin::{Amount, LockTime, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid};
use bitcoin::{psbt, Amount, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid};
use bitcoind::bitcoincore_rpc::{json, Client, RpcApi};
use miniscript::bitcoin::absolute;
use miniscript::psbt::PsbtExt;
use miniscript::{bitcoin, Descriptor};

Expand Down Expand Up @@ -76,7 +76,10 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
let pks = &testdata.pubdata.pks;
// Generate some blocks
let blocks = cl
.generate_to_address(500, &cl.get_new_address(None, None).unwrap())
.generate_to_address(
500,
&cl.get_new_address(None, None).unwrap().assume_checked(),
)
.unwrap();
assert_eq!(blocks.len(), 500);

Expand All @@ -99,7 +102,10 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
}
// Wait for the funds to mature.
let blocks = cl
.generate_to_address(50, &cl.get_new_address(None, None).unwrap())
.generate_to_address(
50,
&cl.get_new_address(None, None).unwrap().assume_checked(),
)
.unwrap();
assert_eq!(blocks.len(), 50);
// Create a PSBT for each transaction.
Expand All @@ -109,7 +115,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
let mut psbt = Psbt {
unsigned_tx: Transaction {
version: 2,
lock_time: LockTime::from_time(1_603_866_330)
lock_time: absolute::LockTime::from_time(1_603_866_330)
.expect("valid timestamp")
.into(), // 10/28/2020 @ 6:25am (UTC)
input: vec![],
Expand All @@ -136,7 +142,8 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
// and we can check it by gettransaction RPC.
let addr = cl
.get_new_address(None, Some(json::AddressType::Bech32))
.unwrap();
.unwrap()
.assume_checked();
psbt.unsigned_tx.output.push(TxOut {
value: 99_999_000,
script_pubkey: addr.script_pubkey(),
Expand Down Expand Up @@ -168,8 +175,8 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
.collect();
// Get the required sighash message
let amt = btc(1).to_sat();
let mut sighash_cache = bitcoin::util::sighash::SighashCache::new(&psbts[i].unsigned_tx);
let sighash_ty = bitcoin::EcdsaSighashType::All;
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)
.unwrap();
Expand All @@ -184,7 +191,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
let pk = pks[sks.iter().position(|&x| x == sk).unwrap()];
psbts[i].inputs[0].partial_sigs.insert(
pk,
bitcoin::EcdsaSig {
bitcoin::ecdsa::Signature {
sig,
hash_ty: sighash_ty,
},
Expand All @@ -196,7 +203,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
testdata.secretdata.sha256_pre.to_vec(),
);
psbts[i].inputs[0].hash256_preimages.insert(
sha256d::Hash::from_inner(testdata.pubdata.hash256.into_inner()),
sha256d::Hash::from_byte_array(testdata.pubdata.hash256.to_byte_array()),
testdata.secretdata.hash256_pre.to_vec(),
);
println!("{}", ms);
Expand Down Expand Up @@ -227,7 +234,10 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
}
// Finally mine the blocks and await confirmations
let _blocks = cl
.generate_to_address(10, &cl.get_new_address(None, None).unwrap())
.generate_to_address(
10,
&cl.get_new_address(None, None).unwrap().assume_checked(),
)
.unwrap();
// Get the required transactions from the node mined in the blocks.
for txid in spend_txids {
Expand Down
44 changes: 21 additions & 23 deletions bitcoind-tests/tests/test_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ use std::{error, fmt};
use actual_rand as rand;
use bitcoin::blockdata::witness::Witness;
use bitcoin::hashes::{sha256d, Hash};
use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;
use bitcoin::util::sighash::SighashCache;
use bitcoin::util::taproot::{LeafVersion, TapLeafHash};
use bitcoin::util::{psbt, sighash};
use bitcoin::psbt::Psbt;
use bitcoin::sighash::SighashCache;
use bitcoin::taproot::{LeafVersion, TapLeafHash};
use bitcoin::{
secp256k1, Amount, LockTime, OutPoint, SchnorrSig, Script, Sequence, Transaction, TxIn, TxOut,
Txid,
absolute, psbt, secp256k1, sighash, Amount, OutPoint, Sequence, Transaction, TxIn, TxOut, Txid,
};
use bitcoind::bitcoincore_rpc::{json, Client, RpcApi};
use miniscript::bitcoin::{self, ecdsa, taproot, ScriptBuf};
use miniscript::psbt::{PsbtExt, PsbtInputExt};
use miniscript::{bitcoin, Descriptor, Miniscript, ScriptContext, ToPublicKey};
use miniscript::{Descriptor, Miniscript, ScriptContext, ToPublicKey};
mod setup;

use rand::RngCore;
Expand All @@ -31,7 +30,7 @@ fn btc<F: Into<f64>>(btc: F) -> Amount {
}

// Find the Outpoint by spk
fn get_vout(cl: &Client, txid: Txid, value: u64, spk: Script) -> (OutPoint, TxOut) {
fn get_vout(cl: &Client, txid: Txid, value: u64, spk: ScriptBuf) -> (OutPoint, TxOut) {
let tx = cl
.get_transaction(&txid, None)
.unwrap()
Expand Down Expand Up @@ -79,7 +78,7 @@ pub fn test_desc_satisfy(
let x_only_pks = &testdata.pubdata.x_only_pks;
// Generate some blocks
let blocks = cl
.generate_to_address(1, &cl.get_new_address(None, None).unwrap())
.generate_to_address(1, &cl.get_new_address(None, None).unwrap().assume_checked())
.unwrap();
assert_eq!(blocks.len(), 1);

Expand All @@ -98,15 +97,15 @@ pub fn test_desc_satisfy(
.unwrap();
// Wait for the funds to mature.
let blocks = cl
.generate_to_address(2, &cl.get_new_address(None, None).unwrap())
.generate_to_address(2, &cl.get_new_address(None, None).unwrap().assume_checked())
.unwrap();
assert_eq!(blocks.len(), 2);
// Create a PSBT for each transaction.
// Spend one input and spend one output for simplicity.
let mut psbt = Psbt {
unsigned_tx: Transaction {
version: 2,
lock_time: LockTime::from_time(1_603_866_330)
lock_time: absolute::LockTime::from_time(1_603_866_330)
.expect("valid timestamp")
.into(), // 10/28/2020 @ 6:25am (UTC)
input: vec![],
Expand Down Expand Up @@ -134,7 +133,8 @@ pub fn test_desc_satisfy(
// and we can check it by gettransaction RPC.
let addr = cl
.get_new_address(None, Some(json::AddressType::Bech32))
.unwrap();
.unwrap()
.assume_checked();
// Had to decrease 'value', so that fees can be increased
// (Was getting insufficient fees error, for deep script trees)
psbt.unsigned_tx.output.push(TxOut {
Expand All @@ -158,7 +158,7 @@ pub fn test_desc_satisfy(
match derived_desc {
Descriptor::Tr(ref tr) => {
// Fixme: take a parameter
let hash_ty = sighash::SchnorrSighashType::Default;
let hash_ty = sighash::TapSighashType::Default;

let internal_key_present = x_only_pks
.iter()
Expand All @@ -180,7 +180,7 @@ pub fn test_desc_satisfy(
rand::thread_rng().fill_bytes(&mut aux_rand);
let schnorr_sig =
secp.sign_schnorr_with_aux_rand(&msg, &internal_keypair, &aux_rand);
psbt.inputs[0].tap_key_sig = Some(SchnorrSig {
psbt.inputs[0].tap_key_sig = Some(taproot::Signature {
sig: schnorr_sig,
hash_ty: hash_ty,
});
Expand All @@ -206,13 +206,11 @@ pub fn test_desc_satisfy(
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);
// FIXME: uncomment when == is supported for secp256k1::KeyPair. (next major release)
// let x_only_pk = pks[xonly_keypairs.iter().position(|&x| x == keypair).unwrap()];
// Just recalc public key
let (x_only_pk, _parity) = secp256k1::XOnlyPublicKey::from_keypair(&keypair);
let x_only_pk =
x_only_pks[xonly_keypairs.iter().position(|&x| x == keypair).unwrap()];
psbt.inputs[0].tap_script_sigs.insert(
(x_only_pk, leaf_hash),
bitcoin::SchnorrSig {
taproot::Signature {
sig,
hash_ty: hash_ty,
},
Expand Down Expand Up @@ -258,7 +256,7 @@ pub fn test_desc_satisfy(
.to_secp_msg();

// Fixme: Take a parameter
let hash_ty = bitcoin::EcdsaSighashType::All;
let hash_ty = sighash::EcdsaSighashType::All;

// Finally construct the signature and add to psbt
for sk in sks_reqd {
Expand All @@ -267,7 +265,7 @@ pub fn test_desc_satisfy(
assert!(secp.verify_ecdsa(&msg, &sig, &pk.inner).is_ok());
psbt.inputs[0].partial_sigs.insert(
pk,
bitcoin::EcdsaSig {
ecdsa::Signature {
sig,
hash_ty: hash_ty,
},
Expand All @@ -281,7 +279,7 @@ pub fn test_desc_satisfy(
testdata.secretdata.sha256_pre.to_vec(),
);
psbt.inputs[0].hash256_preimages.insert(
sha256d::Hash::from_inner(testdata.pubdata.hash256.into_inner()),
sha256d::Hash::from_byte_array(testdata.pubdata.hash256.to_byte_array()),
testdata.secretdata.hash256_pre.to_vec(),
);
psbt.inputs[0].hash160_preimages.insert(
Expand Down Expand Up @@ -309,7 +307,7 @@ pub fn test_desc_satisfy(

// Finally mine the blocks and await confirmations
let _blocks = cl
.generate_to_address(1, &cl.get_new_address(None, None).unwrap())
.generate_to_address(1, &cl.get_new_address(None, None).unwrap().assume_checked())
.unwrap();
// Get the required transactions from the node mined in the blocks.
// Check whether the transaction is mined in blocks
Expand Down
Loading