completed KeyStoreNodeKeyPair

This commit is contained in:
Svyatoslav Nikolsky 2017-07-25 17:54:32 +03:00
parent 2e9df2c39d
commit eb895fbb31
9 changed files with 35 additions and 4 deletions

1
Cargo.lock generated
View File

@ -794,6 +794,7 @@ name = "ethstore"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ethcore-bigint 0.1.3", "ethcore-bigint 0.1.3",
"ethcore-util 1.8.0",
"ethcrypto 0.1.0", "ethcrypto 0.1.0",
"ethkey 0.2.0", "ethkey 0.2.0",
"itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -702,6 +702,13 @@ impl AccountProvider {
Ok(self.sstore.decrypt(&account, &password, shared_mac, message)?) Ok(self.sstore.decrypt(&account, &password, shared_mac, message)?)
} }
/// Agree on shared key.
pub fn agree(&self, address: Address, password: Option<String>, other_public: &Public) -> Result<Secret, SignError> {
let account = self.sstore.account_ref(&address)?;
let password = password.map(Ok).unwrap_or_else(|| self.password(&account))?;
Ok(self.sstore.agree(&account, &password, other_public)?)
}
/// Returns the underlying `SecretStore` reference if one exists. /// Returns the underlying `SecretStore` reference if one exists.
pub fn list_geth_accounts(&self, testnet: bool) -> Vec<Address> { pub fn list_geth_accounts(&self, testnet: bool) -> Vec<Address> {
self.sstore.list_geth_accounts(testnet).into_iter().map(|a| Address::from(a).into()).collect() self.sstore.list_geth_accounts(testnet).into_iter().map(|a| Address::from(a).into()).collect()

View File

@ -19,6 +19,7 @@ itertools = "0.5"
parking_lot = "0.4" parking_lot = "0.4"
ethcrypto = { path = "../ethcrypto" } ethcrypto = { path = "../ethcrypto" }
ethcore-bigint = { path = "../util/bigint" } ethcore-bigint = { path = "../util/bigint" }
ethcore-util = { path = "../util" }
smallvec = "0.4" smallvec = "0.4"
parity-wordlist = "1.0" parity-wordlist = "1.0"
tempdir = "0.3" tempdir = "0.3"

View File

@ -14,7 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use ethkey::{KeyPair, sign, Address, Signature, Message, Public}; use ethkey::{KeyPair, sign, Address, Signature, Message, Public, Secret};
use crypto::ecdh::agree;
use {json, Error, crypto}; use {json, Error, crypto};
use account::Version; use account::Version;
use super::crypto::Crypto; use super::crypto::Crypto;
@ -135,6 +136,12 @@ impl SafeAccount {
crypto::ecies::decrypt(&secret, shared_mac, message).map_err(From::from) crypto::ecies::decrypt(&secret, shared_mac, message).map_err(From::from)
} }
/// Agree on shared key.
pub fn agree(&self, password: &str, other: &Public) -> Result<Secret, Error> {
let secret = self.crypto.secret(password)?;
agree(&secret, other).map_err(From::from)
}
/// Derive public key. /// Derive public key.
pub fn public(&self, password: &str) -> Result<Public, Error> { pub fn public(&self, password: &str) -> Result<Public, Error> {
let secret = self.crypto.secret(password)?; let secret = self.crypto.secret(password)?;

View File

@ -97,6 +97,10 @@ impl SimpleSecretStore for EthStore {
self.store.sign_derived(account_ref, password, derivation, message) self.store.sign_derived(account_ref, password, derivation, message)
} }
fn agree(&self, account: &StoreAccountRef, password: &str, other: &Public) -> Result<Secret, Error> {
self.store.agree(account, password, other)
}
fn decrypt(&self, account: &StoreAccountRef, password: &str, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> { fn decrypt(&self, account: &StoreAccountRef, password: &str, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
let account = self.get(account)?; let account = self.get(account)?;
account.decrypt(password, shared_mac, message) account.decrypt(password, shared_mac, message)
@ -509,6 +513,14 @@ impl SimpleSecretStore for EthMultiStore {
Err(Error::InvalidPassword) Err(Error::InvalidPassword)
} }
fn agree(&self, account: &StoreAccountRef, password: &str, other: &Public) -> Result<Secret, Error> {
let accounts = self.get_matching(account, password)?;
for account in accounts {
return account.agree(password, other);
}
Err(Error::InvalidPassword)
}
fn create_vault(&self, name: &str, password: &str) -> Result<(), Error> { fn create_vault(&self, name: &str, password: &str) -> Result<(), Error> {
let is_vault_created = { // lock border let is_vault_created = { // lock border
let mut vaults = self.vaults.lock(); let mut vaults = self.vaults.lock();

View File

@ -35,6 +35,7 @@ extern crate ethcore_bigint as bigint;
extern crate ethcrypto as crypto; extern crate ethcrypto as crypto;
extern crate ethkey as _ethkey; extern crate ethkey as _ethkey;
extern crate parity_wordlist; extern crate parity_wordlist;
extern crate ethcore_util as util;
#[macro_use] #[macro_use]
extern crate log; extern crate log;

View File

@ -60,6 +60,8 @@ pub trait SimpleSecretStore: Send + Sync {
fn sign_derived(&self, account_ref: &StoreAccountRef, password: &str, derivation: Derivation, message: &Message) -> Result<Signature, Error>; fn sign_derived(&self, account_ref: &StoreAccountRef, password: &str, derivation: Derivation, message: &Message) -> Result<Signature, Error>;
/// Decrypt a messages with given account. /// Decrypt a messages with given account.
fn decrypt(&self, account: &StoreAccountRef, password: &str, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error>; fn decrypt(&self, account: &StoreAccountRef, password: &str, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error>;
/// Agree on shared key.
fn agree(&self, account: &StoreAccountRef, password: &str, other: &Public) -> Result<Secret, Error>;
/// Returns all accounts in this secret store. /// Returns all accounts in this secret store.
fn accounts(&self) -> Result<Vec<StoreAccountRef>, Error>; fn accounts(&self) -> Result<Vec<StoreAccountRef>, Error>;

View File

@ -245,7 +245,6 @@ pub mod tests {
let key_pairs: Vec<_> = (0..num_nodes).map(|_| Random.generate().unwrap()).collect(); let key_pairs: Vec<_> = (0..num_nodes).map(|_| Random.generate().unwrap()).collect();
let configs: Vec<_> = (0..num_nodes).map(|i| ClusterConfiguration { let configs: Vec<_> = (0..num_nodes).map(|i| ClusterConfiguration {
threads: 1, threads: 1,
// self_key_pair: Arc::new(PlainNodeKeyPair::new(key_pairs[i].clone())),
listener_address: NodeAddress { listener_address: NodeAddress {
address: "127.0.0.1".into(), address: "127.0.0.1".into(),
port: start_port + (i as u16), port: start_port + (i as u16),

View File

@ -77,7 +77,8 @@ impl NodeKeyPair for KeyStoreNodeKeyPair {
.map_err(|e| EthKeyError::Custom(format!("{}", e))) .map_err(|e| EthKeyError::Custom(format!("{}", e)))
} }
fn compute_shared_key(&self, _peer_public: &Public) -> Result<KeyPair, EthKeyError> { fn compute_shared_key(&self, peer_public: &Public) -> Result<KeyPair, EthKeyError> {
unimplemented!() KeyPair::from_secret(self.account_provider.agree(self.address.clone(), Some(self.password.clone()), peer_public)
.map_err(|e| EthKeyError::Custom(format!("{}", e)))?)
} }
} }