From b17d261558cad664151cf92fddd1ba2a722e2368 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Mon, 9 Sep 2024 15:38:14 +0200 Subject: [PATCH] f BDK: Account for persistence changes 3: switch to new Wallet API --- src/builder.rs | 63 +++++++++++++++++++++++------------------------ src/wallet/mod.rs | 11 +++++---- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 6a6963e8b..7413316a8 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -15,6 +15,8 @@ use crate::fee_estimator::OnchainFeeEstimator; use crate::gossip::GossipSource; use crate::io; use crate::io::sqlite_store::SqliteStore; +#[cfg(any(vss, vss_test))] +use crate::io::vss_store::VssStore; use crate::liquidity::LiquiditySource; use crate::logger::{log_error, log_info, FilesystemLogger, Logger}; use crate::message_handler::NodeCustomMessageHandler; @@ -25,6 +27,7 @@ use crate::types::{ ChainMonitor, ChannelManager, DynStore, GossipSync, Graph, KeysManager, MessageRouter, OnionMessenger, PeerManager, }; +use crate::wallet::persist::KVStoreWalletPersister; use crate::wallet::Wallet; use crate::{LogLevel, Node}; @@ -53,12 +56,9 @@ use lightning_transaction_sync::EsploraSyncClient; use lightning_liquidity::lsps2::client::LSPS2ClientConfig; use lightning_liquidity::{LiquidityClientConfig, LiquidityManager}; -#[cfg(any(vss, vss_test))] -use crate::io::vss_store::VssStore; -use bdk::bitcoin::secp256k1::Secp256k1; -use bdk::blockchain::esplora::EsploraBlockchain; -use bdk::database::SqliteDatabase; -use bdk::template::Bip84; +use bdk_wallet::template::Bip84; +use bdk_wallet::KeychainKind; +use bdk_wallet::Wallet as BdkWallet; use bip39::Mnemonic; @@ -532,36 +532,35 @@ fn build_with_store_internal( logger: Arc, kv_store: Arc, ) -> Result { // Initialize the on-chain wallet and chain access - let xprv = bitcoin::bip32::ExtendedPrivKey::new_master(config.network.into(), &seed_bytes) - .map_err(|e| { + let xprv = + bitcoin::bip32::Xpriv::new_master(config.network.into(), &seed_bytes).map_err(|e| { log_error!(logger, "Failed to derive master secret: {}", e); BuildError::InvalidSeedBytes })?; - let wallet_name = bdk::wallet::wallet_name_from_descriptor( - Bip84(xprv, bdk::KeychainKind::External), - Some(Bip84(xprv, bdk::KeychainKind::Internal)), - config.network.into(), - &Secp256k1::new(), - ) - .map_err(|e| { - log_error!(logger, "Failed to derive wallet name: {}", e); - BuildError::WalletSetupFailed - })?; - - let database_path = format!("{}/bdk_wallet_{}.sqlite", config.storage_dir_path, wallet_name); - let database = SqliteDatabase::new(database_path); - - let bdk_wallet = bdk::Wallet::new( - Bip84(xprv, bdk::KeychainKind::External), - Some(Bip84(xprv, bdk::KeychainKind::Internal)), - config.network.into(), - database, - ) - .map_err(|e| { - log_error!(logger, "Failed to set up wallet: {}", e); - BuildError::WalletSetupFailed - })?; + let descriptor = Bip84(xprv, KeychainKind::External); + let change_descriptor = Bip84(xprv, KeychainKind::Internal); + let wallet_persister = KVStoreWalletPersister::new(Arc::clone(&kv_store), Arc::clone(&logger)); + let wallet_opt = BdkWallet::load() + .descriptor(KeychainKind::External, Some(descriptor)) + .descriptor(KeychainKind::Internal, Some(change_descriptor)) + .extract_keys() + .check_network(config.network) + .load_wallet(&mut wallet_persister) + .map_err(|e| { + log_error!(logger, "Failed to set up wallet: {}", e); + BuildError::WalletSetupFailed + })?; + let mut bdk_wallet = match wallet_opt { + Some(wallet) => wallet, + None => BdkWallet::create(descriptor, change_descriptor) + .network(config.network) + .create_wallet(&mut wallet_persister) + .map_err(|e| { + log_error!(logger, "Failed to set up wallet: {}", e); + BuildError::WalletSetupFailed + })?, + }; let (blockchain, tx_sync, tx_broadcaster, fee_estimator) = match chain_data_source_config { Some(ChainDataSourceConfig::Esplora(server_url)) => { diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 2bca3c1c2..66e14d201 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -5,6 +5,8 @@ // http://opensource.org/licenses/MIT>, at your option. You may not use this file except in // accordance with one or both of these licenses. +use persist::KVStoreWalletPersister; + use crate::logger::{log_error, log_info, log_trace, Logger}; use crate::config::BDK_WALLET_SYNC_TIMEOUT_SECS; @@ -27,8 +29,7 @@ use lightning_invoice::RawBolt11Invoice; use bdk::blockchain::EsploraBlockchain; use bdk::{SignOptions, SyncOptions}; use bdk_chain::ChainPosition; -use bdk_wallet::KeychainKind; -use bdk_wallet::Wallet as BdkWallet; +use bdk_wallet::{KeychainKind, PersistedWallet}; use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR; use bitcoin::blockdata::locktime::absolute::LockTime; @@ -63,7 +64,7 @@ where // A BDK blockchain used for wallet sync. blockchain: EsploraBlockchain, // A BDK on-chain wallet. - inner: Mutex, + inner: Mutex>, // A cache storing the most recently retrieved fee rate estimations. broadcaster: B, fee_estimator: E, @@ -79,8 +80,8 @@ where L::Target: Logger, { pub(crate) fn new( - blockchain: EsploraBlockchain, wallet: BdkWallet, broadcaster: B, fee_estimator: E, - logger: L, + blockchain: EsploraBlockchain, wallet: bdk_wallet::PersistedWallet, + broadcaster: B, fee_estimator: E, logger: L, ) -> Self { let inner = Mutex::new(wallet); let sync_status = Mutex::new(WalletSyncStatus::Completed);