Skip to content

Commit

Permalink
rewrite wallet binary with structopt and new environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Melnik committed Sep 26, 2019
1 parent 4f78a2d commit 298699c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 125 deletions.
2 changes: 1 addition & 1 deletion rust-wallet-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ crossbeam = "0.7"
bitcoin_rpc_client = { git = "https://github.com/LightningPeach/bitcoinrpc-rust-client.git", package = "bitcoincore-rpc" }
log = "0.4"
hex = "0.3"
clap = "2.32"
structopt = "0.3"
simple_logger = "1.0"

[dev-dependencies]
Expand Down
3 changes: 1 addition & 2 deletions rust-wallet-grpc/src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
extern crate grpc;
extern crate tls_api;
extern crate tls_api_native_tls;
extern crate clap;
extern crate wallet;
extern crate rust_wallet_grpc;

use clap::{Arg, App, SubCommand};
use structopt::clap::{Arg, App, SubCommand};

use wallet::account::AccountAddressType;
use rust_wallet_grpc::{
Expand Down
2 changes: 2 additions & 0 deletions rust-wallet-grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct WalletClientWrapper {

impl WalletClientWrapper {
pub fn new(port: u16) -> WalletClientWrapper {
use grpc::ClientStubExt;

// let port = 50051;
let client_conf = Default::default();
let client = WalletClient::new_plain("127.0.0.1", port, client_conf).unwrap();
Expand Down
223 changes: 101 additions & 122 deletions rust-wallet-grpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,137 +13,116 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use clap::{Arg, App};

use std::str::FromStr;

use bitcoin_core_io::BitcoinCoreIO;
use wallet::{
walletlibrary::{
WalletConfig, BitcoindConfig, WalletLibraryMode, KeyGenConfig,
DEFAULT_NETWORK, DEFAULT_PASSPHRASE, DEFAULT_SALT, DEFAULT_DB_PATH,
DEFAULT_BITCOIND_RPC_CONNECT, DEFAULT_BITCOIND_RPC_USER, DEFAULT_BITCOIND_RPC_PASSWORD,
DEFAULT_ZMQ_PUB_RAW_BLOCK_ENDPOINT, DEFAULT_ZMQ_PUB_RAW_TX_ENDPOINT,
},
default::WalletWithTrustedFullNode,
electrumx::ElectrumxWallet,
interface::Wallet,
};
use rust_wallet_grpc::server::{launch_server_new, DEFAULT_WALLET_RPC_PORT};
use structopt::StructOpt;
use std::path::PathBuf;
use wallet::mnemonic::Mnemonic;

#[derive(StructOpt, Debug)]
#[structopt(name = "wallet")]
/// Rust Wallet Config
pub struct Config {
#[structopt(long="log-level", default_value="INFO")]
/// should be one of ERROR, WARN, INFO, DEBUG, TRACE
log_level: String,

#[structopt(long="db-path", parse(from_os_str), default_value="target/db/wallet")]
/// path to directory with wallet data
db_path: PathBuf,

#[structopt(long="rpc-port", default_value="5051")]
/// port of wallet's grpc server
rpc_port: u16,

#[structopt(long="zmqpubrawblock", default_value="tcp://127.0.0.1:18501")]
/// address of bitcoind's zmqpubrawblock endpoint
/// relevant only if `bitcoind_uri` is not specified
zmqpubrawblock: String,

#[structopt(long="zmqpubrawtx", default_value="tcp://127.0.0.1:18501")]
/// address of bitcoind's zmqpubrawtx endpoint
/// relevant only if `bitcoind_uri` is not specified
zmqpubrawtx: String,

#[structopt(long="user")]
/// bitcoind's rpc user
user: String,

#[structopt(long="password")]
/// bitcoind's rpc password
password: String,

#[structopt(long="bitcoin-address")]
/// address of bitcoind's rpc server, run bitcoind locally if not specified
bitcoind_address: Option<String>,

#[structopt(long="electrumx-address")]
/// address of bitcoind's rpc server, run electrs locally if not specified
/// relevant only if `electrumx` flag is set
electrumx_address: Option<String>,

#[structopt(long="electrumx")]
/// create electrumx wallet
electrumx: bool,

#[structopt(long="mode", default_value="decrypt")]
/// should be one of create|decrypt|recover
mode: String,

#[structopt(long="mnemonic")]
/// relevant only `mode` is recover
mnemonic: Option<String>,
}

fn main() {
use bitcoin_rpc_client::{Client, Auth};

let default_wallet_rpc_port_str: &str = &DEFAULT_WALLET_RPC_PORT.to_string();

let matches = App::new("wallet")
.version("1.0")
.arg(
Arg::with_name("log_level")
.long("log_level")
.help("should be one of ERROR, WARN, INFO, DEBUG, TRACE")
.takes_value(true)
.default_value("INFO"),
)
.arg(
Arg::with_name("db_path")
.long("db_path")
.help("path to file with wallet data")
.takes_value(true)
.default_value(DEFAULT_DB_PATH),
)
.arg(
Arg::with_name("connect")
.long("connect")
.help("address of bitcoind's rpc server")
.takes_value(true)
.default_value(DEFAULT_BITCOIND_RPC_CONNECT),
)
.arg(
Arg::with_name("user")
.long("user")
.help("bitcoind's rpc user")
.takes_value(true)
.default_value(DEFAULT_BITCOIND_RPC_USER),
)
.arg(
Arg::with_name("password")
.long("password")
.help("bitcoind's rpc password")
.takes_value(true)
.default_value(DEFAULT_BITCOIND_RPC_PASSWORD),
)
.arg(
Arg::with_name("zmqpubrawblock")
.long("zmqpubrawblock")
.help("address of bitcoind's zmqpubrawblock endpoint")
.takes_value(true)
.default_value(DEFAULT_ZMQ_PUB_RAW_BLOCK_ENDPOINT),
)
.arg(
Arg::with_name("zmqpubrawtx")
.long("zmqpubrawtx")
.help("address of bitcoind's zmqpubrawtx endpoint")
.takes_value(true)
.default_value(DEFAULT_ZMQ_PUB_RAW_TX_ENDPOINT),
)
.arg(
Arg::with_name("wallet_rpc_port")
.long("wallet_rpc_port")
.help("port of wallet's grpc server")
.takes_value(true)
.default_value(default_wallet_rpc_port_str),
)
.arg(Arg::with_name("electrumx").long("electrumx"))
.get_matches();

let log_level = {
let rez = matches.value_of("log_level").unwrap();
let rez = log::Level::from_str(rez).unwrap();
rez
};
use rust_wallet_grpc::server;
use std::str::FromStr;

use wallet::{walletlibrary::{WalletLibraryMode, KeyGenConfig, DEFAULT_NETWORK}, context::GlobalContext};

let config: Config = Config::from_args();

let log_level = log::Level::from_str(config.log_level.as_str()).unwrap();
simple_logger::init_with_level(log_level).unwrap();

let wc = WalletConfig::new(
let context = GlobalContext::new(
DEFAULT_NETWORK,
DEFAULT_PASSPHRASE.to_string(),
DEFAULT_SALT.to_string(),
matches.value_of("db_path").unwrap().to_string(),
config.user,
config.password,
Some(config.db_path.to_str().unwrap().to_owned()),
config.bitcoind_address.as_ref().map(|s| s.parse().unwrap()),
config.electrumx_address.as_ref().map(|s| s.parse().unwrap()),
);

let cfg = BitcoindConfig::new(
matches.value_of("connect").unwrap().to_string(),
matches.value_of("user").unwrap().to_string(),
matches.value_of("password").unwrap().to_string(),
matches.value_of("zmqpubrawblock").unwrap().to_string(),
matches.value_of("zmqpubrawtx").unwrap().to_string(),
);
// if `bitcoind_uri` is not specified run bitcoind locally
let bitcoind = if config.bitcoind_address.is_none() {
Some(context.bitcoind(config.zmqpubrawblock, config.zmqpubrawtx).unwrap())
} else {
None
};

// if `electrumx_uri` is not specified run electrs locally
let electrs = if config.electrumx_address.is_none() {
Some(context.electrs().unwrap())
} else {
None
};

let mode = if config.mode == "create" {
WalletLibraryMode::Create(KeyGenConfig::default())
} else if config.mode == "recover" {
let mnemonic = config.mnemonic.unwrap();
WalletLibraryMode::RecoverFromMnemonic(Mnemonic::from(mnemonic.trim_matches('"')).unwrap())
} else {
WalletLibraryMode::Decrypt
};

// TODO(evg): rewrite it; add --create param; use WalletLibraryMode::Decrypt mode as well
let wallet: Box<dyn Wallet + Send> = if matches.is_present("electrumx") {
let (electrumx_wallet, mnemonic) =
ElectrumxWallet::new(wc, WalletLibraryMode::Create(KeyGenConfig::default())).unwrap();
println!("{}", mnemonic.to_string());
Box::new(electrumx_wallet)
let (wallet_context, mnemonic) = if config.electrumx {
context.electrs_context(mode).unwrap()
} else {
let bio = BitcoinCoreIO::new(Client::new(
cfg.url,
Auth::UserPass(cfg.user, cfg.password)
).unwrap());
let (default_wallet, mnemonic) = WalletWithTrustedFullNode::new(
wc,
bio,
WalletLibraryMode::Create(KeyGenConfig::default()),
)
.unwrap();
println!("{}", mnemonic.to_string());
Box::new(default_wallet)
context.default_context(mode).unwrap()
};
println!("{}", mnemonic.to_string());

let wallet_rpc_port: u16 = matches
.value_of("wallet_rpc_port")
.unwrap()
.parse()
.unwrap();
launch_server_new(wallet, wallet_rpc_port);
server::launch_server_new(wallet_context.wallet(), config.rpc_port);
let _ = (bitcoind, electrs);
}

0 comments on commit 298699c

Please sign in to comment.