/
CertifierHandler.sol
225 lines (189 loc) · 7.21 KB
/
CertifierHandler.sol
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
//! CertifierHandler contract.
//! By Parity Technologies, 2017.
//! Released under the Apache Licence 2.
pragma solidity 0.4.17;
// From Owned.sol
contract Owned {
/// STORAGE
address public owner = msg.sender;
/// EVENTS
event NewOwner(address indexed old, address indexed current);
/// MODIFIERS
modifier only_owner { require (msg.sender == owner); _; }
/// RESTRICTED PUBLIC METHODS
function setOwner(address _new) public only_owner { NewOwner(owner, _new); owner = _new; }
}
/**
* @title Multi Certifier
* Contract to allow multiple parties to collaborate over a certification contract.
* Each certified account is associated with the delegate who certified it.
* Delegates can be added and removed only by the contract owner.
*/
contract MultiCertifier {
function certified(address _who) public constant returns (bool);
function getCertifier(address _who) public constant returns (address);
}
/// @title Certifier Handler
/// @author Nicolas Gotchac <[email protected]>
/// @notice This contract can be used by certified people
/// to change their certification address. How it works:
/// 1. A trasaction must be sent from the first certified
/// address, which will contain as argument the new address
/// to certify
/// 2. An event is emitted by the contract, which a process will
/// listen to. It will immediately call `revoke(old_address)`
/// and then `certify(new_address)` in order change the certification
/// address.
/// 3. The certifier account calls `settle` so that the entry is deleted
/// from this contract.
/// A small fee will be asked in order to pay for the transaction costs (3
/// transactions are sent).
/// After the modification of the certified address, the sender and the new account
/// are both locked. This is to prevent people change constantly their certified
/// address.
contract CertifierHandler is Owned {
/// STORAGE
// The address of the MultiCertifier,
// set on creation, constant
MultiCertifier public certifier;
// A mapping of the pending requests for
// modification of the certified address
mapping(address => address) public pending;
// A mapping of addresses that should not
// be able to ask for a modification request
// (currrently limiting to 1 re-certification)
mapping(address => bool) public locked;
// Count of the pending requests. This should
// be 0 most of the time.
uint public count;
// The fee the users should be pay, that covers
// the costs of sending 3 transactions.
// This fee can be modified by the contract owner.
uint public fee;
// The treasury to which the funds are sent
address public treasury;
/// EVENTS
// Emitted when the contract is drained
event Drained (uint _balance);
// Emitted when an account is locked
event Locked (address _who);
// Emitted when a new fee is set
event NewFee(uint _oldFee, uint _newFee);
// Emitted when the treasury is modified
event NewTreasury(address _oldTreasury, address _newTreasury);
// Emitted when a new modification request is received
event Requested (address _sender, address _who);
// Emitted when a request has been cleared ; meaning the
// certified account has been transfered
event Transfered (address _sender, address _who, address _certifier);
/// MODIFIERS
modifier only_unlocked (address _who) {
require(!locked[_who]);
_;
}
/// CONSTRUCTOR
/// @notice Contructor method of the contract, which
/// will set the `certifier` address
/// @param _certifier The address of the main certifier
/// @param _treasury The address of the treasury
function CertifierHandler (address _certifier, address _treasury) public {
certifier = MultiCertifier(_certifier);
treasury = _treasury;
}
/// @notice Fallback function. Should not be called.
function () public {
assert(false);
}
/// PUBLIC METHODS
/// @notice This method will be called by certified
/// accounts that which to certify another address.
/// This function can only be called once per user.
/// After a successful re-certification, these two accounts
/// are locked, and cannot ask for another re-certification.
/// @param who The address to which the certification should be
/// transfered
function claim (address who)
public payable
only_unlocked(msg.sender)
only_unlocked(who)
{
// Make sure that the fee is paid
require(msg.value >= fee);
// The sender should not have a pending certification transfer
require(pending[msg.sender] == 0x0);
// Cannot transfer to 0x0 account
require(who != 0x0);
// The sender should already be certified
require(certifier.certified(msg.sender));
// The new address shouldn't already be certified
require(!certifier.certified(who));
// Ensure that the owner of the contract is the
// certifier of the sender
require(certifier.getCertifier(msg.sender) == owner);
pending[msg.sender] = who;
count++;
// Send the event
Requested(msg.sender, who);
// Transfer the funds to the treasury
treasury.transfer(msg.value);
}
/// @notice This method is called by the certifier account
/// in order to remove the pending request of modification
/// of the certified address.
/// Anyone can call this method, since it checks that the
/// pending request has actually gone through (modification
/// of certification address)
function settle (address sender) public {
address who = pending[sender];
// Ensure that there is a pending transfer
require(who != 0x0);
// Ensure that the new address has been certified
require(certifier.certified(who));
// Ensure that the old address has been revoked
require(!certifier.certified(sender));
// Delete the pending entry
delete pending[sender];
count--;
// Lock the accounts
locked[sender] = true;
locked[who] = true;
// Send the Locked events
Locked(sender);
Locked(who);
// Send the event
Transfered(sender, who, msg.sender);
}
/// RESTRICTED (owner or delegate only) PUBLIC METHODS
/// @notice Send the current balance to the treasury.
/// Could be needed if value is sent outside of the `claim`
/// method (eg. contract suicide)
function drain () external only_owner {
Drained(this.balance);
treasury.transfer(this.balance);
}
/// @notice Change the fee, needed if for whatever reason gas price
/// must be modified. Only the owner of the contract
/// can execute this method.
/// @param _fee The new fee
function setFee (uint _fee) external only_owner {
NewFee(fee, _fee);
fee = _fee;
}
/// @notice The owner can lock an account, which is
/// basically a blacklist. This shouldn't be used
/// often ; but could be useful for re-deployment of
/// new contract for example.
/// @param _who The account to lock.
function setLocked (address _who) external only_owner {
Locked(_who);
locked[_who] = true;
}
/// @notice Change the address of the treasury, the address to which
/// the payments are forwarded to. Only the owner of the contract
/// can execute this method.
/// @param _treasury The new treasury address
function setTreasury (address _treasury) external only_owner {
NewTreasury(treasury, _treasury);
treasury = _treasury;
}
}