[examples] support esplora blockchain source in repl

This commit is contained in:
Riccardo Casatta 2020-11-16 12:23:02 +01:00
parent c5dba115a0
commit d9985c4bbb
No known key found for this signature in database
GPG Key ID: FD986A969E450397
2 changed files with 31 additions and 15 deletions

View File

@ -37,13 +37,15 @@ use log::{debug, error, info, trace, LevelFilter};
use bitcoin::Network; use bitcoin::Network;
use bdk::bitcoin; use bdk::bitcoin;
use bdk::blockchain::ConfigurableBlockchain; use bdk::blockchain::{
use bdk::blockchain::ElectrumBlockchain; AnyBlockchain, AnyBlockchainConfig, ConfigurableBlockchain, ElectrumBlockchainConfig,
use bdk::blockchain::ElectrumBlockchainConfig; };
use bdk::cli; use bdk::cli;
use bdk::sled; use bdk::sled;
use bdk::Wallet; use bdk::Wallet;
use bdk::blockchain::esplora::EsploraBlockchainConfig;
fn prepare_home_dir() -> PathBuf { fn prepare_home_dir() -> PathBuf {
let mut dir = PathBuf::new(); let mut dir = PathBuf::new();
dir.push(&dirs::home_dir().unwrap()); dir.push(&dirs::home_dir().unwrap());
@ -90,19 +92,25 @@ fn main() {
.unwrap(); .unwrap();
debug!("database opened successfully"); debug!("database opened successfully");
let blockchain_config = ElectrumBlockchainConfig { let config = match matches.value_of("esplora") {
url: matches.value_of("server").unwrap().to_string(), Some(base_url) => AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
socks5: matches.value_of("proxy").map(ToString::to_string), base_url: base_url.to_string(),
}),
None => AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
url: matches.value_of("server").unwrap().to_string(),
socks5: matches.value_of("proxy").map(ToString::to_string),
}),
}; };
let wallet = Wallet::new( let wallet = Arc::new(
descriptor, Wallet::new(
change_descriptor, descriptor,
network, change_descriptor,
tree, network,
ElectrumBlockchain::from_config(&blockchain_config).unwrap(), tree,
) AnyBlockchain::from_config(&config).unwrap(),
.unwrap(); )
let wallet = Arc::new(wallet); .unwrap(),
);
if let Some(_sub_matches) = matches.subcommand_matches("repl") { if let Some(_sub_matches) = matches.subcommand_matches("repl") {
let mut rl = Editor::<()>::new(); let mut rl = Editor::<()>::new();

View File

@ -337,6 +337,14 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
.takes_value(true) .takes_value(true)
.default_value("ssl://electrum.blockstream.info:60002"), .default_value("ssl://electrum.blockstream.info:60002"),
) )
.arg(
Arg::with_name("esplora")
.short("e")
.long("esplora")
.value_name("ESPLORA")
.help("Use the esplora server if given as parameter")
.takes_value(true),
)
.arg( .arg(
Arg::with_name("proxy") Arg::with_name("proxy")
.short("p") .short("p")