2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-11-06 12:51:53 +01: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/>.
|
|
|
|
|
|
|
|
//! Unsafe Signing RPC implementation.
|
|
|
|
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
|
|
|
use ethcore::account_provider::AccountProvider;
|
|
|
|
use ethcore::miner::MinerService;
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
|
|
|
|
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;
|
2017-02-08 15:36:53 +01:00
|
|
|
use v1::helpers::{errors, DefaultAccount};
|
|
|
|
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::{
|
|
|
|
U256 as RpcU256,
|
|
|
|
H160 as RpcH160, H256 as RpcH256, H520 as RpcH520, Bytes as RpcBytes,
|
|
|
|
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-11-06 12:51:53 +01:00
|
|
|
|
|
|
|
/// Implementation of functions that require signing when no trusted signer is used.
|
2017-02-08 15:36:53 +01:00
|
|
|
pub struct SigningUnsafeClient<D> {
|
2016-11-06 12:51:53 +01:00
|
|
|
accounts: Weak<AccountProvider>,
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher: D,
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<D: Dispatcher> SigningUnsafeClient<D> {
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Creates new SigningUnsafeClient.
|
2017-02-08 15:36:53 +01:00
|
|
|
pub fn new(accounts: &Arc<AccountProvider>, dispatcher: D) -> Self {
|
2016-11-06 12:51:53 +01:00
|
|
|
SigningUnsafeClient {
|
|
|
|
accounts: Arc::downgrade(accounts),
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher: dispatcher,
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
fn handle(&self, payload: RpcConfirmationPayload, account: DefaultAccount) -> BoxFuture<RpcConfirmationResponse, Error> {
|
|
|
|
let setup = move || {
|
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
let default_account = match account {
|
|
|
|
DefaultAccount::Provided(acc) => acc,
|
|
|
|
DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
|
|
|
|
};
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
(accounts, default_account)
|
2017-01-30 21:08:36 +01:00
|
|
|
};
|
2017-02-08 15:36:53 +01:00
|
|
|
|
|
|
|
let dis = self.dispatcher.clone();
|
|
|
|
future::done(setup())
|
|
|
|
.and_then(move |(accounts, default)| {
|
|
|
|
dispatch::from_rpc(payload, default, &dis)
|
|
|
|
.and_then(|payload| {
|
|
|
|
dispatch::execute(&dis, &accounts, payload, dispatch::SignWith::Nothing)
|
|
|
|
})
|
|
|
|
.map(|v| v.into_value())
|
|
|
|
})
|
|
|
|
.boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<D: Dispatcher + 'static> EthSigning for SigningUnsafeClient<D>
|
2016-11-06 12:51:53 +01:00
|
|
|
{
|
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-02-08 15:36:53 +01:00
|
|
|
self.handle(RpcConfirmationPayload::Signature((address.clone(), data).into()), address.into())
|
|
|
|
.then(|res| match res {
|
|
|
|
Ok(RpcConfirmationResponse::Signature(signature)) => Ok(signature),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
})
|
|
|
|
.boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn send_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcH256, Error> {
|
2017-02-08 15:36:53 +01:00
|
|
|
self.handle(RpcConfirmationPayload::SendTransaction(request), meta.into())
|
|
|
|
.then(|res| match res {
|
|
|
|
Ok(RpcConfirmationResponse::SendTransaction(hash)) => Ok(hash),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
})
|
|
|
|
.boxed()
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn sign_transaction(&self, meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<RpcRichRawTransaction, Error> {
|
2017-02-08 15:36:53 +01:00
|
|
|
self.handle(RpcConfirmationPayload::SignTransaction(request), meta.into())
|
|
|
|
.then(|res| match res {
|
|
|
|
Ok(RpcConfirmationResponse::SignTransaction(tx)) => Ok(tx),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
})
|
|
|
|
.boxed()
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<D: Dispatcher + 'static> ParitySigning for SigningUnsafeClient<D> {
|
2017-01-30 21:08:36 +01:00
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
fn decrypt_message(&self, address: RpcH160, data: RpcBytes) -> BoxFuture<RpcBytes, Error> {
|
2017-02-08 15:36:53 +01:00
|
|
|
self.handle(RpcConfirmationPayload::Decrypt((address.clone(), data).into()), address.into())
|
|
|
|
.then(|res| match res {
|
|
|
|
Ok(RpcConfirmationResponse::Decrypt(data)) => Ok(data),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
})
|
|
|
|
.boxed()
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
fn post_sign(&self, _: RpcH160, _: RpcBytes) -> BoxFuture<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
2016-11-06 12:51:53 +01:00
|
|
|
// We don't support this in non-signer mode.
|
2017-02-08 15:36:53 +01:00
|
|
|
future::err(errors::signer_disabled()).boxed()
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
fn post_transaction(&self, _: Metadata, _: RpcTransactionRequest) -> BoxFuture<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
2016-11-06 12:51:53 +01:00
|
|
|
// We don't support this in non-signer mode.
|
2017-02-08 15:36:53 +01:00
|
|
|
future::err((errors::signer_disabled())).boxed()
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn check_request(&self, _: RpcU256) -> Result<Option<RpcConfirmationResponse>, Error> {
|
2016-11-06 12:51:53 +01:00
|
|
|
// We don't support this in non-signer mode.
|
|
|
|
Err(errors::signer_disabled())
|
|
|
|
}
|
|
|
|
}
|