]> Untitled Git - bdk-cli/commitdiff
Add esplora-reqwest from bdk
authorrajarshimaitra <rajarshi149@gmail.com>
Tue, 14 Sep 2021 10:04:43 +0000 (15:34 +0530)
committerrajarshimaitra <rajarshi149@gmail.com>
Sun, 3 Oct 2021 09:51:00 +0000 (15:21 +0530)
Adds esplora-reqwest capability from bdk.
Allows to use async https connections with esplora from bdk-cli.

src/bdk_cli.rs
src/lib.rs

index dcdd2c4c7f8abd5e4790c4007861ca2814ce5c95..d5df3ffbe6103bfecf0e029b1bf06dff1f4695f4 100644 (file)
@@ -125,7 +125,7 @@ where
         stop_gap: wallet_opts.electrum_opts.stop_gap,
     });
 
-    #[cfg(feature = "esplora")]
+    #[cfg(feature = "esplora-ureq")]
     let config = AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
         base_url: wallet_opts.esplora_opts.server.clone(),
         timeout_read: wallet_opts.esplora_opts.read_timeout,
@@ -133,6 +133,13 @@ where
         stop_gap: wallet_opts.esplora_opts.stop_gap,
     });
 
+    #[cfg(feature = "esplora-reqwest")]
+    let config = AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
+        base_url: wallet_opts.esplora_opts.server.clone(),
+        concurrency: Some(wallet_opts.esplora_opts.conc),
+        stop_gap: wallet_opts.esplora_opts.stop_gap,
+    });
+
     #[cfg(feature = "compact_filters")]
     let config = {
         let mut peers = vec![];
index c3da23629804706e8debb324a62ede60ab7f6f7d..899b517482bcfd802e27bb4ab049b88d725cddb5 100644 (file)
@@ -181,13 +181,19 @@ use bdk::{FeeRate, KeychainKind, Wallet};
 ///                   server: "ssl://electrum.blockstream.info:60002".to_string(),
 ///                   stop_gap: 10
 ///               },
-///               #[cfg(feature = "esplora")]
+///               #[cfg(feature = "esplora-ureq")]
 ///               esplora_opts: EsploraOpts {
 ///                   server: "https://blockstream.info/api/".to_string(),
 ///                   read_timeout: 5,
 ///                   write_timeout: 5,
 ///                   stop_gap: 10
 ///               },
+///               #[cfg(feature = "esplora-reqwest")]
+///               esplora_opts: EsploraOpts {
+///                   server: "https://blockstream.info/api/".to_string(),
+///                   conc: 4,
+///                   stop_gap: 10
+///               },
 ///                #[cfg(feature = "compact_filters")]
 ///                compactfilter_opts: CompactFilterOpts{
 ///                    address: vec!["127.0.0.1:18444".to_string()],
@@ -328,13 +334,19 @@ pub enum WalletSubCommand {
 ///                   server: "ssl://electrum.blockstream.info:60002".to_string(),
 ///                   stop_gap: 10
 ///               },
-///               #[cfg(feature = "esplora")]
+///               #[cfg(feature = "esplora-ureq")]
 ///               esplora_opts: EsploraOpts {
 ///                   server: "https://blockstream.info/api/".to_string(),
 ///                   read_timeout: 5,
 ///                   write_timeout: 5,
 ///                   stop_gap: 10
 ///               },
+///               #[cfg(feature = "esplora-reqwest")]
+///               esplora_opts: EsploraOpts {
+///                   server: "https://blockstream.info/api/".to_string(),
+///                   conc: 4,
+///                   stop_gap: 10
+///               },
 ///                #[cfg(feature = "compact_filters")]
 ///                compactfilter_opts: CompactFilterOpts{
 ///                    address: vec!["127.0.0.1:18444".to_string()],
@@ -468,7 +480,7 @@ pub struct ElectrumOpts {
 /// Esplora options
 ///
 /// Esplora blockchain client information used by [`OnlineWalletSubCommand`]s.
-#[cfg(feature = "esplora")]
+#[cfg(feature = "esplora-ureq")]
 #[derive(Debug, StructOpt, Clone, PartialEq)]
 pub struct EsploraOpts {
     /// Use the esplora server if given as parameter
@@ -498,6 +510,32 @@ pub struct EsploraOpts {
     pub stop_gap: usize,
 }
 
+#[cfg(feature = "esplora-reqwest")]
+#[derive(Debug, StructOpt, Clone, PartialEq)]
+pub struct EsploraOpts {
+    /// Use the esplora server if given as parameter
+    #[structopt(
+        name = "ESPLORA_URL",
+        short = "s",
+        long = "server",
+        default_value = "https://blockstream.info/api/"
+    )]
+    pub server: String,
+
+    /// Number of parallel requests sent to the esplora service (default: 4)
+    #[structopt(name = "CONCURRENCY", long = "conc", short = "p", default_value = "4")]
+    pub conc: u8,
+
+    /// Stop searching addresses for transactions after finding an unused gap of this length.
+    #[structopt(
+        name = "STOP_GAP",
+        long = "stop_gap",
+        short = "g",
+        default_value = "10"
+    )]
+    pub stop_gap: usize,
+}
+
 // This is a workaround for `structopt` issue #333, #391, #418; see https://github.com/TeXitoi/structopt/issues/333#issuecomment-712265332
 #[cfg_attr(not(doc), allow(missing_docs))]
 #[cfg_attr(
@@ -1142,13 +1180,19 @@ mod test {
                         server: "ssl://electrum.blockstream.info:60002".to_string(),
                         stop_gap: 10,
                     },
-                    #[cfg(feature = "esplora")]
+                    #[cfg(feature = "esplora-ureq")]
                     esplora_opts: EsploraOpts {
                         server: "https://blockstream.info/api/".to_string(),
                         read_timeout: 5,
                         write_timeout: 5,
                         stop_gap: 10,
                     },
+                    #[cfg(feature = "esplora-reqwest")]
+                    esplora_opts: EsploraOpts {
+                        server: "https://blockstream.info/api/".to_string(),
+                        conc: 4,
+                        stop_gap: 10,
+                    },
                     #[cfg(feature = "compact_filters")]
                     compactfilter_opts: CompactFilterOpts{
                         address: vec!["127.0.0.1:18444".to_string()],
@@ -1221,7 +1265,7 @@ mod test {
         assert_eq!(expected_cli_opts, cli_opts);
     }
 
-    #[cfg(feature = "esplora")]
+    #[cfg(feature = "esplora-ureq")]
     #[test]
     fn test_parse_wallet_esplora() {
         let cli_args = vec!["bdk-cli", "--network", "bitcoin", "wallet",
@@ -1275,6 +1319,58 @@ mod test {
         assert_eq!(expected_cli_opts, cli_opts);
     }
 
+    #[cfg(feature = "esplora-reqwest")]
+    #[test]
+    fn test_parse_wallet_esplora() {
+        let cli_args = vec!["bdk-cli", "--network", "bitcoin", "wallet",
+                            "--descriptor", "wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)",
+                            "--change_descriptor", "wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/1/*)",
+                            "--server", "https://blockstream.info/api/",
+                            "--conc", "10",
+                            "--stop_gap", "20",
+                            "get_new_address"];
+
+        let cli_opts = CliOpts::from_iter(&cli_args);
+
+        let expected_cli_opts = CliOpts {
+            network: Network::Bitcoin,
+            subcommand: CliSubCommand::Wallet {
+                wallet_opts: WalletOpts {
+                    wallet: "main".to_string(),
+                    verbose: false,
+                    descriptor: "wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)".to_string(),
+                    change_descriptor: Some("wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/1/*)".to_string()),
+                    #[cfg(feature = "electrum")]
+                    electrum_opts: ElectrumOpts {
+                        timeout: None,
+                        server: "ssl://electrum.blockstream.info:60002".to_string(),
+                    },
+                    #[cfg(feature = "esplora")]
+                    esplora_opts: EsploraOpts {
+                        server: "https://blockstream.info/api/".to_string(),
+                        conc: 10,
+                        stop_gap: 20
+                    },
+                    #[cfg(feature = "compact_filters")]
+                    compactfilter_opts: CompactFilterOpts{
+                        address: vec!["127.0.0.1:18444".to_string()],
+                        skip_blocks: 0,
+                        conn_count: 4,
+                    },
+                    #[cfg(any(feature="compact_filters", feature="electrum"))]
+                    proxy_opts: ProxyOpts{
+                        proxy: None,
+                        proxy_auth: None,
+                        retries: 5,
+                    }
+                },
+                subcommand: WalletSubCommand::OfflineWalletSubCommand(GetNewAddress),
+            },
+        };
+
+        assert_eq!(expected_cli_opts, cli_opts);
+    }
+
     #[cfg(feature = "compact_filters")]
     #[test]
     fn test_parse_wallet_compact_filters() {
@@ -1351,13 +1447,19 @@ mod test {
                         server: "ssl://electrum.blockstream.info:60002".to_string(),
                         stop_gap: 10,
                     },
-                    #[cfg(feature = "esplora")]
+                    #[cfg(feature = "esplora-ureq")]
                     esplora_opts: EsploraOpts {
                         server: "https://blockstream.info/api/".to_string(),
                         read_timeout: 5,
                         write_timeout: 5,
                         stop_gap: 10,
                     },
+                    #[cfg(feature = "esplora-reqwest")]
+                    esplora_opts: EsploraOpts {
+                        server: "https://blockstream.info/api/".to_string(),
+                        conc: 4,
+                        stop_gap: 10,
+                    },
                     #[cfg(feature = "compact_filters")]
                     compactfilter_opts: CompactFilterOpts{
                         address: vec!["127.0.0.1:18444".to_string()],
@@ -1420,13 +1522,19 @@ mod test {
                         server: "ssl://electrum.blockstream.info:60002".to_string(),
                         stop_gap: 10,
                     },
-                    #[cfg(feature = "esplora")]
+                    #[cfg(feature = "esplora-ureq")]
                     esplora_opts: EsploraOpts {
                         server: "https://blockstream.info/api/".to_string(),
                         read_timeout: 5,
                         write_timeout: 5,
                         stop_gap: 10,
                     },
+                    #[cfg(feature = "esplora-reqwest")]
+                    esplora_opts: EsploraOpts {
+                        server: "https://blockstream.info/api/".to_string(),
+                        conc: 4,
+                        stop_gap: 10,
+                    },
                     #[cfg(feature = "compact_filters")]
                     compactfilter_opts: CompactFilterOpts{
                         address: vec!["127.0.0.1:18444".to_string()],
@@ -1481,13 +1589,19 @@ mod test {
                         server: "ssl://electrum.blockstream.info:60002".to_string(),
                         stop_gap: 10,
                     },
-                    #[cfg(feature = "esplora")]
+                    #[cfg(feature = "esplora-ureq")]
                     esplora_opts: EsploraOpts {
                         server: "https://blockstream.info/api/".to_string(),
                         read_timeout: 5,
                         write_timeout: 5,
                         stop_gap: 10,
                     },
+                    #[cfg(feature = "esplora-reqwest")]
+                    esplora_opts: EsploraOpts {
+                        server: "https://blockstream.info/api/".to_string(),
+                        conc: 4,
+                        stop_gap: 10,
+                    },
                     #[cfg(feature = "compact_filters")]
                     compactfilter_opts: CompactFilterOpts{
                         address: vec!["127.0.0.1:18444".to_string()],