Merge pull request #3659 from ethcore/unlock-rpc

Unlock expecting quantity
This commit is contained in:
Gav Wood 2016-11-28 18:18:15 +01:00 committed by GitHub
commit 77ebbedbba
6 changed files with 65 additions and 10 deletions

View File

@ -20,11 +20,11 @@ use std::sync::{Arc, Weak};
use ethcore::account_provider::AccountProvider;
use ethcore::client::MiningBlockChainClient;
use ethcore::miner::MinerService;
use util::Address;
use util::{Address, U128, Uint};
use jsonrpc_core::Error;
use v1::traits::Personal;
use v1::types::{H160 as RpcH160, H256 as RpcH256, TransactionRequest};
use v1::types::{H160 as RpcH160, H256 as RpcH256, U128 as RpcU128, TransactionRequest};
use v1::helpers::errors;
use v1::helpers::dispatch::{self, sign_and_dispatch};
@ -72,15 +72,27 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
.map_err(|e| errors::account("Could not create account.", e))
}
fn unlock_account(&self, account: RpcH160, account_pass: String, duration: Option<u64>) -> Result<bool, Error> {
fn unlock_account(&self, account: RpcH160, account_pass: String, duration: Option<RpcU128>) -> Result<bool, Error> {
try!(self.active());
let account: Address = account.into();
let store = take_weak!(self.accounts);
let duration = match duration {
None => None,
Some(duration) => {
let duration: U128 = duration.into();
let v = duration.low_u64() as u32;
if duration != v.into() {
return Err(errors::invalid_params("Duration", "Invalid Number"));
} else {
Some(v)
}
},
};
let r = match (self.allow_perm_unlock, duration) {
(false, _) => store.unlock_account_temporarily(account, account_pass),
(true, Some(0)) => store.unlock_account_permanently(account, account_pass),
(true, Some(d)) => store.unlock_account_timed(account, account_pass, d as u32 * 1000),
(true, Some(d)) => store.unlock_account_timed(account, account_pass, d * 1000),
(true, None) => store.unlock_account_timed(account, account_pass, 300_000),
};
match r {

View File

@ -164,3 +164,45 @@ fn sign_and_send_transaction() {
assert_eq!(tester.io.handle_request_sync(request.as_ref()), Some(response));
}
#[test]
fn should_unlock_account_temporarily() {
let tester = setup();
let address = tester.accounts.new_account("password123").unwrap();
let request = r#"{
"jsonrpc": "2.0",
"method": "personal_unlockAccount",
"params": [
""#.to_owned() + &format!("0x{:?}", address) + r#"",
"password123",
"0x100"
],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
assert_eq!(tester.io.handle_request_sync(&request), Some(response.into()));
assert!(tester.accounts.sign(address, None, Default::default()).is_ok(), "Should unlock account.");
}
#[test]
fn should_unlock_account_permanently() {
let tester = setup();
let address = tester.accounts.new_account("password123").unwrap();
let request = r#"{
"jsonrpc": "2.0",
"method": "personal_unlockAccount",
"params": [
""#.to_owned() + &format!("0x{:?}", address) + r#"",
"password123",
null
],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
assert_eq!(tester.io.handle_request_sync(&request), Some(response.into()));
assert!(tester.accounts.sign(address, None, Default::default()).is_ok(), "Should unlock account.");
}

View File

@ -18,7 +18,7 @@
use jsonrpc_core::Error;
use v1::helpers::auto_args::Wrap;
use v1::types::{H160, H256, TransactionRequest};
use v1::types::{U128, H160, H256, TransactionRequest};
build_rpc_trait! {
/// Personal rpc interface. Safe (read-only) functions.
@ -34,7 +34,7 @@ build_rpc_trait! {
/// Unlocks specified account for use (can only be one unlocked account at one moment)
#[rpc(name = "personal_unlockAccount")]
fn unlock_account(&self, H160, String, Option<u64>) -> Result<bool, Error>;
fn unlock_account(&self, H160, String, Option<U128>) -> Result<bool, Error>;
/// Sends transaction and signs it in single call. The account is not unlocked in such case.
#[rpc(name = "personal_signAndSendTransaction")]

View File

@ -124,8 +124,8 @@ impl Serialize for FilterChanges {
mod tests {
use serde_json;
use std::str::FromStr;
use util::hash::*;
use super::*;
use util::hash::H256;
use super::{VariadicValue, Topic, Filter};
use v1::types::BlockNumber;
use ethcore::filter::Filter as EthFilter;
use ethcore::client::BlockID;

View File

@ -50,6 +50,6 @@ pub use self::receipt::Receipt;
pub use self::rpc_settings::RpcSettings;
pub use self::trace::{LocalizedTrace, TraceResults};
pub use self::trace_filter::TraceFilter;
pub use self::uint::U256;
pub use self::uint::{U128, U256};
pub use self::work::Work;
pub use self::histogram::Histogram;

View File

@ -18,7 +18,7 @@ use std::cmp;
use std::str::FromStr;
use rustc_serialize::hex::ToHex;
use serde;
use util::{U256 as EthU256, Uint};
use util::{U256 as EthU256, U128 as EthU128, Uint};
macro_rules! impl_uint {
($name: ident, $other: ident, $size: expr) => {
@ -98,6 +98,7 @@ macro_rules! impl_uint {
}
}
impl_uint!(U128, EthU128, 2);
impl_uint!(U256, EthU256, 4);