Skip to content

Commit

Permalink
f BDK: Account for persistence changes 3: switch to new Wallet API
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Sep 12, 2024
1 parent 2fa52c8 commit 934dbae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
69 changes: 34 additions & 35 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -532,36 +532,35 @@ fn build_with_store_internal(
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>,
) -> Result<Node, BuildError> {
// Initialize the on-chain wallet and chain access
let xprv = bitcoin::bip32::ExtendedPrivKey::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 xprv = bitcoin::bip32::Xpriv::new_master(config.network, &seed_bytes).map_err(|e| {
log_error!(logger, "Failed to derive master secret: {}", e);
BuildError::InvalidSeedBytes
})?;

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 mut wallet_persister =
KVStoreWalletPersister::new(Arc::clone(&kv_store), Arc::clone(&logger));
let wallet_opt = BdkWallet::load()
.descriptor(KeychainKind::External, Some(descriptor.clone()))
.descriptor(KeychainKind::Internal, Some(change_descriptor.clone()))
.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 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)) => {
Expand Down Expand Up @@ -741,7 +740,7 @@ fn build_with_store_internal(
} else {
// We're starting a fresh node.
let genesis_block_hash =
bitcoin::blockdata::constants::genesis_block(config.network.into()).block_hash();
bitcoin::blockdata::constants::genesis_block(config.network).block_hash();

let chain_params = ChainParameters {
network: config.network.into(),
Expand Down
11 changes: 6 additions & 5 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +28,7 @@ use lightning_invoice::RawBolt11Invoice;

use bdk::blockchain::EsploraBlockchain;
use bdk_chain::ChainPosition;
use bdk_wallet::{KeychainKind, SignOptions};
use bdk_wallet::Wallet as BdkWallet;
use bdk_wallet::{KeychainKind, PersistedWallet, SignOptions};

use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
use bitcoin::blockdata::locktime::absolute::LockTime;
Expand Down Expand Up @@ -62,7 +63,7 @@ where
// A BDK blockchain used for wallet sync.
blockchain: EsploraBlockchain,
// A BDK on-chain wallet.
inner: Mutex<BdkWallet>,
inner: Mutex<PersistedWallet<KVStoreWalletPersister>>,
// A cache storing the most recently retrieved fee rate estimations.
broadcaster: B,
fee_estimator: E,
Expand All @@ -78,8 +79,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<KVStoreWalletPersister>,
broadcaster: B, fee_estimator: E, logger: L,
) -> Self {
let inner = Mutex::new(wallet);
let sync_status = Mutex::new(WalletSyncStatus::Completed);
Expand Down

0 comments on commit 934dbae

Please sign in to comment.