Skip to content

Commit

Permalink
Depend on bitcoin v0.32.0-rc1
Browse files Browse the repository at this point in the history
Test the latest bitcoin release candidate. Includes bumping the version
numbers so we can use this branch to test crates further up the stack.
  • Loading branch information
tcharding committed Apr 10, 2024
1 parent 984c758 commit 5f6d477
Show file tree
Hide file tree
Showing 41 changed files with 532 additions and 277 deletions.
52 changes: 52 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,55 @@ members = [

[workspace.package]
authors = ["Bitcoin Dev Kit Developers"]

[patch.crates-io.electrsd]
git = "https://github.com/tcharding/electrsd"
branch = "test-bitcoin"

[patch.crates-io.esplora-client]
git = "https://github.com/tcharding/rust-esplora-client"
branch = "test-bitcoin"

[patch.crates-io.electrum-client]
git = "https://github.com/tcharding/rust-electrum-client"
branch = "test-bitcoin"

[patch.crates-io.hwi]
git = "https://github.com/tcharding/rust-hwi"
branch = "test-bitcoin"

[patch.crates-io.miniscript]
git = "https://github.com/tcharding/rust-miniscript"
branch = "test-bitcoin"

[patch.crates-io.bitcoind]
git = "https://github.com/tcharding/bitcoind/"
branch = "test-bitcoin"

[patch.crates-io.bitcoincore-rpc]
git = "https://github.com/tcharding/rust-bitcoincore-rpc"
branch = "test-bitcoin"

[patch.crates-io.base58ck]
git = "https://github.com/rust-bitcoin/rust-bitcoin"
branch = "rc1-fixes"

[patch.crates-io.bitcoin]
git = "https://github.com/rust-bitcoin/rust-bitcoin"
branch = "rc1-fixes"

[patch.crates-io.bitcoin_hashes]
git = "https://github.com/rust-bitcoin/rust-bitcoin"
branch = "rc1-fixes"

[patch.crates-io.bitcoin-internals]
git = "https://github.com/rust-bitcoin/rust-bitcoin"
branch = "rc1-fixes"

[patch.crates-io.bitcoin-io]
git = "https://github.com/rust-bitcoin/rust-bitcoin"
branch = "rc1-fixes"

[patch.crates-io.bitcoin-units]
git = "https://github.com/rust-bitcoin/rust-bitcoin"
branch = "rc1-fixes"
6 changes: 3 additions & 3 deletions crates/bdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ rust-version = "1.63"

[dependencies]
rand = "^0.8"
miniscript = { version = "11.0.0", features = ["serde"], default-features = false }
bitcoin = { version = "0.31.0", features = ["serde", "base64", "rand-std"], default-features = false }
miniscript = { version = "12.0.0", features = ["serde"], default-features = false }
bitcoin = { version = "0.32.0-rc1", features = ["serde", "base64", "rand-std"], default-features = false }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
bdk_chain = { path = "../chain", version = "0.11.0", features = ["miniscript", "serde"], default-features = false }
bdk_chain = { path = "../chain", features = ["miniscript", "serde"], default-features = false }

# Optional dependencies
bip39 = { version = "2.0", optional = true }
Expand Down
16 changes: 12 additions & 4 deletions crates/bdk/src/descriptor/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,10 @@ macro_rules! fragment {
$crate::keys::make_pkh($key, &secp)
});
( after ( $value:expr ) ) => ({
$crate::impl_leaf_opcode_value!(After, $crate::miniscript::AbsLockTime::from_consensus($value))
$crate::impl_leaf_opcode_value!(After, $crate::miniscript::AbsLockTime::from_consensus($value).expect("TODO: Handle error"))
});
( older ( $value:expr ) ) => ({
$crate::impl_leaf_opcode_value!(Older, $crate::bitcoin::Sequence($value)) // TODO!!
$crate::impl_leaf_opcode_value!(Older, $crate::miniscript::RelLockTime::from_consensus($value).expect("TODO: Handle error"))
});
( sha256 ( $hash:expr ) ) => ({
$crate::impl_leaf_opcode_value!(Sha256, $hash)
Expand Down Expand Up @@ -768,7 +768,11 @@ macro_rules! fragment {
( multi_vec ( $thresh:expr, $keys:expr ) ) => ({
let secp = $crate::bitcoin::secp256k1::Secp256k1::new();

$crate::keys::make_multi($thresh, $crate::miniscript::Terminal::Multi, $keys, &secp)
let fun = |k, pks| {
let thresh = $crate::miniscript::Threshold::new(k, pks).expect("TODO: Handle this error");
$crate::miniscript::Terminal::Multi(thresh)
};
$crate::keys::make_multi($thresh, fun, $keys, &secp)
});
( multi ( $thresh:expr $(, $key:expr )+ ) ) => ({
$crate::group_multi_keys!( $( $key ),* )
Expand All @@ -777,7 +781,11 @@ macro_rules! fragment {
( multi_a_vec ( $thresh:expr, $keys:expr ) ) => ({
let secp = $crate::bitcoin::secp256k1::Secp256k1::new();

$crate::keys::make_multi($thresh, $crate::miniscript::Terminal::MultiA, $keys, &secp)
let fun = |k, pks| {
let thresh = $crate::miniscript::Threshold::new(k, pks).expect("TODO: Handle this error");
$crate::miniscript::Terminal::MultiA(thresh)
};
$crate::keys::make_multi($thresh, fun, $keys, &secp)
});
( multi_a ( $thresh:expr $(, $key:expr )+ ) ) => ({
$crate::group_multi_keys!( $( $key ),* )
Expand Down
6 changes: 3 additions & 3 deletions crates/bdk/src/descriptor/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum Error {
/// Error during base58 decoding
Base58(bitcoin::base58::Error),
/// Key-related error
Pk(bitcoin::key::Error),
Pk(bitcoin::key::ParsePublicKeyError),
/// Miniscript error
Miniscript(miniscript::Error),
/// Hex decoding error
Expand Down Expand Up @@ -98,8 +98,8 @@ impl From<bitcoin::base58::Error> for Error {
}
}

impl From<bitcoin::key::Error> for Error {
fn from(err: bitcoin::key::Error) -> Self {
impl From<bitcoin::key::ParsePublicKeyError> for Error {
fn from(err: bitcoin::key::ParsePublicKeyError) -> Self {
Error::Pk(err)
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/bdk/src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl IntoWalletDescriptor for DescriptorTemplateOut {
let pk = match pk {
DescriptorPublicKey::XPub(ref xpub) => {
let mut xpub = xpub.clone();
xpub.xkey.network = self.network;
xpub.xkey.network = self.network.into();

DescriptorPublicKey::XPub(xpub)
}
Expand Down Expand Up @@ -264,11 +264,11 @@ impl IntoWalletDescriptor for DescriptorTemplateOut {
.map(|(mut k, mut v)| {
match (&mut k, &mut v) {
(DescriptorPublicKey::XPub(xpub), DescriptorSecretKey::XPrv(xprv)) => {
xpub.xkey.network = network;
xprv.xkey.network = network;
xpub.xkey.network = network.into();
xprv.xkey.network = network.into();
}
(_, DescriptorSecretKey::Single(key)) => {
key.key.network = network;
key.key.network = network.into();
}
_ => {}
}
Expand Down Expand Up @@ -606,8 +606,8 @@ mod test {
use assert_matches::assert_matches;
use bitcoin::hex::FromHex;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::ScriptBuf;
use bitcoin::{bip32, Psbt};
use bitcoin::{NetworkKind, ScriptBuf};

use super::*;
use crate::psbt::PsbtUtils;
Expand Down Expand Up @@ -743,7 +743,7 @@ mod test {
.unwrap();

let mut xprv_testnet = xprv;
xprv_testnet.network = Network::Testnet;
xprv_testnet.network = NetworkKind::Test;

let xpub_testnet = bip32::Xpub::from_priv(&secp, &xprv_testnet);
let desc_pubkey = DescriptorPublicKey::XPub(DescriptorXKey {
Expand Down
104 changes: 79 additions & 25 deletions crates/bdk/src/descriptor/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ use miniscript::descriptor::{
DescriptorPublicKey, ShInner, SinglePub, SinglePubKey, SortedMultiVec, WshInner,
};
use miniscript::hash256;
use miniscript::miniscript::limits::{MAX_PUBKEYS_IN_CHECKSIGADD, MAX_PUBKEYS_PER_MULTISIG};
use miniscript::{
Descriptor, Miniscript, Satisfier, ScriptContext, SigType, Terminal, ToPublicKey,
Descriptor, Miniscript, Satisfier, ScriptContext, SigType, Terminal, Threshold, ToPublicKey,
};

use crate::descriptor::ExtractPolicy;
Expand Down Expand Up @@ -586,30 +587,78 @@ impl Policy {
Ok(Some(policy))
}

fn make_multisig<Ctx: ScriptContext + 'static>(
keys: &[DescriptorPublicKey],
fn make_multi<Ctx: ScriptContext + 'static>(
threshold: &Threshold<DescriptorPublicKey, MAX_PUBKEYS_PER_MULTISIG>,
signers: &SignersContainer,
build_sat: BuildSatisfaction,
threshold: usize,
sorted: bool,
secp: &SecpCtx,
) -> Result<Option<Policy>, PolicyError> {
if threshold == 0 {
return Ok(None);
let parsed_keys = threshold.iter().map(|k| PkOrF::from_key(k, secp)).collect();

let mut contribution = Satisfaction::Partial {
n: threshold.n(),
m: threshold.k(),
items: vec![],
conditions: Default::default(),
sorted: Some(sorted),
};
let mut satisfaction = contribution.clone();

for (index, key) in threshold.iter().enumerate() {
if signers.find(signer_id(key, secp)).is_some() {
contribution.add(
&Satisfaction::Complete {
condition: Default::default(),
},
index,
)?;
}

if let Some(psbt) = build_sat.psbt() {
if Ctx::find_signature(psbt, key, secp) {
satisfaction.add(
&Satisfaction::Complete {
condition: Default::default(),
},
index,
)?;
}
}
}
satisfaction.finalize();
contribution.finalize();

let mut policy: Policy = SatisfiableItem::Multisig {
keys: parsed_keys,
threshold: threshold.k(),
}
.into();
policy.contribution = contribution;
policy.satisfaction = satisfaction;

Ok(Some(policy))
}

let parsed_keys = keys.iter().map(|k| PkOrF::from_key(k, secp)).collect();
fn make_multi_a<Ctx: ScriptContext + 'static>(
threshold: &Threshold<DescriptorPublicKey, MAX_PUBKEYS_IN_CHECKSIGADD>,
signers: &SignersContainer,
build_sat: BuildSatisfaction,
sorted: bool,
secp: &SecpCtx,
) -> Result<Option<Policy>, PolicyError> {
let parsed_keys = threshold.iter().map(|k| PkOrF::from_key(k, secp)).collect();

let mut contribution = Satisfaction::Partial {
n: keys.len(),
m: threshold,
n: threshold.n(),
m: threshold.k(),
items: vec![],
conditions: Default::default(),
sorted: Some(sorted),
};
let mut satisfaction = contribution.clone();

for (index, key) in keys.iter().enumerate() {
for (index, key) in threshold.iter().enumerate() {
if signers.find(signer_id(key, secp)).is_some() {
contribution.add(
&Satisfaction::Complete {
Expand All @@ -635,7 +684,7 @@ impl Policy {

let mut policy: Policy = SatisfiableItem::Multisig {
keys: parsed_keys,
threshold,
threshold: threshold.k(),
}
.into();
policy.contribution = contribution;
Expand Down Expand Up @@ -952,11 +1001,14 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
Some(policy)
}
Terminal::Older(value) => {
let mut policy: Policy = SatisfiableItem::RelativeTimelock { value: *value }.into();
let mut policy: Policy = SatisfiableItem::RelativeTimelock {
value: ((*value).into()),
}
.into();
policy.contribution = Satisfaction::Complete {
condition: Condition {
timelock: None,
csv: Some(*value),
csv: Some(Sequence::from(*value)),
},
};
if let BuildSatisfaction::PsbtTimelocks {
Expand All @@ -966,9 +1018,11 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
} = build_sat
{
let older = Older::new(Some(current_height), Some(input_max_height), false);
let older_sat = Satisfier::<bitcoin::PublicKey>::check_older(&older, *value);
let inputs_sat = psbt_inputs_sat(psbt)
.all(|sat| Satisfier::<bitcoin::PublicKey>::check_older(&sat, *value));
let older_sat =
Satisfier::<bitcoin::PublicKey>::check_older(&older, (*value).into());
let inputs_sat = psbt_inputs_sat(psbt).all(|sat| {
Satisfier::<bitcoin::PublicKey>::check_older(&sat, (*value).into())
});
if older_sat && inputs_sat {
policy.satisfaction = policy.contribution.clone();
}
Expand All @@ -986,8 +1040,11 @@ impl<Ctx: ScriptContext + 'static> ExtractPolicy for Miniscript<DescriptorPublic
Terminal::Hash160(hash) => {
Some(SatisfiableItem::Hash160Preimage { hash: *hash }.into())
}
Terminal::Multi(k, pks) | Terminal::MultiA(k, pks) => {
Policy::make_multisig::<Ctx>(pks, signers, build_sat, *k, false, secp)?
Terminal::Multi(ref thresh) => {
Policy::make_multi::<Ctx>(thresh, signers, build_sat, false, secp)?
}
Terminal::MultiA(ref thresh) => {
Policy::make_multi_a::<Ctx>(thresh, signers, build_sat, false, secp)?
}
// Identities
Terminal::Alt(inner)
Expand Down Expand Up @@ -1087,13 +1144,10 @@ impl ExtractPolicy for Descriptor<DescriptorPublicKey> {
build_sat: BuildSatisfaction,
secp: &SecpCtx,
) -> Result<Option<Policy>, Error> {
Ok(Policy::make_multisig::<Ctx>(
keys.pks.as_ref(),
signers,
build_sat,
keys.k,
true,
secp,
let thresh =
Threshold::new(keys.k(), keys.pks().to_vec()).expect("TODO: Handle this error");
Ok(Policy::make_multi::<Ctx>(
&thresh, signers, build_sat, true, secp,
)?)
}

Expand Down
5 changes: 3 additions & 2 deletions crates/bdk/src/descriptor/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ mod test {
use crate::descriptor::{DescriptorError, DescriptorMeta};
use crate::keys::ValidNetworks;
use assert_matches::assert_matches;
use bitcoin::NetworkKind;
use miniscript::descriptor::{DescriptorPublicKey, KeyMap};
use miniscript::Descriptor;

Expand All @@ -568,7 +569,7 @@ mod test {
use bitcoin::bip32::ChildNumber::{self, Hardened};

let xprvkey = bitcoin::bip32::Xpriv::from_str("xprv9s21ZrQH143K2fpbqApQL69a4oKdGVnVN52R82Ft7d1pSqgKmajF62acJo3aMszZb6qQ22QsVECSFxvf9uyxFUvFYQMq3QbtwtRSMjLAhMf").unwrap();
assert_eq!(Network::Bitcoin, xprvkey.network);
assert_eq!(NetworkKind::Main, xprvkey.network);
let xdesc = Bip44(xprvkey, KeychainKind::Internal)
.build(Network::Bitcoin)
.unwrap();
Expand All @@ -582,7 +583,7 @@ mod test {
}

let tprvkey = bitcoin::bip32::Xpriv::from_str("tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy").unwrap();
assert_eq!(Network::Testnet, tprvkey.network);
assert_eq!(NetworkKind::Test, tprvkey.network);
let tdesc = Bip44(tprvkey, KeychainKind::Internal)
.build(Network::Testnet)
.unwrap();
Expand Down
Loading

0 comments on commit 5f6d477

Please sign in to comment.