2016-12-11 19:30:54 +01:00
|
|
|
// Copyright 2015, 2016 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-09 13:13:35 +01:00
|
|
|
use rlp;
|
2016-10-15 14:44:08 +02:00
|
|
|
use util::{Address, H256, U256, Uint, Bytes};
|
2016-08-08 17:25:15 +02:00
|
|
|
use util::bytes::ToPretty;
|
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;
|
|
|
|
use ethcore::transaction::{Action, SignedTransaction, Transaction};
|
|
|
|
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-09 13:13:35 +01:00
|
|
|
pub fn execute<C, M>(client: &C, miner: &M, accounts: &AccountProvider, payload: ConfirmationPayload, pass: Option<String>) -> Result<ConfirmationResponse, Error>
|
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
|
|
|
match payload {
|
|
|
|
ConfirmationPayload::SendTransaction(request) => {
|
|
|
|
sign_and_dispatch(client, miner, accounts, request, pass)
|
|
|
|
.map(RpcH256::from)
|
|
|
|
.map(ConfirmationResponse::SendTransaction)
|
|
|
|
},
|
|
|
|
ConfirmationPayload::SignTransaction(request) => {
|
|
|
|
sign_no_dispatch(client, miner, accounts, request, pass)
|
2016-11-18 11:03:29 +01:00
|
|
|
.map(RpcRichRawTransaction::from)
|
2016-11-09 13:13:35 +01:00
|
|
|
.map(ConfirmationResponse::SignTransaction)
|
|
|
|
},
|
|
|
|
ConfirmationPayload::Signature(address, hash) => {
|
|
|
|
signature(accounts, address, hash, pass)
|
|
|
|
.map(RpcH520::from)
|
|
|
|
.map(ConfirmationResponse::Signature)
|
|
|
|
},
|
|
|
|
ConfirmationPayload::Decrypt(address, data) => {
|
|
|
|
decrypt(accounts, address, data, pass)
|
|
|
|
.map(RpcBytes)
|
|
|
|
.map(ConfirmationResponse::Decrypt)
|
|
|
|
},
|
|
|
|
}
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn signature(accounts: &AccountProvider, address: Address, hash: H256, password: Option<String>) -> Result<Signature, Error> {
|
2016-10-15 14:44:08 +02:00
|
|
|
accounts.sign(address, password.clone(), hash).map_err(|e| match password {
|
|
|
|
Some(_) => errors::from_password_error(e),
|
|
|
|
None => errors::from_signing_error(e),
|
|
|
|
})
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
fn decrypt(accounts: &AccountProvider, address: Address, msg: Bytes, password: Option<String>) -> Result<Bytes, Error> {
|
2016-10-15 14:44:08 +02:00
|
|
|
accounts.decrypt(address, password.clone(), &DEFAULT_MAC, &msg)
|
|
|
|
.map_err(|e| match password {
|
|
|
|
Some(_) => errors::from_password_error(e),
|
|
|
|
None => errors::from_signing_error(e),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
pub fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedTransaction) -> Result<H256, Error>
|
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
let hash = signed_transaction.hash();
|
|
|
|
|
|
|
|
miner.import_own_transaction(client, signed_transaction)
|
|
|
|
.map_err(errors::from_transaction_error)
|
|
|
|
.map(|_| hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sign_no_dispatch<C, M>(client: &C, miner: &M, accounts: &AccountProvider, filled: FilledTransactionRequest, password: Option<String>) -> Result<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-11-09 13:13:35 +01:00
|
|
|
let signature = try!(signature(accounts, address, hash, password));
|
2016-11-03 22:22:25 +01:00
|
|
|
t.with_signature(signature, network_id)
|
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-09 13:13:35 +01:00
|
|
|
pub fn sign_and_dispatch<C, M>(client: &C, miner: &M, accounts: &AccountProvider, filled: FilledTransactionRequest, password: Option<String>) -> Result<H256, Error>
|
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
|
|
|
|
|
|
|
let network_id = client.signing_network_id();
|
|
|
|
let signed_transaction = try!(sign_no_dispatch(client, miner, accounts, filled, password));
|
|
|
|
|
|
|
|
trace!(target: "miner", "send_transaction: dispatching tx: {} for network ID {:?}", rlp::encode(&signed_transaction).to_vec().pretty(), network_id);
|
2016-10-31 17:11:56 +01:00
|
|
|
dispatch_transaction(&*client, &*miner, signed_transaction)
|
2016-08-08 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:13:35 +01:00
|
|
|
pub fn fill_optional_fields<C, M>(request: TransactionRequest, client: &C, miner: &M) -> FilledTransactionRequest
|
|
|
|
where C: MiningBlockChainClient, M: MinerService
|
|
|
|
{
|
|
|
|
FilledTransactionRequest {
|
|
|
|
from: request.from,
|
|
|
|
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),
|
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
|
|
|
|
|
|
|
pub fn from_rpc<C, M>(payload: RpcConfirmationPayload, client: &C, miner: &M) -> ConfirmationPayload
|
|
|
|
where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
|
|
|
|
match payload {
|
|
|
|
RpcConfirmationPayload::SendTransaction(request) => {
|
|
|
|
ConfirmationPayload::SendTransaction(fill_optional_fields(request.into(), client, miner))
|
|
|
|
},
|
|
|
|
RpcConfirmationPayload::SignTransaction(request) => {
|
|
|
|
ConfirmationPayload::SignTransaction(fill_optional_fields(request.into(), client, miner))
|
|
|
|
},
|
|
|
|
RpcConfirmationPayload::Decrypt(RpcDecryptRequest { address, msg }) => {
|
|
|
|
ConfirmationPayload::Decrypt(address.into(), msg.into())
|
|
|
|
},
|
|
|
|
RpcConfirmationPayload::Signature(RpcSignRequest { address, hash }) => {
|
|
|
|
ConfirmationPayload::Signature(address.into(), hash.into())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|