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-06-01 19:37:34 +02:00
|
|
|
use util::numbers::*;
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-06-01 19:37:34 +02:00
|
|
|
use v1::helpers::{SigningQueue, ConfirmationsQueue};
|
|
|
|
use v1::traits::EthSigning;
|
2016-06-18 14:55:46 +02:00
|
|
|
use v1::types::{TransactionRequest, Bytes};
|
2016-06-22 15:55:07 +02:00
|
|
|
use v1::impls::{default_gas_price, sign_and_dispatch};
|
|
|
|
|
|
|
|
fn fill_optional_fields<C, M>(request: &mut TransactionRequest, client: &C, miner: &M)
|
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
if request.gas.is_none() {
|
|
|
|
request.gas = Some(miner.sensible_gas_limit());
|
|
|
|
}
|
|
|
|
if request.gas_price.is_none() {
|
|
|
|
request.gas_price = Some(default_gas_price(client, miner));
|
|
|
|
}
|
|
|
|
if request.data.is_none() {
|
|
|
|
request.data = Some(Bytes::new(Vec::new()));
|
|
|
|
}
|
|
|
|
}
|
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-06-01 19:37:34 +02:00
|
|
|
queue: Weak<ConfirmationsQueue>,
|
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-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
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-06-22 21:32:26 +02:00
|
|
|
pub fn new(queue: &Arc<ConfirmationsQueue>, client: &Arc<C>, miner: &Arc<M>, accounts: &Arc<AccountProvider>) -> Self {
|
2016-06-01 19:37:34 +02:00
|
|
|
EthSigningQueueClient {
|
|
|
|
queue: Arc::downgrade(queue),
|
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-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 15:55:07 +02:00
|
|
|
impl<C, M> EthSigning for EthSigningQueueClient<C, M>
|
|
|
|
where C: MiningBlockChainClient + 'static, M: MinerService + 'static
|
|
|
|
{
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
fn sign(&self, _params: Params) -> Result<Value, Error> {
|
2016-06-15 00:17:23 +02:00
|
|
|
warn!("Invoking eth_sign is not yet supported with signer enabled.");
|
2016-06-01 19:37:34 +02:00
|
|
|
// TODO [ToDr] Implement sign when rest of the signing queue is ready.
|
|
|
|
rpc_unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_transaction(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(TransactionRequest, )>(params)
|
2016-06-22 15:55:07 +02:00
|
|
|
.and_then(|(mut request, )| {
|
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;
|
|
|
|
return match sign_and_dispatch(&*client, &*miner, request, &*accounts, sender) {
|
|
|
|
Ok(hash) => to_value(&hash),
|
|
|
|
_ => to_value(&H256::zero()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let queue = take_weak!(self.queue);
|
2016-06-22 15:55:07 +02:00
|
|
|
fill_optional_fields(&mut request, &*client, &*miner);
|
2016-06-02 17:05:13 +02:00
|
|
|
let id = queue.add_request(request);
|
|
|
|
let result = id.wait_with_timeout();
|
2016-06-15 00:17:23 +02:00
|
|
|
result.unwrap_or_else(|| to_value(&H256::new()))
|
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-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 {
|
|
|
|
|
|
|
|
fn sign(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(Address, H256)>(params).and_then(|(addr, msg)| {
|
2016-06-20 00:10:34 +02:00
|
|
|
to_value(&take_weak!(self.accounts).sign(addr, msg).unwrap_or(H520::zero()))
|
2016-06-01 19:37:34 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_transaction(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(TransactionRequest, )>(params)
|
|
|
|
.and_then(|(request, )| {
|
2016-06-20 00:10:34 +02:00
|
|
|
let sender = request.from;
|
|
|
|
match sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), request, &*take_weak!(self.accounts), sender) {
|
|
|
|
Ok(hash) => to_value(&hash),
|
|
|
|
_ => to_value(&H256::zero()),
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-06-15 00:17:23 +02:00
|
|
|
}
|