2016-06-01 19:37:34 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Transactions Confirmations (personal) rpc implementation
|
|
|
|
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
use jsonrpc_core::*;
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-06-01 19:57:34 +02:00
|
|
|
use ethcore::client::MiningBlockChainClient;
|
|
|
|
use ethcore::miner::MinerService;
|
2016-07-06 11:23:29 +02:00
|
|
|
use v1::traits::PersonalSigner;
|
2016-08-03 10:36:54 +02:00
|
|
|
use v1::types::{TransactionModification, ConfirmationRequest, U256};
|
2016-09-21 12:44:49 +02:00
|
|
|
use v1::helpers::{errors, SignerService, SigningQueue, ConfirmationPayload};
|
2016-10-15 14:44:08 +02:00
|
|
|
use v1::helpers::dispatch::{sign_and_dispatch, sign, decrypt};
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Transactions confirmation (personal) rpc implementation.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub struct SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Weak<SignerService>,
|
2016-06-20 00:10:34 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2016-06-01 19:37:34 +02:00
|
|
|
client: Weak<C>,
|
|
|
|
miner: Weak<M>,
|
|
|
|
}
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
impl<C: 'static, M: 'static> SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Create new instance of signer client.
|
2016-09-21 12:44:49 +02:00
|
|
|
pub fn new(
|
|
|
|
store: &Arc<AccountProvider>,
|
|
|
|
client: &Arc<C>,
|
|
|
|
miner: &Arc<M>,
|
|
|
|
signer: &Arc<SignerService>,
|
|
|
|
) -> Self {
|
2016-06-01 19:37:34 +02:00
|
|
|
SignerClient {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Arc::downgrade(signer),
|
2016-06-01 19:37:34 +02:00
|
|
|
accounts: Arc::downgrade(store),
|
|
|
|
client: Arc::downgrade(client),
|
|
|
|
miner: Arc::downgrade(miner),
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:50:46 +02:00
|
|
|
|
|
|
|
fn active(&self) -> Result<(), Error> {
|
|
|
|
// TODO: only call every 30s at most.
|
|
|
|
take_weak!(self.client).keep_alive();
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
impl<C: 'static, M: 'static> PersonalSigner for SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2016-10-31 17:11:56 +01:00
|
|
|
fn requests_to_confirm(&self) -> Result<Vec<ConfirmationRequest>, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-09-21 12:44:49 +02:00
|
|
|
let signer = take_weak!(self.signer);
|
2016-10-31 17:11:56 +01:00
|
|
|
|
|
|
|
Ok(signer.requests()
|
|
|
|
.into_iter()
|
|
|
|
.map(Into::into)
|
|
|
|
.collect()
|
|
|
|
)
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 17:11:56 +01:00
|
|
|
// TODO [ToDr] TransactionModification is redundant for some calls
|
|
|
|
// might be better to replace it in future
|
|
|
|
fn confirm_request(&self, id: U256, modification: TransactionModification, pass: String) -> Result<Value, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-10-31 17:11:56 +01:00
|
|
|
|
|
|
|
let id = id.into();
|
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
|
|
|
|
signer.peek(&id).map(|confirmation| {
|
|
|
|
let result = match confirmation.payload {
|
|
|
|
ConfirmationPayload::Transaction(mut request) => {
|
|
|
|
// apply modification
|
|
|
|
if let Some(gas_price) = modification.gas_price {
|
|
|
|
request.gas_price = gas_price.into();
|
2016-07-17 23:00:57 +02:00
|
|
|
}
|
2016-10-31 17:11:56 +01:00
|
|
|
sign_and_dispatch(&*client, &*miner, &*accounts, request.into(), Some(pass)).map(to_value)
|
|
|
|
},
|
|
|
|
ConfirmationPayload::Sign(address, hash) => {
|
|
|
|
sign(&*accounts, address, Some(pass), hash)
|
|
|
|
},
|
|
|
|
ConfirmationPayload::Decrypt(address, msg) => {
|
|
|
|
decrypt(&*accounts, address, Some(pass), msg)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if let Ok(ref response) = result {
|
|
|
|
signer.request_confirmed(id, Ok(response.clone()));
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-10-31 17:11:56 +01:00
|
|
|
result
|
|
|
|
}).unwrap_or_else(|| Err(errors::invalid_params("Unknown RequestID", id)))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 17:11:56 +01:00
|
|
|
fn reject_request(&self, id: U256) -> Result<bool, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-10-31 17:11:56 +01:00
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
|
|
|
|
let res = signer.request_rejected(id.into());
|
|
|
|
Ok(res.is_some())
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-09-21 12:44:49 +02:00
|
|
|
|
2016-10-31 17:11:56 +01:00
|
|
|
fn generate_token(&self) -> Result<String, Error> {
|
2016-09-21 12:44:49 +02:00
|
|
|
try!(self.active());
|
|
|
|
let signer = take_weak!(self.signer);
|
2016-10-31 17:11:56 +01:00
|
|
|
|
2016-09-21 12:44:49 +02:00
|
|
|
signer.generate_token()
|
|
|
|
.map_err(|e| errors::token(e))
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|