Fabcoin Core  0.16.2
P2P Digital Currency
AccountHolder.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-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  cpp-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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #pragma once
25 
26 #include <functional>
27 #include <algorithm>
28 #include <vector>
29 #include <map>
30 #include <chrono>
31 #include <libdevcrypto/Common.h>
32 #include <libethcore/CommonJS.h>
34 
35 namespace dev
36 {
37 namespace eth
38 {
39 
40 class KeyManager;
41 class Interface;
42 
44 {
45  Unknown,
47  Locked,
48  Refused,
50  Success
51 };
52 
54 {
58 };
59 
65 {
66 public:
67  explicit AccountHolder(std::function<Interface*()> const& _client): m_client(_client) {}
68 
69  virtual AddressHash realAccounts() const = 0;
70  // use m_web3's submitTransaction
71  // or use AccountHolder::queueTransaction(_t) to accept
72  virtual TransactionNotification authenticate(dev::eth::TransactionSkeleton const& _t) = 0;
73 
74  Addresses allAccounts() const;
75  bool isRealAccount(Address const& _account) const { return realAccounts().count(_account) > 0; }
76  bool isProxyAccount(Address const& _account) const { return m_proxyAccounts.count(_account) > 0; }
77  Address const& defaultTransactAccount() const;
78 
82  virtual bool unlockAccount(
83  Address const& /*_account*/,
84  std::string const& /*_password*/,
85  unsigned /*_duration*/
86  )
87  {
88  return false;
89  }
90 
91  int addProxyAccount(Address const& _account);
92  bool removeProxyAccount(unsigned _id);
93  void queueTransaction(eth::TransactionSkeleton const& _transaction);
94 
95  std::vector<eth::TransactionSkeleton> const& queuedTransactions(int _id) const;
96  void clearQueue(int _id);
97 
98 protected:
99  std::function<Interface*()> m_client;
100 
101 private:
102  using TransactionQueue = std::vector<eth::TransactionSkeleton>;
103 
104  std::unordered_map<Address, int> m_proxyAccounts;
105  std::unordered_map<int, std::pair<Address, TransactionQueue>> m_transactionQueues;
106 };
107 
109 {
110 public:
111  SimpleAccountHolder(std::function<Interface*()> const& _client, std::function<std::string(Address)> const& _getPassword, KeyManager& _keyman, std::function<bool(TransactionSkeleton const&, bool)> _getAuthorisation = std::function<bool(TransactionSkeleton const&, bool)>()):
112  AccountHolder(_client),
113  m_getPassword(_getPassword),
114  m_getAuthorisation(_getAuthorisation),
115  m_keyManager(_keyman)
116  {}
117 
118  AddressHash realAccounts() const override;
119  TransactionNotification authenticate(dev::eth::TransactionSkeleton const& _t) override;
120 
121  virtual bool unlockAccount(Address const& _account, std::string const& _password, unsigned _duration) override;
122 
123 private:
124  std::function<std::string(Address)> m_getPassword;
125  std::function<bool(TransactionSkeleton const&, bool)> m_getAuthorisation;
127  std::map<Address, std::pair<std::chrono::steady_clock::time_point, unsigned>> m_unlockedAccounts;
128 };
129 
131 {
132 public:
133  FixedAccountHolder(std::function<Interface*()> const& _client, std::vector<dev::KeyPair> const& _accounts):
134  AccountHolder(_client)
135  {
136  setAccounts(_accounts);
137  }
138 
139  void setAccounts(std::vector<dev::KeyPair> const& _accounts)
140  {
141  for (auto const& i: _accounts)
142  m_accounts[i.address()] = i.secret();
143  }
144 
146  {
147  dev::AddressHash ret;
148  for (auto const& i: m_accounts)
149  ret.insert(i.first);
150  return ret;
151  }
152 
153  // use m_web3's submitTransaction
154  // or use AccountHolder::queueTransaction(_t) to accept
155  TransactionNotification authenticate(dev::eth::TransactionSkeleton const& _t) override;
156 
157 private:
158  std::unordered_map<dev::Address, dev::Secret> m_accounts;
159 };
160 
161 
162 }
163 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define function(a, b, c, d, k, s)
Manages real accounts (where we know the secret key) and proxy accounts (where transactions to be sen...
Definition: AccountHolder.h:64
std::vector< eth::TransactionSkeleton > TransactionQueue
FixedAccountHolder(std::function< Interface *()> const &_client, std::vector< dev::KeyPair > const &_accounts)
bool isProxyAccount(Address const &_account) const
Definition: AccountHolder.h:76
std::unordered_map< int, std::pair< Address, TransactionQueue > > m_transactionQueues
virtual bool unlockAccount(Address const &, std::string const &, unsigned)
Automatically authenticate all transactions for the given account for the next _duration seconds...
Definition: AccountHolder.h:82
std::function< std::string(Address)> m_getPassword
std::function< Interface *()> m_client
Definition: AccountHolder.h:99
AccountHolder(std::function< Interface *()> const &_client)
Definition: AccountHolder.h:67
High-level manager of password-encrypted keys for Ethereum.
Definition: KeyManager.h:72
bool isRealAccount(Address const &_account) const
Definition: AccountHolder.h:75
h160s Addresses
A vector of Ethereum addresses.
Definition: Common.h:68
TransactionRepercussion r
Definition: AccountHolder.h:55
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
dev::AddressHash realAccounts() const override
std::function< bool(TransactionSkeleton const &, bool)> m_getAuthorisation
SimpleAccountHolder(std::function< Interface *()> const &_client, std::function< std::string(Address)> const &_getPassword, KeyManager &_keyman, std::function< bool(TransactionSkeleton const &, bool)> _getAuthorisation=std::function< bool(TransactionSkeleton const &, bool)>())
void setAccounts(std::vector< dev::KeyPair > const &_accounts)
std::unordered_map< dev::Address, dev::Secret > m_accounts
TransactionRepercussion
Definition: AccountHolder.h:43
std::map< Address, std::pair< std::chrono::steady_clock::time_point, unsigned > > m_unlockedAccounts
std::unordered_map< Address, int > m_proxyAccounts
std::unordered_set< h160 > AddressHash
A hash set of Ethereum addresses.
Definition: Common.h:71