2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-01 19:37:34 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
//! Signing RPC implementation.
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
use std::sync::{Arc, Weak};
|
2016-11-09 13:13:35 +01:00
|
|
|
use transient_hashmap::TransientHashMap;
|
2017-01-30 15:08:02 +01:00
|
|
|
use util::{U256, Mutex};
|
2016-11-06 12:51:53 +01:00
|
|
|
|
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-06-01 19:57:34 +02:00
|
|
|
use ethcore::miner::MinerService;
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
use futures::{self, BoxFuture, Future};
|
2016-11-09 13:13:35 +01:00
|
|
|
use jsonrpc_core::Error;
|
|
|
|
use v1::helpers::{
|
|
|
|
errors, dispatch,
|
2017-01-30 21:08:36 +01:00
|
|
|
DefaultAccount,
|
2016-11-09 13:13:35 +01:00
|
|
|
SigningQueue, ConfirmationPromise, ConfirmationResult, ConfirmationPayload, SignerService
|
|
|
|
};
|
2017-01-30 21:08:36 +01:00
|
|
|
use v1::metadata::Metadata;
|
2016-11-06 12:51:53 +01:00
|
|
|
use v1::traits::{EthSigning, ParitySigning};
|
2016-11-09 13:13:35 +01:00
|
|
|
use v1::types::{
|
|
|
|
H160 as RpcH160, H256 as RpcH256, U256 as RpcU256, Bytes as RpcBytes, H520 as RpcH520,
|
|
|
|
Either as RpcEither,
|
2016-11-18 11:03:29 +01:00
|
|
|
RichRawTransaction as RpcRichRawTransaction,
|
2016-11-09 13:13:35 +01:00
|
|
|
TransactionRequest as RpcTransactionRequest,
|
|
|
|
ConfirmationPayload as RpcConfirmationPayload,
|
|
|
|
ConfirmationResponse as RpcConfirmationResponse
|
|
|
|
};
|
2016-06-22 15:55:07 +02:00
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
const MAX_PENDING_DURATION: u64 = 60 * 60;
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
enum DispatchResult {
|
2016-11-06 12:51:53 +01:00
|
|
|
Promise(ConfirmationPromise),
|
2016-11-09 13:13:35 +01:00
|
|
|
Value(RpcConfirmationResponse),
|
2016-06-22 15:55:07 +02:00
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Implementation of functions that require signing when no trusted signer is used.
|
2016-11-06 12:51:53 +01:00
|
|
|
pub struct SigningQueueClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Weak<SignerService>,
|
2016-06-22 21:32:26 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2016-06-22 15:55:07 +02:00
|
|
|
client: Weak<C>,
|
2016-06-18 14:55:46 +02:00
|
|
|
miner: Weak<M>,
|
2016-07-19 09:19:58 +02:00
|
|
|
|
|
|
|
pending: Mutex<TransientHashMap<U256, ConfirmationPromise>>,
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
impl<C, M> SigningQueueClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
{
|
2016-06-01 19:37:34 +02:00
|
|
|
/// Creates a new signing queue client given shared signing queue.
|
2016-09-21 12:44:49 +02:00
|
|
|
pub fn new(signer: &Arc<SignerService>, client: &Arc<C>, miner: &Arc<M>, accounts: &Arc<AccountProvider>) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
SigningQueueClient {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Arc::downgrade(signer),
|
2016-06-22 21:32:26 +02:00
|
|
|
accounts: Arc::downgrade(accounts),
|
2016-06-22 15:55:07 +02:00
|
|
|
client: Arc::downgrade(client),
|
2016-06-18 14:55:46 +02:00
|
|
|
miner: Arc::downgrade(miner),
|
2016-07-19 09:19:58 +02:00
|
|
|
pending: Mutex::new(TransientHashMap::new(MAX_PENDING_DURATION)),
|
2016-06-18 14:55:46 +02:00
|
|
|
}
|
|
|
|
}
|
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-11-09 13:13:35 +01:00
|
|
|
fn handle_dispatch<OnResponse>(&self, res: Result<DispatchResult, Error>, on_response: OnResponse)
|
|
|
|
where OnResponse: FnOnce(Result<RpcConfirmationResponse, Error>) + Send + 'static
|
|
|
|
{
|
2016-10-15 14:44:08 +02:00
|
|
|
match res {
|
2016-11-09 13:13:35 +01:00
|
|
|
Ok(DispatchResult::Value(result)) => on_response(Ok(result)),
|
2016-10-15 14:44:08 +02:00
|
|
|
Ok(DispatchResult::Promise(promise)) => {
|
|
|
|
promise.wait_for_result(move |result| {
|
2016-11-09 13:13:35 +01:00
|
|
|
on_response(result.unwrap_or_else(|| Err(errors::request_rejected())))
|
2016-10-15 14:44:08 +02:00
|
|
|
})
|
|
|
|
},
|
2016-11-09 13:13:35 +01:00
|
|
|
Err(e) => on_response(Err(e)),
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn add_to_queue(&self, payload: ConfirmationPayload) -> Result<DispatchResult, Error> {
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
|
|
|
|
let sender = payload.sender();
|
|
|
|
if accounts.is_unlocked(sender) {
|
2016-11-30 16:11:41 +01:00
|
|
|
return dispatch::execute(&*client, &*miner, &*accounts, payload, dispatch::SignWith::Nothing)
|
|
|
|
.map(|v| v.into_value())
|
|
|
|
.map(DispatchResult::Value);
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
take_weak!(self.signer).add_request(payload)
|
|
|
|
.map(DispatchResult::Promise)
|
|
|
|
.map_err(|_| errors::request_rejected_limit())
|
2016-08-03 10:36:54 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn dispatch(&self, payload: RpcConfirmationPayload, default_account: DefaultAccount) -> Result<DispatchResult, Error> {
|
2016-11-09 13:13:35 +01:00
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
let default_account = match default_account {
|
|
|
|
DefaultAccount::Provided(acc) => acc,
|
|
|
|
DefaultAccount::ForDapp(dapp) => take_weak!(self.accounts).default_address(dapp).ok().unwrap_or_default(),
|
|
|
|
};
|
|
|
|
let payload = dispatch::from_rpc(payload, default_account, &*client, &*miner);
|
2016-11-09 13:13:35 +01:00
|
|
|
self.add_to_queue(payload)
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
impl<C: 'static, M: 'static> ParitySigning for SigningQueueClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
2016-07-19 09:19:58 +02:00
|
|
|
{
|
2017-01-30 21:08:36 +01:00
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-01-30 15:08:02 +01:00
|
|
|
fn post_sign(&self, address: RpcH160, data: RpcBytes) -> Result<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2017-01-30 21:08:36 +01:00
|
|
|
self.dispatch(RpcConfirmationPayload::Signature((address.clone(), data).into()), DefaultAccount::Provided(address.into()))
|
2016-11-09 13:13:35 +01:00
|
|
|
.map(|result| match result {
|
|
|
|
DispatchResult::Value(v) => RpcEither::Or(v),
|
|
|
|
DispatchResult::Promise(promise) => {
|
|
|
|
let id = promise.id();
|
|
|
|
self.pending.lock().insert(id, promise);
|
|
|
|
RpcEither::Either(id.into())
|
|
|
|
},
|
|
|
|
})
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn post_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
|
|
|
let post_transaction = move || {
|
|
|
|
self.active()?;
|
|
|
|
self.dispatch(RpcConfirmationPayload::SendTransaction(request), meta.into())
|
|
|
|
.map(|result| match result {
|
|
|
|
DispatchResult::Value(v) => RpcEither::Or(v),
|
|
|
|
DispatchResult::Promise(promise) => {
|
|
|
|
let id = promise.id();
|
|
|
|
self.pending.lock().insert(id, promise);
|
|
|
|
RpcEither::Either(id.into())
|
|
|
|
},
|
|
|
|
})
|
|
|
|
};
|
|
|
|
futures::done(post_transaction()).boxed()
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn check_request(&self, id: RpcU256) -> Result<Option<RpcConfirmationResponse>, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-07-19 09:19:58 +02:00
|
|
|
let mut pending = self.pending.lock();
|
2016-11-09 13:13:35 +01:00
|
|
|
let id: U256 = id.into();
|
|
|
|
let res = match pending.get(&id) {
|
|
|
|
Some(ref promise) => match promise.result() {
|
|
|
|
ConfirmationResult::Waiting => { return Ok(None); }
|
|
|
|
ConfirmationResult::Rejected => Err(errors::request_rejected()),
|
|
|
|
ConfirmationResult::Confirmed(rpc_response) => rpc_response.map(Some),
|
|
|
|
},
|
|
|
|
_ => { return Err(errors::request_not_found()); }
|
|
|
|
};
|
|
|
|
pending.remove(&id);
|
|
|
|
res
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-10-15 14:44:08 +02:00
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
fn decrypt_message(&self, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcBytes, Error> {
|
2016-10-15 14:44:08 +02:00
|
|
|
let res = self.active()
|
2017-01-30 21:08:36 +01:00
|
|
|
.and_then(|_| self.dispatch(RpcConfirmationPayload::Decrypt((address.clone(), data).into()), address.into()));
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2016-11-09 13:13:35 +01:00
|
|
|
// TODO [todr] typed handle_dispatch
|
|
|
|
self.handle_dispatch(res, |response| {
|
|
|
|
match response {
|
2017-01-11 20:02:27 +01:00
|
|
|
Ok(RpcConfirmationResponse::Decrypt(data)) => ready.complete(Ok(data)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled."))).boxed()
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
impl<C: 'static, M: 'static> EthSigning for SigningQueueClient<C, M> where
|
2016-06-01 19:57:34 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-11-06 12:51:53 +01:00
|
|
|
M: MinerService,
|
|
|
|
{
|
2017-01-30 21:08:36 +01:00
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
fn sign(&self, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcH520, Error> {
|
2017-01-30 21:08:36 +01:00
|
|
|
let res = self.active()
|
|
|
|
.and_then(|_| self.dispatch(RpcConfirmationPayload::Signature((address.clone(), data).into()), address.into()));
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2016-11-09 13:13:35 +01:00
|
|
|
self.handle_dispatch(res, |response| {
|
|
|
|
match response {
|
2017-01-11 20:02:27 +01:00
|
|
|
Ok(RpcConfirmationResponse::Signature(signature)) => ready.complete(Ok(signature)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled."))).boxed()
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn send_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcH256, Error> {
|
|
|
|
let res = self.active()
|
|
|
|
.and_then(|_| self.dispatch(RpcConfirmationPayload::SendTransaction(request), meta.into()));
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2016-11-09 13:13:35 +01:00
|
|
|
self.handle_dispatch(res, |response| {
|
|
|
|
match response {
|
2017-01-11 20:02:27 +01:00
|
|
|
Ok(RpcConfirmationResponse::SendTransaction(hash)) => ready.complete(Ok(hash)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled."))).boxed()
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn sign_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcRichRawTransaction, Error> {
|
|
|
|
let res = self.active().and_then(|_| self.dispatch(RpcConfirmationPayload::SignTransaction(request), meta.into()));
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2016-11-09 13:13:35 +01:00
|
|
|
self.handle_dispatch(res, |response| {
|
|
|
|
match response {
|
2017-01-11 20:02:27 +01:00
|
|
|
Ok(RpcConfirmationResponse::SignTransaction(tx)) => ready.complete(Ok(tx)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled."))).boxed()
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
2016-06-15 00:17:23 +02:00
|
|
|
}
|