2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 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;
|
2017-02-08 16:55:06 +01:00
|
|
|
use futures::{self, future, BoxFuture, Future, IntoFuture};
|
2016-11-10 11:27:05 +01:00
|
|
|
|
|
|
|
use jsonrpc_core::Error;
|
2016-09-21 12:44:49 +02:00
|
|
|
use v1::helpers::{errors, SignerService, SigningQueue, ConfirmationPayload};
|
2017-02-08 16:55:06 +01:00
|
|
|
use v1::helpers::dispatch::{self, Dispatcher, WithToken};
|
2017-01-30 21:08:36 +01:00
|
|
|
use v1::traits::Signer;
|
|
|
|
use v1::types::{TransactionModification, ConfirmationRequest, ConfirmationResponse, ConfirmationResponseWithToken, U256, Bytes};
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
/// Transactions confirmation (personal) rpc implementation.
|
2017-02-08 16:55:06 +01:00
|
|
|
pub struct SignerClient<D: Dispatcher> {
|
2016-09-21 12:44:49 +02:00
|
|
|
signer: Weak<SignerService>,
|
2016-06-20 00:10:34 +02:00
|
|
|
accounts: Weak<AccountProvider>,
|
2017-02-08 16:55:06 +01:00
|
|
|
dispatcher: D
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:55:06 +01:00
|
|
|
impl<D: Dispatcher + 'static> SignerClient<D> {
|
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>,
|
2017-02-08 16:55:06 +01:00
|
|
|
dispatcher: D,
|
2016-09-21 12:44:49 +02:00
|
|
|
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),
|
2017-02-08 16:55:06 +01:00
|
|
|
dispatcher: dispatcher,
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:50:46 +02:00
|
|
|
|
2017-02-08 16:55:06 +01:00
|
|
|
fn confirm_internal<F, T>(&self, id: U256, modification: TransactionModification, f: F) -> BoxFuture<WithToken<ConfirmationResponse>, Error> where
|
|
|
|
F: FnOnce(D, &AccountProvider, ConfirmationPayload) -> T,
|
|
|
|
T: IntoFuture<Item=WithToken<ConfirmationResponse>, Error=Error>,
|
|
|
|
T::Future: Send + 'static
|
2016-11-30 16:11:41 +01:00
|
|
|
{
|
2016-10-31 17:11:56 +01:00
|
|
|
let id = id.into();
|
2017-02-08 16:55:06 +01:00
|
|
|
let dispatcher = self.dispatcher.clone();
|
|
|
|
|
|
|
|
let setup = || {
|
|
|
|
Ok((take_weak!(self.accounts), take_weak!(self.signer)))
|
|
|
|
};
|
|
|
|
|
|
|
|
let (accounts, signer) = match setup() {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => return future::err(e).boxed(),
|
|
|
|
};
|
2016-10-31 17:11:56 +01:00
|
|
|
|
|
|
|
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 {
|
2017-01-30 11:10:58 +01:00
|
|
|
if let Some(sender) = modification.sender.clone() {
|
|
|
|
request.from = sender.into();
|
|
|
|
// Altering sender should always reset the nonce.
|
|
|
|
request.nonce = None;
|
|
|
|
}
|
2016-12-10 20:18:42 +01:00
|
|
|
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();
|
|
|
|
}
|
2017-02-03 19:32:10 +01:00
|
|
|
if let Some(ref condition) = modification.condition {
|
|
|
|
request.condition = condition.clone().map(Into::into);
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
2016-11-09 13:13:35 +01:00
|
|
|
}
|
2017-02-08 16:55:06 +01:00
|
|
|
let fut = f(dispatcher, &*accounts, payload);
|
|
|
|
fut.into_future().then(move |result| {
|
|
|
|
// Execute
|
|
|
|
if let Ok(ref response) = result {
|
|
|
|
signer.request_confirmed(id, Ok((*response).clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}).boxed()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| future::err(errors::invalid_params("Unknown RequestID", id)).boxed())
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:55:06 +01:00
|
|
|
impl<D: Dispatcher + 'static> Signer for SignerClient<D> {
|
2016-11-30 16:11:41 +01:00
|
|
|
|
|
|
|
fn requests_to_confirm(&self) -> Result<Vec<ConfirmationRequest>, Error> {
|
|
|
|
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
|
2017-02-08 16:55:06 +01:00
|
|
|
fn confirm_request(&self, id: U256, modification: TransactionModification, pass: String)
|
|
|
|
-> BoxFuture<ConfirmationResponse, Error>
|
|
|
|
{
|
|
|
|
self.confirm_internal(id, modification, move |dis, accounts, payload| {
|
|
|
|
dispatch::execute(dis, accounts, payload, dispatch::SignWith::Password(pass))
|
|
|
|
}).map(|v| v.into_value()).boxed()
|
2016-11-30 16:11:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:55:06 +01:00
|
|
|
fn confirm_request_with_token(&self, id: U256, modification: TransactionModification, token: String)
|
|
|
|
-> BoxFuture<ConfirmationResponseWithToken, Error>
|
|
|
|
{
|
|
|
|
self.confirm_internal(id, modification, move |dis, accounts, payload| {
|
|
|
|
dispatch::execute(dis, accounts, payload, dispatch::SignWith::Token(token))
|
2016-11-30 17:05:31 +01:00
|
|
|
}).and_then(|v| match v {
|
|
|
|
WithToken::No(_) => Err(errors::internal("Unexpected response without token.", "")),
|
|
|
|
WithToken::Yes(response, token) => Ok(ConfirmationResponseWithToken {
|
|
|
|
result: response,
|
|
|
|
token: token,
|
|
|
|
}),
|
2017-02-08 16:55:06 +01:00
|
|
|
}).boxed()
|
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> {
|
|
|
|
let id = id.into();
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
|
|
|
|
signer.peek(&id).map(|confirmation| {
|
|
|
|
let result = match confirmation.payload {
|
|
|
|
ConfirmationPayload::SendTransaction(request) => {
|
2017-01-13 09:51:36 +01:00
|
|
|
let signed_transaction = UntrustedRlp::new(&bytes.0).as_val().map_err(errors::from_rlp_error)?;
|
|
|
|
let signed_transaction = SignedTransaction::new(signed_transaction).map_err(|e| errors::invalid_params("Invalid signature.", e))?;
|
|
|
|
let sender = signed_transaction.sender();
|
2016-11-10 11:27:05 +01:00
|
|
|
|
|
|
|
// 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 {
|
2017-02-03 19:32:10 +01:00
|
|
|
let pending_transaction = PendingTransaction::new(signed_transaction, request.condition.map(Into::into));
|
2017-02-08 16:55:06 +01:00
|
|
|
self.dispatcher.dispatch_transaction(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> {
|
|
|
|
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
|
|
|
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> {
|
|
|
|
let signer = take_weak!(self.signer);
|
|
|
|
|
|
|
|
Ok(signer.generate_web_proxy_access_token())
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|