Skip to content

Commit 5ef474e

Browse files
temp 4: create keyring module
1 parent 8a2217f commit 5ef474e

File tree

3 files changed

+91
-101
lines changed

3 files changed

+91
-101
lines changed

wallet/src/types.rs

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@
1010
// licenses.
1111

1212
use alloc::boxed::Box;
13-
use chain::{ChainPosition, ConfirmationBlockTime, DescriptorExt, DescriptorId};
13+
use chain::{ChainPosition, ConfirmationBlockTime, DescriptorId};
1414
use std::hash::{Hash, Hasher};
15-
use std::prelude::rust_2021::Vec;
1615
use bitcoin::transaction::{OutPoint, Sequence, TxOut};
1716
use bitcoin::{psbt, Network, Weight};
18-
use miniscript::{Descriptor, DescriptorPublicKey};
19-
use miniscript::descriptor::KeyMap;
2017
use serde::{Deserialize, Serialize};
21-
use crate::descriptor::{IntoWalletDescriptor};
22-
use crate::DescriptorToExtract;
23-
use crate::wallet::make_descriptor_to_extract;
24-
use crate::wallet::utils::SecpCtx;
2518

2619
/// Types of keychains
2720
// #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
@@ -32,6 +25,7 @@ use crate::wallet::utils::SecpCtx;
3225
// Internal = 1,
3326
// }
3427

28+
// TODO #226: Figure out why these are there and implement them on the new KeychainKind.
3529
// impl KeychainKind {
3630
// /// Return [`KeychainKind`] as a byte
3731
// pub fn as_byte(&self) -> u8 {
@@ -58,99 +52,6 @@ pub enum KeychainKind {
5852
Other(DescriptorId),
5953
}
6054

61-
// pub type KeychainIdentifier = (KeychainKind, Option<DescriptorId>);
62-
63-
pub type WalletKeychain = (KeychainKind, (Descriptor<DescriptorPublicKey>, KeyMap));
64-
65-
/// A `WalletKeychain` is mostly a descriptor with metadata associated with it. It states whether the
66-
/// keychain is the default keychain for the wallet, and provides an identifier for it which can be
67-
/// used for retrieval.
68-
// #[derive(Clone, Debug, Eq, PartialEq)]
69-
// pub struct WalletKeychain {
70-
// pub keychain_kind: KeychainKind,
71-
// pub public_descriptor: Descriptor<DescriptorPublicKey>,
72-
// pub keymap: KeyMap,
73-
// }
74-
75-
#[derive(Debug, Clone)]
76-
pub struct KeyRing {
77-
keychains: Vec<WalletKeychain>,
78-
network: Network,
79-
}
80-
81-
impl KeyRing {
82-
pub fn new<D: IntoWalletDescriptor + Send + 'static>(
83-
default_descriptor: D,
84-
network: Network,
85-
) -> Self {
86-
let secp = SecpCtx::new();
87-
let descriptor_to_extract: DescriptorToExtract = make_descriptor_to_extract(default_descriptor);
88-
let public_descriptor: (Descriptor<DescriptorPublicKey>, KeyMap) = descriptor_to_extract(&secp, network).unwrap();
89-
let descriptor_id: DescriptorId = public_descriptor.0.descriptor_id();
90-
// Using the type alias
91-
let wallet_keychain = ((KeychainKind::Default), public_descriptor);
92-
93-
// Using the struct
94-
// let wallet_keychain = WalletKeychain {
95-
// keychain_kind: KeychainKind::Default(descriptor_id),
96-
// public_descriptor,
97-
// keymap: KeyMap::default()
98-
// };
99-
100-
KeyRing {
101-
keychains: vec![wallet_keychain],
102-
network,
103-
}
104-
}
105-
106-
// TODO: This needs to never fail because there is always a default keychain.
107-
pub fn get_default_keychain(&self) -> WalletKeychain {
108-
self.keychains.iter().find(|keychain| matches!(keychain.0, KeychainKind::Default)).unwrap().clone()
109-
}
110-
111-
pub fn get_change_keychain(&self) -> Option<WalletKeychain> {
112-
self.keychains.iter().find(|keychain| matches!(keychain.0, KeychainKind::Change)).cloned()
113-
}
114-
115-
pub fn add_other_descriptor<D: IntoWalletDescriptor + Send + 'static>(
116-
&mut self,
117-
other_descriptor: D
118-
) -> &mut KeyRing {
119-
let secp = SecpCtx::new();
120-
let descriptor_to_extract: DescriptorToExtract = make_descriptor_to_extract(other_descriptor);
121-
let public_descriptor = descriptor_to_extract(&secp, self.network).unwrap();
122-
let descriptor_id = public_descriptor.0.descriptor_id();
123-
124-
let wallet_keychain = ((KeychainKind::Other(descriptor_id)), public_descriptor);
125-
126-
self.keychains.push(wallet_keychain);
127-
self
128-
}
129-
130-
pub fn list_keychains(&self) -> &Vec<WalletKeychain> {
131-
&self.keychains
132-
}
133-
134-
pub fn list_keychain_ids(&self) -> Vec<DescriptorId> {
135-
self.keychains
136-
.iter()
137-
.map(|keychain| match keychain.0 {
138-
KeychainKind::Other(descriptor_id) => descriptor_id,
139-
KeychainKind::Default => keychain.1.0.descriptor_id(),
140-
KeychainKind::Change => keychain.1.0.descriptor_id(),
141-
})
142-
.collect()
143-
}
144-
145-
// pub fn add_change_keychain(&mut self, keychain: (DescriptorToExtract, KeyMap), keychain_identifier: KeychainIdentifier) {
146-
//
147-
// }
148-
// pub fn add_wallet_keychain<D: IntoWalletDescriptor + Send + 'static>(
149-
// &mut self,
150-
// descriptor: D,
151-
// )
152-
}
153-
15455
/// An unspent output owned by a [`Wallet`].
15556
///
15657
/// [`Wallet`]: crate::Wallet

wallet/src/wallet/keyring.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Bitcoin Dev Kit
2+
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
3+
//
4+
// Copyright (c) 2020-2025 Bitcoin Dev Kit Developers
5+
//
6+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
7+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
9+
// You may not use this file except in accordance with one or both of these
10+
// licenses.
11+
12+
use std::prelude::rust_2021::Vec;
13+
use bitcoin::Network;
14+
use chain::{DescriptorExt, DescriptorId};
15+
use miniscript::{Descriptor, DescriptorPublicKey};
16+
use miniscript::descriptor::KeyMap;
17+
use crate::descriptor::IntoWalletDescriptor;
18+
use crate::{DescriptorToExtract, KeychainKind};
19+
use crate::wallet::make_descriptor_to_extract;
20+
use crate::wallet::utils::SecpCtx;
21+
22+
/// A `WalletKeychain` is mostly a descriptor with metadata associated with it. It states whether the
23+
/// keychain is the default keychain for the wallet, and provides an identifier for it which can be
24+
/// used for retrieval.
25+
pub type WalletKeychain = (KeychainKind, (Descriptor<DescriptorPublicKey>, KeyMap));
26+
27+
#[derive(Debug, Clone)]
28+
pub struct KeyRing {
29+
keychains: Vec<WalletKeychain>,
30+
network: Network,
31+
}
32+
33+
impl KeyRing {
34+
pub fn new<D: IntoWalletDescriptor + Send + 'static>(
35+
default_descriptor: D,
36+
network: Network,
37+
) -> Self {
38+
let secp = SecpCtx::new();
39+
let descriptor_to_extract: DescriptorToExtract = make_descriptor_to_extract(default_descriptor);
40+
let public_descriptor: (Descriptor<DescriptorPublicKey>, KeyMap) = descriptor_to_extract(&secp, network).unwrap();
41+
let wallet_keychain = (KeychainKind::Default, public_descriptor);
42+
43+
KeyRing {
44+
keychains: vec![wallet_keychain],
45+
network,
46+
}
47+
}
48+
49+
// TODO #226: This needs to never fail because there is always a default keychain.
50+
pub fn get_default_keychain(&self) -> WalletKeychain {
51+
self.keychains.iter().find(|keychain| matches!(keychain.0, KeychainKind::Default)).unwrap().clone()
52+
}
53+
54+
pub fn get_change_keychain(&self) -> Option<WalletKeychain> {
55+
self.keychains.iter().find(|keychain| matches!(keychain.0, KeychainKind::Change)).cloned()
56+
}
57+
58+
pub fn add_other_descriptor<D: IntoWalletDescriptor + Send + 'static>(
59+
&mut self,
60+
other_descriptor: D
61+
) -> &mut KeyRing {
62+
let secp = SecpCtx::new();
63+
let descriptor_to_extract: DescriptorToExtract = make_descriptor_to_extract(other_descriptor);
64+
let public_descriptor = descriptor_to_extract(&secp, self.network).unwrap();
65+
let descriptor_id = public_descriptor.0.descriptor_id();
66+
67+
let wallet_keychain = ((KeychainKind::Other(descriptor_id)), public_descriptor);
68+
69+
self.keychains.push(wallet_keychain);
70+
self
71+
}
72+
73+
pub fn list_keychains(&self) -> &Vec<WalletKeychain> {
74+
&self.keychains
75+
}
76+
77+
pub fn list_keychain_ids(&self) -> Vec<DescriptorId> {
78+
self.keychains
79+
.iter()
80+
.map(|keychain| match keychain.0 {
81+
KeychainKind::Other(descriptor_id) => descriptor_id,
82+
KeychainKind::Default => keychain.1.0.descriptor_id(),
83+
KeychainKind::Change => keychain.1.0.descriptor_id(),
84+
})
85+
.collect()
86+
}
87+
}

wallet/src/wallet/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ mod persisted;
5959
pub mod signer;
6060
pub mod tx_builder;
6161
pub(crate) mod utils;
62+
mod keyring;
6263

6364
use crate::collections::{BTreeMap, HashMap, HashSet};
6465
use crate::descriptor::{
@@ -85,6 +86,7 @@ pub use persisted::*;
8586
pub use utils::IsDust;
8687
use crate::error::KeychainNotInKeyRingError;
8788
use crate::error::KeychainNotInKeyRingError::KeychainNotFound;
89+
use crate::wallet::keyring::KeyRing;
8890

8991
/// A Bitcoin wallet
9092
///

0 commit comments

Comments
 (0)