Browse Source

Merge pull request #5 from DenioD/master

merge updates, fixes some Test settings for Hush
checkpoints
Duke Leto 5 years ago
committed by GitHub
parent
commit
e04531d70e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Cargo.toml
  2. 10
      docker/Dockerfile
  3. 9
      lib/Cargo.toml
  4. 49
      lib/src/commands.rs
  5. 186
      lib/src/lightclient.rs
  6. 1798
      lib/src/lightwallet.rs
  7. 13
      lib/src/lightwallet/bugs.rs
  8. 1996
      lib/src/lightwallet/tests.rs
  9. 22
      mkrelease.sh
  10. 65
      src/main.rs

2
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "silentdragonlite-cli"
version = "1.0.0"
version = "1.1.0"
edition = "2018"
[dependencies]

10
docker/Dockerfile

@ -25,3 +25,13 @@ 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. 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
# Cargo fetch the dependencies so we don't download them over and over again
RUN cd /tmp && git clone https://github.com/adityapk00/zecwallet-light-cli.git && \
cd zecwallet-light-cli && \
cargo fetch && \
cd /tmp && rm -rf zecwallet-light-cli

9
lib/Cargo.toml

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2018"
[dependencies]
tower-grpc = { git = "https://github.com/tower-rs/tower-grpc" }
tower-grpc = "0.1.1"
futures = "0.1"
bytes = "0.4"
base58 = "0.1.0"
@ -14,7 +14,7 @@ dirs = "2.0.2"
http = "0.1"
prost = "0.5"
tokio = "0.1"
tower-request-modifier = { git = "https://github.com/tower-rs/tower-http" }
tower-request-modifier = "0.1.0"
tower-util = "0.1"
hex = "0.3"
protobuf = "2"
@ -31,7 +31,7 @@ tokio-rustls = "0.10.0-alpha.3"
rustls = { version = "0.15.2", features = ["dangerous_configuration"] }
webpki = "0.19.1"
webpki-roots = "0.16.0"
tower-h2 = { git = "https://github.com/tower-rs/tower-h2" }
tower-h2 = { git = "https://github.com/tower-rs/tower-h2", rev="0865040d699697bbaf1c3b77b3f256b72f98cdf4" }
rust-embed = { version = "5.1.0", features = ["debug-embed"] }
rand = "0.7.2"
sodiumoxide = "0.2.5"
@ -71,5 +71,8 @@ features = ["ff_derive"]
[build-dependencies]
tower-grpc-build = { git = "https://github.com/tower-rs/tower-grpc", features = ["tower-hyper"] }
[dev-dependencies]
tempdir = "0.3.7"
[profile.release]
debug = true

49
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 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");
}
let passwd = args[0].to_string();
match lightclient.wallet.write().unwrap().encrypt(passwd) {
@ -322,6 +332,44 @@ impl Command for UnlockCommand {
}
}
struct LockCommand {}
impl Command for LockCommand {
fn help(&self) -> String {
let mut h = vec![];
h.push("Lock a wallet that's been temporarily unlocked. You should already have encryption enabled.");
h.push("Note 1: This will remove all spending keys from memory. The wallet remains encrypted on disk");
h.push("Note 2: If you've forgotten the password, the only way to recover the wallet is to restore");
h.push(" from the seed phrase.");
h.push("Usage:");
h.push("lock");
h.push("");
h.push("Example:");
h.push("lock");
h.join("\n")
}
fn short_help(&self) -> String {
"Lock a wallet that's been temporarily unlocked".to_string()
}
fn exec(&self, args: &[&str], lightclient: &LightClient) -> String {
if args.len() != 0 {
return self.help();
}
match lightclient.wallet.write().unwrap().lock() {
Ok(_) => object!{ "result" => "success" },
Err(e) => object!{
"result" => "error",
"error" => e.to_string()
}
}.pretty(2)
}
}
struct SendCommand {}
impl Command for SendCommand {
fn help(&self) -> String {
@ -655,6 +703,7 @@ pub fn get_commands() -> Box<HashMap<String, Box<dyn Command>>> {
map.insert("encrypt".to_string(), Box::new(EncryptCommand{}));
map.insert("decrypt".to_string(), Box::new(DecryptCommand{}));
map.insert("unlock".to_string(), Box::new(UnlockCommand{}));
map.insert("lock".to_string(), Box::new(LockCommand{}));
map.insert("fixbip39bug".to_string(), Box::new(FixBip39BugCommand{}));
Box::new(map)

186
lib/src/lightclient.rs

@ -5,7 +5,7 @@ use rand::{rngs::OsRng, seq::SliceRandom};
use std::sync::{Arc, RwLock};
use std::sync::atomic::{AtomicU64, AtomicI32, AtomicUsize, Ordering};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::fs::File;
use std::collections::HashMap;
use std::io;
@ -38,12 +38,13 @@ pub struct LightClientConfig {
pub consensus_branch_id : String,
pub anchor_offset : u32,
pub no_cert_verification : bool,
pub data_dir : Option<String>
}
impl LightClientConfig {
// Create an unconnected (to any server) config to test for local wallet etc...
pub fn create_unconnected(chain_name: String) -> LightClientConfig {
pub fn create_unconnected(chain_name: String, dir: Option<String>) -> LightClientConfig {
LightClientConfig {
server : http::Uri::default(),
chain_name : chain_name,
@ -51,6 +52,7 @@ impl LightClientConfig {
consensus_branch_id : "".to_string(),
anchor_offset : ANCHOR_OFFSET,
no_cert_verification : false,
data_dir : dir,
}
}
@ -67,6 +69,7 @@ impl LightClientConfig {
consensus_branch_id : info.consensus_branch_id,
anchor_offset : ANCHOR_OFFSET,
no_cert_verification : dangerous,
data_dir : None,
};
Ok((config, info.block_height))
@ -74,20 +77,24 @@ impl LightClientConfig {
pub fn get_zcash_data_path(&self) -> Box<Path> {
let mut zcash_data_location;
if cfg!(target_os="macos") || cfg!(target_os="windows") {
zcash_data_location = dirs::data_dir().expect("Couldn't determine app data directory!");
zcash_data_location.push("HUSH3");
if self.data_dir.is_some() {
zcash_data_location = PathBuf::from(&self.data_dir.as_ref().unwrap());
} else {
zcash_data_location = dirs::home_dir().expect("Couldn't determine home directory!");
zcash_data_location.push(".komodo/HUSH3/");
};
if cfg!(target_os="macos") || cfg!(target_os="windows") {
zcash_data_location = dirs::data_dir().expect("Couldn't determine app data directory!");
zcash_data_location.push("HUSH3");
} else {
zcash_data_location = dirs::home_dir().expect("Couldn't determine home directory!");
zcash_data_location.push(".komodo/HUSH3/");
};
match &self.chain_name[..] {
"main" => {},
"test" => zcash_data_location.push("testnet3"),
"regtest" => zcash_data_location.push("regtest"),
c => panic!("Unknown chain {}", c),
};
match &self.chain_name[..] {
"main" => {},
"test" => zcash_data_location.push("testnet3"),
"regtest" => zcash_data_location.push("regtest"),
c => panic!("Unknown chain {}", c),
};
}
zcash_data_location.into_boxed_path()
}
@ -226,8 +233,8 @@ impl LightClient {
/// Method to create a test-only version of the LightClient
#[allow(dead_code)]
fn unconnected(seed_phrase: String) -> io::Result<Self> {
let config = LightClientConfig::create_unconnected("test".to_string());
fn unconnected(seed_phrase: String, dir: Option<String>) -> io::Result<Self> {
let config = LightClientConfig::create_unconnected("test".to_string(), dir);
let mut l = LightClient {
wallet : Arc::new(RwLock::new(LightWallet::new(Some(seed_phrase), &config, 0)?)),
config : config.clone(),
@ -244,8 +251,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<Self> {
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<Self> {
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 +298,7 @@ impl LightClient {
}
pub fn read_from_disk(config: &LightClientConfig) -> io::Result<Self> {
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())));
}
@ -296,6 +327,47 @@ impl LightClient {
Ok(lc)
}
pub fn attempt_recover_seed(config: &LightClientConfig) -> Result<String, String> {
use std::io::prelude::*;
use byteorder::{LittleEndian, ReadBytesExt,};
use bip39::{Mnemonic, Language};
use zcash_primitives::serialize::Vector;
let mut reader = BufReader::new(File::open(config.get_wallet_path()).unwrap());
let version = reader.read_u64::<LittleEndian>().unwrap();
println!("Reading wallet version {}", version);
let encrypted = if version >= 4 {
reader.read_u8().unwrap() > 0
} else {
false
};
if encrypted {
return Err("The wallet is encrypted!".to_string());
}
let mut enc_seed = [0u8; 48];
if version >= 4 {
reader.read_exact(&mut enc_seed).unwrap();
}
let _nonce = if version >= 4 {
Vector::read(&mut reader, |r| r.read_u8()).unwrap()
} else {
vec![]
};
// Seed
let mut seed_bytes = [0u8; 32];
reader.read_exact(&mut seed_bytes).unwrap();
let phrase = Mnemonic::from_entropy(&seed_bytes, Language::English,).unwrap().phrase().to_string();
Ok(phrase)
}
pub fn last_scanned_height(&self) -> u64 {
self.wallet.read().unwrap().last_scanned_height() as u64
}
@ -740,6 +812,8 @@ impl LightClient {
return;
}
// Parse the block and save it's time. We'll use this timestamp for
// transactions in this block that might belong to us.
let block: Result<zcash_client_backend::proto::compact_formats::CompactBlock, _>
= parse_from_bytes(encoded_block);
match block {
@ -887,10 +961,11 @@ impl LightClient {
}
}
#[cfg(test)]
pub mod tests {
use lazy_static::lazy_static;
//use super::LightClient;
use tempdir::TempDir;
use super::{LightClient, LightClientConfig};
lazy_static!{
static ref TEST_SEED: String = "youth strong sweet gorilla hammer unhappy congress stamp left stereo riot salute road tag clean toilet artefact fork certain leopard entire civil degree wonder".to_string();
@ -898,7 +973,7 @@ pub mod tests {
#[test]
pub fn test_encrypt_decrypt() {
let lc = super::LightClient::unconnected(TEST_SEED.to_string()).unwrap();
let lc = super::LightClient::unconnected(TEST_SEED.to_string(), None).unwrap();
assert!(!lc.do_export(None).is_err());
assert!(!lc.do_new_address("z").is_err());
@ -923,7 +998,7 @@ pub mod tests {
#[test]
pub fn test_addresses() {
let lc = super::LightClient::unconnected(TEST_SEED.to_string()).unwrap();
let lc = super::LightClient::unconnected(TEST_SEED.to_string(), None).unwrap();
// Add new z and t addresses
@ -942,5 +1017,72 @@ pub mod tests {
assert_eq!(addresses["t_addresses"][2], taddr2);
}
#[test]
pub fn test_wallet_creation() {
// Create a new tmp director
{
let tmp = TempDir::new("lctest").unwrap();
let dir_name = tmp.path().to_str().map(|s| s.to_string());
// A lightclient to a new, empty directory works.
let config = LightClientConfig::create_unconnected("test".to_string(), dir_name);
let lc = LightClient::new(&config, 0).unwrap();
let seed = lc.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string();
lc.do_save().unwrap();
// Doing another new will fail, because the wallet file now already exists
assert!(LightClient::new(&config, 0).is_err());
// new_from_phrase will not work either, again, because wallet file exists
assert!(LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0).is_err());
// Creating a lightclient to the same dir without a seed should re-read the same wallet
// file and therefore the same seed phrase
let lc2 = LightClient::read_from_disk(&config).unwrap();
assert_eq!(seed, lc2.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string());
}
// Now, get a new directory, and try to read from phrase
{
let tmp = TempDir::new("lctest").unwrap();
let dir_name = tmp.path().to_str().map(|s| s.to_string());
let config = LightClientConfig::create_unconnected("test".to_string(), dir_name);
// read_from_disk will fail, because the dir doesn't exist
assert!(LightClient::read_from_disk(&config).is_err());
// New from phrase should work becase a file doesn't exist already
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0).unwrap();
assert_eq!(TEST_SEED.to_string(), lc.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string());
lc.do_save().unwrap();
// Now a new will fail because wallet exists
assert!(LightClient::new(&config, 0).is_err());
}
}
#[test]
pub fn test_recover_seed() {
// Create a new tmp director
{
let tmp = TempDir::new("lctest").unwrap();
let dir_name = tmp.path().to_str().map(|s| s.to_string());
// A lightclient to a new, empty directory works.
let config = LightClientConfig::create_unconnected("test".to_string(), dir_name);
let lc = LightClient::new(&config, 0).unwrap();
let seed = lc.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string();
lc.do_save().unwrap();
assert_eq!(seed, LightClient::attempt_recover_seed(&config).unwrap());
// Now encrypt and save the file
lc.wallet.write().unwrap().encrypt("password".to_string()).unwrap();
lc.do_save().unwrap();
assert!(LightClient::attempt_recover_seed(&config).is_err());
}
}
}

1798
lib/src/lightwallet.rs

File diff suppressed because it is too large

13
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(), "");
@ -70,16 +74,15 @@ impl BugBip39Derivation {
// Tranfer money
// 1. The desination is z address #0
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)]) {
println!("Sending funds to ourself.");
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!{

1996
lib/src/lightwallet/tests.rs

File diff suppressed because it is too large

22
mkrelease.sh

@ -25,20 +25,28 @@ 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
# For Windows and Linux, build via docker
docker run --rm -v $(pwd)/:/opt/silentdragonlite-cli rustbuild:latest bash -c "cd /opt/silentdragonlite-cli && cargo build --release && cargo build --release --target x86_64-pc-windows-gnu"
cargo build --release
# Now sign and zip the binaries
#macOS
rm -rf target/macOS-silentdragonlite-cli-v$APP_VERSION
mkdir -p target/macOS-silentdragonlite-cli-v$APP_VERSION
cp target/release/silentdragonlite-cli target/macOS-silentdragonlite-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
gpg --batch --output target/macOS-silentdragonlite-cli-v$APP_VERSION/silentdragonlite-cli.sig --detach-sig target/macOS-silentdragonlite-cli-v$APP_VERSION/silentdragonlite-cli
cd target
cd macOS-silentdragonlite-cli-v$APP_VERSION

65
src/main.rs

@ -1,4 +1,4 @@
use std::io::{Result, Error, ErrorKind};
use std::io::{self, Error, ErrorKind};
use std::sync::Arc;
use std::sync::mpsc::{channel, Sender, Receiver};
@ -20,7 +20,7 @@ use log4rs::append::rolling_file::policy::compound::{
/// Build the Logging config
fn get_log_config(config: &LightClientConfig) -> Result<Config> {
fn get_log_config(config: &LightClientConfig) -> io::Result<Config> {
let window_size = 3; // log0, log1, log2
let fixed_window_roller =
FixedWindowRoller::builder().build("SilentDragonLite-light-wallet-log{}",window_size).unwrap();
@ -54,7 +54,7 @@ pub fn main() {
// Get command line arguments
use clap::{Arg, App};
let matches = App::new("SilentDragon CLI")
.version("1.0.0")
.version("1.1.0")
.arg(Arg::with_name("seed")
.short("s")
.long("seed")
@ -91,7 +91,21 @@ pub fn main() {
.get_matches();
if matches.is_present("recover") {
attempt_recover_seed();
// Create a Light Client Config in an attempt to recover the file.
let config = LightClientConfig {
server: "0.0.0.0:0".parse().unwrap(),
chain_name: "main".to_string(),
sapling_activation_height: 0,
consensus_branch_id: "000000".to_string(),
anchor_offset: 0,
no_cert_verification: false,
data_dir: None,
};
match LightClient::attempt_recover_seed(&config) {
Ok(seed) => println!("Recovered seed: '{}'", seed),
Err(e) => eprintln!("Failed to recover seed. Error: {}", e)
};
return;
}
@ -120,7 +134,7 @@ pub fn main() {
Some(13) => {
startup_helpers::report_permission_error();
},
_ => eprintln!("Something else!")
_ => {}
}
return;
}
@ -150,7 +164,7 @@ pub fn main() {
}
fn startup(server: http::Uri, dangerous: bool, seed: Option<String>, first_sync: bool, print_updates: bool)
-> Result<(Sender<(String, Vec<String>)>, Receiver<String>)> {
-> io::Result<(Sender<(String, Vec<String>)>, Receiver<String>)> {
// Try to get the configuration
let (config, latest_block_height) = LightClientConfig::create(server.clone(), dangerous)?;
@ -162,7 +176,14 @@ fn startup(server: http::Uri, dangerous: bool, seed: Option<String>, 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
@ -294,33 +315,3 @@ fn command_loop(lightclient: Arc<LightClient>) -> (Sender<(String, Vec<String>)>
(command_tx, resp_rx)
}
fn attempt_recover_seed() {
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader};
use byteorder::{LittleEndian, ReadBytesExt,};
use bip39::{Mnemonic, Language};
// Create a Light Client Config in an attempt to recover the file.
let config = LightClientConfig {
server: "0.0.0.0:0".parse().unwrap(),
chain_name: "main".to_string(),
sapling_activation_height: 0,
consensus_branch_id: "000000".to_string(),
anchor_offset: 0,
no_cert_verification: false,
};
let mut reader = BufReader::new(File::open(config.get_wallet_path()).unwrap());
let version = reader.read_u64::<LittleEndian>().unwrap();
println!("Reading wallet version {}", version);
// Seed
let mut seed_bytes = [0u8; 32];
reader.read_exact(&mut seed_bytes).unwrap();
let phrase = Mnemonic::from_entropy(&seed_bytes, Language::English,).unwrap().phrase().to_string();
println!("Recovered seed phrase:\n{}", phrase);
}

Loading…
Cancel
Save