2016-12-11 19:30:54 +01:00
|
|
|
// Copyright 2015, 2016 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};
|
2016-12-10 20:01:04 +01:00
|
|
|
use util::Hashable;
|
2016-11-06 12:51:53 +01:00
|
|
|
|
|
|
|
use ethcore::account_provider::AccountProvider;
|
|
|
|
use ethcore::miner::MinerService;
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
use jsonrpc_core::Error;
|
2016-12-13 14:27:27 +01:00
|
|
|
use jsonrpc_macros::Ready;
|
2016-11-06 12:51:53 +01:00
|
|
|
use v1::helpers::errors;
|
2016-11-09 13:13:35 +01:00
|
|
|
use v1::helpers::dispatch;
|
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.
|
|
|
|
pub struct SigningUnsafeClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
{
|
|
|
|
accounts: Weak<AccountProvider>,
|
|
|
|
client: Weak<C>,
|
|
|
|
miner: Weak<M>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, M> SigningUnsafeClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Creates new SigningUnsafeClient.
|
|
|
|
pub fn new(client: &Arc<C>, accounts: &Arc<AccountProvider>, miner: &Arc<M>)
|
|
|
|
-> Self {
|
|
|
|
SigningUnsafeClient {
|
|
|
|
client: Arc::downgrade(client),
|
|
|
|
miner: Arc::downgrade(miner),
|
|
|
|
accounts: Arc::downgrade(accounts),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn active(&self) -> Result<(), Error> {
|
|
|
|
// TODO: only call every 30s at most.
|
|
|
|
take_weak!(self.client).keep_alive();
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-11-09 13:13:35 +01:00
|
|
|
|
|
|
|
fn handle(&self, payload: RpcConfirmationPayload) -> Result<RpcConfirmationResponse, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.active()?;
|
2016-11-09 13:13:35 +01:00
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
|
|
|
|
let payload = dispatch::from_rpc(payload, &*client, &*miner);
|
2016-11-30 16:11:41 +01:00
|
|
|
dispatch::execute(&*client, &*miner, &*accounts, payload, dispatch::SignWith::Nothing)
|
|
|
|
.map(|v| v.into_value())
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: 'static, M: 'static> EthSigning for SigningUnsafeClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
{
|
2016-12-10 20:01:04 +01:00
|
|
|
fn sign(&self, ready: Ready<RpcH520>, address: RpcH160, data: RpcBytes) {
|
|
|
|
let hash = data.0.sha3().into();
|
2016-11-09 13:13:35 +01:00
|
|
|
let result = match self.handle(RpcConfirmationPayload::Signature((address, hash).into())) {
|
|
|
|
Ok(RpcConfirmationResponse::Signature(signature)) => Ok(signature),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
};
|
|
|
|
ready.ready(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_transaction(&self, ready: Ready<RpcH256>, request: RpcTransactionRequest) {
|
|
|
|
let result = match self.handle(RpcConfirmationPayload::SendTransaction(request)) {
|
|
|
|
Ok(RpcConfirmationResponse::SendTransaction(hash)) => Ok(hash),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
};
|
|
|
|
ready.ready(result);
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-18 11:03:29 +01:00
|
|
|
fn sign_transaction(&self, ready: Ready<RpcRichRawTransaction>, request: RpcTransactionRequest) {
|
2016-11-09 13:13:35 +01:00
|
|
|
let result = match self.handle(RpcConfirmationPayload::SignTransaction(request)) {
|
2016-11-18 11:03:29 +01:00
|
|
|
Ok(RpcConfirmationResponse::SignTransaction(tx)) => Ok(tx),
|
2016-11-09 13:13:35 +01:00
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
};
|
|
|
|
ready.ready(result);
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: 'static, M: 'static> ParitySigning for SigningUnsafeClient<C, M> where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
|
|
|
{
|
2016-11-09 13:13:35 +01:00
|
|
|
fn decrypt_message(&self, ready: Ready<RpcBytes>, address: RpcH160, data: RpcBytes) {
|
|
|
|
let result = match self.handle(RpcConfirmationPayload::Decrypt((address, data).into())) {
|
|
|
|
Ok(RpcConfirmationResponse::Decrypt(data)) => Ok(data),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
e => Err(errors::internal("Unexpected result", e)),
|
|
|
|
};
|
|
|
|
ready.ready(result);
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn post_sign(&self, _: RpcH160, _: RpcH256) -> Result<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
2016-11-06 12:51:53 +01:00
|
|
|
// We don't support this in non-signer mode.
|
|
|
|
Err(errors::signer_disabled())
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn post_transaction(&self, _: RpcTransactionRequest) -> Result<RpcEither<RpcU256, RpcConfirmationResponse>, Error> {
|
2016-11-06 12:51:53 +01:00
|
|
|
// We don't support this in non-signer mode.
|
|
|
|
Err(errors::signer_disabled())
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|