Signer RPC method to check if signer is enabled (#1238)

* API to check if signer is enabled

* Fixing compilation warnings
This commit is contained in:
Tomasz Drwięga 2016-06-07 19:33:32 +02:00 committed by Gav Wood
parent bf9173e673
commit fdc22db3f4
5 changed files with 29 additions and 4 deletions

View File

@ -147,7 +147,7 @@ pub fn setup_rpc<T: Extendable>(server: T, deps: Arc<Dependencies>, apis: ApiSet
} }
}, },
Api::Personal => { Api::Personal => {
server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate()); server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner, deps.signer_enabled).to_delegate());
if deps.signer_enabled { if deps.signer_enabled {
server.add_delegate(SignerClient::new(&deps.secret_store, &deps.client, &deps.miner, &deps.signer_queue).to_delegate()); server.add_delegate(SignerClient::new(&deps.secret_store, &deps.client, &deps.miner, &deps.signer_queue).to_delegate());
} }

View File

@ -31,22 +31,29 @@ pub struct PersonalClient<A, C, M>
accounts: Weak<A>, accounts: Weak<A>,
client: Weak<C>, client: Weak<C>,
miner: Weak<M>, miner: Weak<M>,
signer_enabled: bool,
} }
impl<A, C, M> PersonalClient<A, C, M> impl<A, C, M> PersonalClient<A, C, M>
where A: AccountProvider, C: MiningBlockChainClient, M: MinerService { where A: AccountProvider, C: MiningBlockChainClient, M: MinerService {
/// Creates new PersonalClient /// Creates new PersonalClient
pub fn new(store: &Arc<A>, client: &Arc<C>, miner: &Arc<M>) -> Self { pub fn new(store: &Arc<A>, client: &Arc<C>, miner: &Arc<M>, signer_enabled: bool) -> Self {
PersonalClient { PersonalClient {
accounts: Arc::downgrade(store), accounts: Arc::downgrade(store),
client: Arc::downgrade(client), client: Arc::downgrade(client),
miner: Arc::downgrade(miner), miner: Arc::downgrade(miner),
signer_enabled: signer_enabled,
} }
} }
} }
impl<A: 'static, C: 'static, M: 'static> Personal for PersonalClient<A, C, M> impl<A: 'static, C: 'static, M: 'static> Personal for PersonalClient<A, C, M>
where A: AccountProvider, C: MiningBlockChainClient, M: MinerService { where A: AccountProvider, C: MiningBlockChainClient, M: MinerService {
fn signer_enabled(&self, _: Params) -> Result<Value, Error> {
to_value(&self.signer_enabled)
}
fn accounts(&self, _: Params) -> Result<Value, Error> { fn accounts(&self, _: Params) -> Result<Value, Error> {
let store = take_weak!(self.accounts); let store = take_weak!(self.accounts);
match store.accounts() { match store.accounts() {

View File

@ -19,7 +19,7 @@
use std::sync::{Weak, Arc}; use std::sync::{Weak, Arc};
use jsonrpc_core::*; use jsonrpc_core::*;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use util::{H256, U256, Uint}; use util::H256;
use ethcore::client::{BlockChainClient, CallAnalytics, TransactionID, TraceId}; use ethcore::client::{BlockChainClient, CallAnalytics, TransactionID, TraceId};
use ethcore::miner::MinerService; use ethcore::miner::MinerService;
use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action}; use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action};

View File

@ -53,7 +53,7 @@ fn setup() -> PersonalTester {
let accounts = accounts_provider(); let accounts = accounts_provider();
let client = blockchain_client(); let client = blockchain_client();
let miner = miner_service(); let miner = miner_service();
let personal = PersonalClient::new(&accounts, &client, &miner); let personal = PersonalClient::new(&accounts, &client, &miner, false);
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(personal.to_delegate()); io.add_delegate(personal.to_delegate());
@ -68,6 +68,20 @@ fn setup() -> PersonalTester {
tester tester
} }
#[test]
fn should_return_false_if_signer_is_disabled() {
// given
let tester = setup();
// when
let request = r#"{"jsonrpc": "2.0", "method": "personal_signerEnabled", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":false,"id":1}"#;
// then
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
}
#[test] #[test]
fn accounts() { fn accounts() {
let tester = setup(); let tester = setup();

View File

@ -33,9 +33,13 @@ pub trait Personal: Sized + Send + Sync + 'static {
/// 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>;
/// Returns `true` if Trusted Signer is enabled, `false` otherwise.
fn signer_enabled(&self, _: Params) -> Result<Value, Error>;
/// Should be used to convert object to io delegate. /// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> { fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self)); let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("personal_signerEnabled", Personal::signer_enabled);
delegate.add_method("personal_listAccounts", Personal::accounts); delegate.add_method("personal_listAccounts", Personal::accounts);
delegate.add_method("personal_newAccount", Personal::new_account); delegate.add_method("personal_newAccount", Personal::new_account);
delegate.add_method("personal_unlockAccount", Personal::unlock_account); delegate.add_method("personal_unlockAccount", Personal::unlock_account);