2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-06-20 10:06:49 +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/>.
|
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
use std::hash::{Hash, Hasher};
|
2017-02-03 13:56:48 +01:00
|
|
|
use std::path::PathBuf;
|
2017-10-20 20:20:41 +02:00
|
|
|
use std::cmp::Ordering;
|
2018-06-22 15:09:15 +02:00
|
|
|
use ethkey::{Address, Message, Signature, Secret, Password, Public};
|
2016-06-20 00:10:34 +02:00
|
|
|
use Error;
|
2017-03-23 13:23:03 +01:00
|
|
|
use json::{Uuid, OpaqueKeyFile};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2017-06-06 18:06:40 +02:00
|
|
|
use OpaqueSecret;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
/// Key directory reference
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum SecretVaultRef {
|
|
|
|
/// Reference to key in root directory
|
|
|
|
Root,
|
|
|
|
/// Referenc to key in specific vault
|
|
|
|
Vault(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stored account reference
|
2017-10-20 20:20:41 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Ord)]
|
2017-01-30 11:44:09 +01:00
|
|
|
pub struct StoreAccountRef {
|
|
|
|
/// Account address
|
|
|
|
pub address: Address,
|
2017-10-20 20:20:41 +02:00
|
|
|
/// Vault reference
|
|
|
|
pub vault: SecretVaultRef,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for StoreAccountRef {
|
|
|
|
fn partial_cmp(&self, other: &StoreAccountRef) -> Option<Ordering> {
|
|
|
|
Some(self.address.cmp(&other.address).then_with(|| self.vault.cmp(&other.vault)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::borrow::Borrow<Address> for StoreAccountRef {
|
|
|
|
fn borrow(&self) -> &Address {
|
|
|
|
&self.address
|
|
|
|
}
|
2017-01-30 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Simple Secret Store API
|
2016-11-30 15:08:38 +01:00
|
|
|
pub trait SimpleSecretStore: Send + Sync {
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Inserts new accounts to the store (or vault) with given password.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn insert_account(&self, vault: SecretVaultRef, secret: Secret, password: &Password) -> Result<StoreAccountRef, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Inserts new derived account to the store (or vault) with given password.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn insert_derived(&self, vault: SecretVaultRef, account_ref: &StoreAccountRef, password: &Password, derivation: Derivation) -> Result<StoreAccountRef, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Changes accounts password.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn change_password(&self, account: &StoreAccountRef, old_password: &Password, new_password: &Password) -> Result<(), Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Exports key details for account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn export_account(&self, account: &StoreAccountRef, password: &Password) -> Result<OpaqueKeyFile, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Entirely removes account from the store and underlying storage.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn remove_account(&self, account: &StoreAccountRef, password: &Password) -> Result<(), Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Generates new derived account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn generate_derived(&self, account_ref: &StoreAccountRef, password: &Password, derivation: Derivation) -> Result<Address, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Sign a message with given account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn sign(&self, account: &StoreAccountRef, password: &Password, message: &Message) -> Result<Signature, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Sign a message with derived account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn sign_derived(&self, account_ref: &StoreAccountRef, password: &Password, derivation: Derivation, message: &Message) -> Result<Signature, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Decrypt a messages with given account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn decrypt(&self, account: &StoreAccountRef, password: &Password, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error>;
|
2017-08-09 11:09:40 +02:00
|
|
|
/// Agree on shared key.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn agree(&self, account: &StoreAccountRef, password: &Password, other: &Public) -> Result<Secret, Error>;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Returns all accounts in this secret store.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn accounts(&self) -> Result<Vec<StoreAccountRef>, Error>;
|
2017-02-05 16:17:56 +01:00
|
|
|
/// Get reference to some account with given address.
|
|
|
|
/// This method could be removed if we will guarantee that there is max(1) account for given address.
|
|
|
|
fn account_ref(&self, address: &Address) -> Result<StoreAccountRef, Error>;
|
2016-07-24 17:38:21 +02:00
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
/// Create new vault with given password
|
2018-06-22 15:09:15 +02:00
|
|
|
fn create_vault(&self, name: &str, password: &Password) -> Result<(), Error>;
|
2017-01-30 11:44:09 +01:00
|
|
|
/// Open vault with given password
|
2018-06-22 15:09:15 +02:00
|
|
|
fn open_vault(&self, name: &str, password: &Password) -> Result<(), Error>;
|
2017-01-30 11:44:09 +01:00
|
|
|
/// Close vault
|
|
|
|
fn close_vault(&self, name: &str) -> Result<(), Error>;
|
2017-02-05 16:17:56 +01:00
|
|
|
/// List all vaults
|
|
|
|
fn list_vaults(&self) -> Result<Vec<String>, Error>;
|
|
|
|
/// List all currently opened vaults
|
|
|
|
fn list_opened_vaults(&self) -> Result<Vec<String>, Error>;
|
2017-01-30 11:44:09 +01:00
|
|
|
/// Change vault password
|
2018-06-22 15:09:15 +02:00
|
|
|
fn change_vault_password(&self, name: &str, new_password: &Password) -> Result<(), Error>;
|
2017-02-05 16:17:56 +01:00
|
|
|
/// Cnage account' vault
|
|
|
|
fn change_account_vault(&self, vault: SecretVaultRef, account: StoreAccountRef) -> Result<StoreAccountRef, Error>;
|
2017-02-08 13:53:39 +01:00
|
|
|
/// Get vault metadata string.
|
|
|
|
fn get_vault_meta(&self, name: &str) -> Result<String, Error>;
|
|
|
|
/// Set vault metadata string.
|
|
|
|
fn set_vault_meta(&self, name: &str, meta: &str) -> Result<(), Error>;
|
2016-11-30 15:08:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Secret Store API
|
2016-11-30 15:08:38 +01:00
|
|
|
pub trait SecretStore: SimpleSecretStore {
|
2017-06-06 18:06:40 +02:00
|
|
|
|
|
|
|
/// Returns a raw opaque Secret that can be later used to sign a message.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn raw_secret(&self, account: &StoreAccountRef, password: &Password) -> Result<OpaqueSecret, Error>;
|
2017-06-06 18:06:40 +02:00
|
|
|
|
|
|
|
/// Signs a message with raw secret.
|
|
|
|
fn sign_with_secret(&self, secret: &OpaqueSecret, message: &Message) -> Result<Signature, Error> {
|
|
|
|
Ok(::ethkey::sign(&secret.0, message)?)
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Imports presale wallet
|
2018-06-22 15:09:15 +02:00
|
|
|
fn import_presale(&self, vault: SecretVaultRef, json: &[u8], password: &Password) -> Result<StoreAccountRef, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Imports existing JSON wallet
|
2018-06-22 15:09:15 +02:00
|
|
|
fn import_wallet(&self, vault: SecretVaultRef, json: &[u8], password: &Password, gen_id: bool) -> Result<StoreAccountRef, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Copies account between stores and vaults.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn copy_account(&self, new_store: &SimpleSecretStore, new_vault: SecretVaultRef, account: &StoreAccountRef, password: &Password, new_password: &Password) -> Result<(), Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Checks if password matches given account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn test_password(&self, account: &StoreAccountRef, password: &Password) -> Result<bool, Error>;
|
2016-11-30 15:08:38 +01:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Returns a public key for given account.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn public(&self, account: &StoreAccountRef, password: &Password) -> Result<Public, Error>;
|
2016-11-30 15:08:38 +01:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Returns uuid of an account.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn uuid(&self, account: &StoreAccountRef) -> Result<Uuid, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Returns account's name.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn name(&self, account: &StoreAccountRef) -> Result<String, Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Returns account's metadata.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn meta(&self, account: &StoreAccountRef) -> Result<String, Error>;
|
2016-07-24 17:38:21 +02:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Modifies account metadata.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn set_name(&self, account: &StoreAccountRef, name: String) -> Result<(), Error>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Modifies account name.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn set_meta(&self, account: &StoreAccountRef, meta: String) -> Result<(), Error>;
|
2016-08-11 18:31:28 +02:00
|
|
|
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Returns local path of the store.
|
2017-02-03 13:56:48 +01:00
|
|
|
fn local_path(&self) -> PathBuf;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Lists all found geth accounts.
|
2016-08-11 18:31:28 +02:00
|
|
|
fn list_geth_accounts(&self, testnet: bool) -> Vec<Address>;
|
2017-03-23 13:23:03 +01:00
|
|
|
/// Imports geth accounts to the store/vault.
|
2017-01-30 11:44:09 +01:00
|
|
|
fn import_geth_accounts(&self, vault: SecretVaultRef, desired: Vec<Address>, testnet: bool) -> Result<Vec<StoreAccountRef>, Error>;
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
impl StoreAccountRef {
|
|
|
|
/// Create reference to root account with given address
|
|
|
|
pub fn root(address: Address) -> Self {
|
|
|
|
StoreAccountRef::new(SecretVaultRef::Root, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create reference to vault account with given address
|
|
|
|
pub fn vault(vault_name: &str, address: Address) -> Self {
|
|
|
|
StoreAccountRef::new(SecretVaultRef::Vault(vault_name.to_owned()), address)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new account reference
|
|
|
|
pub fn new(vault_ref: SecretVaultRef, address: Address) -> Self {
|
|
|
|
StoreAccountRef {
|
|
|
|
vault: vault_ref,
|
|
|
|
address: address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for StoreAccountRef {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.address.hash(state);
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 16:56:15 +01:00
|
|
|
|
|
|
|
/// Node in hierarchical derivation.
|
|
|
|
pub struct IndexDerivation {
|
|
|
|
/// Node is soft (allows proof of parent from parent node).
|
|
|
|
pub soft: bool,
|
|
|
|
/// Index sequence of the node.
|
|
|
|
pub index: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Derivation scheme for keys
|
|
|
|
pub enum Derivation {
|
|
|
|
/// Hierarchical derivation
|
|
|
|
Hierarchical(Vec<IndexDerivation>),
|
|
|
|
/// Hash derivation, soft.
|
|
|
|
SoftHash(H256),
|
|
|
|
/// Hash derivation, hard.
|
|
|
|
HardHash(H256),
|
|
|
|
}
|