Add lock command

This commit is contained in:
Aditya Kulkarni
2019-10-21 14:13:43 -07:00
parent 08a2fded88
commit 7767caaf3f
2 changed files with 56 additions and 2 deletions

View File

@@ -594,8 +594,8 @@ impl LightWallet {
pub fn encrypt(&mut self, passwd: String) -> io::Result<()> {
use sodiumoxide::crypto::secretbox;
if self.encrypted && !self.unlocked {
return Err(io::Error::new(ErrorKind::AlreadyExists, "Wallet is already encrypted and locked"));
if self.encrypted {
return Err(io::Error::new(ErrorKind::AlreadyExists, "Wallet is already encrypted"));
}
// Get the doublesha256 of the password, which is the right length
@@ -615,6 +615,14 @@ impl LightWallet {
}
pub fn lock(&mut self) -> io::Result<()> {
if !self.encrypted {
return Err(io::Error::new(ErrorKind::AlreadyExists, "Wallet is not encrypted"));
}
if !self.unlocked {
return Err(io::Error::new(ErrorKind::AlreadyExists, "Wallet is already locked"));
}
// Empty the seed and the secret keys
self.seed.copy_from_slice(&[0u8; 32]);
self.tkeys = Arc::new(RwLock::new(vec![]));
@@ -3144,6 +3152,10 @@ pub mod tests {
let seed = wallet.seed;
// Trying to lock a wallet that's not encrpyted is an error
assert!(wallet.lock().is_err());
// Encrypt the wallet
wallet.encrypt("somepassword".to_string()).unwrap();
// Encrypting an already encrypted wallet should fail
@@ -3188,6 +3200,9 @@ pub mod tests {
wallet.lock().unwrap();
wallet.write(&mut vec![]).expect("Serialize wallet");
// Locking an already locked wallet is an error
assert!(wallet.lock().is_err());
// Try from a deserialized, locked wallet
let mut wallet2 = LightWallet::read(&serialized_data[..], &config).unwrap();
wallet2.unlock("somepassword".to_string()).unwrap();