Support for decryption in Signer (#2421)

* Adding some tests

* Implementing decrypt in queue

* Removing code duplication.

* Printing public key in ethstore

* Bump UI

* Normalizing dapps format for signer.

* Fixing tests compilation

* fix whitespace

[ci:skip]
This commit is contained in:
Tomasz Drwięga
2016-10-15 14:44:08 +02:00
committed by Gav Wood
parent 85eeb3ea6e
commit 03c1559ead
27 changed files with 457 additions and 278 deletions

View File

@@ -24,9 +24,9 @@ use util::{U256, Address, H256, Mutex};
use transient_hashmap::TransientHashMap;
use ethcore::account_provider::AccountProvider;
use v1::helpers::{errors, SigningQueue, ConfirmationPromise, ConfirmationResult, ConfirmationPayload, TransactionRequest as TRequest, FilledTransactionRequest as FilledRequest, SignerService};
use v1::helpers::dispatch::{default_gas_price, sign_and_dispatch};
use v1::helpers::dispatch::{default_gas_price, sign_and_dispatch, sign, decrypt};
use v1::traits::EthSigning;
use v1::types::{TransactionRequest, H160 as RpcH160, H256 as RpcH256, H520 as RpcH520, U256 as RpcU256, Bytes as RpcBytes};
use v1::types::{TransactionRequest, H160 as RpcH160, H256 as RpcH256, U256 as RpcU256, Bytes as RpcBytes};
fn fill_optional_fields<C, M>(request: TRequest, client: &C, miner: &M) -> FilledRequest
where C: MiningBlockChainClient, M: MinerService {
@@ -76,41 +76,59 @@ impl<C, M> EthSigningQueueClient<C, M> where C: MiningBlockChainClient, M: Miner
Ok(())
}
fn add_to_queue<WhenUnlocked, Payload>(&self, sender: Address, when_unlocked: WhenUnlocked, payload: Payload)
-> Result<DispatchResult, Error> where
WhenUnlocked: Fn(&AccountProvider) -> Result<Value, Error>,
Payload: Fn() -> ConfirmationPayload, {
let accounts = take_weak!(self.accounts);
if accounts.is_unlocked(sender) {
return when_unlocked(&accounts).map(DispatchResult::Value);
}
take_weak!(self.signer).add_request(payload())
.map(DispatchResult::Promise)
.map_err(|_| errors::request_rejected_limit())
}
fn handle_dispatch(&self, res: Result<DispatchResult, Error>, ready: Ready) {
match res {
Ok(DispatchResult::Value(v)) => ready.ready(Ok(v)),
Ok(DispatchResult::Promise(promise)) => {
promise.wait_for_result(move |result| {
ready.ready(result.unwrap_or_else(|| Err(errors::request_rejected())))
})
},
Err(e) => ready.ready(Err(e)),
}
}
fn dispatch_sign(&self, params: Params) -> Result<DispatchResult, Error> {
from_params::<(RpcH160, RpcH256)>(params).and_then(|(address, msg)| {
let address: Address = address.into();
let msg: H256 = msg.into();
let accounts = take_weak!(self.accounts);
if accounts.is_unlocked(address) {
return Ok(DispatchResult::Value(to_value(&accounts.sign(address, msg).ok().map_or_else(RpcH520::default, Into::into))))
}
let signer = take_weak!(self.signer);
signer.add_request(ConfirmationPayload::Sign(address, msg))
.map(DispatchResult::Promise)
.map_err(|_| errors::request_rejected_limit())
self.add_to_queue(
address,
|accounts| sign(accounts, address, None, msg.clone()),
|| ConfirmationPayload::Sign(address, msg.clone()),
)
})
}
fn dispatch_transaction(&self, params: Params) -> Result<DispatchResult, Error> {
from_params::<(TransactionRequest, )>(params)
.and_then(|(request, )| {
let request: TRequest = request.into();
let accounts = take_weak!(self.accounts);
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
if accounts.is_unlocked(request.from) {
let sender = request.from;
return sign_and_dispatch(&*client, &*miner, request, &*accounts, sender).map(DispatchResult::Value);
from_params::<(TransactionRequest, )>(params).and_then(|(request, )| {
let request: TRequest = request.into();
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
self.add_to_queue(
request.from,
|accounts| sign_and_dispatch(&*client, &*miner, accounts, request.clone(), None),
|| {
let request = fill_optional_fields(request.clone(), &*client, &*miner);
ConfirmationPayload::Transaction(request)
}
let signer = take_weak!(self.signer);
let request = fill_optional_fields(request, &*client, &*miner);
signer.add_request(ConfirmationPayload::Transaction(request))
.map(DispatchResult::Promise)
.map_err(|_| errors::request_rejected_limit())
})
)
})
}
}
@@ -118,19 +136,6 @@ impl<C, M> EthSigning for EthSigningQueueClient<C, M>
where C: MiningBlockChainClient + 'static, M: MinerService + 'static
{
fn sign(&self, params: Params, ready: Ready) {
let res = self.active().and_then(|_| self.dispatch_sign(params));
match res {
Ok(DispatchResult::Promise(promise)) => {
promise.wait_for_result(move |result| {
ready.ready(result.unwrap_or_else(|| Err(errors::request_rejected())))
})
},
Ok(DispatchResult::Value(v)) => ready.ready(Ok(v)),
Err(e) => ready.ready(Err(e)),
}
}
fn post_sign(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
self.dispatch_sign(params).map(|result| match result {
@@ -143,19 +148,6 @@ impl<C, M> EthSigning for EthSigningQueueClient<C, M>
})
}
fn send_transaction(&self, params: Params, ready: Ready) {
let res = self.active().and_then(|_| self.dispatch_transaction(params));
match res {
Ok(DispatchResult::Promise(promise)) => {
promise.wait_for_result(move |result| {
ready.ready(result.unwrap_or_else(|| Err(errors::request_rejected())))
})
},
Ok(DispatchResult::Value(v)) => ready.ready(Ok(v)),
Err(e) => ready.ready(Err(e)),
}
}
fn post_transaction(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
self.dispatch_transaction(params).map(|result| match result {
@@ -168,13 +160,6 @@ impl<C, M> EthSigning for EthSigningQueueClient<C, M>
})
}
fn decrypt_message(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
from_params::<(RpcH160, RpcBytes)>(params).and_then(|(_account, _ciphertext)| {
Err(errors::unimplemented())
})
}
fn check_request(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
let mut pending = self.pending.lock();
@@ -192,6 +177,32 @@ impl<C, M> EthSigning for EthSigningQueueClient<C, M>
res
})
}
fn sign(&self, params: Params, ready: Ready) {
let res = self.active().and_then(|_| self.dispatch_sign(params));
self.handle_dispatch(res, ready);
}
fn send_transaction(&self, params: Params, ready: Ready) {
let res = self.active().and_then(|_| self.dispatch_transaction(params));
self.handle_dispatch(res, ready);
}
fn decrypt_message(&self, params: Params, ready: Ready) {
let res = self.active()
.and_then(|_| from_params::<(RpcH160, RpcBytes)>(params))
.and_then(|(address, msg)| {
let address: Address = address.into();
self.add_to_queue(
address,
|accounts| decrypt(accounts, address, None, msg.clone().into()),
|| ConfirmationPayload::Decrypt(address, msg.clone().into())
)
});
self.handle_dispatch(res, ready);
}
}
/// Implementation of functions that require signing when no trusted signer is used.
@@ -232,9 +243,7 @@ impl<C, M> EthSigning for EthSigningUnsafeClient<C, M> where
ready.ready(self.active()
.and_then(|_| from_params::<(RpcH160, RpcH256)>(params))
.and_then(|(address, msg)| {
let address: Address = address.into();
let msg: H256 = msg.into();
Ok(to_value(&take_weak!(self.accounts).sign(address, msg).ok().map_or_else(RpcH520::default, Into::into)))
sign(&*take_weak!(self.accounts), address.into(), None, msg.into())
}))
}
@@ -242,18 +251,16 @@ impl<C, M> EthSigning for EthSigningUnsafeClient<C, M> where
ready.ready(self.active()
.and_then(|_| from_params::<(TransactionRequest, )>(params))
.and_then(|(request, )| {
let request: TRequest = request.into();
let sender = request.from;
sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), request, &*take_weak!(self.accounts), sender)
sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), &*take_weak!(self.accounts), request.into(), None)
}))
}
fn decrypt_message(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
from_params::<(RpcH160, RpcBytes)>(params).and_then(|(address, ciphertext)| {
let s = try!(take_weak!(self.accounts).decrypt(address.into(), &[0; 0], &ciphertext.0).map_err(|_| Error::internal_error()));
Ok(to_value(RpcBytes::from(s)))
})
fn decrypt_message(&self, params: Params, ready: Ready) {
ready.ready(self.active()
.and_then(|_| from_params::<(RpcH160, RpcBytes)>(params))
.and_then(|(address, ciphertext)| {
decrypt(&*take_weak!(self.accounts), address.into(), None, ciphertext.0)
}))
}
fn post_sign(&self, _: Params) -> Result<Value, Error> {

View File

@@ -35,6 +35,8 @@ use jsonrpc_core::Error;
use v1::traits::Ethcore;
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings};
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::dispatch::DEFAULT_MAC;
use v1::helpers::params::expect_no_params;
use v1::helpers::auto_args::Ready;
/// Ethcore implementation.
@@ -265,8 +267,8 @@ impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
fn encrypt_message(&self, key: H512, phrase: Bytes) -> Result<Bytes, Error> {
try!(self.active());
ecies::encrypt(&key.into(), &[0; 0], &phrase.0)
.map_err(|_| Error::internal_error())
ecies::encrypt(&key.into(), &DEFAULT_MAC, &phrase.0)
.map_err(errors::encryption_error)
.map(Into::into)
}

View File

@@ -22,9 +22,9 @@ use jsonrpc_core::*;
use ethkey::{Brain, Generator};
use v1::traits::Personal;
use v1::types::{H160 as RpcH160, TransactionRequest};
use v1::helpers::{errors, TransactionRequest as TRequest};
use v1::helpers::errors;
use v1::helpers::params::expect_no_params;
use v1::helpers::dispatch::unlock_sign_and_dispatch;
use v1::helpers::dispatch::sign_and_dispatch;
use ethcore::account_provider::AccountProvider;
use ethcore::client::MiningBlockChainClient;
use ethcore::miner::MinerService;
@@ -139,10 +139,13 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
try!(self.active());
from_params::<(TransactionRequest, String)>(params)
.and_then(|(request, password)| {
let request: TRequest = request.into();
let accounts = take_weak!(self.accounts);
unlock_sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), request, &*accounts, password)
sign_and_dispatch(
&*take_weak!(self.client),
&*take_weak!(self.miner),
&*take_weak!(self.accounts),
request.into(),
Some(password)
)
})
}

View File

@@ -25,7 +25,7 @@ use v1::traits::PersonalSigner;
use v1::types::{TransactionModification, ConfirmationRequest, U256};
use v1::helpers::{errors, SignerService, SigningQueue, ConfirmationPayload};
use v1::helpers::params::expect_no_params;
use v1::helpers::dispatch::{unlock_sign_and_dispatch, signature_with_password};
use v1::helpers::dispatch::{sign_and_dispatch, sign, decrypt};
/// Transactions confirmation (personal) rpc implementation.
pub struct SignerClient<C, M> where C: MiningBlockChainClient, M: MinerService {
@@ -87,12 +87,14 @@ impl<C: 'static, M: 'static> PersonalSigner for SignerClient<C, M> where C: Mini
if let Some(gas_price) = modification.gas_price {
request.gas_price = gas_price.into();
}
unlock_sign_and_dispatch(&*client, &*miner, request.into(), &*accounts, pass)
sign_and_dispatch(&*client, &*miner, &*accounts, request.into(), Some(pass))
},
ConfirmationPayload::Sign(address, hash) => {
signature_with_password(&*accounts, address, hash, pass)
}
sign(&*accounts, address, Some(pass), hash)
},
ConfirmationPayload::Decrypt(address, msg) => {
decrypt(&*accounts, address, Some(pass), msg)
},
};
if let Ok(ref response) = result {
signer.request_confirmed(id, Ok(response.clone()));