forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountHolder.h
168 lines (139 loc) · 4.9 KB
/
AccountHolder.h
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
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file AccountHolder.h
* @authors:
* Christian R <c@ethdev.com>
* Lefteris Karapetsas <lefteris@ethdev.com>
* @date 2015
*/
#pragma once
#include <functional>
#include <algorithm>
#include <vector>
#include <map>
#include <chrono>
#include <libdevcrypto/Common.h>
#include <libethcore/CommonJS.h>
#include <libethereum/Transaction.h>
namespace dev
{
namespace eth
{
class KeyManager;
class Interface;
enum class TransactionRepercussion
{
Unknown,
UnknownAccount,
Locked,
Refused,
ProxySuccess,
Success,
NonceError,
BlockLimitError,
DeployPermissionError,
TxPermissionError,
CallPermissionError
};
struct TransactionNotification
{
TransactionRepercussion r;
h256 hash;
Address created;
};
/**
* Manages real accounts (where we know the secret key) and proxy accounts (where transactions
* to be sent from these accounts are forwarded to a proxy on the other side).
*/
class AccountHolder
{
public:
explicit AccountHolder(std::function<Interface*()> const& _client): m_client(_client) {}
virtual AddressHash realAccounts() const = 0;
// use m_web3's submitTransaction
// or use AccountHolder::queueTransaction(_t) to accept
virtual TransactionNotification authenticate(dev::eth::TransactionSkeleton const& _t) = 0;
Addresses allAccounts() const;
bool isRealAccount(Address const& _account) const { return realAccounts().count(_account) > 0; }
bool isProxyAccount(Address const& _account) const { return m_proxyAccounts.count(_account) > 0; }
Address const& defaultTransactAccount() const;
/// Automatically authenticate all transactions for the given account for the next @a _duration
/// seconds. Decrypt the key with @a _password if needed. @returns true on success.
/// Only works for direct accounts.
virtual bool unlockAccount(
Address const& /*_account*/,
std::string const& /*_password*/,
unsigned /*_duration*/
)
{
return false;
}
int addProxyAccount(Address const& _account);
bool removeProxyAccount(unsigned _id);
void queueTransaction(eth::TransactionSkeleton const& _transaction);
std::vector<eth::TransactionSkeleton> const& queuedTransactions(int _id) const;
void clearQueue(int _id);
protected:
std::function<Interface*()> m_client;
private:
using TransactionQueue = std::vector<eth::TransactionSkeleton>;
std::unordered_map<Address, int> m_proxyAccounts;
std::unordered_map<int, std::pair<Address, TransactionQueue>> m_transactionQueues;
};
class SimpleAccountHolder: public AccountHolder
{
public:
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)>()):
AccountHolder(_client),
m_getPassword(_getPassword),
m_getAuthorisation(_getAuthorisation),
m_keyManager(_keyman)
{}
AddressHash realAccounts() const override;
TransactionNotification authenticate(dev::eth::TransactionSkeleton const& _t) override;
virtual bool unlockAccount(Address const& _account, std::string const& _password, unsigned _duration) override;
private:
std::function<std::string(Address)> m_getPassword;
std::function<bool(TransactionSkeleton const&, bool)> m_getAuthorisation;
KeyManager& m_keyManager;
std::map<Address, std::pair<std::chrono::steady_clock::time_point, unsigned>> m_unlockedAccounts;
};
class FixedAccountHolder: public AccountHolder
{
public:
FixedAccountHolder(std::function<Interface*()> const& _client, std::vector<dev::KeyPair> const& _accounts):
AccountHolder(_client)
{
setAccounts(_accounts);
}
void setAccounts(std::vector<dev::KeyPair> const& _accounts)
{
for (auto const& i: _accounts)
m_accounts[i.address()] = i.secret();
}
dev::AddressHash realAccounts() const override
{
dev::AddressHash ret;
for (auto const& i: m_accounts)
ret.insert(i.first);
return ret;
}
// use m_web3's submitTransaction
// or use AccountHolder::queueTransaction(_t) to accept
TransactionNotification authenticate(dev::eth::TransactionSkeleton const& _t) override;
private:
std::unordered_map<dev::Address, dev::Secret> m_accounts;
};
}
}