      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // 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/>.

    //! Address Book and Dapps Settings Store

    use std::{fs, fmt, hash, ops};
    use std::sync::atomic::{self, AtomicUsize};
    use std::collections::HashMap;
    use std::path::PathBuf;

    use ethstore::ethkey::Address;
    use ethjson::misc::{
    	AccountMeta,
    	DappsSettings as JsonSettings,
    	DappsHistory as JsonDappsHistory,
    	NewDappsPolicy as JsonNewDappsPolicy,
    };
    use account_provider::DappId;

    /// Disk-backed map from Address to String. Uses JSON.
    pub struct AddressBook {
    	cache: DiskMap<Address, AccountMeta>,
    }

    impl AddressBook {
    	/// Creates new address book at given directory.
    	pub fn new(path: String) -> Self {
    		let mut r = AddressBook {
    			cache: DiskMap::new(path, "address_book.json".into())
    		};
    		r.cache.revert(AccountMeta::read);
    		r
    	}

    	/// Creates transient address book (no changes are saved to disk).
    	pub fn transient() -> Self {
    		AddressBook {
    			cache: DiskMap::transient()
    		}
    	}

    	/// Get the address book.
    	pub fn get(&self) -> HashMap<Address, AccountMeta> {
    		self.cache.clone()
    	}

    	fn save(&self) {
    		self.cache.save(AccountMeta::write)
    	}

    	/// Sets new name for given address.
    	pub fn set_name(&mut self, a: Address, name: String) {
    		{
    			let mut x = self.cache.entry(a)
    				.or_insert_with(|| AccountMeta {name: Default::default(), meta: "{}".to_owned(), uuid: None});
    			x.name = name;
    		}
    		self.save();
    	}

    	/// Sets new meta for given address.
    	pub fn set_meta(&mut self, a: Address, meta: String) {
    		{
    			let mut x = self.cache.entry(a)
    				.or_insert_with(|| AccountMeta {name: "Anonymous".to_owned(), meta: Default::default(), uuid: None});
    			x.meta = meta;
    		}
    		self.save();
    	}

    	/// Removes an entry
    	pub fn remove(&mut self, a: Address) {
    		self.cache.remove(&a);
    		self.save();
    	}
    }

    /// Dapps user settings
    #[derive(Debug, Default, Clone, Eq, PartialEq)]
    pub struct DappsSettings {
    	/// A list of visible accounts
    	pub accounts: Vec<Address>,
    }

    impl From<JsonSettings> for DappsSettings {
    	fn from(s: JsonSettings) -> Self {
    		DappsSettings {
    			accounts: s.accounts.into_iter().map(Into::into).collect(),
    		}
    	}
    }

    impl From<DappsSettings> for JsonSettings {
    	fn from(s: DappsSettings) -> Self {
    		JsonSettings {
    			accounts: s.accounts.into_iter().map(Into::into).collect(),
    		}
    	}
    }

    /// Dapps user settings
    #[derive(Debug, Clone, Eq, PartialEq)]
    pub enum NewDappsPolicy {
    	AllAccounts,
    	Whitelist(Vec<Address>),
    }

    impl From<JsonNewDappsPolicy> for NewDappsPolicy {
    	fn from(s: JsonNewDappsPolicy) -> Self {
    		match s {
    			JsonNewDappsPolicy::AllAccounts => NewDappsPolicy::AllAccounts,
    			JsonNewDappsPolicy::Whitelist(accounts) => NewDappsPolicy::Whitelist(
    				accounts.into_iter().map(Into::into).collect()
    			),
    		}
    	}
    }

    impl From<NewDappsPolicy> for JsonNewDappsPolicy {
    	fn from(s: NewDappsPolicy) -> Self {
    		match s {
    			NewDappsPolicy::AllAccounts => JsonNewDappsPolicy::AllAccounts,
    			NewDappsPolicy::Whitelist(accounts) => JsonNewDappsPolicy::Whitelist(
    				accounts.into_iter().map(Into::into).collect()
    			),
    		}
    	}
    }

    /// Transient dapps data
    #[derive(Default, Debug, Clone, Eq, PartialEq)]
    pub struct TransientDappsData {
    	/// Timestamp of last access
    	pub last_accessed: u64,
    }

    impl From<JsonDappsHistory> for TransientDappsData {
    	fn from(s: JsonDappsHistory) -> Self {
    		TransientDappsData {
    			last_accessed: s.last_accessed,
    		}
    	}
    }

    impl From<TransientDappsData> for JsonDappsHistory {
    	fn from(s: TransientDappsData) -> Self {
    		JsonDappsHistory {
    			last_accessed: s.last_accessed,
    		}
    	}
    }

    enum TimeProvider {
    	Clock,
    	Incremenetal(AtomicUsize)
    }

    impl TimeProvider {
    	fn get(&self) -> u64 {
    		match *self {
    			TimeProvider::Clock => {
    				::std::time::UNIX_EPOCH.elapsed()
    					.expect("Correct time is required to be set")
    					.as_secs()

    			},
    			TimeProvider::Incremenetal(ref time) => {
    				time.fetch_add(1, atomic::Ordering::SeqCst) as u64
    			},
    		}
    	}
    }

    const MAX_RECENT_DAPPS: usize = 50;

    /// Disk-backed map from DappId to Settings. Uses JSON.
    pub struct DappsSettingsStore {
    	/// Dapps Settings
    	settings: DiskMap<DappId, DappsSettings>,
    	/// New Dapps Policy
    	policy: DiskMap<String, NewDappsPolicy>,
    	/// Transient Data of recently Accessed Dapps
    	history: DiskMap<DappId, TransientDappsData>,
    	/// Time
    	time: TimeProvider,
    }

    impl DappsSettingsStore {
    	/// Creates new store at given directory path.
    	pub fn new(path: String) -> Self {
    		let mut r = DappsSettingsStore {
    			settings: DiskMap::new(path.clone(), "dapps_accounts.json".into()),
    			policy: DiskMap::new(path.clone(), "dapps_policy.json".into()),
    			history: DiskMap::new(path.clone(), "dapps_history.json".into()),
    			time: TimeProvider::Clock,
    		};
    		r.settings.revert(JsonSettings::read);
    		r.policy.revert(JsonNewDappsPolicy::read);
    		r.history.revert(JsonDappsHistory::read);
    		r
    	}

    	/// Creates transient store (no changes are saved to disk).
    	pub fn transient() -> Self {
    		DappsSettingsStore {
    			settings: DiskMap::transient(),
    			policy: DiskMap::transient(),
    			history: DiskMap::transient(),
    			time: TimeProvider::Incremenetal(AtomicUsize::new(1)),
    		}
    	}

    	/// Get copy of the dapps settings
    	pub fn settings(&self) -> HashMap<DappId, DappsSettings> {
    		self.settings.clone()
    	}

    	/// Returns current new dapps policy
    	pub fn policy(&self) -> NewDappsPolicy {
    		self.policy.get("default").cloned().unwrap_or(NewDappsPolicy::AllAccounts)
    	}

    	/// Returns recent dapps with last accessed timestamp
    	pub fn recent_dapps(&self) -> HashMap<DappId, u64> {
    		self.history.iter().map(|(k, v)| (k.clone(), v.last_accessed)).collect()
    	}

    	/// Marks recent dapp as used
    	pub fn mark_dapp_used(&mut self, dapp: DappId) {
    		{
    			let mut entry = self.history.entry(dapp).or_insert_with(|| Default::default());
    			entry.last_accessed = self.time.get();
    		}
    		// Clear extraneous entries
    		while self.history.len() > MAX_RECENT_DAPPS {
    			let min = self.history.iter()
    				.min_by_key(|&(_, ref v)| v.last_accessed)
    				.map(|(ref k, _)| k.clone())
    				.cloned();

    			match min {
    				Some(k) => self.history.remove(&k),
    				None => break,
    			};
    		}
    		self.history.save(JsonDappsHistory::write);
    	}

    	/// Sets current new dapps policy
    	pub fn set_policy(&mut self, policy: NewDappsPolicy) {
    		self.policy.insert("default".into(), policy);
    		self.policy.save(JsonNewDappsPolicy::write);
    	}

    	/// Sets accounts for specific dapp.
    	pub fn set_accounts(&mut self, id: DappId, accounts: Vec<Address>) {
    		{
    			let mut settings = self.settings.entry(id).or_insert_with(DappsSettings::default);
    			settings.accounts = accounts;
    		}
    		self.settings.save(JsonSettings::write);
    	}
    }

    /// Disk-serializable HashMap
    #[derive(Debug)]
    struct DiskMap<K: hash::Hash + Eq, V> {
    	path: PathBuf,
    	cache: HashMap<K, V>,
    	transient: bool,
    }

    impl<K: hash::Hash + Eq, V> ops::Deref for DiskMap<K, V> {
    	type Target = HashMap<K, V>;
    	fn deref(&self) -> &Self::Target {
    		&self.cache
    	}
    }

    impl<K: hash::Hash + Eq, V> ops::DerefMut for DiskMap<K, V> {
    	fn deref_mut(&mut self) -> &mut Self::Target {
    		&mut self.cache
    	}
    }

    impl<K: hash::Hash + Eq, V> DiskMap<K, V> {
    	pub fn new(path: String, file_name: String) -> Self {
    		trace!(target: "diskmap", "new({})", path);
    		let mut path: PathBuf = path.into();
    		path.push(file_name);
    		trace!(target: "diskmap", "path={:?}", path);
    		DiskMap {
    			path: path,
    			cache: HashMap::new(),
    			transient: false,
    		}
    	}

    	pub fn transient() -> Self {
    		let mut map = DiskMap::new(Default::default(), "diskmap.json".into());
    		map.transient = true;
    		map
    	}

    	fn revert<F, E>(&mut self, read: F) where
    		F: Fn(fs::File) -> Result<HashMap<K, V>, E>,
    		E: fmt::Display,
    	{
    		if self.transient { return; }
    		trace!(target: "diskmap", "revert {:?}", self.path);
    		let _ = fs::File::open(self.path.clone())
    			.map_err(|e| trace!(target: "diskmap", "Couldn't open disk map: {}", e))
    			.and_then(|f| read(f).map_err(|e| warn!(target: "diskmap", "Couldn't read disk map: {}", e)))
    			.and_then(|m| {
    				self.cache = m;
    				Ok(())
    			});
    	}

    	fn save<F, E>(&self, write: F) where
    		F: Fn(&HashMap<K, V>, &mut fs::File) -> Result<(), E>,
    		E: fmt::Display,
    	{
    		if self.transient { return; }
    		trace!(target: "diskmap", "save {:?}", self.path);
    		let _ = fs::File::create(self.path.clone())
    			.map_err(|e| warn!(target: "diskmap", "Couldn't open disk map for writing: {}", e))
    			.and_then(|mut f| {
    				write(&self.cache, &mut f).map_err(|e| warn!(target: "diskmap", "Couldn't write to disk map: {}", e))
    			});
    	}
    }

    #[cfg(test)]
    mod tests {
    	use super::{AddressBook, DappsSettingsStore, DappsSettings, NewDappsPolicy};
    	use account_provider::DappId;
    	use std::collections::HashMap;
    	use ethjson::misc::AccountMeta;
    	use devtools::RandomTempPath;

    	#[test]
    	fn should_save_and_reload_address_book() {
    		let temp = RandomTempPath::create_dir();
    		let path = temp.as_str().to_owned();
    		let mut b = AddressBook::new(path.clone());
    		b.set_name(1.into(), "One".to_owned());
    		b.set_meta(1.into(), "{1:1}".to_owned());
    		let b = AddressBook::new(path);
    		assert_eq!(b.get(), hash_map![1.into() => AccountMeta{name: "One".to_owned(), meta: "{1:1}".to_owned(), uuid: None}]);
    	}

    	#[test]
    	fn should_remove_address() {
    		let temp = RandomTempPath::create_dir();
    		let path = temp.as_str().to_owned();
    		let mut b = AddressBook::new(path.clone());

    		b.set_name(1.into(), "One".to_owned());
    		b.set_name(2.into(), "Two".to_owned());
    		b.set_name(3.into(), "Three".to_owned());
    		b.remove(2.into());

    		let b = AddressBook::new(path);
    		assert_eq!(b.get(), hash_map![
    			1.into() => AccountMeta{name: "One".to_owned(), meta: "{}".to_owned(), uuid: None},
    			3.into() => AccountMeta{name: "Three".to_owned(), meta: "{}".to_owned(), uuid: None}
    		]);
    	}

    	#[test]
    	fn should_save_and_reload_dapps_settings() {
    		// given
    		let temp = RandomTempPath::create_dir();
    		let path = temp.as_str().to_owned();
    		let mut b = DappsSettingsStore::new(path.clone());

    		// when
    		b.set_accounts("dappOne".into(), vec![1.into(), 2.into()]);

    		// then
    		let b = DappsSettingsStore::new(path);
    		assert_eq!(b.settings(), hash_map![
    			"dappOne".into() => DappsSettings {
    				accounts: vec![1.into(), 2.into()],
    			}
    		]);
    	}

    	#[test]
    	fn should_maintain_a_map_of_recent_dapps() {
    		let mut store = DappsSettingsStore::transient();
    		assert!(store.recent_dapps().is_empty(), "Initially recent dapps should be empty.");

    		let dapp1: DappId = "dapp1".into();
    		let dapp2: DappId = "dapp2".into();
    		store.mark_dapp_used(dapp1.clone());
    		let recent = store.recent_dapps();
    		assert_eq!(recent.len(), 1);
    		assert_eq!(recent.get(&dapp1), Some(&1));

    		store.mark_dapp_used(dapp2.clone());
    		let recent = store.recent_dapps();
    		assert_eq!(recent.len(), 2);
    		assert_eq!(recent.get(&dapp1), Some(&1));
    		assert_eq!(recent.get(&dapp2), Some(&2));
    	}

    	#[test]
    	fn should_store_dapps_policy() {
    		// given
    		let temp = RandomTempPath::create_dir();
    		let path = temp.as_str().to_owned();
    		let mut store = DappsSettingsStore::new(path.clone());

    		// Test default policy
    		assert_eq!(store.policy(), NewDappsPolicy::AllAccounts);

    		// when
    		store.set_policy(NewDappsPolicy::Whitelist(vec![1.into(), 2.into()]));

    		// then
    		let store = DappsSettingsStore::new(path);
    		assert_eq!(store.policy.clone(), hash_map![
    			"default".into() => NewDappsPolicy::Whitelist(vec![1.into(), 2.into()])
    		]);
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)