-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconfig.rs
809 lines (727 loc) · 25.7 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
use std::{fs, io, net, path, time};
use bitcoin::{Address, Network};
use bitcoincore_rpc::Auth as RpcAuth;
use crate::error::{Context, OptionExt, Result};
use crate::query::QueryConfig;
use crate::types::RescanSince;
use crate::util::auth::AuthMethod;
use crate::util::descriptor::ExtendedDescriptor;
use crate::util::xpub::XyzPubKey;
use crate::util::BoolThen;
#[cfg(any(feature = "pretty_env_logger", feature = "android_logger"))]
use log::Level;
#[cfg(all(feature = "pretty_env_logger", not(feature = "android_logger")))]
use pretty_env_logger::env_logger::Builder as LogBuilder;
#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
#[cfg_attr(feature = "cli", derive(structopt::StructOpt))]
pub struct Config {
//
// General options
//
/// One of 'bitcoin', 'testnet', 'signet' or 'regtest'
#[cfg_attr(
feature = "cli",
structopt(
short = "n",
long,
default_value = "bitcoin",
env,
hide_env_values(true),
display_order(1)
)
)]
#[serde(default = "default_network")]
pub network: Network,
/// Increase verbosity level (up to 4 times) [env: VERBOSE]
// cannot be set using an env var, it does not play nicely with from_occurrences
#[cfg_attr(
feature = "cli",
structopt(short = "v", long, parse(from_occurrences), display_order(1000))
)]
#[serde(default)]
pub verbose: usize,
/// Show timestmaps in log messages [ENV: LOG_TIMESTAMP]
#[cfg_attr(feature = "cli", structopt(short = "T", long, display_order(1001)))]
#[serde(default)]
pub timestamp: bool,
//
// Bitcoin Core options
//
/// Specify the bitcoind wallet to use (optional)
#[cfg_attr(
feature = "cli",
structopt(short = "w", long, env, hide_env_values(true), display_order(30))
)]
pub bitcoind_wallet: Option<String>,
/// Path to bitcoind directory (used for cookie file) [default: ~/.bitcoin]
#[cfg_attr(
feature = "cli",
structopt(short = "r", long, env, hide_env_values(true), display_order(31))
)]
pub bitcoind_dir: Option<path::PathBuf>,
/// URL for the bitcoind RPC server [default: http://localhost:<network-rpc-port>]
#[cfg_attr(
feature = "cli",
structopt(short = "u", long, env, hide_env_values(true), display_order(32))
)]
pub bitcoind_url: Option<String>,
/// Credentials for accessing the bitcoind RPC server (as <username>:<password>, used instead of the cookie file)
#[cfg_attr(
feature = "cli",
structopt(
short = "a",
long,
alias = "bitcoind-cred",
env,
hide_env_values(true),
display_order(33)
)
)]
pub bitcoind_auth: Option<String>,
/// Cookie file for accessing the bitcoind RPC server [default: <bitcoind-dir>/.cookie]
#[cfg_attr(
feature = "cli",
structopt(short = "c", long, env, hide_env_values(true), display_order(34))
)]
pub bitcoind_cookie: Option<path::PathBuf>,
/// SOCKS5h proxy server for connecting to the Bitcoin Core RPC
#[cfg(feature = "proxy")]
#[cfg_attr(
feature = "cli",
structopt(short = "P", long, env, hide_env_values(true), display_order(35))
)]
pub bitcoind_proxy: Option<String>,
/// The connect/read/write timeout for the RPC connection (in seconds)
#[cfg_attr(
feature = "cli",
structopt(short = "m", long, env, hide_env_values(true), display_order(36), parse(try_from_str = parse_duration))
)]
#[serde(default, deserialize_with = "parse_duration_serde_opt")]
pub bitcoind_timeout: Option<time::Duration>,
/// Create the specified bitcoind wallet if it's missing [env: CREATE_WALLET_IF_MISSING]
#[cfg_attr(feature = "cli", structopt(long, short = "W", display_order(1002)))]
#[serde(default)]
pub create_wallet_if_missing: bool,
//
// Wallet tracking settings
//
/// Add a descriptor to track
#[cfg_attr(feature = "cli", structopt(
short = "d",
parse(try_from_str = parse_desc),
long = "descriptor",
env, hide_env_values(true),
use_delimiter(true), value_delimiter(";"),
display_order(20)
))]
#[serde(default)]
pub descriptors: Vec<ExtendedDescriptor>,
/// Add an extended public key to track (with separate internal/external chains)
#[cfg_attr(
feature = "cli",
structopt(
short = "x",
long = "xpub",
env,
hide_env_values(true),
use_delimiter(true),
value_delimiter(";"),
display_order(21)
)
)]
#[serde(default)]
pub xpubs: Vec<XyzPubKey>,
/// Add an address to track
#[cfg_attr(
feature = "cli",
structopt(
short = "A",
long = "address",
env,
hide_env_values(true),
use_delimiter(true),
value_delimiter(";"),
display_order(23)
)
)]
#[serde(default)]
pub addresses: Vec<Address>,
/// File with addresses to track
#[cfg_attr(
feature = "cli",
structopt(short = "f", long, env, hide_env_values(true), display_order(24))
)]
#[serde(default)]
pub addresses_file: Option<path::PathBuf>,
/// Start date for wallet history rescan. Accepts YYYY-MM-DD formatted strings, unix timestamps, or 'now' to watch for new transactions only. Defaults to rescanning from genesis.
// (defaults to scanning from genesis for structopt/cli use, or to 'now' for direct library use)
#[cfg_attr(
feature = "cli",
structopt(
short = "R",
long,
parse(try_from_str = parse_rescan),
default_value = "0",
env,
hide_env_values(true),
display_order(28)
)
)]
#[serde(default = "default_rescan_since")]
pub rescan_since: RescanSince,
/// Force rescanning for historical transactions, even if the addresses were already previously imported [env: FORCE_RESCAN]
#[cfg_attr(feature = "cli", structopt(short = "F", long, display_order(1003)))]
#[serde(default)]
pub force_rescan: bool,
/// Gap limit for importing child addresses
#[cfg_attr(
feature = "cli",
structopt(
short = "g",
long,
default_value = "20",
env,
hide_env_values(true),
display_order(51)
)
)]
#[serde(default = "default_gap_limit")]
pub gap_limit: u32,
/// The batch size for importing addresses during the initial sync (set higher to reduce number of rescans)
#[cfg_attr(
feature = "cli",
structopt(
short = "G",
long,
default_value = "350",
env,
hide_env_values(true),
display_order(52)
)
)]
#[serde(default = "default_initial_import_size")]
pub initial_import_size: u32,
/// Don't wait for bitcoind to finish syncing up before starting bwt (useful with pruning for
/// importing/scanning before blocks get pruned) [env: NO_WAIT_SYNC]
#[cfg_attr(feature = "cli", structopt(
long = "no-wait-sync",
short = "I",
parse(from_flag = std::ops::Not::not),
display_order(1004)
))]
#[serde(default = "default_true")]
pub wait_sync: bool,
/// Prune the chain until the given height, unix timestamp or YYYY-MM-DD formatted date
#[cfg_attr(feature = "cli", structopt(short = "p", long, parse(try_from_str = parse_prune_until),
env,
hide_env_values(true), display_order(1009)))]
#[serde(default)]
pub prune_until: Option<u64>,
//
// Auth settings
//
/// Generate an access token and persist it to the specified cookie file
#[cfg_attr(
feature = "cli",
structopt(short = "C", long, env, hide_env_values(true), display_order(40))
)]
pub auth_cookie: Option<path::PathBuf>,
/// Set access token for authentication
#[cfg_attr(
feature = "cli",
structopt(short = "t", long, env, hide_env_values(true), display_order(41))
)]
pub auth_token: Option<String>,
#[cfg_attr(feature = "cli", structopt(skip = false))]
#[serde(default)]
pub auth_ephemeral: bool,
/// Print access token (useful with --auth-cookie) [env: PRINT_TOKEN]
#[cfg_attr(feature = "cli", structopt(long, display_order(1005)))]
#[serde(default)]
pub print_token: bool,
//
// Electrum options
//
/// Address to bind the electrum rpc server [default: '127.0.0.1:50001' for mainnet, '127.0.0.1:60001' for testnet, '127.0.0.1:60601' for signet or '127.0.0.1:60401' for regtest]
#[cfg(feature = "electrum")]
#[cfg_attr(
feature = "cli",
structopt(short = "e", long, env, hide_env_values(true), display_order(43))
)]
pub electrum_addr: Option<net::SocketAddr>,
/// Skip generating merkle proofs. Reduces resource usage, requires running Electrum with --skipmerklecheck. [env: ELECTRUM_SKIP_MERKLE]
#[cfg(feature = "electrum")]
#[cfg_attr(feature = "cli", structopt(long, short = "M", display_order(1006)))]
#[serde(default)]
pub electrum_skip_merkle: bool,
/// Enable the Electrum SOCKS5-based authentication mechanism
/// (see https://github.com/bwt-dev/bwt/blob/master/doc/auth.md) [env: ELECTRUM_SOCKS_AUTH]
#[cfg(feature = "electrum")]
#[cfg_attr(feature = "cli", structopt(long, short = "5", display_order(1007)))]
#[serde(default)]
pub electrum_socks_auth: bool,
//
// HTTP options
//
/// Address to bind the http api server [default: 127.0.0.1:3060]
#[cfg(feature = "http")]
#[cfg_attr(
feature = "cli",
structopt(short, long, env, hide_env_values(true), display_order(45))
)]
pub http_addr: Option<net::SocketAddr>,
/// Allowed cross-origins for http api server (Access-Control-Allow-Origin)
#[cfg(feature = "http")]
#[cfg_attr(
feature = "cli",
structopt(short = "S", long, env, hide_env_values(true), display_order(46))
)]
pub http_cors: Option<String>,
//
// Miscellaneous options
//
/// Interval for checking for new blocks/seconds (in seconds)
#[cfg_attr(feature = "cli", structopt(
short = "i",
long,
default_value = "5",
parse(try_from_str = parse_duration),
env, hide_env_values(true),
display_order(90)
))]
#[serde(
default = "default_poll_interval",
deserialize_with = "parse_duration_serde"
)]
pub poll_interval: time::Duration,
/// Custom command for broadcasting transactions. {tx_hex} is replaced with the transaction.
#[cfg_attr(
feature = "cli",
structopt(
short = "B",
long = "tx-broadcast-cmd",
env,
hide_env_values(true),
display_order(91)
)
)]
pub broadcast_cmd: Option<String>,
/// Disable the startup banner [env: NO_STARTUP_BANNER]
#[cfg_attr(feature = "cli", structopt(
long = "no-startup-banner",
parse(from_flag = std::ops::Not::not),
display_order(1008)
))]
#[serde(default)]
pub startup_banner: bool,
/// Path to bind the sync notification unix socket
#[cfg(unix)]
#[cfg_attr(
feature = "cli",
structopt(long, short = "U", env, hide_env_values(true), display_order(101))
)]
pub unix_listener_path: Option<path::PathBuf>,
/// Webhook url(s) to notify with index event updates
#[cfg(feature = "webhooks")]
#[cfg_attr(
feature = "cli",
structopt(
long = "webhook-url",
short = "H",
env,
hide_env_values(true),
use_delimiter(true),
value_delimiter(";"),
display_order(102)
)
)]
pub webhook_urls: Option<Vec<String>>,
// Not exposed as a CLI option, always set to true for CLI use
#[cfg_attr(feature = "cli", structopt(skip = true))]
#[serde(default = "default_true")]
pub require_addresses: bool,
// Not exposed as a CLI option, always set to true for CLI use
#[cfg_attr(feature = "cli", structopt(skip = true))]
#[serde(default = "default_true")]
pub setup_logger: bool,
}
impl Config {
pub fn bitcoind_url(&self) -> String {
format!(
"{}/{}",
self.bitcoind_url.as_ref().map_or_else(
|| {
format!(
"http://localhost:{}",
match self.network {
Network::Bitcoin => 8332,
Network::Testnet => 18332,
Network::Regtest => 18443,
Network::Signet => 38332,
}
)
},
|url| url.trim_end_matches('/').into()
),
match self.bitcoind_wallet {
Some(ref wallet) => format!("wallet/{}", wallet),
None => "".into(),
}
)
}
pub fn bitcoind_auth(&self) -> Result<RpcAuth> {
Ok(self.bitcoind_auth
.as_ref()
.and_then(|auth| {
let mut parts = auth.splitn(2, ':');
Some(RpcAuth::UserPass(parts.next()?.into(), parts.next()?.into()))
})
.or_else(|| {
let cookie = self.bitcoind_cookie.clone().or_else(|| get_cookie(self))?;
Some(RpcAuth::CookieFile(cookie))
})
.or_err("no valid authentication found for bitcoind rpc, specify user/pass or a cookie file")?)
}
pub fn addresses(&self) -> Result<Vec<Address>> {
let mut addresses = self.addresses.clone();
if let Some(addresses_file) = &self.addresses_file {
let file = fs::File::open(addresses_file).context("failed opening addresses file")?;
let reader = io::BufReader::new(file);
addresses.append(
&mut io::BufRead::lines(reader)
.filter_map(|l| {
let l = l.ok()?;
let l = l.trim();
(!l.is_empty()).do_then(|| l.parse())
})
.collect::<std::result::Result<Vec<_>, _>>()?,
);
}
Ok(addresses)
}
pub fn auth_method(&self) -> Result<AuthMethod> {
Ok(
match (&self.auth_cookie, &self.auth_token, self.auth_ephemeral) {
(Some(cookie), None, false) => AuthMethod::Cookie(cookie.clone()),
(None, Some(token), false) => AuthMethod::UserProvided(token.clone()),
(None, None, true) => AuthMethod::Ephemeral,
(None, None, false) => AuthMethod::None,
_ => bail!("Invalid combination of authentication options"),
},
)
}
#[cfg(feature = "electrum")]
pub fn electrum_addr(&self) -> Option<net::SocketAddr> {
self.electrum_addr.clone().or_else(|| {
// Use a default value when used as CLI, require explicitly setting it for library use
#[cfg(feature = "cli")]
return Some(net::SocketAddr::new(
[127, 0, 0, 1].into(),
match self.network {
Network::Bitcoin => 50001,
Network::Testnet => 60001,
Network::Regtest => 60401,
Network::Signet => 60601,
},
));
#[cfg(not(feature = "cli"))]
return None;
})
}
#[cfg(feature = "http")]
pub fn http_addr(&self) -> Option<net::SocketAddr> {
self.http_addr.clone().or_else(|| {
// Use a default value when used as CLI, require explicitly setting it for library use
#[cfg(feature = "cli")]
return Some(([127, 0, 0, 1], 3060).into());
#[cfg(not(feature = "cli"))]
return None;
})
}
#[cfg(feature = "cli")]
pub fn from_args_env() -> Result<Config> {
use std::env;
use structopt::StructOpt;
let mut config = Self::from_args();
// Setting boolean options as env vars is not supported by clap/structopt
// https://github.com/TeXitoi/structopt/issues/305
let bool_env = |key| env::var(key).map_or(false, |val| !val.is_empty() && val != "0");
if bool_env("FORCE_RESCAN") {
config.force_rescan = true;
}
if bool_env("CREATE_WALLET_IF_MISSING") {
config.create_wallet_if_missing = true;
}
if bool_env("NO_WAIT_SYNC") {
config.wait_sync = false;
}
if bool_env("NO_STARTUP_BANNER") {
config.startup_banner = false;
}
if bool_env("NO_REQUIRE_ADDRESSES") {
config.require_addresses = false;
}
if bool_env("PRINT_TOKEN") {
config.print_token = true;
}
if bool_env("LOG_TIMESTAMP") {
config.timestamp = true;
}
#[cfg(feature = "electrum")]
if bool_env("ELECTRUM_SKIP_MERKLE") {
config.electrum_skip_merkle = true;
}
#[cfg(feature = "electrum")]
if bool_env("ELECTRUM_SOCKS_AUTH") {
config.electrum_socks_auth = true;
}
if let Ok(verbose) = env::var("VERBOSE") {
config.verbose = iif!(verbose.is_empty(), 0, verbose.parse().unwrap_or(1));
}
// Support configuring xpubs/descriptors using wildcard env vars
// (XPUB_* and DESCRIPTOR_* / DESC_*)
for (key, val) in env::vars() {
if key.starts_with("XPUB_") || key == "XPUB" {
config.xpubs.push(val.parse()?);
}
if key.starts_with("DESC_") || key.starts_with("DESCRIPTOR_") || key == "DESCRIPTOR" {
config.descriptors.push(val.parse()?);
}
if key.starts_with("ADDRESS_") {
config.addresses.push(val.parse()?);
}
}
if config.prune_until.is_some() && config.wait_sync {
warn!("prune-until was enabled without no-wait-sync. This means that the chain won't get pruned until after its fully synced. Consider enabling no-wait-sync.");
}
Ok(config)
}
#[cfg(feature = "cli")]
pub fn dotenv() {
dirs::home_dir().map(|home| dotenv::from_path(home.join("bwt.env")).ok());
}
#[cfg(all(not(feature = "pretty_env_logger"), not(feature = "android_logger")))]
pub fn setup_logger(&self) {}
#[cfg(any(feature = "pretty_env_logger", feature = "android_logger"))]
pub fn setup_logger(&self) {
if !self.setup_logger {
return;
}
// If both pretty_env_logger and android_logger are enabled, android_logger takes priority
#[cfg(all(feature = "pretty_env_logger", not(feature = "android_logger")))]
let mut builder = apply_log_env(if self.timestamp {
pretty_env_logger::formatted_timed_builder()
} else {
pretty_env_logger::formatted_builder()
});
#[cfg(feature = "android_logger")]
let mut builder = android_logger::FilterBuilder::from_env("RUST_LOG");
builder
.filter_module(
"bwt",
match self.verbose {
0 => Level::Info,
1 => Level::Debug,
_ => Level::Trace,
}
.to_level_filter(),
)
.filter_module(
"bitcoincore_rpc",
match self.verbose {
0 | 1 => Level::Warn,
2 => Level::Debug,
_ => Level::Trace,
}
.to_level_filter(),
)
.filter_module(
"warp",
match self.verbose {
0 | 1 => Level::Warn,
2 => Level::Info,
3 => Level::Debug,
_ => Level::Trace,
}
.to_level_filter(),
)
.filter_module("hyper", Level::Warn.to_level_filter())
.filter_level(
match self.verbose {
0 | 1 => Level::Warn,
2 | 3 => Level::Info,
4 => Level::Debug,
_ => Level::Trace,
}
.to_level_filter(),
);
#[cfg(all(feature = "pretty_env_logger", not(feature = "android_logger")))]
builder.init();
#[cfg(feature = "android_logger")]
android_logger::init_once(
android_logger::Config::default()
.with_min_level(match self.verbose {
0 => Level::Info,
1 => Level::Debug,
_ => Level::Trace,
})
.with_filter(builder.build()),
);
}
}
#[cfg(all(feature = "pretty_env_logger", not(feature = "android_logger")))]
fn apply_log_env(mut builder: LogBuilder) -> LogBuilder {
use std::env;
if let Ok(s) = env::var("RUST_LOG") {
builder.parse_filters(&s);
}
if let Ok(s) = env::var("RUST_LOG_STYLE") {
builder.parse_write_style(&s);
}
builder
}
#[cfg(feature = "cli")]
fn parse_desc(s: &str) -> Result<ExtendedDescriptor> {
use crate::util::descriptor::DescriptorExt;
Ok(ExtendedDescriptor::parse_canonical(s)?)
}
#[cfg(feature = "cli")]
fn parse_rescan(s: &str) -> Result<RescanSince> {
Ok(match s {
"all" | "genesis" => RescanSince::Timestamp(0),
"now" | "none" => RescanSince::Now,
s => {
// try as a unix timestamp first, then as a date string
RescanSince::Timestamp(
s.parse::<u64>()
.or_else(|_| parse_yyyymmdd(s))
.context("invalid rescan-since value")?,
)
}
})
}
#[cfg(feature = "cli")]
fn parse_prune_until(s: &str) -> Result<u64> {
// try as a number (height or unix timestamp) first, then as a date string
s.parse::<u64>()
.or_else(|_| parse_yyyymmdd(s))
.context("invalid prune-until value")
}
#[cfg(feature = "cli")]
fn parse_yyyymmdd(s: &str) -> Result<u64> {
use chrono::{TimeZone, Utc};
let mut parts = s.splitn(3, '-');
Ok(Utc
.ymd_opt(
parts.next().required()?.parse()?,
parts.next().required()?.parse()?,
parts.next().required()?.parse()?,
)
.single()
.required()?
.and_hms(0, 0, 0)
.timestamp() as u64)
}
#[cfg(feature = "cli")]
fn parse_duration(s: &str) -> Result<time::Duration> {
Ok(time::Duration::from_secs(s.parse()?))
}
fn parse_duration_serde<'de, D>(deserializer: D) -> std::result::Result<time::Duration, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let secs = u64::deserialize(deserializer)?;
Ok(time::Duration::from_secs(secs))
}
fn parse_duration_serde_opt<'de, D>(
deserializer: D,
) -> std::result::Result<Option<time::Duration>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Some(parse_duration_serde(deserializer)?))
}
fn get_cookie(config: &Config) -> Option<path::PathBuf> {
let mut dir = config.bitcoind_dir.clone().or_else(bitcoind_default_dir)?;
match config.network {
Network::Bitcoin => (),
Network::Testnet => dir.push("testnet3"),
Network::Regtest => dir.push("regtest"),
Network::Signet => dir.push("signet"),
}
let cookie = dir.join(".cookie");
iif!(cookie.exists(), Some(cookie), None)
}
#[cfg(feature = "dirs")]
fn bitcoind_default_dir() -> Option<path::PathBuf> {
// Windows: C:\Users\Satoshi\Appdata\Roaming\Bitcoin
#[cfg(target_os = "windows")]
return Some(dirs::data_dir()?.join("Bitcoin"));
// macOS: ~/Library/Application Support/Bitcoin
#[cfg(target_os = "macos")]
return Some(dirs::config_dir()?.join("Bitcoin"));
// Linux and others: ~/.bitcoin
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
return Some(dirs::home_dir()?.join(".bitcoin"));
}
#[cfg(not(feature = "dirs"))]
fn bitcoind_default_dir() -> Option<path::PathBuf> {
None
}
impl From<&Config> for QueryConfig {
fn from(config: &Config) -> QueryConfig {
QueryConfig {
network: config.network,
broadcast_cmd: config.broadcast_cmd.clone(),
}
}
}
// NOTE: the default values below are also duplicated in structopt's attributes
// Create a Default implementation
defaultable!(Config,
@default(
verbose, timestamp, broadcast_cmd, startup_banner, prune_until,
descriptors, xpubs, addresses, addresses_file, force_rescan,
bitcoind_wallet, bitcoind_dir, bitcoind_url, bitcoind_auth, bitcoind_cookie, bitcoind_timeout, create_wallet_if_missing,
auth_cookie, auth_token, auth_ephemeral, print_token,
#[cfg(feature = "electrum")] electrum_addr,
#[cfg(feature = "electrum")] electrum_skip_merkle,
#[cfg(feature = "electrum")] electrum_socks_auth,
#[cfg(feature = "http")] http_addr,
#[cfg(feature = "http")] http_cors,
#[cfg(feature = "webhooks")] webhook_urls,
#[cfg(feature = "proxy")] bitcoind_proxy,
#[cfg(unix)] unix_listener_path,
)
@custom(
network=Network::Bitcoin,
rescan_since=RescanSince::Now,
gap_limit=20,
initial_import_size=350,
poll_interval=time::Duration::from_secs(5),
wait_sync=true,
require_addresses=true,
setup_logger=true,
)
);
// Used for serde's default attributes, which must be provided as functions
fn default_network() -> Network {
Network::Bitcoin
}
fn default_rescan_since() -> RescanSince {
RescanSince::Now
}
fn default_gap_limit() -> u32 {
20
}
fn default_initial_import_size() -> u32 {
350
}
fn default_poll_interval() -> time::Duration {
time::Duration::from_secs(5)
}
fn default_true() -> bool {
true
}