Skip to content

Commit

Permalink
add electrum support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbus committed Mar 11, 2024
1 parent 8552e16 commit 8503b7b
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 14 deletions.
104 changes: 99 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ bp-std = "0.11.0-beta.4"
psbt = "0.11.0-beta.4"
descriptors = "0.11.0-beta.4"
bp-esplora = "0.11.0-beta.4"
bitcoin = "0.31.1"
electrum-client = "0.19.0"
hex = "0.4.3"
serde_crate = { package = "serde", version = "1", features = ["derive"] }
serde_json = "1.0.114"
serde_with = "3.4.0"
serde_yaml = "0.9.19"
toml = "0.8.2"
Expand Down Expand Up @@ -48,17 +52,22 @@ name = "bpwallet"
amplify = { workspace = true }
bp-std = { workspace = true }
bp-esplora = { workspace = true, optional = true }
bitcoin = { workspace = true, optional = true }
electrum-client = { workspace = true, optional = true }
hex = { workspace = true, optional = true }
psbt = { workspace = true }
descriptors = { workspace = true }
serde_crate = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
serde_with = { workspace = true, optional = true }
serde_yaml = { workspace = true, optional = true }
toml = { workspace = true, optional = true }
cfg_eval = { workspace = true, optional = true }

[features]
default = []
all = ["esplora", "fs"]
all = ["electrum", "esplora", "fs"]
electrum = ["bitcoin", "electrum-client", "hex", "serde", "serde_json"]
esplora = ["bp-esplora"]
fs = ["serde"]
serde = ["cfg_eval", "serde_crate", "serde_with", "serde_yaml", "toml", "bp-std/serde"]
5 changes: 4 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ path = "src/bin/bp.rs"
[dependencies]
amplify = { workspace = true, features = ["serde"] }
strict_encoding = { workspace = true }
bp-wallet = { version = "0.11.0-beta.2", path = "..", features = ["serde", "fs", "esplora"] }
bp-wallet = { version = "0.11.0-beta.4", path = "..", features = ["serde", "fs", "electrum", "esplora"] }
bp-std = { workspace = true, features = ["serde"] }
descriptors = { workspace = true, features = ["serde"] }
psbt = { workspace = true, features = ["serde"] }
bp-esplora = { workspace = true }
bitcoin = { workspace = true }
electrum-client = { workspace = true }
hex = { workspace = true }
base64 = "0.21.5"
log = { workspace = true }
env_logger = "0.10.0"
Expand Down
14 changes: 11 additions & 3 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
use std::fmt::Debug;
use std::path::PathBuf;

use bpwallet::Runtime;
use bpwallet::{AnyIndexer, Runtime};
use clap::Subcommand;
use descriptors::Descriptor;
use strict_encoding::Ident;

use crate::opts::{DescrStdOpts, DescriptorOpts};
use crate::opts::{DescrStdOpts, DescriptorOpts, DEFAULT_ELECTRUM};
use crate::{Config, GeneralOpts, ResolverOpt, RuntimeError, WalletOpts};

/// Command-line arguments
Expand Down Expand Up @@ -118,7 +118,15 @@ impl<C: Clone + Eq + Debug + Subcommand, O: DescriptorOpts> Args<C, O> {

if sync || self.wallet.descriptor_opts.is_some() {
eprint!("Syncing");
let indexer = esplora::Builder::new(&self.resolver.esplora).build_blocking()?;
let indexer = if self.resolver.electrum != DEFAULT_ELECTRUM {
AnyIndexer::Electrum(Box::new(electrum_client::Client::new(
&self.resolver.electrum,
)?))
} else {
AnyIndexer::Esplora(Box::new(
esplora::Builder::new(&self.resolver.esplora).build_blocking()?,
))
};
if let Err(errors) = runtime.sync(&indexer) {
eprintln!(" partial, some requests has failed:");
for err in errors {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ pub use config::Config;
pub use loglevel::LogLevel;
pub use opts::{
DescrStdOpts, DescriptorOpts, GeneralOpts, ResolverOpt, WalletOpts, DATA_DIR, DATA_DIR_ENV,
DEFAULT_ESPLORA,
DEFAULT_ELECTRUM, DEFAULT_ESPLORA,
};
15 changes: 14 additions & 1 deletion cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ pub const DATA_DIR: &str = "~/Documents";
#[cfg(target_os = "android")]
pub const DATA_DIR: &str = ".";

pub const DEFAULT_ELECTRUM: &str = "example.com:50001";
pub const DEFAULT_ESPLORA: &str = "https://blockstream.info/testnet/api";

#[derive(Args, Clone, PartialEq, Eq, Debug)]
pub struct ResolverOpt {
/// Electrum server to use.
#[arg(
conflicts_with = "esplora",
long,
global = true,
default_value = DEFAULT_ELECTRUM,
env = "ELECRTUM_SERVER",
value_hint = ValueHint::Url,
value_name = "URL"
)]
pub electrum: String,

/// Esplora server to use.
#[arg(
short,
conflicts_with = "electrum",
long,
global = true,
default_value = DEFAULT_ESPLORA,
Expand Down
Loading

0 comments on commit 8503b7b

Please sign in to comment.