Skip to content

Commit 0885c16

Browse files
committed
feat: add wallet init
- remove storing network in config file as it top level parameter - fix clippy issues
1 parent 3473017 commit 0885c16

File tree

5 files changed

+32
-43
lines changed

5 files changed

+32
-43
lines changed

Justfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ rpc command wallet=default_wallet:
102102
bitcoin-cli -datadir={{default_datadir}} -regtest -rpcwallet={{wallet}} -rpcuser={{rpc_user}} -rpcpassword={{rpc_password}} {{command}}
103103

104104
[group('wallet')]
105-
init network wallet_name ext_descriptor int_descriptor client_type url database_type='sqlite' rpc_user='user' rpc_password='pass' force='false':
105+
init wallet_name ext_descriptor int_descriptor client_type url database_type='sqlite' rpc_user='user' rpc_password='pass' force='false':
106106
mkdir -p {{default_datadir}}
107107
# Check if wallet configuration exists
108108
if [ "{{force}}" = "false" ] && grep -Fx "[wallets.{{wallet_name}}]" {{default_datadir}}/config.toml > /dev/null; then \
@@ -119,7 +119,6 @@ init network wallet_name ext_descriptor int_descriptor client_type url database_
119119
echo "" >> {{default_datadir}}/config.toml || touch {{default_datadir}}/config.toml
120120
echo "[wallets.{{wallet_name}}]" >> {{default_datadir}}/config.toml
121121
echo "name = \"{{wallet_name}}\"" >> {{default_datadir}}/config.toml
122-
echo "network = \"{{network}}\"" >> {{default_datadir}}/config.toml
123122
echo "ext_descriptor = \"{{ext_descriptor}}\"" >> {{default_datadir}}/config.toml
124123
echo "int_descriptor = \"{{int_descriptor}}\"" >> {{default_datadir}}/config.toml
125124
echo "database_type = \"sqlite\"" >> {{default_datadir}}/config.toml

src/commands.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ impl WalletOpts {
231231
}
232232
#[cfg(feature = "sqlite")]
233233
{
234-
self.database_type = self.database_type.clone().or(config_opts.database_type);
234+
// prioritizing the config.toml value for database type as it has a default value
235+
self.database_type = config_opts.database_type.or(self.database_type.clone());
235236
}
236237
#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]
237238
{

src/config.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use crate::commands::ClientType;
88
use crate::commands::{DatabaseType, WalletOpts};
99
use crate::error::BDKCliError as Error;
10-
use bdk_wallet::bitcoin::Network;
1110
use serde::Deserialize;
1211
use std::collections::HashMap;
1312
use std::fs;
@@ -21,13 +20,26 @@ pub struct WalletConfig {
2120
#[derive(Debug, Deserialize)]
2221
pub struct WalletConfigInner {
2322
pub name: String,
24-
pub network: String,
2523
pub ext_descriptor: String,
2624
pub int_descriptor: String,
2725
pub database_type: String,
26+
#[cfg(any(
27+
feature = "electrum",
28+
feature = "esplora",
29+
feature = "rpc",
30+
feature = "cbf"
31+
))]
2832
pub client_type: String,
33+
#[cfg(any(
34+
feature = "electrum",
35+
feature = "esplora",
36+
feature = "rpc",
37+
feature = "cbf"
38+
))]
2939
pub server_url: String,
40+
#[cfg(feature = "rpc")]
3041
pub rpc_user: String,
42+
#[cfg(feature = "rpc")]
3143
pub rpc_password: String,
3244
}
3345

@@ -39,10 +51,9 @@ impl WalletConfig {
3951
return Ok(None);
4052
}
4153
let config_content = fs::read_to_string(&config_path)
42-
.map_err(|e| Error::Generic(format!("Failed to read config file: {}", e.to_string())))?;
43-
let config: WalletConfig = toml::from_str(&config_content).map_err(|e| {
44-
Error::Generic(format!("Failed to parse config file: {}", e.to_string()))
45-
})?;
54+
.map_err(|e| Error::Generic(format!("Failed to read config file: {}", e)))?;
55+
let config: WalletConfig = toml::from_str(&config_content)
56+
.map_err(|e| Error::Generic(format!("Failed to parse config file: {}", e)))?;
4657
Ok(Some(config))
4758
}
4859

@@ -53,14 +64,6 @@ impl WalletConfig {
5364
.get(wallet_name)
5465
.ok_or_else(|| Error::Generic(format!("Wallet {} not found in config", wallet_name)))?;
5566

56-
let network = match wallet_config.network.as_str() {
57-
"bitcoin" => Network::Bitcoin,
58-
"testnet" => Network::Testnet,
59-
"regtest" => Network::Regtest,
60-
"signet" => Network::Signet,
61-
_ => return Err(Error::InvalidNetwork),
62-
};
63-
6467
#[cfg(feature = "sqlite")]
6568
let database_type = match wallet_config.database_type.as_str() {
6669
"sqlite" => DatabaseType::Sqlite,
@@ -129,5 +132,4 @@ impl WalletConfig {
129132
},
130133
})
131134
}
132-
133135
}

src/error.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ pub enum BDKCliError {
3535

3636
#[error("Hex conversion error: {0}")]
3737
HexToArrayError(#[from] bdk_wallet::bitcoin::hashes::hex::HexToArrayError),
38-
39-
#[error("Invalid network")]
40-
InvalidNetwork,
41-
38+
4239
#[error("Key error: {0}")]
4340
KeyError(#[from] bdk_wallet::keys::KeyError),
4441

src/handlers.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -744,25 +744,23 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
744744
feature = "rpc"
745745
))]
746746
CliSubCommand::Wallet {
747-
wallet_opts,
747+
mut wallet_opts,
748748
subcommand: WalletSubCommand::OnlineWalletSubCommand(online_subcommand),
749749
} => {
750750
let network = cli_opts.network;
751751
let home_dir = prepare_home_dir(cli_opts.datadir)?;
752752
let wallet_name = &wallet_opts.wallet;
753-
let database_path =
754-
prepare_wallet_db_dir(wallet_name, &home_dir, &mut wallet_opts.clone())?;
753+
let database_path = prepare_wallet_db_dir(&home_dir, &mut wallet_opts)?;
755754
#[cfg(feature = "sqlite")]
756755
let result = {
757756
let mut persister = match &wallet_opts.database_type {
758-
#[cfg(feature = "sqlite")]
759757
Some(DatabaseType::Sqlite) => {
760758
let db_file = database_path.join("wallet.sqlite");
761759
let connection = Connection::open(db_file)?;
762760
log::debug!("Sqlite database opened successfully");
763761
connection
764-
},
765-
None => return Err(Error::Generic("Dataase type is required".to_string())),
762+
}
763+
None => return Err(Error::Generic("Dataase type is required".to_string())),
766764
};
767765

768766
let mut wallet = new_persisted_wallet(network, &mut persister, &wallet_opts)?;
@@ -790,25 +788,22 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
790788
serde_json::to_string_pretty(&result)
791789
}
792790
CliSubCommand::Wallet {
793-
wallet_opts,
791+
mut wallet_opts,
794792
subcommand: WalletSubCommand::OfflineWalletSubCommand(offline_subcommand),
795793
} => {
796794
let network = cli_opts.network;
797795
#[cfg(feature = "sqlite")]
798796
let result = {
799797
let home_dir = prepare_home_dir(cli_opts.datadir)?;
800-
let wallet_name = &wallet_opts.wallet;
801-
let database_path =
802-
prepare_wallet_db_dir(wallet_name, &home_dir, &mut wallet_opts.clone())?;
798+
let database_path = prepare_wallet_db_dir(&home_dir, &mut wallet_opts)?;
803799
let mut persister = match &wallet_opts.database_type {
804-
#[cfg(feature = "sqlite")]
805800
Some(DatabaseType::Sqlite) => {
806801
let db_file = database_path.join("wallet.sqlite");
807802
let connection = Connection::open(db_file)?;
808803
log::debug!("Sqlite database opened successfully");
809804
connection
810-
},
811-
None => return Err(Error::Generic("Databas type is required".to_string())),
805+
}
806+
None => return Err(Error::Generic("Database type is required".to_string())),
812807
};
813808

814809
let mut wallet = new_persisted_wallet(network, &mut persister, &wallet_opts)?;
@@ -846,12 +841,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
846841
let network = cli_opts.network;
847842
#[cfg(feature = "sqlite")]
848843
let (mut wallet, mut persister) = {
849-
let wallet_name = &wallet_opts.wallet;
850-
851844
let home_dir = prepare_home_dir(cli_opts.datadir.clone())?;
852-
853-
let database_path =
854-
prepare_wallet_db_dir(wallet_name, &home_dir, &mut wallet_opts.clone())?;
845+
let database_path = prepare_wallet_db_dir(&home_dir, &mut wallet_opts.clone())?;
855846

856847
let mut persister = match &wallet_opts.database_type {
857848
#[cfg(feature = "sqlite")]
@@ -860,7 +851,7 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
860851
let connection = Connection::open(db_file)?;
861852
log::debug!("Sqlite database opened successfully");
862853
connection
863-
},
854+
}
864855
None => return Err(Error::Generic("Database typ is required".to_string())),
865856
};
866857
let wallet = new_persisted_wallet(network, &mut persister, &wallet_opts)?;
@@ -869,8 +860,7 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
869860
#[cfg(not(any(feature = "sqlite")))]
870861
let mut wallet = new_wallet(network, &wallet_opts)?;
871862
let home_dir = prepare_home_dir(cli_opts.datadir.clone())?;
872-
let database_path =
873-
prepare_wallet_db_dir(&wallet_opts.wallet, &home_dir, &mut wallet_opts.clone())?;
863+
let database_path = prepare_wallet_db_dir(&home_dir, &mut wallet_opts.clone())?;
874864
loop {
875865
let line = readline()?;
876866
let line = line.trim();

0 commit comments

Comments
 (0)