Skip to content

Commit 551a7e9

Browse files
committed
feat: Add a basic constructor for Wallet
1 parent 588121b commit 551a7e9

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

wallet/src/wallet/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,49 @@ impl std::error::Error for ApplyBlockError {}
312312
/// A `CanonicalTx` managed by a `Wallet`.
313313
pub type WalletTx<'a> = CanonicalTx<'a, Arc<Transaction>, ConfirmationBlockTime>;
314314

315+
impl<K> Wallet<K>
316+
where
317+
K: Clone + fmt::Debug + Ord,
318+
{
319+
/// Construct a new [`Wallet`] with the given `keyring`.
320+
///
321+
/// Note: The network must be either the mainnet or one of the test networks. Also default value
322+
/// of lookahead is used with no spk_cache.
323+
pub fn new(mut keyring: KeyRing<K>) -> Self {
324+
let network = keyring.network;
325+
326+
let genesis_hash = bitcoin::constants::genesis_block(network).block_hash();
327+
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
328+
329+
let keyring_changeset = keyring.initial_changeset();
330+
331+
let mut index = KeychainTxOutIndex::new(DEFAULT_LOOKAHEAD, false);
332+
let descriptors = core::mem::take(&mut keyring.descriptors);
333+
for (keychain, desc) in descriptors {
334+
let _inserted = index
335+
.insert_descriptor(keychain, desc)
336+
.expect("err: failed to insert descriptor");
337+
assert!(_inserted);
338+
}
339+
340+
let tx_graph = KeychainTxGraph::new(index);
341+
342+
let stage = ChangeSet {
343+
keyring: keyring_changeset,
344+
local_chain: chain_changeset,
345+
tx_graph: bdk_chain::tx_graph::ChangeSet::default(),
346+
indexer: bdk_chain::keychain_txout::ChangeSet::default(),
347+
};
348+
349+
Self {
350+
keyring,
351+
chain,
352+
tx_graph,
353+
stage,
354+
}
355+
}
356+
}
357+
315358
// impl Wallet {
316359
// /// Build a new single descriptor [`Wallet`].
317360
// ///

wallet/tests/wallet.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,6 +3099,3 @@ macro_rules! from_str {
30993099
// // Check vout is sorted by recipient insertion order
31003100
// assert!(txouts == vec![400, 300, 500]);
31013101
// }
3102-
3103-
#[test]
3104-
fn test_new() {}

0 commit comments

Comments
 (0)