Skip to content

Commit feb8814

Browse files
committed
deps: bump miniscript to v13
1 parent f9567ad commit feb8814

11 files changed

Lines changed: 173 additions & 119 deletions

File tree

Cargo.toml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1919
[dependencies]
2020
bdk_chain = { version = "0.23.1", features = ["miniscript", "serde"], default-features = false }
2121
bitcoin = { version = "0.32.7", features = ["serde", "base64"], default-features = false }
22-
miniscript = { version = "12.3.1", features = ["serde"], default-features = false }
22+
miniscript = { version = "13.0.0", features = ["serde"], default-features = false }
2323
rand_core = { version = "0.6.0" }
2424
serde_json = { version = "1" }
2525
serde = { version = "1", features = ["derive"] }
@@ -78,24 +78,30 @@ name = "bitcoind_rpc"
7878

7979
[patch.crates-io.bdk_chain]
8080
git = "https://github.com/bitcoindevkit/bdk"
81-
rev = "b4c8c6a110de14c1b04d2a525cd8fb3e4d67cec6"
81+
branch = "master"
82+
# rev = "b45ecb9bd41c80a03921dcf42a9845b19774502e"
8283

8384
[patch.crates-io.bdk_core]
8485
git = "https://github.com/bitcoindevkit/bdk"
85-
rev = "b4c8c6a110de14c1b04d2a525cd8fb3e4d67cec6"
86+
branch = "master"
87+
# rev = "b45ecb9bd41c80a03921dcf42a9845b19774502e"
8688

8789
[patch.crates-io.bdk_bitcoind_rpc]
8890
git = "https://github.com/bitcoindevkit/bdk"
89-
rev = "b4c8c6a110de14c1b04d2a525cd8fb3e4d67cec6"
91+
branch = "master"
92+
# rev = "b45ecb9bd41c80a03921dcf42a9845b19774502e"
9093

9194
[patch.crates-io.bdk_electrum]
9295
git = "https://github.com/bitcoindevkit/bdk"
93-
rev = "b4c8c6a110de14c1b04d2a525cd8fb3e4d67cec6"
96+
branch = "master"
97+
# rev = "b45ecb9bd41c80a03921dcf42a9845b19774502e"
9498

9599
[patch.crates-io.bdk_esplora]
96100
git = "https://github.com/bitcoindevkit/bdk"
97-
rev = "b4c8c6a110de14c1b04d2a525cd8fb3e4d67cec6"
101+
branch = "master"
102+
# rev = "b45ecb9bd41c80a03921dcf42a9845b19774502e"
98103

99104
[patch.crates-io.bdk_file_store]
100105
git = "https://github.com/bitcoindevkit/bdk"
101-
rev = "b4c8c6a110de14c1b04d2a525cd8fb3e4d67cec6"
106+
branch = "master"
107+
# rev = "b45ecb9bd41c80a03921dcf42a9845b19774502e"

src/descriptor/checksum.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,31 @@
1717
use crate::descriptor::DescriptorError;
1818
use alloc::string::String;
1919

20-
use miniscript::descriptor::checksum::desc_checksum;
20+
use miniscript::descriptor::checksum::Engine;
2121

2222
/// Compute the checksum of a descriptor, excludes any existing checksum in the descriptor string
2323
/// from the calculation
2424
pub fn calc_checksum(desc: &str) -> Result<String, DescriptorError> {
25-
if let Some(split) = desc.split_once('#') {
26-
let og_checksum = split.1;
27-
let checksum = desc_checksum(split.0)?;
28-
if og_checksum != checksum {
25+
if let Some((descriptor, original_checksum)) = desc.split_once('#') {
26+
let mut engine = Engine::new();
27+
engine.input(descriptor)?;
28+
let checksum = engine.checksum();
29+
if original_checksum != checksum {
2930
return Err(DescriptorError::InvalidDescriptorChecksum);
3031
}
3132
Ok(checksum)
3233
} else {
33-
Ok(desc_checksum(desc)?)
34+
let mut engine = Engine::new();
35+
engine.input(desc)?;
36+
Ok(engine.checksum())
3437
}
3538
}
3639

3740
#[cfg(test)]
3841
mod test {
3942
use super::*;
4043
use crate::descriptor::calc_checksum;
44+
use alloc::string::ToString;
4145
use assert_matches::assert_matches;
4246

4347
// test calc_checksum() function; it should return the same value as Bitcoin Core
@@ -80,7 +84,17 @@ mod test {
8084

8185
assert_matches!(
8286
calc_checksum(&invalid_desc),
83-
Err(DescriptorError::Miniscript(miniscript::Error::BadDescriptor(e))) if e == format!("Invalid character in checksum: '{sparkle_heart}'")
87+
Err(DescriptorError::Miniscript(
88+
miniscript::Error::Parse(
89+
miniscript::ParseError::Tree(
90+
miniscript::ParseTreeError::Checksum(
91+
miniscript::descriptor::checksum::Error::InvalidCharacter { ch, pos })
92+
)
93+
)
94+
)
95+
)
96+
if ch.to_string() == sparkle_heart
97+
&& pos == 85
8498
);
8599
}
86100
}

src/descriptor/dsl.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ macro_rules! impl_top_level_sh {
3030
use $crate::miniscript::$ctx;
3131

3232
let build_desc = |k, pks| {
33-
Ok((Descriptor::<DescriptorPublicKey>::$inner_struct($inner_struct::$sortedmulti_constructor(k, pks)?), PhantomData::<$ctx>))
33+
let thresh = miniscript::Threshold::new(k, pks)?;
34+
Ok((Descriptor::<DescriptorPublicKey>::$inner_struct($inner_struct::$sortedmulti_constructor(thresh)?), PhantomData::<$ctx>))
3435
};
3536

3637
$crate::impl_sortedmulti!(build_desc, sortedmulti $( $inner )*)
@@ -42,7 +43,8 @@ macro_rules! impl_top_level_sh {
4243
use $crate::miniscript::$ctx;
4344

4445
let build_desc = |k, pks| {
45-
Ok((Descriptor::<DescriptorPublicKey>::$inner_struct($inner_struct::$sortedmulti_constructor(k, pks)?), PhantomData::<$ctx>))
46+
let thresh = miniscript::Threshold::new(k, pks)?;
47+
Ok((Descriptor::<DescriptorPublicKey>::$inner_struct($inner_struct::$sortedmulti_constructor(thresh)?), PhantomData::<$ctx>))
4648
};
4749

4850
$crate::impl_sortedmulti!(build_desc, sortedmulti_vec $( $inner )*)
@@ -294,7 +296,7 @@ macro_rules! parse_tap_tree {
294296
.and_then(|tree_a| Ok((tree_a, $tree_b?)))
295297
.and_then(|((a_tree, mut a_keymap, a_network_kinds), (b_tree, b_keymap, b_network_kinds))| {
296298
a_keymap.extend(b_keymap.into_iter());
297-
Ok((TapTree::combine(a_tree, b_tree), a_keymap, $crate::keys::intersect_network_kinds(&a_network_kinds, &b_network_kinds)))
299+
Ok((TapTree::combine(a_tree, b_tree)?, a_keymap, $crate::keys::intersect_network_kinds(&a_network_kinds, &b_network_kinds)))
298300
})
299301

300302
}};
@@ -331,11 +333,10 @@ macro_rules! parse_tap_tree {
331333

332334
// Single leaf
333335
( $op:ident ( $( $minisc:tt )* ) ) => {{
334-
use $crate::alloc::sync::Arc;
335336
use $crate::miniscript::descriptor::TapTree;
336337

337338
$crate::fragment!( $op ( $( $minisc )* ) )
338-
.map(|(a_minisc, a_keymap, a_network_kinds)| (TapTree::Leaf(Arc::new(a_minisc)), a_keymap, a_network_kinds))
339+
.map(|(a_minisc, a_keymap, a_network_kinds)| (TapTree::leaf(a_minisc), a_keymap, a_network_kinds))
339340
}};
340341
}
341342

@@ -721,10 +722,14 @@ macro_rules! fragment {
721722
$crate::keys::make_pkh($key, &secp)
722723
});
723724
( after ( $value:expr ) ) => ({
724-
$crate::impl_leaf_opcode_value!(After, $crate::miniscript::AbsLockTime::from_consensus($value).expect("valid `AbsLockTime`"))
725+
$crate::miniscript::AbsLockTime::from_consensus($value)
726+
.map_err($crate::descriptor::DescriptorError::from)
727+
.and_then(|abs_lt| $crate::impl_leaf_opcode_value!(After, abs_lt))
725728
});
726729
( older ( $value:expr ) ) => ({
727-
$crate::impl_leaf_opcode_value!(Older, $crate::miniscript::RelLockTime::from_consensus($value).expect("valid `RelLockTime`")) // TODO!!
730+
$crate::miniscript::RelLockTime::from_consensus($value)
731+
.map_err($crate::descriptor::DescriptorError::from)
732+
.and_then(|rel_lt| $crate::impl_leaf_opcode_value!(Older, rel_lt))
728733
});
729734
( sha256 ( $hash:expr ) ) => ({
730735
$crate::impl_leaf_opcode_value!(Sha256, $hash)
@@ -775,9 +780,12 @@ macro_rules! fragment {
775780
(keys_acc, net_acc)
776781
});
777782

778-
let thresh = $crate::miniscript::Threshold::new($thresh, items).expect("valid threshold and pks collection");
779-
$crate::impl_leaf_opcode_value!(Thresh, thresh)
780-
.map(|(minisc, _, _)| (minisc, key_maps, valid_network_kinds))
783+
$crate::miniscript::Threshold::new($thresh, items)
784+
.map_err($crate::descriptor::DescriptorError::from)
785+
.and_then(|thresh| {
786+
$crate::impl_leaf_opcode_value!(Thresh, thresh)
787+
.map(|(minisc, _, _)| (minisc, key_maps, valid_network_kinds))
788+
})
781789
});
782790
( thresh ( $thresh:expr, $( $inner:tt )* ) ) => ({
783791
let items = $crate::fragment_internal!( @v $( $inner )* );
@@ -789,8 +797,8 @@ macro_rules! fragment {
789797
let secp = $crate::bitcoin::secp256k1::Secp256k1::new();
790798

791799
let fun = |k, pks| {
792-
let thresh = $crate::miniscript::Threshold::new(k, pks).expect("valid threshold and pks collection");
793-
$crate::miniscript::Terminal::Multi(thresh)
800+
let thresh = $crate::miniscript::Threshold::new(k, pks)?;
801+
Ok($crate::miniscript::Terminal::Multi(thresh))
794802
};
795803

796804
$crate::keys::make_multi($thresh, fun, $keys, &secp)
@@ -803,8 +811,8 @@ macro_rules! fragment {
803811
let secp = $crate::bitcoin::secp256k1::Secp256k1::new();
804812

805813
let fun = |k, pks| {
806-
let thresh = $crate::miniscript::Threshold::new(k, pks).expect("valid threshold and pks collection");
807-
$crate::miniscript::Terminal::MultiA(thresh)
814+
let thresh = $crate::miniscript::Threshold::new(k, pks)?;
815+
Ok($crate::miniscript::Terminal::MultiA(thresh))
808816
};
809817

810818
$crate::keys::make_multi($thresh, fun, $keys, &secp)

src/descriptor/error.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use core::fmt;
1414

1515
/// Errors related to the parsing and usage of descriptors
16-
#[derive(Debug, PartialEq)]
16+
#[derive(Debug)]
1717
pub enum Error {
1818
/// Invalid HD Key path, such as having a wildcard but a length != 1
1919
InvalidHdKeyPath,
@@ -126,3 +126,35 @@ impl From<crate::descriptor::policy::PolicyError> for Error {
126126
Error::Policy(err)
127127
}
128128
}
129+
130+
impl From<miniscript::descriptor::checksum::Error> for Error {
131+
fn from(e: miniscript::descriptor::checksum::Error) -> Self {
132+
Self::Miniscript(miniscript::Error::Parse(miniscript::ParseError::Tree(
133+
miniscript::ParseTreeError::Checksum(e),
134+
)))
135+
}
136+
}
137+
138+
impl From<miniscript::descriptor::TapTreeDepthError> for Error {
139+
fn from(e: miniscript::descriptor::TapTreeDepthError) -> Self {
140+
Self::Miniscript(miniscript::Error::TapTreeDepthError(e))
141+
}
142+
}
143+
144+
impl From<miniscript::ThresholdError> for Error {
145+
fn from(e: miniscript::ThresholdError) -> Self {
146+
Self::Miniscript(miniscript::Error::Threshold(e))
147+
}
148+
}
149+
150+
impl From<miniscript::AbsLockTimeError> for Error {
151+
fn from(e: miniscript::AbsLockTimeError) -> Self {
152+
Self::Miniscript(miniscript::Error::AbsoluteLockTime(e))
153+
}
154+
}
155+
156+
impl From<miniscript::RelLockTimeError> for Error {
157+
fn from(e: miniscript::RelLockTimeError) -> Self {
158+
Self::Miniscript(miniscript::Error::RelativeLockTime(e))
159+
}
160+
}

src/descriptor/mod.rs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use miniscript::descriptor::{
3232
pub use miniscript::{
3333
Descriptor, DescriptorPublicKey, Legacy, Miniscript, ScriptContext, Segwitv0,
3434
};
35-
use miniscript::{ForEachKey, MiniscriptKey, TranslatePk};
35+
use miniscript::{ForEachKey, MiniscriptKey};
3636

3737
use crate::descriptor::policy::BuildSatisfaction;
3838

@@ -147,8 +147,11 @@ impl IntoWalletDescriptor for (ExtendedDescriptor, KeyMap) {
147147
network_kind: NetworkKind,
148148
}
149149

150-
impl miniscript::Translator<DescriptorPublicKey, String, DescriptorError> for Translator<'_, '_> {
151-
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<String, DescriptorError> {
150+
impl miniscript::Translator<DescriptorPublicKey> for Translator<'_, '_> {
151+
type TargetPk = String;
152+
type Error = DescriptorError;
153+
154+
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<Self::TargetPk, Self::Error> {
152155
let secp = &self.secp;
153156

154157
let (_, _, network_kinds) = if self.descriptor.is_taproot() {
@@ -223,9 +226,10 @@ impl IntoWalletDescriptor for DescriptorTemplateOut {
223226
network_kind: NetworkKind,
224227
}
225228

226-
impl miniscript::Translator<DescriptorPublicKey, DescriptorPublicKey, DescriptorError>
227-
for Translator
228-
{
229+
impl miniscript::Translator<DescriptorPublicKey> for Translator {
230+
type TargetPk = DescriptorPublicKey;
231+
type Error = DescriptorError;
232+
229233
fn pk(
230234
&mut self,
231235
pk: &DescriptorPublicKey,
@@ -269,23 +273,22 @@ impl IntoWalletDescriptor for DescriptorTemplateOut {
269273
Err(TranslateErr::OuterError(e)) => return Err(e.into()),
270274
};
271275
// ...and in the key map.
272-
let fixed_keymap = keymap
273-
.into_iter()
274-
.map(|(mut k, mut v)| {
275-
match (&mut k, &mut v) {
276-
(DescriptorPublicKey::XPub(xpub), DescriptorSecretKey::XPrv(xprv)) => {
277-
xpub.xkey.network = network_kind;
278-
xprv.xkey.network = network_kind;
279-
}
280-
(_, DescriptorSecretKey::Single(key)) => {
281-
key.key.network = network_kind;
282-
}
283-
_ => {}
276+
let inner = keymap.into_iter().map(|(mut k, mut v)| {
277+
match (&mut k, &mut v) {
278+
(DescriptorPublicKey::XPub(xpub), DescriptorSecretKey::XPrv(xprv)) => {
279+
xpub.xkey.network = network_kind;
280+
xprv.xkey.network = network_kind;
281+
}
282+
(_, DescriptorSecretKey::Single(key)) => {
283+
key.key.network = network_kind;
284284
}
285+
_ => {}
286+
}
285287

286-
(k, v)
287-
})
288-
.collect();
288+
(k, v)
289+
});
290+
let mut fixed_keymap = KeyMap::new();
291+
fixed_keymap.extend(inner);
289292

290293
Ok((translated, fixed_keymap))
291294
}
@@ -314,11 +317,9 @@ pub(crate) fn check_wallet_descriptor(
314317
}
315318

316319
if descriptor.is_multipath() {
317-
return Err(DescriptorError::Miniscript(
318-
miniscript::Error::BadDescriptor(
319-
"`check_wallet_descriptor` must not contain multipath keys".to_string(),
320-
),
321-
));
320+
return Err(DescriptorError::Miniscript(miniscript::Error::Unexpected(
321+
"`check_wallet_descriptor` must not contain multipath keys".to_string(),
322+
)));
322323
}
323324

324325
// Run miniscript's sanity check, which will look for duplicated keys and other potential
@@ -898,9 +899,9 @@ mod test {
898899

899900
assert_matches!(
900901
result,
901-
Err(DescriptorError::Miniscript(
902-
miniscript::Error::BadDescriptor(_)
903-
))
902+
Err(DescriptorError::Miniscript(miniscript::Error::Unexpected(
903+
..
904+
)))
904905
);
905906

906907
// Repeated pubkeys.
@@ -968,13 +969,9 @@ mod test {
968969

969970
assert!(descriptor.is_multipath());
970971

971-
// Miniscript can't make an extended private key with multiple paths into a public key.
972-
// ref: <https://docs.rs/miniscript/12.3.2/miniscript/descriptor/enum.DescriptorSecretKey.html#method.to_public>
972+
// `miniscript` should make an extended private key with multiple paths into a public key.
973973
let descriptor_str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/<0;1>/*)";
974-
assert!(matches!(
975-
Descriptor::parse_descriptor(&secp, descriptor_str),
976-
Err(miniscript::Error::Unexpected(..)),
977-
));
974+
Descriptor::parse_descriptor(&secp, descriptor_str).expect("should parse multi xkey");
978975
let _ = descriptor_str
979976
.into_wallet_descriptor(&secp, NetworkKind::Main)
980977
.unwrap_err();

src/descriptor/policy.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,9 @@ impl ExtractPolicy for Descriptor<DescriptorPublicKey> {
11591159
let mut items = vec![key_spend_sig];
11601160
items.append(
11611161
&mut tr
1162-
.iter_scripts()
1163-
.filter_map(|(_, ms)| {
1162+
.leaves()
1163+
.filter_map(|item| {
1164+
let ms = item.miniscript();
11641165
ms.extract_policy(signers, build_sat, secp).transpose()
11651166
})
11661167
.collect::<Result<Vec<_>, _>>()?,

0 commit comments

Comments
 (0)