From 29b5e4459bfc44831904f0f36dca167163ce0417 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 09:38:35 -0700 Subject: [PATCH 1/9] Update version number --- Cargo.toml | 2 +- lib/src/lightwallet.rs | 2 -- src/main.rs | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9cd0e83..4b0a909 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zecwallet-cli" -version = "1.0.0" +version = "1.1.0" edition = "2018" [dependencies] diff --git a/lib/src/lightwallet.rs b/lib/src/lightwallet.rs index 5fa0d67..9a3e0f8 100644 --- a/lib/src/lightwallet.rs +++ b/lib/src/lightwallet.rs @@ -39,7 +39,6 @@ use zcash_primitives::{ primitives::{PaymentAddress}, }; - use crate::lightclient::{LightClientConfig}; mod data; @@ -58,7 +57,6 @@ fn now() -> f64 { SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as f64 } - /// Sha256(Sha256(value)) pub fn double_sha256(payload: &[u8]) -> Vec { let h1 = Sha256::digest(&payload); diff --git a/src/main.rs b/src/main.rs index 780a08c..96acb52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,7 +54,7 @@ pub fn main() { // Get command line arguments use clap::{Arg, App}; let matches = App::new("Zecwallet CLI") - .version("1.0.0") + .version("1.1.0") .arg(Arg::with_name("seed") .short("s") .long("seed") From 50988da0f52d5822a8aabe6f8604117add90516f Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 10:41:29 -0700 Subject: [PATCH 2/9] Fix windows xcompile for libsodium --- docker/Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docker/Dockerfile b/docker/Dockerfile index 9636f82..d2669cb 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -25,3 +25,8 @@ ENV CC_armv7_unknown_linux_gnueabhihf="arm-linux-gnueabihf-gcc" # This is a bug fix for the windows cross compiler for Rust. RUN cp /usr/x86_64-w64-mingw32/lib/crt2.o /usr/local/rustup/toolchains/1.38.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/crt2.o +# For windows cross compilation, use a pre-build binary +RUN cd /opt && wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.17-mingw.tar.gz && \ + tar xvf libsodium-1.0.17-mingw.tar.gz + +ENV SODIUM_LIB_DIR="/opt/libsodium-win64/lib/" \ No newline at end of file From 274f3c9c09f246a82f6c0df553ecd1abf3c2d459 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 10:50:11 -0700 Subject: [PATCH 3/9] Fix libsodium cross compile for win --- docker/Dockerfile | 7 +++---- mkrelease.sh | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index d2669cb..9c78c80 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -25,8 +25,7 @@ ENV CC_armv7_unknown_linux_gnueabhihf="arm-linux-gnueabihf-gcc" # This is a bug fix for the windows cross compiler for Rust. RUN cp /usr/x86_64-w64-mingw32/lib/crt2.o /usr/local/rustup/toolchains/1.38.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/crt2.o -# For windows cross compilation, use a pre-build binary +# For windows cross compilation, use a pre-build binary. Remember to set the +# SODIUM_LIB_DIR for windows cross compilation RUN cd /opt && wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.17-mingw.tar.gz && \ - tar xvf libsodium-1.0.17-mingw.tar.gz - -ENV SODIUM_LIB_DIR="/opt/libsodium-win64/lib/" \ No newline at end of file + tar xvf libsodium-1.0.17-mingw.tar.gz \ No newline at end of file diff --git a/mkrelease.sh b/mkrelease.sh index d201628..e3101a1 100755 --- a/mkrelease.sh +++ b/mkrelease.sh @@ -32,7 +32,7 @@ if [ -z $APP_VERSION ]; then echo "APP_VERSION is not set"; exit 1; fi #cargo build --release # For Windows and Linux, build via docker -docker run --rm -v $(pwd)/:/opt/zecwallet-light-cli rustbuild:latest bash -c "cd /opt/zecwallet-light-cli && cargo build --release && cargo build --release --target x86_64-pc-windows-gnu" +docker run --rm -v $(pwd)/:/opt/zecwallet-light-cli rustbuild:latest bash -c "cd /opt/zecwallet-light-cli && cargo build --release && SODIUM_LIB_DIR='/opt/libsodium-win64/lib/' cargo build --release --target x86_64-pc-windows-gnu" # Now sign and zip the binaries #macOS From 8ab297208d653b6f79714dde98fa0f2381c3c0ab Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 10:59:42 -0700 Subject: [PATCH 4/9] Do clean first --- mkrelease.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mkrelease.sh b/mkrelease.sh index e3101a1..67c3887 100755 --- a/mkrelease.sh +++ b/mkrelease.sh @@ -26,10 +26,10 @@ set -- "${POSITIONAL[@]}" # restore positional parameters if [ -z $APP_VERSION ]; then echo "APP_VERSION is not set"; exit 1; fi # Clean everything first -#cargo clean +cargo clean # Compile for mac directly -#cargo build --release +cargo build --release # For Windows and Linux, build via docker docker run --rm -v $(pwd)/:/opt/zecwallet-light-cli rustbuild:latest bash -c "cd /opt/zecwallet-light-cli && cargo build --release && SODIUM_LIB_DIR='/opt/libsodium-win64/lib/' cargo build --release --target x86_64-pc-windows-gnu" From 674a9ee1858baad06c25b449666136935518c31f Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 12:04:19 -0700 Subject: [PATCH 5/9] Fix build script --- mkrelease.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mkrelease.sh b/mkrelease.sh index 67c3887..c328780 100755 --- a/mkrelease.sh +++ b/mkrelease.sh @@ -31,14 +31,16 @@ cargo clean # Compile for mac directly cargo build --release +#macOS +rm -rf target/macOS-zecwallet-cli-v$APP_VERSION +mkdir -p target/macOS-zecwallet-cli-v$APP_VERSION +cp target/release/zecwallet-cli target/macOS-zecwallet-cli-v$APP_VERSION/ + # For Windows and Linux, build via docker docker run --rm -v $(pwd)/:/opt/zecwallet-light-cli rustbuild:latest bash -c "cd /opt/zecwallet-light-cli && cargo build --release && SODIUM_LIB_DIR='/opt/libsodium-win64/lib/' cargo build --release --target x86_64-pc-windows-gnu" # Now sign and zip the binaries #macOS -rm -rf target/macOS-zecwallet-cli-v$APP_VERSION -mkdir -p target/macOS-zecwallet-cli-v$APP_VERSION -cp target/release/zecwallet-cli target/macOS-zecwallet-cli-v$APP_VERSION/ gpg --batch --output target/macOS-zecwallet-cli-v$APP_VERSION/zecwallet-cli.sig --detach-sig target/macOS-zecwallet-cli-v$APP_VERSION/zecwallet-cli cd target cd macOS-zecwallet-cli-v$APP_VERSION From 27fb3f984b75ac0c4a1ad0627f3c9192f374bb96 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 12:32:15 -0700 Subject: [PATCH 6/9] Fix issue where new wallets weren't being created --- lib/src/lightclient.rs | 28 ++++++++++++++++++++++++++-- src/main.rs | 11 +++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index 877bebc..45dfe32 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -244,8 +244,32 @@ impl LightClient { Ok(l) } + /// Create a brand new wallet with a new seed phrase. Will fail if a wallet file + /// already exists on disk + pub fn new(config: &LightClientConfig, latest_block: u64) -> io::Result { + if config.wallet_exists() { + return Err(Error::new(ErrorKind::AlreadyExists, + "Cannot create a new wallet from seed, because a wallet already exists")); + } + + let mut l = LightClient { + wallet : Arc::new(RwLock::new(LightWallet::new(None, config, latest_block)?)), + config : config.clone(), + sapling_output : vec![], + sapling_spend : vec![] + }; + + l.set_wallet_initial_state(); + l.read_sapling_params(); + + info!("Created new wallet with a new seed!"); + info!("Created LightClient to {}", &config.server); + + Ok(l) + } + pub fn new_from_phrase(seed_phrase: String, config: &LightClientConfig, latest_block: u64) -> io::Result { - if config.get_wallet_path().exists() { + if config.wallet_exists() { return Err(Error::new(ErrorKind::AlreadyExists, "Cannot create a new wallet from seed, because a wallet already exists")); } @@ -267,7 +291,7 @@ impl LightClient { } pub fn read_from_disk(config: &LightClientConfig) -> io::Result { - if !config.get_wallet_path().exists() { + if !config.wallet_exists() { return Err(Error::new(ErrorKind::AlreadyExists, format!("Cannot read wallet. No file at {}", config.get_wallet_path().display()))); } diff --git a/src/main.rs b/src/main.rs index 96acb52..125f20a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,7 @@ pub fn main() { Some(13) => { startup_helpers::report_permission_error(); }, - _ => eprintln!("Something else!") + _ => {} } return; } @@ -162,7 +162,14 @@ fn startup(server: http::Uri, dangerous: bool, seed: Option, first_sync: let lightclient = match seed { Some(phrase) => Arc::new(LightClient::new_from_phrase(phrase, &config, latest_block_height)?), - None => Arc::new(LightClient::read_from_disk(&config)?) + None => { + if config.wallet_exists() { + Arc::new(LightClient::read_from_disk(&config)?) + } else { + println!("Creating a new wallet"); + Arc::new(LightClient::new(&config, latest_block_height)?) + } + } }; // Print startup Messages From 94e70cc3f776ce2b3d15cf03e5cded649e46a9d8 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 13:00:31 -0700 Subject: [PATCH 7/9] Fix bip39 bug before encryption --- lib/src/commands.rs | 10 ++++++++++ mkrelease.sh | 12 +++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/src/commands.rs b/lib/src/commands.rs index fd6b2ca..f8d282c 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -231,6 +231,16 @@ impl Command for EncryptCommand { return self.help(); } + // Refuse to encrypt if the bip39 bug has not been fixed + use crate::lightwallet::bugs::BugBip39Derivation; + if BugBip39Derivation::has_bug(lightclient) { + let mut h = vec![]; + h.push("It looks like your wallet has the bop39bug. Please run 'fixbip39bug' to fix it"); + h.push("before encrypting your wallet."); + h.push("ERROR: Cannot encrypt while wallet has the bip39bug."); + return h.join("\n"); + } + let passwd = args[0].to_string(); match lightclient.wallet.write().unwrap().encrypt(passwd) { diff --git a/mkrelease.sh b/mkrelease.sh index c328780..134569f 100755 --- a/mkrelease.sh +++ b/mkrelease.sh @@ -25,8 +25,14 @@ set -- "${POSITIONAL[@]}" # restore positional parameters if [ -z $APP_VERSION ]; then echo "APP_VERSION is not set"; exit 1; fi -# Clean everything first -cargo clean +# First, do the tests +cd lib && cargo test --release +retVal=$? +if [ $retVal -ne 0 ]; then + echo "Error" + exit $retVal +fi +cd .. # Compile for mac directly cargo build --release @@ -40,7 +46,7 @@ cp target/release/zecwallet-cli target/macOS-zecwallet-cli-v$APP_VERSION/ docker run --rm -v $(pwd)/:/opt/zecwallet-light-cli rustbuild:latest bash -c "cd /opt/zecwallet-light-cli && cargo build --release && SODIUM_LIB_DIR='/opt/libsodium-win64/lib/' cargo build --release --target x86_64-pc-windows-gnu" # Now sign and zip the binaries -#macOS +# macOS gpg --batch --output target/macOS-zecwallet-cli-v$APP_VERSION/zecwallet-cli.sig --detach-sig target/macOS-zecwallet-cli-v$APP_VERSION/zecwallet-cli cd target cd macOS-zecwallet-cli-v$APP_VERSION From 5cf2c955e3436d9ddda1c0125746f95e8f3ac523 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 13:09:47 -0700 Subject: [PATCH 8/9] Don't fix bug when no money is involved. --- lib/src/commands.rs | 2 +- lib/src/lightwallet/bugs.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/src/commands.rs b/lib/src/commands.rs index f8d282c..075dbe8 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -235,7 +235,7 @@ impl Command for EncryptCommand { use crate::lightwallet::bugs::BugBip39Derivation; if BugBip39Derivation::has_bug(lightclient) { let mut h = vec![]; - h.push("It looks like your wallet has the bop39bug. Please run 'fixbip39bug' to fix it"); + h.push("It looks like your wallet has the bip39bug. Please run 'fixbip39bug' to fix it"); h.push("before encrypting your wallet."); h.push("ERROR: Cannot encrypt while wallet has the bip39bug."); return h.join("\n"); diff --git a/lib/src/lightwallet/bugs.rs b/lib/src/lightwallet/bugs.rs index 48eeba7..ed4295b 100644 --- a/lib/src/lightwallet/bugs.rs +++ b/lib/src/lightwallet/bugs.rs @@ -73,13 +73,12 @@ impl BugBip39Derivation { println!("Sending funds to ourself."); let zaddr = client.do_address()["z_addresses"][0].as_str().unwrap().to_string(); let balance_json = client.do_balance(); - let fee: u64 = DEFAULT_FEE.try_into().unwrap(); let amount: u64 = balance_json["zbalance"].as_u64().unwrap() - + balance_json["tbalance"].as_u64().unwrap() - - fee; + + balance_json["tbalance"].as_u64().unwrap(); let txid = if amount > 0 { - match client.do_send(vec![(&zaddr, amount, None)]) { + let fee: u64 = DEFAULT_FEE.try_into().unwrap(); + match client.do_send(vec![(&zaddr, amount-fee, None)]) { Ok(txid) => txid, Err(e) => { let r = object!{ From 654f526918446aac31e9653447785c0da47bc4dc Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 13:20:54 -0700 Subject: [PATCH 9/9] Check for wallet encryption first. --- lib/src/lightwallet/bugs.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/lightwallet/bugs.rs b/lib/src/lightwallet/bugs.rs index ed4295b..0e9c925 100644 --- a/lib/src/lightwallet/bugs.rs +++ b/lib/src/lightwallet/bugs.rs @@ -28,6 +28,10 @@ impl BugBip39Derivation { return false; } + if wallet.is_encrypted() { + return false; + } + // The seed bytes is the raw entropy. To pass it to HD wallet generation, // we need to get the 64 byte bip39 entropy let bip39_seed = bip39::Seed::new(&Mnemonic::from_entropy(&wallet.seed, Language::English).unwrap(), "");