From: Steve Myers Date: Mon, 28 Dec 2020 04:22:58 +0000 (-0800) Subject: Add Network parser to lib.rs, remove from repl.rs X-Git-Tag: v0.1.0~6 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/enum.HexToArrayError.html?a=commitdiff_plain;h=d38efcb9e007e6baaea0ee321ee51d549b67d0d3;p=bdk-cli Add Network parser to lib.rs, remove from repl.rs --- diff --git a/src/lib.rs b/src/lib.rs index 232ecf7..d5ec993 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,7 @@ //! "sync", "--max_addresses", "50"]; //! let cli_opt = WalletOpt::from_iter(&cli_args); //! -//! let network = Network::from_str(cli_opt.network.as_str()).unwrap_or(Network::Testnet); +//! let network = cli_opt.network; //! //! let descriptor = cli_opt.descriptor.as_str(); //! let change_descriptor = cli_opt.change_descriptor.as_deref(); @@ -106,7 +106,7 @@ use structopt::StructOpt; use bdk::bitcoin::consensus::encode::{deserialize, serialize, serialize_hex}; use bdk::bitcoin::hashes::hex::FromHex; use bdk::bitcoin::util::psbt::PartiallySignedTransaction; -use bdk::bitcoin::{Address, OutPoint, Script, Txid}; +use bdk::bitcoin::{Address, Network, OutPoint, Script, Txid}; use bdk::blockchain::log_progress; use bdk::Error; use bdk::{FeeRate, KeychainKind, TxBuilder, Wallet}; @@ -120,6 +120,7 @@ use bdk::{FeeRate, KeychainKind, TxBuilder, Wallet}; /// # Example /// /// ``` +/// # use bdk::bitcoin::Network; /// # use structopt::StructOpt; /// # use bdk_cli::{WalletSubCommand, WalletOpt}; /// @@ -133,7 +134,7 @@ use bdk::{FeeRate, KeychainKind, TxBuilder, Wallet}; /// let wallet_opt = WalletOpt::from_iter(&cli_args); /// /// let expected_wallet_opt = WalletOpt { -/// network: "testnet".to_string(), +/// network: Network::Testnet, /// wallet: "main".to_string(), /// proxy: None, /// descriptor: "wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/44'/1'/0'/0/*)".to_string(), @@ -163,7 +164,7 @@ pub struct WalletOpt { long = "network", default_value = "testnet" )] - pub network: String, + pub network: Network, /// Selects the wallet to use #[structopt( name = "WALLET_NAME", @@ -222,11 +223,11 @@ pub struct WalletOpt { /// /// let sync_sub_command = WalletSubCommand::from_iter(&["repl", "sync", "--max_addresses", "50"]); /// assert!(matches!( -/// sync_sub_command, -/// WalletSubCommand::Sync { -/// max_addresses: Some(50) -/// } -/// )); +/// sync_sub_command, +/// WalletSubCommand::Sync { +/// max_addresses: Some(50) +/// } +/// )); /// ``` /// /// To capture wallet sub-commands from a string vector without a preceeding binary name you can @@ -590,7 +591,7 @@ where #[cfg(test)] mod test { use super::{WalletOpt, WalletSubCommand}; - use bdk::bitcoin::{Address, OutPoint}; + use bdk::bitcoin::{Address, Network, OutPoint}; use std::str::FromStr; use structopt::StructOpt; @@ -606,7 +607,7 @@ mod test { let wallet_opt = WalletOpt::from_iter(&cli_args); let expected_wallet_opt = WalletOpt { - network: "bitcoin".to_string(), + network: Network::Bitcoin, wallet: "main".to_string(), proxy: None, descriptor: "wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)".to_string(), @@ -631,7 +632,7 @@ mod test { let wallet_opt = WalletOpt::from_iter(&cli_args); let expected_wallet_opt = WalletOpt { - network: "testnet".to_string(), + network: Network::Testnet, wallet: "main".to_string(), proxy: None, descriptor: "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)".to_string(), @@ -677,7 +678,7 @@ mod test { .unwrap(); let expected_wallet_opt = WalletOpt { - network: "testnet".to_string(), + network: Network::Testnet, wallet: "main".to_string(), proxy: Some("127.0.0.1:9150".to_string()), descriptor: "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)".to_string(), @@ -713,7 +714,7 @@ mod test { let wallet_opt = WalletOpt::from_iter(&cli_args); let expected_wallet_opt = WalletOpt { - network: "testnet".to_string(), + network: Network::Testnet, wallet: "main".to_string(), proxy: None, descriptor: "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)".to_string(), @@ -731,4 +732,14 @@ mod test { assert_eq!(expected_wallet_opt, wallet_opt); } + + #[test] + fn test_wrong_network() { + let cli_args = vec!["repl", "--network", "badnet", + "--descriptor", "wpkh(tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)", + "sync", "--max_addresses", "50"]; + + let wallet_opt = WalletOpt::from_iter_safe(&cli_args); + assert!(wallet_opt.is_err()); + } } diff --git a/src/repl.rs b/src/repl.rs index 281456a..17b6cad 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -24,7 +24,6 @@ use std::fs; use std::path::PathBuf; -use std::str::FromStr; use std::sync::Arc; use bitcoin::Network; @@ -73,7 +72,7 @@ fn main() { let cli_opt: WalletOpt = WalletOpt::from_args(); - let network = Network::from_str(cli_opt.network.as_str()).unwrap_or(Network::Testnet); + let network = cli_opt.network; debug!("network: {:?}", network); if network == Network::Bitcoin { warn!("This is experimental software and not currently recommended for use on Bitcoin mainnet, proceed with caution.")