cfxcore_accounts/
account_data.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//! Account Metadata
18
19use std::{collections::HashMap, time::Instant};
20
21use cfxkey::{Address, Password};
22use serde_derive::{Deserialize, Serialize};
23use serde_json;
24
25/// Type of unlock.
26#[derive(Clone, PartialEq)]
27pub enum Unlock {
28    /// If account is unlocked temporarily, it should be locked after first
29    /// usage.
30    OneTime,
31    /// Account unlocked permanently can always sign message.
32    /// Use with caution.
33    Perm,
34    /// Account unlocked with a timeout
35    Timed(Instant),
36}
37
38/// Data associated with account.
39#[derive(Clone)]
40pub struct AccountData {
41    pub unlock: Unlock,
42    pub password: Password,
43}
44
45/// Collected account metadata
46#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
47pub struct AccountMeta {
48    /// The name of the account.
49    pub name: String,
50    /// The rest of the metadata of the account.
51    pub meta: String,
52    /// The 128-bit Uuid of the account, if it has one (brain-wallets don't).
53    pub uuid: Option<String>,
54}
55
56impl AccountMeta {
57    /// Read a hash map of Address -> AccountMeta
58    pub fn read<R>(
59        reader: R,
60    ) -> Result<HashMap<Address, Self>, serde_json::Error>
61    where R: ::std::io::Read {
62        serde_json::from_reader(reader)
63    }
64
65    /// Write a hash map of Address -> AccountMeta
66    pub fn write<W>(
67        m: &HashMap<Address, Self>, writer: &mut W,
68    ) -> Result<(), serde_json::Error>
69    where W: ::std::io::Write {
70        serde_json::to_writer(writer, m)
71    }
72}