Skip to content

Commit 6c50f00

Browse files
committed
feat!: modify Wallet struct, add constructor
Modified the `Wallet` struct to hold a `KeyRing`, added a basic constructor `new` to the wallet. Modified the `ChangeSet` accordingly.
1 parent fa984e6 commit 6c50f00

File tree

2 files changed

+53
-63
lines changed

2 files changed

+53
-63
lines changed

wallet/src/wallet/changeset.rs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
88
type IndexedTxGraphChangeSet =
99
indexed_tx_graph::ChangeSet<ConfirmationBlockTime, keychain_txout::ChangeSet>;
1010

11+
use crate::keyring;
12+
1113
/// A change set for [`Wallet`]
1214
///
1315
/// ## Definition
@@ -101,58 +103,47 @@ type IndexedTxGraphChangeSet =
101103
/// [`WalletPersister`]: crate::WalletPersister
102104
/// [`Wallet::staged`]: crate::Wallet::staged
103105
/// [`Wallet`]: crate::Wallet
104-
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
105-
pub struct ChangeSet {
106-
/// Descriptor for recipient addresses.
107-
pub descriptor: Option<Descriptor<DescriptorPublicKey>>,
108-
/// Descriptor for change addresses.
109-
pub change_descriptor: Option<Descriptor<DescriptorPublicKey>>,
110-
/// Stores the network type of the transaction data.
111-
pub network: Option<bitcoin::Network>,
106+
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
107+
pub struct ChangeSet<K: Ord> {
108+
/// Stores the `KeyRing` containing the network and descriptor data.
109+
pub keyring: keyring::changeset::ChangeSet<K>,
112110
/// Changes to the [`LocalChain`](local_chain::LocalChain).
113-
pub local_chain: local_chain::ChangeSet,
111+
pub chain: local_chain::ChangeSet,
114112
/// Changes to [`TxGraph`](tx_graph::TxGraph).
115113
pub tx_graph: tx_graph::ChangeSet<ConfirmationBlockTime>,
116114
/// Changes to [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex).
117115
pub indexer: keychain_txout::ChangeSet,
118116
}
119117

120-
impl Merge for ChangeSet {
121-
/// Merge another [`ChangeSet`] into itself.
122-
fn merge(&mut self, other: Self) {
123-
if other.descriptor.is_some() {
124-
debug_assert!(
125-
self.descriptor.is_none() || self.descriptor == other.descriptor,
126-
"descriptor must never change"
127-
);
128-
self.descriptor = other.descriptor;
129-
}
130-
if other.change_descriptor.is_some() {
131-
debug_assert!(
132-
self.change_descriptor.is_none()
133-
|| self.change_descriptor == other.change_descriptor,
134-
"change descriptor must never change"
135-
);
136-
self.change_descriptor = other.change_descriptor;
137-
}
138-
if other.network.is_some() {
139-
debug_assert!(
140-
self.network.is_none() || self.network == other.network,
141-
"network must never change"
142-
);
143-
self.network = other.network;
118+
impl<K> Default for ChangeSet<K>
119+
where
120+
K: Ord,
121+
{
122+
fn default() -> Self {
123+
Self {
124+
keyring: Default::default(),
125+
chain: Default::default(),
126+
tx_graph: Default::default(),
127+
indexer: Default::default(),
144128
}
129+
}
130+
}
145131

146-
Merge::merge(&mut self.local_chain, other.local_chain);
132+
impl<K> Merge for ChangeSet<K>
133+
where
134+
K: Ord,
135+
{
136+
/// Merge another [`ChangeSet`] into itself.
137+
fn merge(&mut self, other: Self) {
138+
Merge::merge(&mut self.keyring, other.keyring);
139+
Merge::merge(&mut self.chain, other.chain);
147140
Merge::merge(&mut self.tx_graph, other.tx_graph);
148141
Merge::merge(&mut self.indexer, other.indexer);
149142
}
150143

151144
fn is_empty(&self) -> bool {
152-
self.descriptor.is_none()
153-
&& self.change_descriptor.is_none()
154-
&& self.network.is_none()
155-
&& self.local_chain.is_empty()
145+
self.keyring.is_empty()
146+
&& self.chain.is_empty()
156147
&& self.tx_graph.is_empty()
157148
&& self.indexer.is_empty()
158149
}
@@ -276,16 +267,16 @@ impl Merge for ChangeSet {
276267
// }
277268
// }
278269

279-
impl From<local_chain::ChangeSet> for ChangeSet {
270+
impl<K: Ord> From<local_chain::ChangeSet> for ChangeSet<K> {
280271
fn from(chain: local_chain::ChangeSet) -> Self {
281272
Self {
282-
local_chain: chain,
273+
chain: chain,
283274
..Default::default()
284275
}
285276
}
286277
}
287278

288-
impl From<IndexedTxGraphChangeSet> for ChangeSet {
279+
impl<K: Ord> From<IndexedTxGraphChangeSet> for ChangeSet<K> {
289280
fn from(indexed_tx_graph: IndexedTxGraphChangeSet) -> Self {
290281
Self {
291282
tx_graph: indexed_tx_graph.tx_graph,
@@ -295,7 +286,7 @@ impl From<IndexedTxGraphChangeSet> for ChangeSet {
295286
}
296287
}
297288

298-
impl From<tx_graph::ChangeSet<ConfirmationBlockTime>> for ChangeSet {
289+
impl<K: Ord> From<tx_graph::ChangeSet<ConfirmationBlockTime>> for ChangeSet<K> {
299290
fn from(tx_graph: tx_graph::ChangeSet<ConfirmationBlockTime>) -> Self {
300291
Self {
301292
tx_graph,
@@ -304,7 +295,7 @@ impl From<tx_graph::ChangeSet<ConfirmationBlockTime>> for ChangeSet {
304295
}
305296
}
306297

307-
impl From<keychain_txout::ChangeSet> for ChangeSet {
298+
impl<K: Ord> From<keychain_txout::ChangeSet> for ChangeSet<K> {
308299
fn from(indexer: keychain_txout::ChangeSet) -> Self {
309300
Self {
310301
indexer,

wallet/src/wallet/mod.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ use alloc::{
2222
use core::{cmp::Ordering, fmt, mem, ops::Deref};
2323

2424
use bdk_chain::{
25-
indexed_tx_graph,
26-
indexer::keychain_txout::KeychainTxOutIndex,
25+
indexer::keychain_txout::{self, KeychainTxOutIndex, DEFAULT_LOOKAHEAD},
2726
local_chain::{ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain},
2827
spk_client::{
2928
FullScanRequest, FullScanRequestBuilder, FullScanResponse, SyncRequest, SyncRequestBuilder,
3029
SyncResponse,
3130
},
32-
tx_graph::{CalculateFeeError, CanonicalTx, TxGraph, TxUpdate},
31+
tx_graph::{self, CalculateFeeError, CanonicalTx, TxGraph, TxUpdate},
3332
BlockId, CanonicalizationParams, ChainPosition, ConfirmationBlockTime, DescriptorExt,
3433
FullTxOut, Indexed, IndexedTxGraph, Indexer, Merge,
3534
};
@@ -59,12 +58,12 @@ pub mod signer;
5958
pub mod tx_builder;
6059
pub(crate) mod utils;
6160

62-
use crate::collections::{BTreeMap, HashMap, HashSet};
6361
use crate::descriptor::{
6462
check_wallet_descriptor, error::Error as DescriptorError, policy::BuildSatisfaction,
6563
DerivedDescriptor, DescriptorMeta, ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor,
6664
Policy, XKeyUtils,
6765
};
66+
use crate::keyring::KeyRing;
6867
use crate::psbt::PsbtUtils;
6968
use crate::types::*;
7069
use crate::wallet::{
@@ -74,6 +73,10 @@ use crate::wallet::{
7473
// tx_builder::{FeePolicy, TxBuilder, TxParams},
7574
utils::{check_nsequence_rbf, After, Older, SecpCtx},
7675
};
76+
use crate::{
77+
collections::{BTreeMap, HashMap, HashSet},
78+
keyring,
79+
};
7780

7881
// re-exports
7982
pub use bdk_chain::Balance;
@@ -83,32 +86,28 @@ pub use persisted::*;
8386
pub use utils::IsDust;
8487
pub use utils::TxDetails;
8588

89+
type KeychainTxGraph<K> = IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<K>>;
90+
8691
/// A Bitcoin wallet
8792
///
8893
/// The `Wallet` acts as a way of coherently interfacing with output descriptors and related
89-
/// transactions. Its main components are:
90-
///
91-
/// 1. output *descriptors* from which it can derive addresses.
92-
/// 2. [`signer`]s that can contribute signatures to addresses instantiated from the descriptors.
94+
/// transactions. Its main component is a [`KeyRing`] which holds the network and output
95+
/// descriptors.
9396
///
9497
/// The user is responsible for loading and writing wallet changes which are represented as
9598
/// [`ChangeSet`]s (see [`take_staged`]). Also see individual functions and example for instructions
9699
/// on when [`Wallet`] state needs to be persisted.
97100
///
98-
/// The `Wallet` descriptor (external) and change descriptor (internal) must not derive the same
99-
/// script pubkeys. See [`KeychainTxOutIndex::insert_descriptor()`] for more details.
101+
/// The `Wallet` descriptors must not derive the same script pubkeys.
102+
/// See [`KeychainTxOutIndex::insert_descriptor()`] for more details.
100103
///
101-
/// [`signer`]: crate::signer
102104
/// [`take_staged`]: Wallet::take_staged
103105
#[derive(Debug)]
104-
pub struct Wallet {
105-
signers: Arc<SignersContainer>,
106-
change_signers: Arc<SignersContainer>,
106+
pub struct Wallet<K: Ord> {
107+
keyring: KeyRing<K>,
107108
chain: LocalChain,
108-
indexed_graph: IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<KeychainKind>>,
109-
stage: ChangeSet,
110-
network: Network,
111-
secp: SecpCtx,
109+
keychain_tx_graph: KeychainTxGraph<K>,
110+
stage: ChangeSet<K>,
112111
}
113112

114113
/// An update to [`Wallet`].
@@ -626,7 +625,7 @@ pub type WalletTx<'a> = CanonicalTx<'a, Arc<Transaction>, ConfirmationBlockTime>
626625
// let (exp_desc, _) = make_desc(&secp, network).map_err(LoadError::Descriptor)?;
627626
// return Err(LoadError::Mismatch(LoadMismatch::Descriptor {
628627
// keychain: KeychainKind::Internal,
629-
// loaded: None,
628+
// r: co loaded: None,
630629
// expected: Some(exp_desc),
631630
// }));
632631
// }
@@ -2638,7 +2637,7 @@ pub type WalletTx<'a> = CanonicalTx<'a, Arc<Transaction>, ConfirmationBlockTime>
26382637

26392638
impl AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationBlockTime>> for Wallet {
26402639
fn as_ref(&self) -> &bdk_chain::tx_graph::TxGraph<ConfirmationBlockTime> {
2641-
self.indexed_graph.graph()
2640+
self.keychain_tx_graph.graph()
26422641
}
26432642
}
26442643

0 commit comments

Comments
 (0)