cfxstore/accounts_dir/mod.rs
1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
16
17//! Accounts Directory
18
19use crate::{Error, SafeAccount};
20use cfxkey::Password;
21use std::path::PathBuf;
22
23mod disk;
24mod memory;
25mod vault;
26
27/// `VaultKeyDirectory::set_key` error
28#[derive(Debug)]
29pub enum SetKeyError {
30 /// Error is fatal and directory is probably in inconsistent state
31 Fatal(Error),
32 /// Error is non fatal, directory is reverted to pre-operation state
33 NonFatalOld(Error),
34 /// Error is non fatal, directory is consistent with new key
35 NonFatalNew(Error),
36}
37
38/// Vault key
39#[derive(Clone, PartialEq, Eq)]
40pub struct VaultKey {
41 /// Vault password
42 pub password: Password,
43 /// Number of iterations to produce a derived key from password
44 pub iterations: u32,
45}
46
47/// Keys directory
48pub trait KeyDirectory: Send + Sync {
49 /// Read keys from directory
50 fn load(&self) -> Result<Vec<SafeAccount>, Error>;
51 /// Insert new key to directory
52 fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error>;
53 /// Update key in the directory
54 fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error>;
55 /// Remove key from directory
56 fn remove(&self, account: &SafeAccount) -> Result<(), Error>;
57 /// Get directory filesystem path, if available
58 fn path(&self) -> Option<&PathBuf> { None }
59 /// Return vault provider, if available
60 fn as_vault_provider(&self) -> Option<&dyn VaultKeyDirectoryProvider> {
61 None
62 }
63 /// Unique representation of directory account collection
64 fn unique_repr(&self) -> Result<u64, Error>;
65}
66
67/// Vaults provider
68pub trait VaultKeyDirectoryProvider {
69 /// Create new vault with given key
70 fn create(
71 &self, name: &str, key: VaultKey,
72 ) -> Result<Box<dyn VaultKeyDirectory>, Error>;
73 /// Open existing vault with given key
74 fn open(
75 &self, name: &str, key: VaultKey,
76 ) -> Result<Box<dyn VaultKeyDirectory>, Error>;
77 /// List all vaults
78 fn list_vaults(&self) -> Result<Vec<String>, Error>;
79 /// Get vault meta
80 fn vault_meta(&self, name: &str) -> Result<String, Error>;
81}
82
83/// Vault directory
84pub trait VaultKeyDirectory: KeyDirectory {
85 /// Cast to `KeyDirectory`
86 fn as_key_directory(&self) -> &dyn KeyDirectory;
87 /// Vault name
88 fn name(&self) -> &str;
89 /// Get vault key
90 fn key(&self) -> VaultKey;
91 /// Set new key for vault
92 fn set_key(&self, key: VaultKey) -> Result<(), SetKeyError>;
93 /// Get vault meta
94 fn meta(&self) -> String;
95 /// Set vault meta
96 fn set_meta(&self, meta: &str) -> Result<(), Error>;
97}
98
99pub use self::{
100 disk::{DiskKeyFileManager, KeyFileManager, RootDiskDirectory},
101 memory::MemoryDirectory,
102 vault::VaultDiskDirectory,
103};
104
105impl VaultKey {
106 /// Create new vault key
107 pub fn new(password: &Password, iterations: u32) -> Self {
108 VaultKey {
109 password: password.clone(),
110 iterations,
111 }
112 }
113}