Additional RPCs for password management (#2779)

* Add personal_testPassword and stub for personal_changePassword

* Add change-password functionality.

* Address grumble.

* Update tests.

* Update build.
This commit is contained in:
Gav Wood 2016-10-22 14:24:02 +01:00 committed by GitHub
parent 7bd37e3972
commit 37a2ee98de
3 changed files with 53 additions and 2 deletions

View File

@ -265,6 +265,20 @@ impl AccountProvider {
Ok(()) Ok(())
} }
/// Returns `true` if the password for `account` is `password`. `false` if not.
pub fn test_password(&self, account: &Address, password: String) -> Result<bool, Error> {
match self.sstore.sign(&account, &password, &Default::default()) {
Ok(_) => Ok(true),
Err(SSError::InvalidPassword) => Ok(false),
Err(e) => Err(Error::SStore(e)),
}
}
/// Changes the password of `account` from `password` to `new_password`. Fails if incorrect `password` given.
pub fn change_password(&self, account: &Address, password: String, new_password: String) -> Result<(), Error> {
self.sstore.change_password(&account, &password, &new_password).map_err(Error::SStore)
}
/// Helper method used for unlocking accounts. /// Helper method used for unlocking accounts.
fn unlock_account(&self, account: Address, password: String, unlock: Unlock) -> Result<(), Error> { fn unlock_account(&self, account: Address, password: String, unlock: Unlock) -> Result<(), Error> {
// verify password by signing dump message // verify password by signing dump message

View File

@ -119,7 +119,7 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
fn unlock_account(&self, params: Params) -> Result<Value, Error> { fn unlock_account(&self, params: Params) -> Result<Value, Error> {
try!(self.active()); try!(self.active());
from_params::<(RpcH160, String, Option<u64>)>(params).and_then( from_params::<(RpcH160, String, Option<u64>)>(params).and_then(
|(account, account_pass, duration)|{ |(account, account_pass, duration)| {
let account: Address = account.into(); let account: Address = account.into();
let store = take_weak!(self.accounts); let store = take_weak!(self.accounts);
let r = match (self.allow_perm_unlock, duration) { let r = match (self.allow_perm_unlock, duration) {
@ -132,7 +132,34 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
Ok(_) => Ok(Value::Bool(true)), Ok(_) => Ok(Value::Bool(true)),
Err(_) => Ok(Value::Bool(false)), Err(_) => Ok(Value::Bool(false)),
} }
}) }
)
}
fn test_password(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
from_params::<(RpcH160, String)>(params).and_then(
|(account, password)| {
let account: Address = account.into();
take_weak!(self.accounts)
.test_password(&account, password)
.map(|b| Value::Bool(b))
.map_err(|e| errors::account("Could not fetch account info.", e))
}
)
}
fn change_password(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
from_params::<(RpcH160, String, String)>(params).and_then(
|(account, password, new_password)| {
let account: Address = account.into();
take_weak!(self.accounts)
.change_password(&account, password, new_password)
.map(|_| Value::Null)
.map_err(|e| errors::account("Could not fetch account info.", e))
}
)
} }
fn sign_and_send_transaction(&self, params: Params) -> Result<Value, Error> { fn sign_and_send_transaction(&self, params: Params) -> Result<Value, Error> {

View File

@ -39,6 +39,14 @@ pub trait Personal: Sized + Send + Sync + 'static {
/// Unlocks specified account for use (can only be one unlocked account at one moment) /// Unlocks specified account for use (can only be one unlocked account at one moment)
fn unlock_account(&self, _: Params) -> Result<Value, Error>; fn unlock_account(&self, _: Params) -> Result<Value, Error>;
/// Returns true if given `password` would unlock given `account`.
/// Arguments: `account`, `password`.
fn test_password(&self, _: Params) -> Result<Value, Error>;
/// Changes an account's password.
/// Arguments: `account`, `password`, `new_password`.
fn change_password(&self, _: Params) -> Result<Value, Error>;
/// Sends transaction and signs it in single call. The account is not unlocked in such case. /// Sends transaction and signs it in single call. The account is not unlocked in such case.
fn sign_and_send_transaction(&self, _: Params) -> Result<Value, Error>; fn sign_and_send_transaction(&self, _: Params) -> Result<Value, Error>;
@ -69,6 +77,8 @@ pub trait Personal: Sized + Send + Sync + 'static {
delegate.add_method("personal_newAccountFromPhrase", Personal::new_account_from_phrase); delegate.add_method("personal_newAccountFromPhrase", Personal::new_account_from_phrase);
delegate.add_method("personal_newAccountFromWallet", Personal::new_account_from_wallet); delegate.add_method("personal_newAccountFromWallet", Personal::new_account_from_wallet);
delegate.add_method("personal_unlockAccount", Personal::unlock_account); delegate.add_method("personal_unlockAccount", Personal::unlock_account);
delegate.add_method("personal_testPassword", Personal::test_password);
delegate.add_method("personal_changePassword", Personal::change_password);
delegate.add_method("personal_signAndSendTransaction", Personal::sign_and_send_transaction); delegate.add_method("personal_signAndSendTransaction", Personal::sign_and_send_transaction);
delegate.add_method("personal_setAccountName", Personal::set_account_name); delegate.add_method("personal_setAccountName", Personal::set_account_name);
delegate.add_method("personal_setAccountMeta", Personal::set_account_meta); delegate.add_method("personal_setAccountMeta", Personal::set_account_meta);