2016-12-11 19:30:54 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-06-01 19:37:34 +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-06 12:51:53 +01:00
|
|
|
//! Transactions Confirmations rpc implementation
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
use std::sync::{Arc, Weak};
|
2016-11-09 13:13:35 +01:00
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
use rlp::{UntrustedRlp, View};
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-06-01 19:57:34 +02:00
|
|
|
use ethcore::client::MiningBlockChainClient;
|
2016-12-15 18:19:19 +01:00
|
|
|
use ethcore::transaction::{SignedTransaction, PendingTransaction};
|
2016-06-01 19:57:34 +02:00
|
|
|
use ethcore::miner::MinerService;
|
2016-11-10 11:27:05 +01:00
|
|
|
|
|
|
|
use jsonrpc_core::Error;
|
2016-11-06 12:51:53 +01:00
|
|
|
use v1::traits::Signer;
|
2016-11-30 16:11:41 +01:00
|
|
|
use v1::types::{TransactionModification, ConfirmationRequest, ConfirmationResponse, ConfirmationResponseWithToken, U256, Bytes};
|
2016-09-21 12:44:49 +02:00
|
|
|
use v1::helpers::{errors, SignerService, SigningQueue, ConfirmationPayload};
|
2016-11-30 17:05:31 +01:00
|
|
|
use v1::helpers::dispatch::{self, dispatch_transaction, WithToken};
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Transactions confirmation (personal) rpc implementation.
|
2016-06-20 00:10:34 +02:00
|
|
|
pub struct SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Weak<SignerService>,
|
2016-06-20 00:10:34 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2016-06-01 19:37:34 +02:00
|
|
|
client: Weak<C>,
|
|
|
|
miner: Weak<M>,
|
|
|
|
}
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
impl<C: 'static, M: 'static> SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Create new instance of signer client.
|
2016-09-21 12:44:49 +02:00
|
|
|
pub fn new(
|
|
|
|
store: &Arc<AccountProvider>,
|
|
|
|
client: &Arc<C>,
|
|
|
|
miner: &Arc<M>,
|
|
|
|
signer: &Arc<SignerService>,
|
|
|
|
) -> Self {
|
2016-06-01 19:37:34 +02:00
|
|
|
SignerClient {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Arc::downgrade(signer),
|
2016-06-01 19:37:34 +02:00
|
|
|
accounts: Arc::downgrade(store),
|
|
|
|
client: Arc::downgrade(client),
|
|
|
|
miner: Arc::downgrade(miner),
|
|
|
|
}
|
|
|
|
}
|
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-11-30 17:05:31 +01:00
|
|
|
fn confirm_internal<F>(&self, id: U256, modification: TransactionModification, f: F) -> Result<WithToken<ConfirmationResponse>, Error> where
|
|
|
|
F: FnOnce(&C, &M, &AccountProvider, ConfirmationPayload) -> Result<WithToken<ConfirmationResponse>, Error>,
|
2016-11-30 16:11:41 +01:00
|
|
|
{
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-10-31 17:11:56 +01:00
|
|
|
|
|
|
|
let id = id.into();
|
|
|
|
let accounts = take_weak!(self.accounts);
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
|
|
|
|
signer.peek(&id).map(|confirmation| {
|
2016-11-09 13:13:35 +01:00
|
|
|
let mut payload = confirmation.payload.clone();
|
|
|
|
// Modify payload
|
2016-12-10 20:18:42 +01:00
|
|
|
if let ConfirmationPayload::SendTransaction(ref mut request) = payload {
|
|
|
|
if let Some(gas_price) = modification.gas_price {
|
2016-11-09 13:13:35 +01:00
|
|
|
request.gas_price = gas_price.into();
|
2016-12-10 20:18:42 +01:00
|
|
|
}
|
|
|
|
if let Some(gas) = modification.gas {
|
|
|
|
request.gas = gas.into();
|
|
|
|
}
|
2016-12-15 21:16:32 +01:00
|
|
|
if let Some(ref min_block) = modification.min_block {
|
|
|
|
request.min_block = min_block.as_ref().and_then(|b| b.to_min_block_num());
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
2016-11-30 16:11:41 +01:00
|
|
|
let result = f(&*client, &*miner, &*accounts, payload);
|
2016-11-09 13:13:35 +01:00
|
|
|
// Execute
|
2016-10-31 17:11:56 +01:00
|
|
|
if let Ok(ref response) = result {
|
2016-11-30 17:05:31 +01:00
|
|
|
signer.request_confirmed(id, Ok((*response).clone()));
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-10-31 17:11:56 +01:00
|
|
|
result
|
|
|
|
}).unwrap_or_else(|| Err(errors::invalid_params("Unknown RequestID", id)))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: 'static, M: 'static> Signer for SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
|
|
|
|
|
|
|
|
fn requests_to_confirm(&self) -> Result<Vec<ConfirmationRequest>, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
|
|
|
|
Ok(signer.requests()
|
|
|
|
.into_iter()
|
|
|
|
.map(Into::into)
|
|
|
|
.collect()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO [ToDr] TransactionModification is redundant for some calls
|
|
|
|
// might be better to replace it in future
|
|
|
|
fn confirm_request(&self, id: U256, modification: TransactionModification, pass: String) -> Result<ConfirmationResponse, Error> {
|
|
|
|
self.confirm_internal(id, modification, move |client, miner, accounts, payload| {
|
|
|
|
dispatch::execute(client, miner, accounts, payload, dispatch::SignWith::Password(pass))
|
2016-11-30 17:05:31 +01:00
|
|
|
}).map(|v| v.into_value())
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_request_with_token(&self, id: U256, modification: TransactionModification, token: String) -> Result<ConfirmationResponseWithToken, Error> {
|
2016-11-30 17:05:31 +01:00
|
|
|
self.confirm_internal(id, modification, move |client, miner, accounts, payload| {
|
|
|
|
dispatch::execute(client, miner, accounts, payload, dispatch::SignWith::Token(token))
|
|
|
|
}).and_then(|v| match v {
|
|
|
|
WithToken::No(_) => Err(errors::internal("Unexpected response without token.", "")),
|
|
|
|
WithToken::Yes(response, token) => Ok(ConfirmationResponseWithToken {
|
|
|
|
result: response,
|
|
|
|
token: token,
|
|
|
|
}),
|
|
|
|
})
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2016-11-10 11:27:05 +01:00
|
|
|
fn confirm_request_raw(&self, id: U256, bytes: Bytes) -> Result<ConfirmationResponse, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
|
|
|
|
let id = id.into();
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
let client = take_weak!(self.client);
|
|
|
|
let miner = take_weak!(self.miner);
|
|
|
|
|
|
|
|
signer.peek(&id).map(|confirmation| {
|
|
|
|
let result = match confirmation.payload {
|
|
|
|
ConfirmationPayload::SendTransaction(request) => {
|
|
|
|
let signed_transaction: SignedTransaction = try!(
|
|
|
|
UntrustedRlp::new(&bytes.0).as_val().map_err(errors::from_rlp_error)
|
|
|
|
);
|
|
|
|
let sender = try!(
|
|
|
|
signed_transaction.sender().map_err(|e| errors::invalid_params("Invalid signature.", e))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verification
|
|
|
|
let sender_matches = sender == request.from;
|
|
|
|
let data_matches = signed_transaction.data == request.data;
|
|
|
|
let value_matches = signed_transaction.value == request.value;
|
|
|
|
let nonce_matches = match request.nonce {
|
|
|
|
Some(nonce) => signed_transaction.nonce == nonce,
|
|
|
|
None => true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dispatch if everything is ok
|
|
|
|
if sender_matches && data_matches && value_matches && nonce_matches {
|
2016-12-15 18:19:19 +01:00
|
|
|
let pending_transaction = PendingTransaction::new(signed_transaction, request.min_block);
|
|
|
|
dispatch_transaction(&*client, &*miner, pending_transaction)
|
2016-11-10 11:27:05 +01:00
|
|
|
.map(Into::into)
|
|
|
|
.map(ConfirmationResponse::SendTransaction)
|
|
|
|
} else {
|
|
|
|
let mut error = Vec::new();
|
|
|
|
if !sender_matches { error.push("from") }
|
|
|
|
if !data_matches { error.push("data") }
|
|
|
|
if !value_matches { error.push("value") }
|
|
|
|
if !nonce_matches { error.push("nonce") }
|
|
|
|
|
|
|
|
Err(errors::invalid_params("Sent transaction does not match the request.", error))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// TODO [ToDr]:
|
|
|
|
// 1. Sign - verify signature
|
|
|
|
// 2. Decrypt - pass through?
|
|
|
|
_ => Err(errors::unimplemented(Some("Non-transaction requests does not support RAW signing yet.".into()))),
|
|
|
|
};
|
|
|
|
if let Ok(ref response) = result {
|
|
|
|
signer.request_confirmed(id, Ok(response.clone()));
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}).unwrap_or_else(|| Err(errors::invalid_params("Unknown RequestID", id)))
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:11:56 +01:00
|
|
|
fn reject_request(&self, id: U256) -> Result<bool, Error> {
|
2016-07-05 17:50:46 +02:00
|
|
|
try!(self.active());
|
2016-10-31 17:11:56 +01:00
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
|
|
|
|
let res = signer.request_rejected(id.into());
|
|
|
|
Ok(res.is_some())
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-09-21 12:44:49 +02:00
|
|
|
|
2016-10-31 17:11:56 +01:00
|
|
|
fn generate_token(&self) -> Result<String, Error> {
|
2016-09-21 12:44:49 +02:00
|
|
|
try!(self.active());
|
|
|
|
let signer = take_weak!(self.signer);
|
2016-10-31 17:11:56 +01:00
|
|
|
|
2016-09-21 12:44:49 +02:00
|
|
|
signer.generate_token()
|
|
|
|
.map_err(|e| errors::token(e))
|
|
|
|
}
|
2016-12-27 11:15:02 +01:00
|
|
|
|
|
|
|
fn generate_web_proxy_token(&self) -> Result<String, Error> {
|
|
|
|
try!(self.active());
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
|
|
|
|
Ok(signer.generate_web_proxy_access_token())
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|