Skip to content

Commit f966cbf

Browse files
committed
Add support for BIP47 (reusable payment codes)
1 parent 0114805 commit f966cbf

File tree

6 files changed

+903
-16
lines changed

6 files changed

+903
-16
lines changed

src/keys/mod.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ use bitcoin::secp256k1::{self, Secp256k1, Signing};
2222
use bitcoin::util::bip32;
2323
use bitcoin::{Network, PrivateKey, PublicKey};
2424

25-
use miniscript::descriptor::{Descriptor, DescriptorXKey, Wildcard};
25+
use miniscript::descriptor::{Descriptor, Wildcard};
2626
pub use miniscript::descriptor::{
27-
DescriptorPublicKey, DescriptorSecretKey, DescriptorSinglePriv, DescriptorSinglePub, KeyMap,
28-
SortedMultiVec,
27+
DescriptorPublicKey, DescriptorSecretKey, DescriptorSinglePriv, DescriptorSinglePub,
28+
DescriptorXKey, KeyMap, SortedMultiVec,
2929
};
3030
pub use miniscript::ScriptContext;
3131
use miniscript::{Miniscript, Terminal};
@@ -94,6 +94,13 @@ impl<Ctx: ScriptContext> DescriptorKey<Ctx> {
9494
}
9595
}
9696

97+
pub fn as_public(&self, secp: &SecpCtx) -> Result<DescriptorPublicKey, KeyError> {
98+
match self {
99+
DescriptorKey::Public(pk, _, _) => Ok(pk.clone()),
100+
DescriptorKey::Secret(secret, _, _) => Ok(secret.as_public(secp)?),
101+
}
102+
}
103+
97104
// This method is used internally by `bdk::fragment!` and `bdk::descriptor!`. It has to be
98105
// public because it is effectively called by external crates, once the macros are expanded,
99106
// but since it is not meant to be part of the public api we hide it from the docs.
@@ -102,18 +109,15 @@ impl<Ctx: ScriptContext> DescriptorKey<Ctx> {
102109
self,
103110
secp: &SecpCtx,
104111
) -> Result<(DescriptorPublicKey, KeyMap, ValidNetworks), KeyError> {
112+
let public = self.as_public(secp)?;
113+
105114
match self {
106-
DescriptorKey::Public(public, valid_networks, _) => {
115+
DescriptorKey::Public(_, valid_networks, _) => {
107116
Ok((public, KeyMap::default(), valid_networks))
108117
}
109118
DescriptorKey::Secret(secret, valid_networks, _) => {
110119
let mut key_map = KeyMap::with_capacity(1);
111-
112-
let public = secret
113-
.as_public(secp)
114-
.map_err(|e| miniscript::Error::Unexpected(e.to_string()))?;
115120
key_map.insert(public.clone(), secret);
116-
117121
Ok((public, key_map, valid_networks))
118122
}
119123
}
@@ -891,9 +895,15 @@ pub enum KeyError {
891895
Bip32(bitcoin::util::bip32::Error),
892896
/// Miniscript error
893897
Miniscript(miniscript::Error),
898+
KeyParseError(miniscript::descriptor::DescriptorKeyParseError),
894899
}
895900

896901
impl_error!(miniscript::Error, Miniscript, KeyError);
902+
impl_error!(
903+
miniscript::descriptor::DescriptorKeyParseError,
904+
KeyParseError,
905+
KeyError
906+
);
897907
impl_error!(bitcoin::util::bip32::Error, Bip32, KeyError);
898908

899909
impl std::fmt::Display for KeyError {

src/lib.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ pub extern crate sled;
251251
#[cfg(feature = "sqlite")]
252252
pub extern crate rusqlite;
253253

254+
// We should consider putting this under a feature flag but we need the macro in doctests so we need
255+
// to wait until https://github.com/rust-lang/rust/issues/67295 is fixed.
256+
//
257+
// Stuff in here is too rough to document atm
258+
#[doc(hidden)]
259+
#[macro_use]
260+
pub mod testutils;
261+
254262
#[allow(unused_imports)]
255263
#[macro_use]
256264
pub(crate) mod error;
@@ -262,6 +270,7 @@ mod doctest;
262270
pub mod keys;
263271
pub(crate) mod psbt;
264272
pub(crate) mod types;
273+
pub mod utils;
265274
pub mod wallet;
266275

267276
pub use descriptor::template;
@@ -279,10 +288,3 @@ pub use wallet::Wallet;
279288
pub fn version() -> &'static str {
280289
env!("CARGO_PKG_VERSION", "unknown")
281290
}
282-
283-
// We should consider putting this under a feature flag but we need the macro in doctests so we need
284-
// to wait until https://github.com/rust-lang/rust/issues/67295 is fixed.
285-
//
286-
// Stuff in here is too rough to document atm
287-
#[doc(hidden)]
288-
pub mod testutils;

src/testutils/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,5 @@ macro_rules! testutils {
267267
(external, internal)
268268
})
269269
}
270+
271+
pub use testutils;

0 commit comments

Comments
 (0)