Skip to content

Commit 330739d

Browse files
temp 1: total mess but it compiles with default keychain
1 parent fc3fce5 commit 330739d

File tree

11 files changed

+1463
-1313
lines changed

11 files changed

+1463
-1313
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
resolver = "2"
33
members = [
44
"wallet",
5-
"examples/example_wallet_electrum",
6-
"examples/example_wallet_esplora_blocking",
7-
"examples/example_wallet_esplora_async",
8-
"examples/example_wallet_rpc",
5+
"examples/example_wallet_n_keychains"
6+
# "examples/example_wallet_electrum",
7+
# "examples/example_wallet_esplora_blocking",
8+
# "examples/example_wallet_esplora_async",
9+
# "examples/example_wallet_rpc",
910
]
1011

1112
[workspace.package]

wallet/examples/n_keychains.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use bdk_wallet::{KeyRing, Wallet};
2+
use bdk_wallet::bitcoin::Network;
3+
use bdk_wallet::KeychainKind;
4+
5+
const EXTERNAL_DESC: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
6+
const OTHER_DESC_21: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/21/*)";
7+
const OTHER_DESC_33: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/33/*)";
8+
const OTHER_DESC_44: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/44/*)";
9+
10+
fn main() -> Result<(), anyhow::Error> {
11+
let mut keyring = KeyRing::new(EXTERNAL_DESC, Network::Testnet4);
12+
// keyring.add_other_descriptor(OTHER_DESC_21);
13+
// let keychains = keyring.list_keychains();
14+
// println!("{:?}", keychains);
15+
16+
let wallet: Wallet = Wallet::new(keyring).create_wallet_no_persist()?;
17+
let address_1 = wallet.peek_address(KeychainKind::Default, 0).unwrap();
18+
// let address_2 = wallet.peek_address((KeychainKind::Other(), 0).unwrap();
19+
20+
println!("Address {:?} at index {:?} on keychain {:?}", address_1.address, address_1.index, address_1.keychain);
21+
22+
Ok(())
23+
}

wallet/src/descriptor/template.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ use super::{ExtendedDescriptor, IntoWalletDescriptor, KeyMap};
2323
use crate::descriptor::DescriptorError;
2424
use crate::keys::{DerivableKey, IntoDescriptorKey, ValidNetworks};
2525
use crate::wallet::utils::SecpCtx;
26-
use crate::{descriptor, KeychainKind};
26+
use crate::descriptor;
2727

2828
/// Type alias for the return type of [`DescriptorTemplate`], [`descriptor!`](crate::descriptor!) and others
2929
pub type DescriptorTemplateOut = (ExtendedDescriptor, KeyMap, ValidNetworks);
3030

31+
#[derive(Clone, Debug)]
32+
pub enum TemplateKeychainKind {
33+
External,
34+
Internal,
35+
}
36+
3137
/// Trait for descriptor templates that can be built into a full descriptor
3238
///
3339
/// Since [`IntoWalletDescriptor`] is implemented for any [`DescriptorTemplate`], they can also be
@@ -232,7 +238,7 @@ impl<K: IntoDescriptorKey<Tap>> DescriptorTemplate for P2TR<K> {
232238
/// # Ok::<_, Box<dyn std::error::Error>>(())
233239
/// ```
234240
#[derive(Debug, Clone)]
235-
pub struct Bip44<K: DerivableKey<Legacy>>(pub K, pub KeychainKind);
241+
pub struct Bip44<K: DerivableKey<Legacy>>(pub K, pub TemplateKeychainKind);
236242

237243
impl<K: DerivableKey<Legacy>> DescriptorTemplate for Bip44<K> {
238244
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -271,7 +277,7 @@ impl<K: DerivableKey<Legacy>> DescriptorTemplate for Bip44<K> {
271277
/// # Ok::<_, Box<dyn std::error::Error>>(())
272278
/// ```
273279
#[derive(Debug, Clone)]
274-
pub struct Bip44Public<K: DerivableKey<Legacy>>(pub K, pub bip32::Fingerprint, pub KeychainKind);
280+
pub struct Bip44Public<K: DerivableKey<Legacy>>(pub K, pub bip32::Fingerprint, pub TemplateKeychainKind);
275281

276282
impl<K: DerivableKey<Legacy>> DescriptorTemplate for Bip44Public<K> {
277283
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -309,7 +315,7 @@ impl<K: DerivableKey<Legacy>> DescriptorTemplate for Bip44Public<K> {
309315
/// # Ok::<_, Box<dyn std::error::Error>>(())
310316
/// ```
311317
#[derive(Debug, Clone)]
312-
pub struct Bip49<K: DerivableKey<Segwitv0>>(pub K, pub KeychainKind);
318+
pub struct Bip49<K: DerivableKey<Segwitv0>>(pub K, pub TemplateKeychainKind);
313319

314320
impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip49<K> {
315321
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -348,7 +354,7 @@ impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip49<K> {
348354
/// # Ok::<_, Box<dyn std::error::Error>>(())
349355
/// ```
350356
#[derive(Debug, Clone)]
351-
pub struct Bip49Public<K: DerivableKey<Segwitv0>>(pub K, pub bip32::Fingerprint, pub KeychainKind);
357+
pub struct Bip49Public<K: DerivableKey<Segwitv0>>(pub K, pub bip32::Fingerprint, pub TemplateKeychainKind);
352358

353359
impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip49Public<K> {
354360
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -386,7 +392,7 @@ impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip49Public<K> {
386392
/// # Ok::<_, Box<dyn std::error::Error>>(())
387393
/// ```
388394
#[derive(Debug, Clone)]
389-
pub struct Bip84<K: DerivableKey<Segwitv0>>(pub K, pub KeychainKind);
395+
pub struct Bip84<K: DerivableKey<Segwitv0>>(pub K, pub TemplateKeychainKind);
390396

391397
impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip84<K> {
392398
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -425,7 +431,7 @@ impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip84<K> {
425431
/// # Ok::<_, Box<dyn std::error::Error>>(())
426432
/// ```
427433
#[derive(Debug, Clone)]
428-
pub struct Bip84Public<K: DerivableKey<Segwitv0>>(pub K, pub bip32::Fingerprint, pub KeychainKind);
434+
pub struct Bip84Public<K: DerivableKey<Segwitv0>>(pub K, pub bip32::Fingerprint, pub TemplateKeychainKind);
429435

430436
impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip84Public<K> {
431437
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -463,7 +469,7 @@ impl<K: DerivableKey<Segwitv0>> DescriptorTemplate for Bip84Public<K> {
463469
/// # Ok::<_, Box<dyn std::error::Error>>(())
464470
/// ```
465471
#[derive(Debug, Clone)]
466-
pub struct Bip86<K: DerivableKey<Tap>>(pub K, pub KeychainKind);
472+
pub struct Bip86<K: DerivableKey<Tap>>(pub K, pub TemplateKeychainKind);
467473

468474
impl<K: DerivableKey<Tap>> DescriptorTemplate for Bip86<K> {
469475
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -502,7 +508,7 @@ impl<K: DerivableKey<Tap>> DescriptorTemplate for Bip86<K> {
502508
/// # Ok::<_, Box<dyn std::error::Error>>(())
503509
/// ```
504510
#[derive(Debug, Clone)]
505-
pub struct Bip86Public<K: DerivableKey<Tap>>(pub K, pub bip32::Fingerprint, pub KeychainKind);
511+
pub struct Bip86Public<K: DerivableKey<Tap>>(pub K, pub bip32::Fingerprint, pub TemplateKeychainKind);
506512

507513
impl<K: DerivableKey<Tap>> DescriptorTemplate for Bip86Public<K> {
508514
fn build(self, network: Network) -> Result<DescriptorTemplateOut, DescriptorError> {
@@ -521,7 +527,7 @@ macro_rules! expand_make_bipxx {
521527
pub(super) fn make_bipxx_private<K: DerivableKey<$ctx>>(
522528
bip: u32,
523529
key: K,
524-
keychain: KeychainKind,
530+
keychain: TemplateKeychainKind,
525531
network: Network,
526532
) -> Result<impl IntoDescriptorKey<$ctx>, DescriptorError> {
527533
let mut derivation_path = alloc::vec::Vec::with_capacity(4);
@@ -538,10 +544,10 @@ macro_rules! expand_make_bipxx {
538544
derivation_path.push(bip32::ChildNumber::from_hardened_idx(0)?);
539545

540546
match keychain {
541-
KeychainKind::External => {
547+
TemplateKeychainKind::External => {
542548
derivation_path.push(bip32::ChildNumber::from_normal_idx(0)?)
543549
}
544-
KeychainKind::Internal => {
550+
TemplateKeychainKind::Internal => {
545551
derivation_path.push(bip32::ChildNumber::from_normal_idx(1)?)
546552
}
547553
};
@@ -554,12 +560,12 @@ macro_rules! expand_make_bipxx {
554560
bip: u32,
555561
key: K,
556562
parent_fingerprint: bip32::Fingerprint,
557-
keychain: KeychainKind,
563+
keychain: TemplateKeychainKind,
558564
network: Network,
559565
) -> Result<impl IntoDescriptorKey<$ctx>, DescriptorError> {
560566
let derivation_path: bip32::DerivationPath = match keychain {
561-
KeychainKind::External => vec![bip32::ChildNumber::from_normal_idx(0)?].into(),
562-
KeychainKind::Internal => vec![bip32::ChildNumber::from_normal_idx(1)?].into(),
567+
TemplateKeychainKind::External => vec![bip32::ChildNumber::from_normal_idx(0)?].into(),
568+
TemplateKeychainKind::Internal => vec![bip32::ChildNumber::from_normal_idx(1)?].into(),
563569
};
564570

565571
let source_path = bip32::DerivationPath::from(vec![

0 commit comments

Comments
 (0)