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
|
|
|
|
2017-05-28 14:40:36 +02:00
|
|
|
use std::sync::Arc;
|
2016-11-09 13:13:35 +01:00
|
|
|
use transient_hashmap::TransientHashMap;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::Mutex;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
|
|
|
use ethcore::account_provider::AccountProvider;
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
use jsonrpc_core::{BoxFuture, Result, Error};
|
2017-10-17 15:41:32 +02:00
|
|
|
use jsonrpc_core::futures::{future, Future, Poll, Async};
|
2017-10-05 12:35:01 +02:00
|
|
|
use jsonrpc_core::futures::future::Either;
|
2016-11-09 13:13:35 +01:00
|
|
|
use v1::helpers::{
|
2017-10-17 14:50:53 +02:00
|
|
|
errors, DefaultAccount, SignerService, SigningQueue,
|
|
|
|
ConfirmationReceiver as RpcConfirmationReceiver,
|
|
|
|
ConfirmationResult as RpcConfirmationResult,
|
2016-11-09 13:13:35 +01:00
|
|
|
};
|
2017-02-08 15:36:53 +01:00
|
|
|
use v1::helpers::dispatch::{self, Dispatcher};
|
2017-03-29 17:07:58 +02:00
|
|
|
use v1::helpers::accounts::unwrap_provider;
|
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-10-17 14:50:53 +02:00
|
|
|
use parity_reactor::Remote;
|
|
|
|
|
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;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
#[must_use = "futures do nothing unless polled"]
|
2017-01-30 21:08:36 +01:00
|
|
|
enum DispatchResult {
|
2017-10-17 14:50:53 +02:00
|
|
|
Future(U256, RpcConfirmationReceiver),
|
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
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
impl Future for DispatchResult {
|
|
|
|
type Item = RpcConfirmationResponse;
|
|
|
|
type Error = Error;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match *self {
|
|
|
|
DispatchResult::Value(ref response) => Ok(Async::Ready(response.clone())),
|
|
|
|
DispatchResult::Future(_uid, ref mut future) => try_ready!(future.poll()).map(Async::Ready),
|
|
|
|
}
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
fn schedule(remote: Remote,
|
|
|
|
confirmations: Arc<Mutex<TransientHashMap<U256, Option<RpcConfirmationResult>>>>,
|
|
|
|
id: U256,
|
|
|
|
future: RpcConfirmationReceiver) {
|
|
|
|
{
|
|
|
|
let mut confirmations = confirmations.lock();
|
|
|
|
confirmations.insert(id.clone(), None);
|
2017-03-16 13:15:56 +01:00
|
|
|
}
|
2017-10-17 14:50:53 +02:00
|
|
|
|
|
|
|
let future = future.then(move |result| {
|
|
|
|
let mut confirmations = confirmations.lock();
|
|
|
|
confirmations.prune();
|
|
|
|
let result = result.and_then(|response| response);
|
|
|
|
confirmations.insert(id, Some(result));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
remote.spawn(future);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of functions that require signing when no trusted signer is used.
|
|
|
|
pub struct SigningQueueClient<D> {
|
|
|
|
signer: Arc<SignerService>,
|
|
|
|
accounts: Option<Arc<AccountProvider>>,
|
|
|
|
dispatcher: D,
|
|
|
|
remote: Remote,
|
|
|
|
// None here means that the request hasn't yet been confirmed
|
|
|
|
confirmations: Arc<Mutex<TransientHashMap<U256, Option<RpcConfirmationResult>>>>,
|
2017-03-16 13:15:56 +01:00
|
|
|
}
|
|
|
|
|
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-10-17 14:50:53 +02:00
|
|
|
pub fn new(signer: &Arc<SignerService>, dispatcher: D, remote: Remote, accounts: &Option<Arc<AccountProvider>>) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
SigningQueueClient {
|
2017-05-28 14:40:36 +02:00
|
|
|
signer: signer.clone(),
|
|
|
|
accounts: accounts.clone(),
|
2017-10-17 14:50:53 +02:00
|
|
|
dispatcher,
|
|
|
|
remote,
|
|
|
|
confirmations: 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-11-14 11:38:17 +01:00
|
|
|
fn account_provider(&self) -> Result<Arc<AccountProvider>> {
|
2017-03-29 17:07:58 +02:00
|
|
|
unwrap_provider(&self.accounts)
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn dispatch(&self, payload: RpcConfirmationPayload, default_account: DefaultAccount, origin: Origin) -> BoxFuture<DispatchResult> {
|
2017-03-29 17:07:58 +02:00
|
|
|
let accounts = try_bf!(self.account_provider());
|
2017-02-09 15:01:15 +01:00
|
|
|
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();
|
2017-05-28 14:40:36 +02:00
|
|
|
let signer = self.signer.clone();
|
2017-10-05 12:35:01 +02:00
|
|
|
Box::new(dispatch::from_rpc(payload, default_account, &dispatcher)
|
2017-02-09 15:01:15 +01:00
|
|
|
.and_then(move |payload| {
|
|
|
|
let sender = payload.sender();
|
2017-11-01 11:23:18 +01:00
|
|
|
if accounts.is_unlocked(&sender) {
|
2017-10-05 12:35:01 +02:00
|
|
|
Either::A(dispatch::execute(dispatcher, accounts, payload, dispatch::SignWith::Nothing)
|
2017-02-09 15:01:15 +01:00
|
|
|
.map(|v| v.into_value())
|
2017-10-05 12:35:01 +02:00
|
|
|
.map(DispatchResult::Value))
|
2017-02-09 15:01:15 +01:00
|
|
|
} else {
|
2017-10-05 12:35:01 +02:00
|
|
|
Either::B(future::done(
|
2017-02-14 22:45:43 +01:00
|
|
|
signer.add_request(payload, origin)
|
2017-10-17 14:50:53 +02:00
|
|
|
.map(|(id, future)| DispatchResult::Future(id, future))
|
2017-02-09 15:01:15 +01:00
|
|
|
.map_err(|_| errors::request_rejected_limit())
|
2017-10-05 12:35:01 +02:00
|
|
|
))
|
2017-02-09 15:01:15 +01:00
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
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-11-14 11:38:17 +01:00
|
|
|
fn compose_transaction(&self, meta: Metadata, transaction: RpcTransactionRequest) -> BoxFuture<RpcTransactionRequest> {
|
2017-05-02 11:39:48 +02:00
|
|
|
let accounts = try_bf!(self.account_provider());
|
|
|
|
let default_account = accounts.dapp_default_address(meta.dapp_id().into()).ok().unwrap_or_default();
|
2017-10-05 12:35:01 +02:00
|
|
|
Box::new(self.dispatcher.fill_optional_fields(transaction.into(), default_account, true).map(Into::into))
|
2017-05-02 11:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn post_sign(&self, meta: Metadata, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcEither<RpcU256, RpcConfirmationResponse>> {
|
2017-10-17 14:50:53 +02:00
|
|
|
let remote = self.remote.clone();
|
|
|
|
let confirmations = self.confirmations.clone();
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
Box::new(self.dispatch(
|
2017-04-12 12:15:13 +02:00
|
|
|
RpcConfirmationPayload::EthSignMessage((address.clone(), data).into()),
|
2017-02-14 22:45:43 +01:00
|
|
|
DefaultAccount::Provided(address.into()),
|
|
|
|
meta.origin
|
|
|
|
).map(move |result| match result {
|
|
|
|
DispatchResult::Value(v) => RpcEither::Or(v),
|
2017-10-17 14:50:53 +02:00
|
|
|
DispatchResult::Future(id, future) => {
|
|
|
|
schedule(remote, confirmations, id, future);
|
2017-02-14 22:45:43 +01:00
|
|
|
RpcEither::Either(id.into())
|
|
|
|
},
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn post_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcEither<RpcU256, RpcConfirmationResponse>> {
|
2017-10-17 14:50:53 +02:00
|
|
|
let remote = self.remote.clone();
|
|
|
|
let confirmations = self.confirmations.clone();
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
Box::new(self.dispatch(RpcConfirmationPayload::SendTransaction(request), meta.dapp_id().into(), meta.origin)
|
2017-10-17 14:50:53 +02:00
|
|
|
.map(|result| match result {
|
2017-02-08 15:36:53 +01:00
|
|
|
DispatchResult::Value(v) => RpcEither::Or(v),
|
2017-10-17 14:50:53 +02:00
|
|
|
DispatchResult::Future(id, future) => {
|
|
|
|
schedule(remote, confirmations, id, future);
|
2017-02-08 15:36:53 +01:00
|
|
|
RpcEither::Either(id.into())
|
|
|
|
},
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn check_request(&self, id: RpcU256) -> Result<Option<RpcConfirmationResponse>> {
|
2016-11-09 13:13:35 +01:00
|
|
|
let id: U256 = id.into();
|
2017-10-17 14:50:53 +02:00
|
|
|
match self.confirmations.lock().get(&id) {
|
|
|
|
None => Err(errors::request_not_found()), // Request info has been dropped, or even never been there
|
|
|
|
Some(&None) => Ok(None), // No confirmation yet, request is known, confirmation is pending
|
|
|
|
Some(&Some(ref confirmation)) => confirmation.clone().map(Some), // Confirmation is there
|
2017-03-16 13:15:56 +01:00
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-10-15 14:44:08 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn decrypt_message(&self, meta: Metadata, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcBytes> {
|
2017-02-14 22:45:43 +01:00
|
|
|
let res = self.dispatch(
|
|
|
|
RpcConfirmationPayload::Decrypt((address.clone(), data).into()),
|
|
|
|
address.into(),
|
|
|
|
meta.origin,
|
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
// when dispatch is complete - wait for result and then
|
|
|
|
Box::new(res.flatten().and_then(move |response| {
|
|
|
|
match response {
|
|
|
|
RpcConfirmationResponse::Decrypt(data) => Ok(data),
|
|
|
|
e => Err(errors::internal("Unexpected result.", e)),
|
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
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-11-14 11:38:17 +01:00
|
|
|
fn sign(&self, meta: Metadata, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcH520> {
|
2017-02-14 22:45:43 +01:00
|
|
|
let res = self.dispatch(
|
2017-04-12 12:15:13 +02:00
|
|
|
RpcConfirmationPayload::EthSignMessage((address.clone(), data).into()),
|
2017-02-14 22:45:43 +01:00
|
|
|
address.into(),
|
|
|
|
meta.origin,
|
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
Box::new(res.flatten().and_then(move |response| {
|
|
|
|
match response {
|
|
|
|
RpcConfirmationResponse::Signature(sig) => Ok(sig),
|
|
|
|
e => Err(errors::internal("Unexpected result.", e)),
|
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn send_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcH256> {
|
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
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
Box::new(res.flatten().and_then(move |response| {
|
|
|
|
match response {
|
|
|
|
RpcConfirmationResponse::SendTransaction(hash) => Ok(hash),
|
|
|
|
e => Err(errors::internal("Unexpected result.", e)),
|
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn sign_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcRichRawTransaction> {
|
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
|
|
|
|
2017-10-17 14:50:53 +02:00
|
|
|
Box::new(res.flatten().and_then(move |response| {
|
|
|
|
match response {
|
|
|
|
RpcConfirmationResponse::SignTransaction(tx) => Ok(tx),
|
|
|
|
e => Err(errors::internal("Unexpected result.", e)),
|
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
}))
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
2016-06-15 00:17:23 +02:00
|
|
|
}
|