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/>.
|
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
use std::fmt::Debug;
|
2016-11-30 17:05:31 +01:00
|
|
|
use std::ops::Deref;
|
2016-11-09 13:13:35 +01:00
|
|
|
use rlp;
|
2017-02-04 16:00:28 +01:00
|
|
|
use util::{Address, H520, H256, U256, Uint, Bytes};
|
2016-08-08 17:25:15 +02:00
|
|
|
use util::bytes::ToPretty;
|
2017-01-30 15:08:02 +01:00
|
|
|
use util::sha3::Hashable;
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
use ethkey::Signature;
|
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;
|
|
|
|
|
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
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
pub const DEFAULT_MAC: [u8; 2] = [0, 0];
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
type AccountToken = String;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum SignWith {
|
|
|
|
Nothing,
|
|
|
|
Password(String),
|
|
|
|
Token(AccountToken),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum WithToken<T: Debug> {
|
|
|
|
No(T),
|
|
|
|
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> {
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Debug> From<(T, AccountToken)> for WithToken<T> {
|
|
|
|
fn from(tuple: (T, AccountToken)) -> Self {
|
|
|
|
WithToken::Yes(tuple.0, tuple.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute<C, M>(client: &C, miner: &M, accounts: &AccountProvider, payload: ConfirmationPayload, pass: SignWith) -> Result<WithToken<ConfirmationResponse>, Error>
|
2016-11-09 13:13:35 +01:00
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
|
|
|
match payload {
|
|
|
|
ConfirmationPayload::SendTransaction(request) => {
|
|
|
|
sign_and_dispatch(client, miner, accounts, request, pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
|
|
|
.map(RpcH256::from)
|
|
|
|
.map(ConfirmationResponse::SendTransaction)
|
|
|
|
)
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
ConfirmationPayload::SignTransaction(request) => {
|
|
|
|
sign_no_dispatch(client, miner, accounts, request, pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
|
|
|
.map(RpcRichRawTransaction::from)
|
|
|
|
.map(ConfirmationResponse::SignTransaction)
|
|
|
|
)
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
2017-01-30 15:08:02 +01:00
|
|
|
ConfirmationPayload::Signature(address, data) => {
|
|
|
|
signature(accounts, address, data.sha3(), pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
2017-02-04 16:00:28 +01:00
|
|
|
.map(|rsv| {
|
|
|
|
let mut vrs = [0u8; 65];
|
|
|
|
let rsv = rsv.as_ref();
|
|
|
|
vrs[0] = rsv[64] + 27;
|
|
|
|
vrs[1..33].copy_from_slice(&rsv[0..32]);
|
|
|
|
vrs[33..65].copy_from_slice(&rsv[32..64]);
|
|
|
|
H520(vrs)
|
|
|
|
})
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(RpcH520::from)
|
|
|
|
.map(ConfirmationResponse::Signature)
|
|
|
|
)
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
ConfirmationPayload::Decrypt(address, data) => {
|
|
|
|
decrypt(accounts, address, data, pass)
|
2016-11-30 16:11:41 +01:00
|
|
|
.map(|result| result
|
|
|
|
.map(RpcBytes)
|
|
|
|
.map(ConfirmationResponse::Decrypt)
|
|
|
|
)
|
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 {
|
|
|
|
SignWith::Nothing => errors::from_signing_error(e),
|
|
|
|
_ => errors::from_password_error(e),
|
2016-10-15 14:44:08 +02:00
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
SignWith::Nothing => errors::from_signing_error(e),
|
|
|
|
_ => errors::from_password_error(e),
|
|
|
|
})
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 18:19:19 +01:00
|
|
|
pub fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: PendingTransaction) -> Result<H256, Error>
|
2016-11-09 13:13:35 +01:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
2016-12-15 18:19:19 +01:00
|
|
|
let hash = signed_transaction.transaction.hash();
|
2016-11-09 13:13:35 +01:00
|
|
|
|
|
|
|
miner.import_own_transaction(client, signed_transaction)
|
|
|
|
.map_err(errors::from_transaction_error)
|
|
|
|
.map(|_| hash)
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
pub fn sign_no_dispatch<C, M>(client: &C, miner: &M, accounts: &AccountProvider, filled: FilledTransactionRequest, password: SignWith) -> Result<WithToken<SignedTransaction>, Error>
|
2016-08-08 17:25:15 +02:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
|
2016-11-03 22:22:25 +01:00
|
|
|
let network_id = client.signing_network_id();
|
2016-11-09 13:13:35 +01:00
|
|
|
let address = filled.from;
|
2016-08-08 17:25:15 +02:00
|
|
|
let signed_transaction = {
|
2016-11-09 13:13:35 +01:00
|
|
|
let t = Transaction {
|
|
|
|
nonce: filled.nonce
|
|
|
|
.or_else(|| miner
|
|
|
|
.last_nonce(&filled.from)
|
|
|
|
.map(|nonce| nonce + U256::one()))
|
|
|
|
.unwrap_or_else(|| client.latest_nonce(&filled.from)),
|
|
|
|
|
|
|
|
action: filled.to.map_or(Action::Create, Action::Call),
|
|
|
|
gas: filled.gas,
|
|
|
|
gas_price: filled.gas_price,
|
|
|
|
value: filled.value,
|
|
|
|
data: filled.data,
|
|
|
|
};
|
|
|
|
|
2016-11-03 22:22:25 +01:00
|
|
|
let hash = t.hash(network_id);
|
2016-12-27 12:53:56 +01:00
|
|
|
let signature = signature(accounts, address, hash, password)?;
|
2016-11-30 16:11:41 +01:00
|
|
|
signature.map(|sig| {
|
2017-01-13 09:51:36 +01:00
|
|
|
SignedTransaction::new(t.with_signature(sig, network_id))
|
|
|
|
.expect("Transaction was signed by AccountsProvider; it never produces invalid signatures; qed")
|
2016-11-30 16:11:41 +01:00
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
};
|
2016-11-09 13:13:35 +01:00
|
|
|
Ok(signed_transaction)
|
|
|
|
}
|
2016-08-08 17:25:15 +02:00
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
pub fn sign_and_dispatch<C, M>(client: &C, miner: &M, accounts: &AccountProvider, filled: FilledTransactionRequest, password: SignWith) -> Result<WithToken<H256>, Error>
|
2016-11-09 13:13:35 +01:00
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
|
|
|
|
|
|
|
let network_id = client.signing_network_id();
|
2017-02-03 19:32:10 +01:00
|
|
|
let condition = filled.condition.clone();
|
2016-12-27 12:53:56 +01:00
|
|
|
let signed_transaction = sign_no_dispatch(client, miner, accounts, filled, password)?;
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2016-11-30 16:11:41 +01:00
|
|
|
let (signed_transaction, token) = match signed_transaction {
|
|
|
|
WithToken::No(signed_transaction) => (signed_transaction, None),
|
|
|
|
WithToken::Yes(signed_transaction, token) => (signed_transaction, Some(token)),
|
|
|
|
};
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
trace!(target: "miner", "send_transaction: dispatching tx: {} for network ID {:?}", rlp::encode(&signed_transaction).to_vec().pretty(), network_id);
|
2017-02-03 19:32:10 +01:00
|
|
|
let pending_transaction = PendingTransaction::new(signed_transaction, condition.map(Into::into));
|
2016-12-15 19:10:33 +01:00
|
|
|
dispatch_transaction(&*client, &*miner, pending_transaction).map(|hash| {
|
2016-11-30 16:11:41 +01:00
|
|
|
match token {
|
|
|
|
Some(ref token) => WithToken::Yes(hash, token.clone()),
|
|
|
|
None => WithToken::No(hash),
|
|
|
|
}
|
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
pub fn fill_optional_fields<C, M>(request: TransactionRequest, default_sender: Address, client: &C, miner: &M) -> FilledTransactionRequest
|
2016-11-09 13:13:35 +01:00
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
|
|
|
FilledTransactionRequest {
|
2017-01-30 21:08:36 +01:00
|
|
|
from: request.from.unwrap_or(default_sender),
|
|
|
|
used_default_from: request.from.is_none(),
|
2016-11-09 13:13:35 +01:00
|
|
|
to: request.to,
|
|
|
|
nonce: request.nonce,
|
2016-10-15 14:44:08 +02:00
|
|
|
gas_price: request.gas_price.unwrap_or_else(|| default_gas_price(client, miner)),
|
2016-11-09 13:13:35 +01:00
|
|
|
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),
|
2017-02-03 19:32:10 +01:00
|
|
|
condition: request.condition,
|
2016-10-15 14:44:08 +02:00
|
|
|
}
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
pub fn default_gas_price<C, M>(client: &C, miner: &M) -> U256
|
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
2016-10-31 12:57:48 +01:00
|
|
|
client.gas_price_median(100).unwrap_or_else(|| miner.sensible_gas_price())
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2017-01-30 21:08:36 +01:00
|
|
|
pub fn from_rpc<C, M>(payload: RpcConfirmationPayload, default_account: Address, client: &C, miner: &M) -> ConfirmationPayload
|
2016-11-09 13:13:35 +01:00
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
|
|
|
|
match payload {
|
|
|
|
RpcConfirmationPayload::SendTransaction(request) => {
|
2017-01-30 21:08:36 +01:00
|
|
|
ConfirmationPayload::SendTransaction(fill_optional_fields(request.into(), default_account, client, miner))
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
RpcConfirmationPayload::SignTransaction(request) => {
|
2017-01-30 21:08:36 +01:00
|
|
|
ConfirmationPayload::SignTransaction(fill_optional_fields(request.into(), default_account, client, miner))
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
RpcConfirmationPayload::Decrypt(RpcDecryptRequest { address, msg }) => {
|
|
|
|
ConfirmationPayload::Decrypt(address.into(), msg.into())
|
|
|
|
},
|
2017-01-30 15:08:02 +01:00
|
|
|
RpcConfirmationPayload::Signature(RpcSignRequest { address, data }) => {
|
|
|
|
ConfirmationPayload::Signature(address.into(), data.into())
|
2016-11-09 13:13:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|