1414
1515#![ allow( clippy:: large_enum_variant) ]
1616
17+ use std:: path:: Path ;
18+ use crate :: config:: WalletConfig ;
19+ use crate :: error:: BDKCliError as Error ;
1720use bdk_wallet:: bitcoin:: {
1821 bip32:: { DerivationPath , Xpriv } ,
1922 Address , Network , OutPoint , ScriptBuf ,
@@ -167,15 +170,15 @@ pub struct WalletOpts {
167170 feature = "rpc" ,
168171 feature = "cbf"
169172 ) ) ]
170- #[ arg( env = "CLIENT_TYPE" , short = 'c' , long, value_enum, required = true ) ]
171- pub client_type : ClientType ,
173+ #[ arg( env = "CLIENT_TYPE" , short = 'c' , long, value_enum) ]
174+ pub client_type : Option < ClientType > ,
172175 #[ cfg( feature = "sqlite" ) ]
173- #[ arg( env = "DATABASE_TYPE" , short = 'd' , long, value_enum, required = true ) ]
174- pub database_type : DatabaseType ,
176+ #[ arg( env = "DATABASE_TYPE" , short = 'd' , long, value_enum, default_value= "sqlite" ) ]
177+ pub database_type : Option < DatabaseType > ,
175178 /// Sets the server url.
176179 #[ cfg( any( feature = "electrum" , feature = "esplora" , feature = "rpc" ) ) ]
177- #[ arg( env = "SERVER_URL" , short = 'u' , long, required = true ) ]
178- pub url : String ,
180+ #[ arg( env = "SERVER_URL" , short = 'u' , long) ]
181+ pub url : Option < String > ,
179182 /// Electrum batch size.
180183 #[ cfg( feature = "electrum" ) ]
181184 #[ arg( env = "ELECTRUM_BATCH_SIZE" , short = 'b' , long, default_value = "10" ) ]
@@ -198,7 +201,7 @@ pub struct WalletOpts {
198201 value_parser = parse_proxy_auth,
199202 default_value = "user:password" ,
200203 ) ]
201- pub basic_auth : ( String , String ) ,
204+ pub basic_auth : Option < ( String , String ) > ,
202205 #[ cfg( feature = "rpc" ) ]
203206 /// Sets an optional cookie authentication.
204207 #[ arg( env = "COOKIE" ) ]
@@ -208,6 +211,64 @@ pub struct WalletOpts {
208211 pub compactfilter_opts : CompactFilterOpts ,
209212}
210213
214+ impl WalletOpts {
215+ /// Load configuration from TOML file and merge with CLI options
216+ pub fn load_config ( & mut self , wallet_name : & str , datadir : & Path ) -> Result < ( ) , Error > {
217+ if let Some ( config) = WalletConfig :: load ( datadir) ? {
218+ if let Ok ( config_opts) = config. get_wallet_opts ( wallet_name) {
219+ self . wallet = self . wallet . take ( ) . or ( config_opts. wallet ) ;
220+ self . verbose = self . verbose || config_opts. verbose ;
221+ self . ext_descriptor = self . ext_descriptor . take ( ) . or ( config_opts. ext_descriptor ) ;
222+ self . int_descriptor = self . int_descriptor . take ( ) . or ( config_opts. int_descriptor ) ;
223+ #[ cfg( any(
224+ feature = "electrum" ,
225+ feature = "esplora" ,
226+ feature = "rpc" ,
227+ feature = "cbf"
228+ ) ) ]
229+ {
230+ self . client_type = self . client_type . clone ( ) . or ( config_opts. client_type ) ;
231+ }
232+ #[ cfg( feature = "sqlite" ) ]
233+ {
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 ( ) ) ;
236+ }
237+ #[ cfg( any( feature = "electrum" , feature = "esplora" , feature = "rpc" ) ) ]
238+ {
239+ self . url = self . url . take ( ) . or ( config_opts. url ) ;
240+ }
241+ #[ cfg( feature = "electrum" ) ]
242+ {
243+ self . batch_size = if self . batch_size != 10 {
244+ config_opts. batch_size
245+ } else {
246+ self . batch_size
247+ } ;
248+ }
249+ #[ cfg( feature = "esplora" ) ]
250+ {
251+ self . parallel_requests = if self . parallel_requests != 5 {
252+ config_opts. parallel_requests
253+ } else {
254+ self . parallel_requests
255+ } ;
256+ }
257+ #[ cfg( feature = "rpc" ) ]
258+ {
259+ self . basic_auth = self . basic_auth . take ( ) . or ( config_opts. basic_auth ) ;
260+ self . cookie = self . cookie . take ( ) . or ( config_opts. cookie ) ;
261+ }
262+ #[ cfg( feature = "cbf" ) ]
263+ {
264+ self . compactfilter_opts = config_opts. compactfilter_opts ;
265+ }
266+ }
267+ }
268+ Ok ( ( ) )
269+ }
270+ }
271+
211272/// Options to configure a SOCKS5 proxy for a blockchain client connection.
212273#[ cfg( any( feature = "electrum" , feature = "esplora" ) ) ]
213274#[ derive( Debug , Args , Clone , PartialEq , Eq ) ]
0 commit comments