2016-06-01 19:37:34 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Eth Signing RPC implementation.
|
|
|
|
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
use jsonrpc_core::*;
|
2016-06-01 19:57:34 +02:00
|
|
|
use ethcore::miner::MinerService;
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
2016-07-19 09:19:58 +02:00
|
|
|
use util::{U256, Address, H256, Mutex};
|
|
|
|
use transient_hashmap::TransientHashMap;
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-09-21 12:44:49 +02:00
|
|
|
use v1::helpers::{errors, SigningQueue, ConfirmationPromise, ConfirmationResult, ConfirmationPayload, TransactionRequest as TRequest, FilledTransactionRequest as FilledRequest, SignerService};
|
2016-08-08 17:25:15 +02:00
|
|
|
use v1::helpers::dispatch::{default_gas_price, sign_and_dispatch};
|
2016-06-01 19:37:34 +02:00
|
|
|
use v1::traits::EthSigning;
|
2016-07-19 09:19:58 +02:00
|
|
|
use v1::types::{TransactionRequest, H160 as RpcH160, H256 as RpcH256, H520 as RpcH520, U256 as RpcU256};
|
2016-06-22 15:55:07 +02:00
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn fill_optional_fields<C, M>(request: TRequest, client: &C, miner: &M) -> FilledRequest
|
2016-06-22 15:55:07 +02:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
2016-08-03 10:36:54 +02:00
|
|
|
FilledRequest {
|
|
|
|
from: request.from,
|
|
|
|
to: request.to,
|
|
|
|
nonce: request.nonce,
|
|
|
|
gas_price: request.gas_price.unwrap_or_else(|| default_gas_price(client, miner)),
|
|
|
|
gas: request.gas.unwrap_or_else(|| miner.sensible_gas_limit()),
|
|
|
|
value: request.value.unwrap_or_else(|| 0.into()),
|
|
|
|
data: request.data.unwrap_or_else(Vec::new),
|
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-06-22 15:55:07 +02:00
|
|
|
pub struct EthSigningQueueClient<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-07-19 09:19:58 +02:00
|
|
|
const MAX_PENDING_DURATION: u64 = 60 * 60;
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
pub enum DispatchResult {
|
|
|
|
Promise(ConfirmationPromise),
|
|
|
|
Value(Value),
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:55:07 +02:00
|
|
|
impl<C, M> EthSigningQueueClient<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-06-01 19:37:34 +02:00
|
|
|
EthSigningQueueClient {
|
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-09-01 12:00:00 +02:00
|
|
|
fn dispatch_sign(&self, params: Params) -> Result<DispatchResult, Error> {
|
2016-08-03 10:36:54 +02:00
|
|
|
from_params::<(RpcH160, RpcH256)>(params).and_then(|(address, msg)| {
|
|
|
|
let address: Address = address.into();
|
|
|
|
let msg: H256 = msg.into();
|
|
|
|
|
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
if accounts.is_unlocked(address) {
|
2016-09-01 12:00:00 +02:00
|
|
|
return Ok(DispatchResult::Value(to_value(&accounts.sign(address, msg).ok().map_or_else(RpcH520::default, Into::into))))
|
2016-08-03 10:36:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 12:44:49 +02:00
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
signer.add_request(ConfirmationPayload::Sign(address, msg))
|
2016-09-01 12:00:00 +02:00
|
|
|
.map(DispatchResult::Promise)
|
|
|
|
.map_err(|_| errors::request_rejected_limit())
|
2016-08-03 10:36:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
fn dispatch_transaction(&self, params: Params) -> Result<DispatchResult, Error> {
|
2016-06-01 19:37:34 +02:00
|
|
|
from_params::<(TransactionRequest, )>(params)
|
2016-07-06 11:23:29 +02:00
|
|
|
.and_then(|(request, )| {
|
2016-08-03 10:36:54 +02:00
|
|
|
let request: TRequest = request.into();
|
2016-06-22 21:32:26 +02:00
|
|
|
let accounts = take_weak!(self.accounts);
|
2016-06-22 15:55:07 +02:00
|
|
|
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
|
|
|
|
|
2016-06-22 21:32:26 +02:00
|
|
|
if accounts.is_unlocked(request.from) {
|
|
|
|
let sender = request.from;
|
2016-09-01 12:00:00 +02:00
|
|
|
return sign_and_dispatch(&*client, &*miner, request, &*accounts, sender).map(DispatchResult::Value);
|
2016-06-22 21:32:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 12:44:49 +02:00
|
|
|
let signer = take_weak!(self.signer);
|
2016-08-03 10:36:54 +02:00
|
|
|
let request = fill_optional_fields(request, &*client, &*miner);
|
2016-09-21 12:44:49 +02:00
|
|
|
signer.add_request(ConfirmationPayload::Transaction(request))
|
2016-09-01 12:00:00 +02:00
|
|
|
.map(DispatchResult::Promise)
|
|
|
|
.map_err(|_| errors::request_rejected_limit())
|
2016-07-20 12:37:49 +02:00
|
|
|
})
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, M> EthSigning for EthSigningQueueClient<C, M>
|
|
|
|
where C: MiningBlockChainClient + 'static, M: MinerService + 'static
|
|
|
|
{
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
fn sign(&self, params: Params, ready: Ready) {
|
|
|
|
let res = self.active().and_then(|_| self.dispatch_sign(params));
|
|
|
|
match res {
|
|
|
|
Ok(DispatchResult::Promise(promise)) => {
|
|
|
|
promise.wait_for_result(move |result| {
|
|
|
|
ready.ready(result.unwrap_or_else(|| Err(errors::request_rejected())))
|
|
|
|
})
|
|
|
|
},
|
|
|
|
Ok(DispatchResult::Value(v)) => ready.ready(Ok(v)),
|
|
|
|
Err(e) => ready.ready(Err(e)),
|
|
|
|
}
|
2016-08-03 10:36:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn post_sign(&self, params: Params) -> Result<Value, Error> {
|
2016-07-19 09:19:58 +02:00
|
|
|
try!(self.active());
|
2016-09-01 12:00:00 +02:00
|
|
|
self.dispatch_sign(params).map(|result| match result {
|
|
|
|
DispatchResult::Value(v) => v,
|
|
|
|
DispatchResult::Promise(promise) => {
|
|
|
|
let id = promise.id();
|
|
|
|
self.pending.lock().insert(id, promise);
|
|
|
|
to_value(&RpcU256::from(id))
|
|
|
|
},
|
2016-08-03 10:36:54 +02:00
|
|
|
})
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
fn send_transaction(&self, params: Params, ready: Ready) {
|
|
|
|
let res = self.active().and_then(|_| self.dispatch_transaction(params));
|
|
|
|
match res {
|
|
|
|
Ok(DispatchResult::Promise(promise)) => {
|
|
|
|
promise.wait_for_result(move |result| {
|
|
|
|
ready.ready(result.unwrap_or_else(|| Err(errors::request_rejected())))
|
|
|
|
})
|
|
|
|
},
|
|
|
|
Ok(DispatchResult::Value(v)) => ready.ready(Ok(v)),
|
|
|
|
Err(e) => ready.ready(Err(e)),
|
|
|
|
}
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn post_transaction(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
try!(self.active());
|
2016-09-01 12:00:00 +02:00
|
|
|
self.dispatch_transaction(params).map(|result| match result {
|
|
|
|
DispatchResult::Value(v) => v,
|
|
|
|
DispatchResult::Promise(promise) => {
|
|
|
|
let id = promise.id();
|
|
|
|
self.pending.lock().insert(id, promise);
|
|
|
|
to_value(&RpcU256::from(id))
|
|
|
|
},
|
2016-07-19 09:19:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-04 16:42:29 +02:00
|
|
|
fn check_request(&self, params: Params) -> Result<Value, Error> {
|
2016-07-19 09:19:58 +02:00
|
|
|
try!(self.active());
|
|
|
|
let mut pending = self.pending.lock();
|
|
|
|
from_params::<(RpcU256, )>(params).and_then(|(id, )| {
|
|
|
|
let id: U256 = id.into();
|
|
|
|
let res = match pending.get(&id) {
|
|
|
|
Some(ref promise) => match promise.result() {
|
|
|
|
ConfirmationResult::Waiting => { return Ok(Value::Null); }
|
2016-08-08 17:25:15 +02:00
|
|
|
ConfirmationResult::Rejected => Err(errors::request_rejected()),
|
2016-07-19 09:19:58 +02:00
|
|
|
ConfirmationResult::Confirmed(rpc_response) => rpc_response,
|
|
|
|
},
|
2016-08-08 17:25:15 +02:00
|
|
|
_ => { return Err(errors::request_not_found()); }
|
2016-07-19 09:19:58 +02:00
|
|
|
};
|
|
|
|
pending.remove(&id);
|
|
|
|
res
|
2016-06-01 19:37:34 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of functions that require signing when no trusted signer is used.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub struct EthSigningUnsafeClient<C, M> where
|
2016-06-01 19:57:34 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-06-01 19:37:34 +02:00
|
|
|
M: MinerService {
|
|
|
|
client: Weak<C>,
|
2016-06-20 00:10:34 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2016-06-01 19:37:34 +02:00
|
|
|
miner: Weak<M>,
|
|
|
|
}
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
impl<C, M> EthSigningUnsafeClient<C, M> where
|
2016-06-01 19:57:34 +02:00
|
|
|
C: MiningBlockChainClient,
|
2016-06-01 19:37:34 +02:00
|
|
|
M: MinerService {
|
|
|
|
|
|
|
|
/// Creates new EthClient.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub fn new(client: &Arc<C>, accounts: &Arc<AccountProvider>, miner: &Arc<M>)
|
2016-06-01 19:37:34 +02:00
|
|
|
-> Self {
|
|
|
|
EthSigningUnsafeClient {
|
|
|
|
client: Arc::downgrade(client),
|
|
|
|
miner: Arc::downgrade(miner),
|
|
|
|
accounts: Arc::downgrade(accounts),
|
|
|
|
}
|
|
|
|
}
|
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-06-20 00:10:34 +02:00
|
|
|
impl<C, M> EthSigning for EthSigningUnsafeClient<C, M> where
|
2016-06-01 19:57:34 +02:00
|
|
|
C: MiningBlockChainClient + 'static,
|
2016-06-01 19:37:34 +02:00
|
|
|
M: MinerService + 'static {
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
fn sign(&self, params: Params, ready: Ready) {
|
|
|
|
ready.ready(self.active()
|
|
|
|
.and_then(|_| from_params::<(RpcH160, RpcH256)>(params))
|
|
|
|
.and_then(|(address, msg)| {
|
|
|
|
let address: Address = address.into();
|
|
|
|
let msg: H256 = msg.into();
|
|
|
|
Ok(to_value(&take_weak!(self.accounts).sign(address, msg).ok().map_or_else(RpcH520::default, Into::into)))
|
|
|
|
}))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:00:00 +02:00
|
|
|
fn send_transaction(&self, params: Params, ready: Ready) {
|
|
|
|
ready.ready(self.active()
|
|
|
|
.and_then(|_| from_params::<(TransactionRequest, )>(params))
|
2016-06-01 19:37:34 +02:00
|
|
|
.and_then(|(request, )| {
|
2016-07-06 11:23:29 +02:00
|
|
|
let request: TRequest = request.into();
|
2016-06-20 00:10:34 +02:00
|
|
|
let sender = request.from;
|
2016-07-20 12:37:49 +02:00
|
|
|
sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), request, &*take_weak!(self.accounts), sender)
|
2016-09-01 12:00:00 +02:00
|
|
|
}))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-07-19 09:19:58 +02:00
|
|
|
|
2016-08-03 10:36:54 +02:00
|
|
|
fn post_sign(&self, _: Params) -> Result<Value, Error> {
|
|
|
|
// We don't support this in non-signer mode.
|
2016-08-08 17:25:15 +02:00
|
|
|
Err(errors::signer_disabled())
|
2016-08-03 10:36:54 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 09:19:58 +02:00
|
|
|
fn post_transaction(&self, _: Params) -> Result<Value, Error> {
|
|
|
|
// We don't support this in non-signer mode.
|
2016-08-08 17:25:15 +02:00
|
|
|
Err(errors::signer_disabled())
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 16:42:29 +02:00
|
|
|
fn check_request(&self, _: Params) -> Result<Value, Error> {
|
2016-07-19 09:19:58 +02:00
|
|
|
// We don't support this in non-signer mode.
|
2016-08-08 17:25:15 +02:00
|
|
|
Err(errors::signer_disabled())
|
2016-07-19 09:19:58 +02:00
|
|
|
}
|
2016-06-15 00:17:23 +02:00
|
|
|
}
|