2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-08-08 17:25:15 +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/>.
|
|
|
|
|
2017-02-08 20:44:40 +01:00
|
|
|
//! Utilities and helpers for transaction dispatch.
|
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
use std::fmt::Debug;
|
2016-11-30 17:05:31 +01:00
|
|
|
use std::ops::Deref;
|
2017-05-28 14:40:36 +02:00
|
|
|
use std::sync::Arc;
|
2017-02-08 15:36:53 +01:00
|
|
|
|
2017-05-23 12:39:25 +02:00
|
|
|
use futures::{future, Future, BoxFuture};
|
2017-02-17 16:18:31 +01:00
|
|
|
use light::cache::Cache as LightDataCache;
|
2017-02-09 20:22:31 +01:00
|
|
|
use light::client::LightChainClient;
|
|
|
|
use light::on_demand::{request, OnDemand};
|
|
|
|
use light::TransactionQueue as LightTransactionQueue;
|
2017-03-20 19:14:29 +01:00
|
|
|
use rlp;
|
2017-08-31 11:35:41 +02:00
|
|
|
use hash::keccak;
|
2017-09-02 20:09:13 +02:00
|
|
|
use util::{Address, H520, H256, U256, Bytes};
|
|
|
|
use parking_lot::{Mutex, RwLock};
|
2017-02-17 21:38:43 +01:00
|
|
|
use stats::Corpus;
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
use ethkey::Signature;
|
2017-02-09 20:41:42 +01:00
|
|
|
use ethsync::LightSync;
|
2017-02-17 17:08:46 +01:00
|
|
|
use ethcore::ids::BlockId;
|
2016-08-08 17:25:15 +02:00
|
|
|
use ethcore::miner::MinerService;
|
|
|
|
use ethcore::client::MiningBlockChainClient;
|
2016-12-15 18:19:19 +01:00
|
|
|
use ethcore::transaction::{Action, SignedTransaction, PendingTransaction, Transaction};
|
2016-08-08 17:25:15 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2017-05-05 15:57:29 +02:00
|
|
|
use crypto::DEFAULT_MAC;
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
use jsonrpc_core::Error;
|
|
|
|
use v1::helpers::{errors, TransactionRequest, FilledTransactionRequest, ConfirmationPayload};
|
|
|
|
use v1::types::{
|
|
|
|
H256 as RpcH256, H520 as RpcH520, Bytes as RpcBytes,
|
2016-11-18 11:03:29 +01:00
|
|
|
RichRawTransaction as RpcRichRawTransaction,
|
2016-11-09 13:13:35 +01:00
|
|
|
ConfirmationPayload as RpcConfirmationPayload,
|
|
|
|
ConfirmationResponse,
|
|
|
|
SignRequest as RpcSignRequest,
|
|
|
|
DecryptRequest as RpcDecryptRequest,
|
|
|
|
};
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
/// Has the capability to dispatch, sign, and decrypt.
|
|
|
|
///
|
|
|
|
/// Requires a clone implementation, with the implication that it be cheap;
|
|
|
|
/// usually just bumping a reference count or two.
|
|
|
|
pub trait Dispatcher: Send + Sync + Clone {
|
|
|
|
// TODO: when ATC exist, use zero-cost
|
|
|
|
// type Out<T>: IntoFuture<T, Error>
|
|
|
|
|
|
|
|
/// Fill optional fields of a transaction request, fetching gas price but not nonce.
|
2017-05-02 11:39:48 +02:00
|
|
|
fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address, force_nonce: bool)
|
2017-02-08 15:36:53 +01:00
|
|
|
-> BoxFuture<FilledTransactionRequest, Error>;
|
|
|
|
|
|
|
|
/// Sign the given transaction request without dispatching, fetching appropriate nonce.
|
2017-02-09 21:12:28 +01:00
|
|
|
fn sign(&self, accounts: Arc<AccountProvider>, filled: FilledTransactionRequest, password: SignWith)
|
2017-02-08 15:36:53 +01:00
|
|
|
-> BoxFuture<WithToken<SignedTransaction>, Error>;
|
|
|
|
|
|
|
|
/// "Dispatch" a local transaction.
|
|
|
|
fn dispatch_transaction(&self, signed_transaction: PendingTransaction) -> Result<H256, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A dispatcher which uses references to a client and miner in order to sign
|
|
|
|
/// requests locally.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FullDispatcher<C, M> {
|
2017-05-28 14:40:36 +02:00
|
|
|
client: Arc<C>,
|
|
|
|
miner: Arc<M>,
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, M> FullDispatcher<C, M> {
|
2017-05-28 14:40:36 +02:00
|
|
|
/// Create a `FullDispatcher` from Arc references to a client and miner.
|
|
|
|
pub fn new(client: Arc<C>, miner: Arc<M>) -> Self {
|
2017-02-08 15:36:53 +01:00
|
|
|
FullDispatcher {
|
2017-05-28 14:40:36 +02:00
|
|
|
client,
|
|
|
|
miner,
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, M> Clone for FullDispatcher<C, M> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
FullDispatcher {
|
|
|
|
client: self.client.clone(),
|
|
|
|
miner: self.miner.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:39:48 +02:00
|
|
|
impl<C: MiningBlockChainClient, M: MinerService> FullDispatcher<C, M> {
|
|
|
|
fn fill_nonce(nonce: Option<U256>, from: &Address, miner: &M, client: &C) -> U256 {
|
|
|
|
nonce
|
|
|
|
.or_else(|| miner.last_nonce(from).map(|nonce| nonce + U256::one()))
|
|
|
|
.unwrap_or_else(|| client.latest_nonce(from))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C, M> {
|
2017-05-02 11:39:48 +02:00
|
|
|
fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address, force_nonce: bool)
|
2017-02-08 15:36:53 +01:00
|
|
|
-> BoxFuture<FilledTransactionRequest, Error>
|
|
|
|
{
|
2017-05-28 14:40:36 +02:00
|
|
|
let (client, miner) = (self.client.clone(), self.miner.clone());
|
2017-02-09 20:38:43 +01:00
|
|
|
let request = request;
|
2017-05-02 11:39:48 +02:00
|
|
|
let from = request.from.unwrap_or(default_sender);
|
|
|
|
let nonce = match force_nonce {
|
|
|
|
false => request.nonce,
|
|
|
|
true => Some(Self::fill_nonce(request.nonce, &from, &miner, &client)),
|
|
|
|
};
|
2017-02-09 20:38:43 +01:00
|
|
|
future::ok(FilledTransactionRequest {
|
2017-05-02 11:39:48 +02:00
|
|
|
from: from,
|
2017-02-09 20:38:43 +01:00
|
|
|
used_default_from: request.from.is_none(),
|
|
|
|
to: request.to,
|
2017-05-02 11:39:48 +02:00
|
|
|
nonce: nonce,
|
2017-02-09 20:38:43 +01:00
|
|
|
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),
|
|
|
|
condition: request.condition,
|
|
|
|
}).boxed()
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 21:12:28 +01:00
|
|
|
fn sign(&self, accounts: Arc<AccountProvider>, filled: FilledTransactionRequest, password: SignWith)
|
2017-02-08 15:36:53 +01:00
|
|
|
-> BoxFuture<WithToken<SignedTransaction>, Error>
|
|
|
|
{
|
2017-05-28 14:40:36 +02:00
|
|
|
let (client, miner) = (self.client.clone(), self.miner.clone());
|
2017-08-21 13:46:58 +02:00
|
|
|
let chain_id = client.signing_chain_id();
|
2017-02-09 20:38:43 +01:00
|
|
|
let address = filled.from;
|
2017-02-10 14:31:17 +01:00
|
|
|
future::done({
|
2017-02-09 20:38:43 +01:00
|
|
|
let t = Transaction {
|
2017-05-02 11:39:48 +02:00
|
|
|
nonce: Self::fill_nonce(filled.nonce, &filled.from, &miner, &client),
|
2017-02-09 20:38:43 +01:00
|
|
|
action: filled.to.map_or(Action::Create, Action::Call),
|
|
|
|
gas: filled.gas,
|
|
|
|
gas_price: filled.gas_price,
|
|
|
|
value: filled.value,
|
|
|
|
data: filled.data,
|
2017-02-08 15:36:53 +01:00
|
|
|
};
|
|
|
|
|
2017-02-10 02:44:02 +01:00
|
|
|
if accounts.is_hardware_address(address) {
|
2017-08-21 13:46:58 +02:00
|
|
|
hardware_signature(&*accounts, address, t, chain_id).map(WithToken::No)
|
2017-02-10 02:44:02 +01:00
|
|
|
} else {
|
2017-08-21 13:46:58 +02:00
|
|
|
let hash = t.hash(chain_id);
|
2017-02-10 14:31:17 +01:00
|
|
|
let signature = try_bf!(signature(&*accounts, address, hash, password));
|
|
|
|
Ok(signature.map(|sig| {
|
2017-08-21 13:46:58 +02:00
|
|
|
SignedTransaction::new(t.with_signature(sig, chain_id))
|
2017-02-10 02:44:02 +01:00
|
|
|
.expect("Transaction was signed by AccountsProvider; it never produces invalid signatures; qed")
|
2017-02-10 14:31:17 +01:00
|
|
|
}))
|
2017-02-10 02:44:02 +01:00
|
|
|
}
|
2017-02-09 20:38:43 +01:00
|
|
|
}).boxed()
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatch_transaction(&self, signed_transaction: PendingTransaction) -> Result<H256, Error> {
|
|
|
|
let hash = signed_transaction.transaction.hash();
|
|
|
|
|
2017-05-28 14:40:36 +02:00
|
|
|
self.miner.import_own_transaction(&*self.client, signed_transaction)
|
2017-07-04 17:01:06 +02:00
|
|
|
.map_err(errors::transaction)
|
2017-02-08 15:36:53 +01:00
|
|
|
.map(|_| hash)
|
2017-02-09 20:22:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 15:05:33 +01:00
|
|
|
/// Get a recent gas price corpus.
|
|
|
|
// TODO: this could be `impl Trait`.
|
|
|
|
pub fn fetch_gas_price_corpus(
|
|
|
|
sync: Arc<LightSync>,
|
|
|
|
client: Arc<LightChainClient>,
|
|
|
|
on_demand: Arc<OnDemand>,
|
|
|
|
cache: Arc<Mutex<LightDataCache>>,
|
|
|
|
) -> BoxFuture<Corpus<U256>, Error> {
|
|
|
|
const GAS_PRICE_SAMPLE_SIZE: usize = 100;
|
|
|
|
|
2017-07-27 13:50:12 +02:00
|
|
|
if let Some(cached) = { cache.lock().gas_price_corpus() } {
|
2017-02-26 15:05:33 +01:00
|
|
|
return future::ok(cached).boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
let cache = cache.clone();
|
|
|
|
let eventual_corpus = sync.with_context(|ctx| {
|
|
|
|
// get some recent headers with gas used,
|
|
|
|
// and request each of the blocks from the network.
|
2017-05-23 12:39:25 +02:00
|
|
|
let block_requests = client.ancestry_iter(BlockId::Latest)
|
2017-02-26 15:05:33 +01:00
|
|
|
.filter(|hdr| hdr.gas_used() != U256::default())
|
|
|
|
.take(GAS_PRICE_SAMPLE_SIZE)
|
2017-05-23 12:39:25 +02:00
|
|
|
.map(|hdr| request::Body(hdr.into()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// when the blocks come in, collect gas prices into a vector
|
|
|
|
on_demand.request(ctx, block_requests)
|
|
|
|
.expect("no back-references; therefore all back-references are valid; qed")
|
|
|
|
.map(|bodies| {
|
|
|
|
bodies.into_iter().fold(Vec::new(), |mut v, block| {
|
|
|
|
for t in block.transaction_views().iter() {
|
|
|
|
v.push(t.gas_price())
|
|
|
|
}
|
|
|
|
|
|
|
|
v
|
|
|
|
})
|
2017-02-26 15:05:33 +01:00
|
|
|
})
|
2017-05-23 12:39:25 +02:00
|
|
|
.map(move |prices| {
|
2017-02-26 15:05:33 +01:00
|
|
|
// produce a corpus from the vector, cache it, and return
|
|
|
|
// the median as the intended gas price.
|
2017-05-23 12:39:25 +02:00
|
|
|
let corpus: ::stats::Corpus<_> = prices.into();
|
2017-02-26 15:05:33 +01:00
|
|
|
cache.lock().set_gas_price_corpus(corpus.clone());
|
|
|
|
corpus
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
match eventual_corpus {
|
|
|
|
Some(corp) => corp.map_err(|_| errors::no_light_peers()).boxed(),
|
|
|
|
None => future::err(errors::network_disabled()).boxed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 18:23:22 +02:00
|
|
|
/// Returns a eth_sign-compatible hash of data to sign.
|
|
|
|
/// The data is prepended with special message to prevent
|
|
|
|
/// chosen-plaintext attacks.
|
|
|
|
pub fn eth_data_hash(mut data: Bytes) -> H256 {
|
|
|
|
let mut message_data =
|
|
|
|
format!("\x19Ethereum Signed Message:\n{}", data.len())
|
|
|
|
.into_bytes();
|
|
|
|
message_data.append(&mut data);
|
2017-08-31 11:35:41 +02:00
|
|
|
keccak(message_data)
|
2017-04-27 18:23:22 +02:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:22:31 +01:00
|
|
|
/// Dispatcher for light clients -- fetches default gas price, next nonce, etc. from network.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct LightDispatcher {
|
2017-02-17 21:38:43 +01:00
|
|
|
/// Sync service.
|
|
|
|
pub sync: Arc<LightSync>,
|
|
|
|
/// Header chain client.
|
|
|
|
pub client: Arc<LightChainClient>,
|
|
|
|
/// On-demand request service.
|
|
|
|
pub on_demand: Arc<OnDemand>,
|
|
|
|
/// Data cache.
|
|
|
|
pub cache: Arc<Mutex<LightDataCache>>,
|
|
|
|
/// Transaction queue.
|
|
|
|
pub transaction_queue: Arc<RwLock<LightTransactionQueue>>,
|
2017-02-09 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LightDispatcher {
|
|
|
|
/// Create a new `LightDispatcher` from its requisite parts.
|
|
|
|
///
|
|
|
|
/// For correct operation, the OnDemand service is assumed to be registered as a network handler,
|
|
|
|
pub fn new(
|
|
|
|
sync: Arc<LightSync>,
|
|
|
|
client: Arc<LightChainClient>,
|
|
|
|
on_demand: Arc<OnDemand>,
|
2017-02-17 16:18:31 +01:00
|
|
|
cache: Arc<Mutex<LightDataCache>>,
|
2017-02-09 20:22:31 +01:00
|
|
|
transaction_queue: Arc<RwLock<LightTransactionQueue>>,
|
|
|
|
) -> Self {
|
|
|
|
LightDispatcher {
|
|
|
|
sync: sync,
|
|
|
|
client: client,
|
|
|
|
on_demand: on_demand,
|
2017-02-17 16:18:31 +01:00
|
|
|
cache: cache,
|
2017-02-09 20:41:42 +01:00
|
|
|
transaction_queue: transaction_queue,
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
2017-02-09 20:22:31 +01:00
|
|
|
}
|
2017-02-17 21:38:43 +01:00
|
|
|
|
|
|
|
/// Get a recent gas price corpus.
|
|
|
|
// TODO: this could be `impl Trait`.
|
|
|
|
pub fn gas_price_corpus(&self) -> BoxFuture<Corpus<U256>, Error> {
|
2017-02-26 15:05:33 +01:00
|
|
|
fetch_gas_price_corpus(
|
|
|
|
self.sync.clone(),
|
|
|
|
self.client.clone(),
|
|
|
|
self.on_demand.clone(),
|
|
|
|
self.cache.clone(),
|
|
|
|
)
|
2017-02-17 21:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an account's next nonce.
|
|
|
|
pub fn next_nonce(&self, addr: Address) -> BoxFuture<U256, Error> {
|
|
|
|
// fast path where we don't go to network; nonce provided or can be gotten from queue.
|
|
|
|
let maybe_nonce = self.transaction_queue.read().next_nonce(&addr);
|
|
|
|
if let Some(nonce) = maybe_nonce {
|
|
|
|
return future::ok(nonce).boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
let best_header = self.client.best_block_header();
|
2017-06-28 09:10:57 +02:00
|
|
|
let account_start_nonce = self.client.engine().account_start_nonce(best_header.number());
|
2017-05-23 12:39:25 +02:00
|
|
|
let nonce_future = self.sync.with_context(|ctx| self.on_demand.request(ctx, request::Account {
|
|
|
|
header: best_header.into(),
|
2017-02-17 21:38:43 +01:00
|
|
|
address: addr,
|
2017-05-23 12:39:25 +02:00
|
|
|
}).expect("no back-references; therefore all back-references valid; qed"));
|
2017-02-17 21:38:43 +01:00
|
|
|
|
|
|
|
match nonce_future {
|
2017-03-17 00:14:29 +01:00
|
|
|
Some(x) =>
|
2017-04-06 17:59:55 +02:00
|
|
|
x.map(move |acc| acc.map_or(account_start_nonce, |acc| acc.nonce))
|
2017-03-17 00:14:29 +01:00
|
|
|
.map_err(|_| errors::no_light_peers())
|
|
|
|
.boxed(),
|
2017-02-17 21:38:43 +01:00
|
|
|
None => future::err(errors::network_disabled()).boxed()
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 15:36:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 21:12:28 +01:00
|
|
|
impl Dispatcher for LightDispatcher {
|
2017-05-02 11:39:48 +02:00
|
|
|
fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address, force_nonce: bool)
|
2017-02-09 21:12:28 +01:00
|
|
|
-> BoxFuture<FilledTransactionRequest, Error>
|
|
|
|
{
|
2017-02-17 17:08:46 +01:00
|
|
|
const DEFAULT_GAS_PRICE: U256 = U256([0, 0, 0, 21_000_000]);
|
|
|
|
|
2017-02-13 16:49:01 +01:00
|
|
|
let gas_limit = self.client.best_block_header().gas_limit();
|
2017-02-17 17:08:46 +01:00
|
|
|
let request_gas_price = request.gas_price.clone();
|
2017-05-02 11:39:48 +02:00
|
|
|
let request_nonce = request.nonce.clone();
|
|
|
|
let from = request.from.unwrap_or(default_sender);
|
2017-02-09 21:12:28 +01:00
|
|
|
|
2017-02-17 17:08:46 +01:00
|
|
|
let with_gas_price = move |gas_price| {
|
|
|
|
let request = request;
|
|
|
|
FilledTransactionRequest {
|
2017-05-02 11:39:48 +02:00
|
|
|
from: from.clone(),
|
2017-02-17 17:08:46 +01:00
|
|
|
used_default_from: request.from.is_none(),
|
|
|
|
to: request.to,
|
|
|
|
nonce: request.nonce,
|
|
|
|
gas_price: gas_price,
|
|
|
|
gas: request.gas.unwrap_or_else(|| gas_limit / 3.into()),
|
|
|
|
value: request.value.unwrap_or_else(|| 0.into()),
|
|
|
|
data: request.data.unwrap_or_else(Vec::new),
|
|
|
|
condition: request.condition,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-17 21:38:43 +01:00
|
|
|
// fast path for known gas price.
|
2017-05-02 11:39:48 +02:00
|
|
|
let gas_price = match request_gas_price {
|
2017-02-17 17:08:46 +01:00
|
|
|
Some(gas_price) => future::ok(with_gas_price(gas_price)).boxed(),
|
2017-02-26 15:05:33 +01:00
|
|
|
None => fetch_gas_price_corpus(
|
|
|
|
self.sync.clone(),
|
|
|
|
self.client.clone(),
|
|
|
|
self.on_demand.clone(),
|
|
|
|
self.cache.clone()
|
|
|
|
).and_then(|corp| match corp.median() {
|
2017-02-17 21:38:43 +01:00
|
|
|
Some(median) => future::ok(*median),
|
|
|
|
None => future::ok(DEFAULT_GAS_PRICE), // fall back to default on error.
|
|
|
|
}).map(with_gas_price).boxed()
|
2017-05-02 11:39:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match (request_nonce, force_nonce) {
|
|
|
|
(_, false) | (Some(_), true) => gas_price,
|
|
|
|
(None, true) => {
|
|
|
|
let next_nonce = self.next_nonce(from);
|
|
|
|
gas_price.and_then(move |mut filled| next_nonce
|
|
|
|
.map_err(|_| errors::no_light_peers())
|
|
|
|
.map(move |nonce| {
|
|
|
|
filled.nonce = Some(nonce);
|
|
|
|
filled
|
|
|
|
})
|
|
|
|
).boxed()
|
|
|
|
},
|
2017-02-17 17:08:46 +01:00
|
|
|
}
|
2017-02-09 21:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sign(&self, accounts: Arc<AccountProvider>, filled: FilledTransactionRequest, password: SignWith)
|
|
|
|
-> BoxFuture<WithToken<SignedTransaction>, Error>
|
|
|
|
{
|
2017-08-21 13:46:58 +02:00
|
|
|
let chain_id = self.client.signing_chain_id();
|
2017-02-09 21:12:28 +01:00
|
|
|
let address = filled.from;
|
|
|
|
|
|
|
|
let with_nonce = move |filled: FilledTransactionRequest, nonce| {
|
|
|
|
let t = Transaction {
|
|
|
|
nonce: nonce,
|
|
|
|
action: filled.to.map_or(Action::Create, Action::Call),
|
|
|
|
gas: filled.gas,
|
|
|
|
gas_price: filled.gas_price,
|
|
|
|
value: filled.value,
|
|
|
|
data: filled.data,
|
|
|
|
};
|
2017-02-10 14:31:17 +01:00
|
|
|
|
|
|
|
if accounts.is_hardware_address(address) {
|
2017-08-21 13:46:58 +02:00
|
|
|
return hardware_signature(&*accounts, address, t, chain_id).map(WithToken::No)
|
2017-02-10 14:31:17 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
let hash = t.hash(chain_id);
|
2017-02-10 14:31:17 +01:00
|
|
|
let signature = signature(&*accounts, address, hash, password)?;
|
2017-02-09 21:12:28 +01:00
|
|
|
|
|
|
|
Ok(signature.map(|sig| {
|
2017-08-21 13:46:58 +02:00
|
|
|
SignedTransaction::new(t.with_signature(sig, chain_id))
|
2017-02-09 21:12:28 +01:00
|
|
|
.expect("Transaction was signed by AccountsProvider; it never produces invalid signatures; qed")
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
|
2017-02-17 21:38:43 +01:00
|
|
|
// fast path for pre-filled nonce.
|
|
|
|
if let Some(nonce) = filled.nonce {
|
2017-02-09 21:12:28 +01:00
|
|
|
return future::done(with_nonce(filled, nonce)).boxed()
|
|
|
|
}
|
|
|
|
|
2017-02-17 21:38:43 +01:00
|
|
|
self.next_nonce(address)
|
2017-02-09 21:12:28 +01:00
|
|
|
.map_err(|_| errors::no_light_peers())
|
2017-02-17 21:38:43 +01:00
|
|
|
.and_then(move |nonce| with_nonce(filled, nonce))
|
2017-02-09 21:12:28 +01:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatch_transaction(&self, signed_transaction: PendingTransaction) -> Result<H256, Error> {
|
|
|
|
let hash = signed_transaction.transaction.hash();
|
|
|
|
|
|
|
|
self.transaction_queue.write().import(signed_transaction)
|
|
|
|
.map_err(Into::into)
|
2017-07-04 17:01:06 +02:00
|
|
|
.map_err(errors::transaction)
|
2017-02-09 21:12:28 +01:00
|
|
|
.map(|_| hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Single-use account token.
|
|
|
|
pub type AccountToken = String;
|
2016-11-30 16:11:41 +01:00
|
|
|
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Values used to unlock accounts for signing.
|
2016-11-30 16:11:41 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum SignWith {
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Nothing -- implies the account is already unlocked.
|
2016-11-30 16:11:41 +01:00
|
|
|
Nothing,
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Unlock with password.
|
2016-11-30 16:11:41 +01:00
|
|
|
Password(String),
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Unlock with single-use token.
|
2016-11-30 16:11:41 +01:00
|
|
|
Token(AccountToken),
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:44:40 +01:00
|
|
|
/// A value, potentially accompanied by a signing token.
|
2016-11-30 16:11:41 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum WithToken<T: Debug> {
|
2017-02-08 20:44:40 +01:00
|
|
|
/// No token.
|
2016-11-30 16:11:41 +01:00
|
|
|
No(T),
|
2017-02-08 20:44:40 +01:00
|
|
|
/// With token.
|
2016-11-30 16:11:41 +01:00
|
|
|
Yes(T, AccountToken),
|
|
|
|
}
|
|
|
|
|
2016-11-30 17:05:31 +01:00
|
|
|
impl<T: Debug> Deref for WithToken<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
match *self {
|
|
|
|
WithToken::No(ref v) => v,
|
|
|
|
WithToken::Yes(ref v, _) => v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
impl<T: Debug> WithToken<T> {
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Map the value with the given closure, preserving the token.
|
2016-11-30 16:11:41 +01:00
|
|
|
pub fn map<S, F>(self, f: F) -> WithToken<S> where
|
|
|
|
S: Debug,
|
|
|
|
F: FnOnce(T) -> S,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
WithToken::No(v) => WithToken::No(f(v)),
|
|
|
|
WithToken::Yes(v, token) => WithToken::Yes(f(v), token),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Convert into inner value, ignoring possible token.
|
2016-11-30 16:11:41 +01:00
|
|
|
pub fn into_value(self) -> T {
|
|
|
|
match self {
|
|
|
|
WithToken::No(v) => v,
|
2016-11-30 17:05:31 +01:00
|
|
|
WithToken::Yes(v, _) => v,
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-08 15:36:53 +01:00
|
|
|
|
|
|
|
/// Convert the `WithToken` into a tuple.
|
|
|
|
pub fn into_tuple(self) -> (T, Option<AccountToken>) {
|
|
|
|
match self {
|
|
|
|
WithToken::No(v) => (v, None),
|
|
|
|
WithToken::Yes(v, token) => (v, Some(token))
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Debug> From<(T, AccountToken)> for WithToken<T> {
|
|
|
|
fn from(tuple: (T, AccountToken)) -> Self {
|
|
|
|
WithToken::Yes(tuple.0, tuple.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl<T: Debug> From<(T, Option<AccountToken>)> for WithToken<T> {
|
|
|
|
fn from(tuple: (T, Option<AccountToken>)) -> Self {
|
|
|
|
match tuple.1 {
|
|
|
|
Some(token) => WithToken::Yes(tuple.0, token),
|
|
|
|
None => WithToken::No(tuple.0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:44:40 +01:00
|
|
|
/// Execute a confirmation payload.
|
2017-02-08 16:55:06 +01:00
|
|
|
pub fn execute<D: Dispatcher + 'static>(
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher: D,
|
2017-02-09 21:12:28 +01:00
|
|
|
accounts: Arc<AccountProvider>,
|
2017-02-08 15:36:53 +01:00
|
|
|
payload: ConfirmationPayload,
|
|
|
|
pass: SignWith
|
|
|
|
) -> BoxFuture<WithToken<ConfirmationResponse>, Error> {
|
2016-11-09 13:13:35 +01:00
|
|
|
match payload {
|
|
|
|
ConfirmationPayload::SendTransaction(request) => {
|
2017-02-08 16:55:06 +01:00
|
|
|
let condition = request.condition.clone().map(Into::into);
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher.sign(accounts, request, pass)
|
|
|
|
.map(move |v| v.map(move |tx| PendingTransaction::new(tx, condition)))
|
|
|
|
.map(WithToken::into_tuple)
|
|
|
|
.map(|(tx, token)| (tx, token, dispatcher))
|
|
|
|
.and_then(|(tx, tok, dispatcher)| {
|
|
|
|
dispatcher.dispatch_transaction(tx)
|
|
|
|
.map(RpcH256::from)
|
|
|
|
.map(ConfirmationResponse::SendTransaction)
|
|
|
|
.map(move |h| WithToken::from((h, tok)))
|
|
|
|
}).boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
ConfirmationPayload::SignTransaction(request) => {
|
2017-02-08 15:36:53 +01:00
|
|
|
dispatcher.sign(accounts, request, pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
|
|
|
.map(RpcRichRawTransaction::from)
|
|
|
|
.map(ConfirmationResponse::SignTransaction)
|
2017-02-08 15:36:53 +01:00
|
|
|
).boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
2017-04-27 18:23:22 +02:00
|
|
|
ConfirmationPayload::EthSignMessage(address, data) => {
|
2017-08-15 12:11:34 +02:00
|
|
|
if accounts.is_hardware_address(address) {
|
|
|
|
return future::err(errors::unsupported("Signing via hardware wallets is not supported.", None)).boxed();
|
|
|
|
}
|
|
|
|
|
2017-04-27 18:23:22 +02:00
|
|
|
let hash = eth_data_hash(data);
|
|
|
|
let res = signature(&accounts, address, hash, pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
2017-05-11 12:18:20 +02:00
|
|
|
.map(|rsv| H520(rsv.into_electrum()))
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(RpcH520::from)
|
|
|
|
.map(ConfirmationResponse::Signature)
|
2017-02-08 15:36:53 +01:00
|
|
|
);
|
|
|
|
future::done(res).boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
ConfirmationPayload::Decrypt(address, data) => {
|
2017-08-15 12:11:34 +02:00
|
|
|
if accounts.is_hardware_address(address) {
|
|
|
|
return future::err(errors::unsupported("Decrypting via hardware wallets is not supported.", None)).boxed();
|
|
|
|
}
|
|
|
|
|
2017-02-09 21:12:28 +01:00
|
|
|
let res = decrypt(&accounts, address, data, pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
|
|
|
.map(RpcBytes)
|
|
|
|
.map(ConfirmationResponse::Decrypt)
|
2017-02-08 15:36:53 +01:00
|
|
|
);
|
|
|
|
future::done(res).boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
}
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
fn signature(accounts: &AccountProvider, address: Address, hash: H256, password: SignWith) -> Result<WithToken<Signature>, Error> {
|
|
|
|
match password.clone() {
|
|
|
|
SignWith::Nothing => accounts.sign(address, None, hash).map(WithToken::No),
|
|
|
|
SignWith::Password(pass) => accounts.sign(address, Some(pass), hash).map(WithToken::No),
|
|
|
|
SignWith::Token(token) => accounts.sign_with_token(address, token, hash).map(Into::into),
|
|
|
|
}.map_err(|e| match password {
|
2017-07-04 17:01:06 +02:00
|
|
|
SignWith::Nothing => errors::signing(e),
|
|
|
|
_ => errors::password(e),
|
2016-10-15 14:44:08 +02:00
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 14:31:17 +01:00
|
|
|
// obtain a hardware signature from the given account.
|
2017-08-21 13:46:58 +02:00
|
|
|
fn hardware_signature(accounts: &AccountProvider, address: Address, t: Transaction, chain_id: Option<u64>)
|
2017-02-10 14:31:17 +01:00
|
|
|
-> Result<SignedTransaction, Error>
|
|
|
|
{
|
|
|
|
debug_assert!(accounts.is_hardware_address(address));
|
|
|
|
|
|
|
|
let mut stream = rlp::RlpStream::new();
|
2017-08-21 13:46:58 +02:00
|
|
|
t.rlp_append_unsigned_transaction(&mut stream, chain_id);
|
2017-02-10 14:31:17 +01:00
|
|
|
let signature = accounts.sign_with_hardware(address, &stream.as_raw())
|
|
|
|
.map_err(|e| {
|
|
|
|
debug!(target: "miner", "Error signing transaction with hardware wallet: {}", e);
|
|
|
|
errors::account("Error signing transaction with hardware wallet", e)
|
|
|
|
})?;
|
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
SignedTransaction::new(t.with_signature(signature, chain_id))
|
2017-02-10 14:31:17 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
debug!(target: "miner", "Hardware wallet has produced invalid signature: {}", e);
|
|
|
|
errors::account("Invalid signature generated", e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
fn decrypt(accounts: &AccountProvider, address: Address, msg: Bytes, password: SignWith) -> Result<WithToken<Bytes>, Error> {
|
|
|
|
match password.clone() {
|
|
|
|
SignWith::Nothing => accounts.decrypt(address, None, &DEFAULT_MAC, &msg).map(WithToken::No),
|
|
|
|
SignWith::Password(pass) => accounts.decrypt(address, Some(pass), &DEFAULT_MAC, &msg).map(WithToken::No),
|
|
|
|
SignWith::Token(token) => accounts.decrypt_with_token(address, token, &DEFAULT_MAC, &msg).map(Into::into),
|
|
|
|
}.map_err(|e| match password {
|
2017-07-04 17:01:06 +02:00
|
|
|
SignWith::Nothing => errors::signing(e),
|
|
|
|
_ => errors::password(e),
|
2016-11-30 16:11:41 +01:00
|
|
|
})
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 02:44:02 +01:00
|
|
|
/// Extract the default gas price from a client and miner.
|
2017-08-28 14:11:55 +02:00
|
|
|
pub fn default_gas_price<C, M>(client: &C, miner: &M) -> U256 where
|
|
|
|
C: MiningBlockChainClient,
|
|
|
|
M: MinerService,
|
2016-11-09 13:13:35 +01:00
|
|
|
{
|
2017-02-17 16:18:31 +01:00
|
|
|
client.gas_price_corpus(100).median().cloned().unwrap_or_else(|| miner.sensible_gas_price())
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
/// Convert RPC confirmation payload to signer confirmation payload.
|
|
|
|
/// May need to resolve in the future to fetch things like gas price.
|
|
|
|
pub fn from_rpc<D>(payload: RpcConfirmationPayload, default_account: Address, dispatcher: &D) -> BoxFuture<ConfirmationPayload, Error>
|
|
|
|
where D: Dispatcher
|
|
|
|
{
|
2016-11-09 13:13:35 +01:00
|
|
|
match payload {
|
|
|
|
RpcConfirmationPayload::SendTransaction(request) => {
|
2017-05-02 11:39:48 +02:00
|
|
|
dispatcher.fill_optional_fields(request.into(), default_account, false)
|
2017-02-08 15:36:53 +01:00
|
|
|
.map(ConfirmationPayload::SendTransaction)
|
|
|
|
.boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
RpcConfirmationPayload::SignTransaction(request) => {
|
2017-05-02 11:39:48 +02:00
|
|
|
dispatcher.fill_optional_fields(request.into(), default_account, false)
|
2017-02-08 15:36:53 +01:00
|
|
|
.map(ConfirmationPayload::SignTransaction)
|
|
|
|
.boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
RpcConfirmationPayload::Decrypt(RpcDecryptRequest { address, msg }) => {
|
2017-02-08 15:36:53 +01:00
|
|
|
future::ok(ConfirmationPayload::Decrypt(address.into(), msg.into())).boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
2017-04-12 12:15:13 +02:00
|
|
|
RpcConfirmationPayload::EthSignMessage(RpcSignRequest { address, data }) => {
|
|
|
|
future::ok(ConfirmationPayload::EthSignMessage(address.into(), data.into())).boxed()
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|