2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2017-08-09 11:09:40 +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/>.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
2018-05-05 11:02:33 +02:00
|
|
|
use ethkey::crypto::ecdh::agree;
|
2018-01-10 11:33:45 +01:00
|
|
|
use ethkey::{KeyPair, Public, Signature, Error as EthKeyError, sign, public_to_address};
|
2017-08-09 11:09:40 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, Address};
|
2017-08-09 11:09:40 +02:00
|
|
|
use traits::NodeKeyPair;
|
|
|
|
|
|
|
|
pub struct PlainNodeKeyPair {
|
|
|
|
key_pair: KeyPair,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KeyStoreNodeKeyPair {
|
|
|
|
account_provider: Arc<AccountProvider>,
|
|
|
|
address: Address,
|
|
|
|
public: Public,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PlainNodeKeyPair {
|
|
|
|
pub fn new(key_pair: KeyPair) -> Self {
|
|
|
|
PlainNodeKeyPair {
|
|
|
|
key_pair: key_pair,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeKeyPair for PlainNodeKeyPair {
|
|
|
|
fn public(&self) -> &Public {
|
|
|
|
self.key_pair.public()
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:33:45 +01:00
|
|
|
fn address(&self) -> Address {
|
|
|
|
public_to_address(self.key_pair.public())
|
|
|
|
}
|
|
|
|
|
2017-08-09 11:09:40 +02:00
|
|
|
fn sign(&self, data: &H256) -> Result<Signature, EthKeyError> {
|
|
|
|
sign(self.key_pair.secret(), data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_shared_key(&self, peer_public: &Public) -> Result<KeyPair, EthKeyError> {
|
2018-05-05 11:02:33 +02:00
|
|
|
agree(self.key_pair.secret(), peer_public)
|
|
|
|
.map_err(|e| EthKeyError::Custom(e.to_string()))
|
2017-08-09 11:09:40 +02:00
|
|
|
.and_then(KeyPair::from_secret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyStoreNodeKeyPair {
|
|
|
|
pub fn new(account_provider: Arc<AccountProvider>, address: Address, password: String) -> Result<Self, EthKeyError> {
|
|
|
|
let public = account_provider.account_public(address.clone(), &password).map_err(|e| EthKeyError::Custom(format!("{}", e)))?;
|
|
|
|
Ok(KeyStoreNodeKeyPair {
|
|
|
|
account_provider: account_provider,
|
|
|
|
address: address,
|
|
|
|
public: public,
|
|
|
|
password: password,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeKeyPair for KeyStoreNodeKeyPair {
|
|
|
|
fn public(&self) -> &Public {
|
|
|
|
&self.public
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:33:45 +01:00
|
|
|
fn address(&self) -> Address {
|
|
|
|
public_to_address(&self.public)
|
|
|
|
}
|
|
|
|
|
2017-08-09 11:09:40 +02:00
|
|
|
fn sign(&self, data: &H256) -> Result<Signature, EthKeyError> {
|
|
|
|
self.account_provider.sign(self.address.clone(), Some(self.password.clone()), data.clone())
|
|
|
|
.map_err(|e| EthKeyError::Custom(format!("{}", e)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_shared_key(&self, peer_public: &Public) -> Result<KeyPair, EthKeyError> {
|
|
|
|
KeyPair::from_secret(self.account_provider.agree(self.address.clone(), Some(self.password.clone()), peer_public)
|
|
|
|
.map_err(|e| EthKeyError::Custom(format!("{}", e)))?)
|
|
|
|
}
|
|
|
|
}
|