Resolved merge conflict by incorporating both suggestions.

This commit is contained in:
DenioD
2019-10-21 13:31:06 +02:00
7 changed files with 130 additions and 76 deletions

View File

@@ -168,7 +168,8 @@ impl LightClientConfig {
pub fn base58_pubkey_address(&self) -> [u8; 1] {
match &self.chain_name[..] {
"main" => mainnet::B58_PUBKEY_ADDRESS_PREFIX,
c => panic!("Unknown chain {}", c)
}
}
@@ -177,7 +178,8 @@ impl LightClientConfig {
pub fn base58_script_address(&self) -> [u8; 1] {
match &self.chain_name[..] {
"main" => mainnet::B58_SCRIPT_ADDRESS_PREFIX,
c => panic!("Unknown chain {}", c)
}
}
@@ -222,6 +224,26 @@ 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());
let mut l = LightClient {
wallet : Arc::new(RwLock::new(LightWallet::new(Some(seed_phrase), &config, 0)?)),
config : config.clone(),
sapling_output : vec![],
sapling_spend : vec![]
};
l.set_wallet_initial_state();
l.read_sapling_params();
info!("Created new wallet!");
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() {
return Err(Error::new(ErrorKind::AlreadyExists,
@@ -279,12 +301,10 @@ impl LightClient {
}
// Export private keys
pub fn do_export(&self, addr: Option<String>) -> JsonValue {
pub fn do_export(&self, addr: Option<String>) -> Result<JsonValue, &str> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return object!{
"error" => "Wallet is locked"
};
return Err("Wallet is locked");
}
// Clone address so it can be moved into the closure
@@ -317,11 +337,12 @@ impl LightClient {
all_keys.extend_from_slice(&z_keys);
all_keys.extend_from_slice(&t_keys);
all_keys.into()
Ok(all_keys.into())
}
pub fn do_address(&self) -> JsonValue {
let wallet = self.wallet.read().unwrap();
// Collect z addresses
let z_addresses = wallet.zaddress.read().unwrap().iter().map( |ad| {
encode_payment_address(self.config.hrp_sapling_address(), &ad)
@@ -370,8 +391,7 @@ impl LightClient {
}
}
pub fn do_save(&self) -> Result<(), String> {
pub fn do_save(&self) -> Result<(), String> {
// If the wallet is encrypted but unlocked, lock it again.
{
let mut wallet = self.wallet.write().unwrap();
@@ -423,19 +443,17 @@ impl LightClient {
}
}
pub fn do_seed_phrase(&self) -> JsonValue {
pub fn do_seed_phrase(&self) -> Result<JsonValue, &str> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return object!{
"error" => "Wallet is locked"
};
return Err("Wallet is locked");
}
let wallet = self.wallet.read().unwrap();
object!{
Ok(object!{
"seed" => wallet.get_seed_phrase(),
"birthday" => wallet.get_birthday()
}
})
}
// Return a list of all notes, spent and unspent
@@ -610,12 +628,10 @@ impl LightClient {
}
/// Create a new address, deriving it from the seed.
pub fn do_new_address(&self, addr_type: &str) -> JsonValue {
pub fn do_new_address(&self, addr_type: &str) -> Result<JsonValue, String> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return object!{
"error" => "Wallet is locked"
};
return Err("Wallet is locked".to_string());
}
let wallet = self.wallet.write().unwrap();
@@ -626,13 +642,11 @@ impl LightClient {
_ => {
let e = format!("Unrecognized address type: {}", addr_type);
error!("{}", e);
return object!{
"error" => e
};
return Err(e);
}
};
array![new_address]
Ok(array![new_address])
}
pub fn do_rescan(&self) -> String {
@@ -872,3 +886,61 @@ impl LightClient {
}
}
}
pub mod tests {
use lazy_static::lazy_static;
//use super::LightClient;
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();
}
#[test]
pub fn test_encrypt_decrypt() {
let lc = super::LightClient::unconnected(TEST_SEED.to_string()).unwrap();
assert!(!lc.do_export(None).is_err());
assert!(!lc.do_new_address("z").is_err());
assert!(!lc.do_new_address("t").is_err());
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
// Encrypt and Lock the wallet
lc.wallet.write().unwrap().encrypt("password".to_string()).unwrap();
assert!(lc.do_export(None).is_err());
assert!(lc.do_seed_phrase().is_err());
assert!(lc.do_new_address("t").is_err());
assert!(lc.do_new_address("z").is_err());
assert!(lc.do_send(vec![("z", 0, None)]).is_err());
// Do a unlock, and make sure it all works now
lc.wallet.write().unwrap().unlock("password".to_string()).unwrap();
assert!(!lc.do_export(None).is_err());
assert!(!lc.do_seed_phrase().is_err());
assert!(!lc.do_new_address("t").is_err());
assert!(!lc.do_new_address("z").is_err());
}
#[test]
pub fn test_addresses() {
let lc = super::LightClient::unconnected(TEST_SEED.to_string()).unwrap();
// Add new z and t addresses
let taddr1 = lc.do_new_address("t").unwrap()[0].as_str().unwrap().to_string();
let taddr2 = lc.do_new_address("t").unwrap()[0].as_str().unwrap().to_string();
let zaddr1 = lc.do_new_address("z").unwrap()[0].as_str().unwrap().to_string();
let zaddr2 = lc.do_new_address("z").unwrap()[0].as_str().unwrap().to_string();
let addresses = lc.do_address();
assert_eq!(addresses["z_addresses"].len(), 3);
assert_eq!(addresses["z_addresses"][1], zaddr1);
assert_eq!(addresses["z_addresses"][2], zaddr2);
assert_eq!(addresses["t_addresses"].len(), 3);
assert_eq!(addresses["t_addresses"][1], taddr1);
assert_eq!(addresses["t_addresses"][2], taddr2);
}
}