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;
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
use futures::{self, future, BoxFuture, Future};
|
2016-11-09 13:13:35 +01:00
|
|
|
use jsonrpc_core::Error;
|
|
|
|
use v1::helpers::{
|
2017-02-08 15:36:53 +01:00
|
|
|
errors,
|
2017-01-30 21:08:36 +01:00
|
|
|
DefaultAccount,
|
2017-03-16 13:15:56 +01:00
|
|
|
SIGNING_QUEUE_LIMIT, SigningQueue, ConfirmationPromise, ConfirmationResult, SignerService
|
2016-11-09 13:13:35 +01:00
|
|
|
};
|
2017-02-08 15:36:53 +01:00
|
|
|
use v1::helpers::dispatch::{self, Dispatcher};
|
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,
|
2017-02-14 22:45:43 +01:00
|
|
|
ConfirmationResponse as RpcConfirmationResponse,
|
|
|
|
Origin,
|
2016-11-09 13:13:35 +01:00
|
|
|
};
|
2016-06-22 15:55:07 +02:00
|
|
|
|
2017-03-16 13:15:56 +01:00
|
|
|
/// After 60s entries that are not queried with `check_request` will get garbage collected.
|
2017-03-20 19:15:02 +01:00
|
|
|
const MAX_PENDING_DURATION_SEC: u32 = 60;
|
2017-03-16 13:15:56 +01:00
|
|
|
/// Max number of total requests pending and completed, before we start garbage collecting them.
|
|
|
|
const MAX_TOTAL_REQUESTS: usize = SIGNING_QUEUE_LIMIT;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
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.
|
2017-02-08 15:36:53 +01:00
|
|
|
pub struct SigningQueueClient<D> {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Weak<SignerService>,
|
2016-06-22 21:32:26 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher: D,
|
|
|
|
pending: Arc<Mutex<TransientHashMap<U256, ConfirmationPromise>>>,
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
fn handle_dispatch<OnResponse>(res: Result<DispatchResult, Error>, on_response: OnResponse)
|
|
|
|
where OnResponse: FnOnce(Result<RpcConfirmationResponse, Error>) + Send + 'static
|
2016-11-06 12:51:53 +01:00
|
|
|
{
|
2017-02-08 15:36:53 +01:00
|
|
|
match res {
|
|
|
|
Ok(DispatchResult::Value(result)) => on_response(Ok(result)),
|
|
|
|
Ok(DispatchResult::Promise(promise)) => {
|
|
|
|
promise.wait_for_result(move |result| {
|
|
|
|
on_response(result.unwrap_or_else(|| Err(errors::request_rejected())))
|
|
|
|
})
|
|
|
|
},
|
|
|
|
Err(e) => on_response(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:15:56 +01:00
|
|
|
fn collect_garbage(map: &mut TransientHashMap<U256, ConfirmationPromise>) {
|
|
|
|
map.prune();
|
|
|
|
if map.len() > MAX_TOTAL_REQUESTS {
|
|
|
|
// Remove all non-waiting entries.
|
|
|
|
let non_waiting: Vec<_> = map
|
|
|
|
.iter()
|
|
|
|
.filter(|&(_, val)| val.result() != ConfirmationResult::Waiting)
|
|
|
|
.map(|(key, _)| *key)
|
|
|
|
.collect();
|
|
|
|
for k in non_waiting {
|
|
|
|
map.remove(&k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:55:06 +01:00
|
|
|
impl<D: Dispatcher + 'static> SigningQueueClient<D> {
|
2016-06-01 19:37:34 +02:00
|
|
|
/// Creates a new signing queue client given shared signing queue.
|
2017-02-08 15:36:53 +01:00
|
|
|
pub fn new(signer: &Arc<SignerService>, dispatcher: D, 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),
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher: dispatcher,
|
2017-03-16 13:15:56 +01:00
|
|
|
pending: Arc::new(Mutex::new(TransientHashMap::new(MAX_PENDING_DURATION_SEC))),
|
2016-06-18 14:55:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:50:46 +02:00
|
|
|
|
2017-02-14 22:45:43 +01:00
|
|
|
fn dispatch(&self, payload: RpcConfirmationPayload, default_account: DefaultAccount, origin: Origin) -> BoxFuture<DispatchResult, Error> {
|
2017-02-09 15:01:15 +01:00
|
|
|
let accounts = take_weakf!(self.accounts);
|
|
|
|
let default_account = match default_account {
|
|
|
|
DefaultAccount::Provided(acc) => acc,
|
2017-02-20 16:33:12 +01:00
|
|
|
DefaultAccount::ForDapp(dapp) => accounts.dapp_default_address(dapp).ok().unwrap_or_default(),
|
2017-01-30 21:08:36 +01:00
|
|
|
};
|
2017-02-08 15:36:53 +01:00
|
|
|
|
2017-02-09 15:01:15 +01:00
|
|
|
let dispatcher = self.dispatcher.clone();
|
|
|
|
let signer = take_weakf!(self.signer);
|
|
|
|
dispatch::from_rpc(payload, default_account, &dispatcher)
|
|
|
|
.and_then(move |payload| {
|
|
|
|
let sender = payload.sender();
|
|
|
|
if accounts.is_unlocked(sender) {
|
2017-02-09 21:12:28 +01:00
|
|
|
dispatch::execute(dispatcher, accounts, payload, dispatch::SignWith::Nothing)
|
2017-02-09 15:01:15 +01:00
|
|
|
.map(|v| v.into_value())
|
|
|
|
.map(DispatchResult::Value)
|
|
|
|
.boxed()
|
|
|
|
} else {
|
|
|
|
future::done(
|
2017-02-14 22:45:43 +01:00
|
|
|
signer.add_request(payload, origin)
|
2017-02-09 15:01:15 +01:00
|
|
|
.map(DispatchResult::Promise)
|
|
|
|
.map_err(|_| errors::request_rejected_limit())
|
|
|
|
).boxed()
|
|
|
|
}
|
2017-02-08 15:36:53 +01:00
|
|
|
})
|
|
|
|
.boxed()
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<D: Dispatcher + 'static> ParitySigning for SigningQueueClient<D> {
|
2017-01-30 21:08:36 +01:00
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-02-14 22:45:43 +01:00
|
|
|
fn post_sign(&self, meta: Metadata, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
2017-02-08 15:36:53 +01:00
|
|
|
let pending = self.pending.clone();
|
2017-02-14 22:45:43 +01:00
|
|
|
self.dispatch(
|
|
|
|
RpcConfirmationPayload::Signature((address.clone(), data).into()),
|
|
|
|
DefaultAccount::Provided(address.into()),
|
|
|
|
meta.origin
|
|
|
|
).map(move |result| match result {
|
|
|
|
DispatchResult::Value(v) => RpcEither::Or(v),
|
|
|
|
DispatchResult::Promise(promise) => {
|
|
|
|
let id = promise.id();
|
2017-03-16 13:15:56 +01:00
|
|
|
let mut pending = pending.lock();
|
|
|
|
collect_garbage(&mut pending);
|
|
|
|
pending.insert(id, promise);
|
|
|
|
|
2017-02-14 22:45:43 +01:00
|
|
|
RpcEither::Either(id.into())
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.boxed()
|
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> {
|
2017-02-08 15:36:53 +01:00
|
|
|
let pending = self.pending.clone();
|
2017-02-14 22:45:43 +01:00
|
|
|
self.dispatch(RpcConfirmationPayload::SendTransaction(request), meta.dapp_id().into(), meta.origin)
|
2017-02-08 15:36:53 +01:00
|
|
|
.map(move |result| match result {
|
|
|
|
DispatchResult::Value(v) => RpcEither::Or(v),
|
|
|
|
DispatchResult::Promise(promise) => {
|
|
|
|
let id = promise.id();
|
2017-03-16 13:15:56 +01:00
|
|
|
let mut pending = pending.lock();
|
|
|
|
collect_garbage(&mut pending);
|
|
|
|
pending.insert(id, promise);
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
RpcEither::Either(id.into())
|
|
|
|
},
|
|
|
|
})
|
2017-02-08 16:55:06 +01:00
|
|
|
.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> {
|
|
|
|
let id: U256 = id.into();
|
2017-03-16 13:15:56 +01:00
|
|
|
match self.pending.lock().get(&id) {
|
2016-11-09 13:13:35 +01:00
|
|
|
Some(ref promise) => match promise.result() {
|
2017-03-16 13:15:56 +01:00
|
|
|
ConfirmationResult::Waiting => Ok(None),
|
2016-11-09 13:13:35 +01:00
|
|
|
ConfirmationResult::Rejected => Err(errors::request_rejected()),
|
|
|
|
ConfirmationResult::Confirmed(rpc_response) => rpc_response.map(Some),
|
|
|
|
},
|
2017-03-16 13:15:56 +01:00
|
|
|
_ => Err(errors::request_not_found()),
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-10-15 14:44:08 +02:00
|
|
|
|
2017-02-14 22:45:43 +01:00
|
|
|
fn decrypt_message(&self, meta: Metadata, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcBytes, Error> {
|
|
|
|
let res = self.dispatch(
|
|
|
|
RpcConfirmationPayload::Decrypt((address.clone(), data).into()),
|
|
|
|
address.into(),
|
|
|
|
meta.origin,
|
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2017-02-08 15:36:53 +01:00
|
|
|
|
|
|
|
// when dispatch is complete
|
|
|
|
res.then(move |res| {
|
|
|
|
// register callback via the oneshot sender.
|
|
|
|
handle_dispatch(res, move |response| {
|
|
|
|
match response {
|
|
|
|
Ok(RpcConfirmationResponse::Decrypt(data)) => ready.complete(Ok(data)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// and wait for that to resolve.
|
2017-02-09 15:10:14 +01:00
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled.")))
|
2017-02-08 15:36:53 +01:00
|
|
|
}).boxed()
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<D: Dispatcher + 'static> EthSigning for SigningQueueClient<D> {
|
2017-01-30 21:08:36 +01:00
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-02-14 22:45:43 +01:00
|
|
|
fn sign(&self, meta: Metadata, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcH520, Error> {
|
|
|
|
let res = self.dispatch(
|
|
|
|
RpcConfirmationPayload::Signature((address.clone(), data).into()),
|
|
|
|
address.into(),
|
|
|
|
meta.origin,
|
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2017-02-08 15:36:53 +01:00
|
|
|
|
|
|
|
res.then(move |res| {
|
|
|
|
handle_dispatch(res, move |response| {
|
|
|
|
match response {
|
|
|
|
Ok(RpcConfirmationResponse::Signature(sig)) => ready.complete(Ok(sig)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-09 15:10:14 +01:00
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled.")))
|
2017-02-08 15:36:53 +01:00
|
|
|
}).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> {
|
2017-02-14 22:45:43 +01:00
|
|
|
let res = self.dispatch(
|
|
|
|
RpcConfirmationPayload::SendTransaction(request),
|
|
|
|
meta.dapp_id().into(),
|
|
|
|
meta.origin,
|
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2017-02-08 15:36:53 +01:00
|
|
|
|
|
|
|
res.then(move |res| {
|
|
|
|
handle_dispatch(res, move |response| {
|
|
|
|
match response {
|
|
|
|
Ok(RpcConfirmationResponse::SendTransaction(hash)) => ready.complete(Ok(hash)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-09 15:10:14 +01:00
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled.")))
|
2017-02-08 15:36:53 +01:00
|
|
|
}).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> {
|
2017-02-14 22:45:43 +01:00
|
|
|
let res = self.dispatch(
|
|
|
|
RpcConfirmationPayload::SignTransaction(request),
|
|
|
|
meta.dapp_id().into(),
|
|
|
|
meta.origin,
|
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
let (ready, p) = futures::oneshot();
|
2017-02-08 15:36:53 +01:00
|
|
|
|
|
|
|
res.then(move |res| {
|
|
|
|
handle_dispatch(res, move |response| {
|
|
|
|
match response {
|
|
|
|
Ok(RpcConfirmationResponse::SignTransaction(tx)) => ready.complete(Ok(tx)),
|
|
|
|
Err(e) => ready.complete(Err(e)),
|
|
|
|
e => ready.complete(Err(errors::internal("Unexpected result.", e))),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-09 15:10:14 +01:00
|
|
|
p.then(|result| futures::done(result.expect("Ready is never dropped nor canceled.")))
|
2017-02-08 15:36:53 +01:00
|
|
|
}).boxed()
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
2016-06-15 00:17:23 +02:00
|
|
|
}
|