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