-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathMultiSigKeyHolders.sol
301 lines (257 loc) · 9.68 KB
/
MultiSigKeyHolders.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
pragma solidity ^0.5.17;
pragma experimental ABIEncoderV2;
import "../openzeppelin/Ownable.sol";
/**
* @title Multi Signature Key Holders contract.
*
* This contract contains the implementation of functions to add and remove
* key holders w/ rBTC and BTC addresses.
* */
contract MultiSigKeyHolders is Ownable {
/* Storage */
uint256 public constant MAX_OWNER_COUNT = 50;
string private constant ERROR_INVALID_ADDRESS = "Invalid address";
string private constant ERROR_INVALID_REQUIRED = "Invalid required";
/// Flag and index for Ethereum address.
mapping(address => Data) private isEthereumAddressAdded;
/// List of Ethereum addresses.
address[] private ethereumAddresses;
/// Required number of signatures for the Ethereum multisig.
uint256 public ethereumRequired = 2;
/// Flag and index for Bitcoin address.
mapping(string => Data) private isBitcoinAddressAdded;
/// List of Bitcoin addresses.
string[] private bitcoinAddresses;
/// Required number of signatures for the Bitcoin multisig.
uint256 public bitcoinRequired = 2;
/// Helps removing items from array.
struct Data {
bool added;
uint248 index;
}
/* Events */
event EthereumAddressAdded(address indexed account);
event EthereumAddressRemoved(address indexed account);
event EthereumRequirementChanged(uint256 required);
event BitcoinAddressAdded(string account);
event BitcoinAddressRemoved(string account);
event BitcoinRequirementChanged(uint256 required);
/* Modifiers */
modifier validRequirement(uint256 ownerCount, uint256 _required) {
require(
ownerCount <= MAX_OWNER_COUNT &&
_required <= ownerCount &&
_required != 0 &&
ownerCount != 0,
ERROR_INVALID_REQUIRED
);
_;
}
/* Functions */
/**
* @notice Add rBTC address to the key holders.
* @param _address The address to be added.
* */
function addEthereumAddress(address _address) public onlyOwner {
_addEthereumAddress(_address);
}
/**
* @notice Add rBTC addresses to the key holders.
* @param _address The addresses to be added.
* */
function addEthereumAddresses(address[] memory _address) public onlyOwner {
for (uint256 i = 0; i < _address.length; i++) {
_addEthereumAddress(_address[i]);
}
}
/**
* @notice Internal function to add rBTC address to the key holders.
* @param _address The address to be added.
* */
function _addEthereumAddress(address _address) internal {
require(_address != address(0), ERROR_INVALID_ADDRESS);
if (!isEthereumAddressAdded[_address].added) {
isEthereumAddressAdded[_address] = Data({
added: true,
index: uint248(ethereumAddresses.length)
});
ethereumAddresses.push(_address);
}
emit EthereumAddressAdded(_address);
}
/**
* @notice Remove rBTC address to the key holders.
* @param _address The address to be removed.
* */
function removeEthereumAddress(address _address) public onlyOwner {
_removeEthereumAddress(_address);
}
/**
* @notice Remove rBTC addresses to the key holders.
* @param _address The addresses to be removed.
* */
function removeEthereumAddresses(address[] memory _address) public onlyOwner {
for (uint256 i = 0; i < _address.length; i++) {
_removeEthereumAddress(_address[i]);
}
}
/**
* @notice Internal function to remove rBTC address to the key holders.
* @param _address The address to be removed.
* */
function _removeEthereumAddress(address _address) internal {
require(_address != address(0), ERROR_INVALID_ADDRESS);
if (isEthereumAddressAdded[_address].added) {
uint248 index = isEthereumAddressAdded[_address].index;
if (index != ethereumAddresses.length - 1) {
ethereumAddresses[index] = ethereumAddresses[ethereumAddresses.length - 1];
isEthereumAddressAdded[ethereumAddresses[index]].index = index;
}
ethereumAddresses.length--;
delete isEthereumAddressAdded[_address];
}
emit EthereumAddressRemoved(_address);
}
/**
* @notice Get whether rBTC address is a key holder.
* @param _address The rBTC address to be checked.
* */
function isEthereumAddressOwner(address _address) public view returns (bool) {
return isEthereumAddressAdded[_address].added;
}
/**
* @notice Get array of rBTC key holders.
* */
function getEthereumAddresses() public view returns (address[] memory) {
return ethereumAddresses;
}
/**
* @notice Set flag ethereumRequired to true/false.
* @param _required The new value of the ethereumRequired flag.
* */
function changeEthereumRequirement(
uint256 _required
) public onlyOwner validRequirement(ethereumAddresses.length, _required) {
ethereumRequired = _required;
emit EthereumRequirementChanged(_required);
}
/**
* @notice Add bitcoin address to the key holders.
* @param _address The address to be added.
* */
function addBitcoinAddress(string memory _address) public onlyOwner {
_addBitcoinAddress(_address);
}
/**
* @notice Add bitcoin addresses to the key holders.
* @param _address The addresses to be added.
* */
function addBitcoinAddresses(string[] memory _address) public onlyOwner {
for (uint256 i = 0; i < _address.length; i++) {
_addBitcoinAddress(_address[i]);
}
}
/**
* @notice Internal function to add bitcoin address to the key holders.
* @param _address The address to be added.
* */
function _addBitcoinAddress(string memory _address) internal {
require(bytes(_address).length != 0, ERROR_INVALID_ADDRESS);
if (!isBitcoinAddressAdded[_address].added) {
isBitcoinAddressAdded[_address] = Data({
added: true,
index: uint248(bitcoinAddresses.length)
});
bitcoinAddresses.push(_address);
}
emit BitcoinAddressAdded(_address);
}
/**
* @notice Remove bitcoin address to the key holders.
* @param _address The address to be removed.
* */
function removeBitcoinAddress(string memory _address) public onlyOwner {
_removeBitcoinAddress(_address);
}
/**
* @notice Remove bitcoin addresses to the key holders.
* @param _address The addresses to be removed.
* */
function removeBitcoinAddresses(string[] memory _address) public onlyOwner {
for (uint256 i = 0; i < _address.length; i++) {
_removeBitcoinAddress(_address[i]);
}
}
/**
* @notice Internal function to remove bitcoin address to the key holders.
* @param _address The address to be removed.
* */
function _removeBitcoinAddress(string memory _address) internal {
require(bytes(_address).length != 0, ERROR_INVALID_ADDRESS);
if (isBitcoinAddressAdded[_address].added) {
uint248 index = isBitcoinAddressAdded[_address].index;
if (index != bitcoinAddresses.length - 1) {
bitcoinAddresses[index] = bitcoinAddresses[bitcoinAddresses.length - 1];
isBitcoinAddressAdded[bitcoinAddresses[index]].index = index;
}
bitcoinAddresses.length--;
delete isBitcoinAddressAdded[_address];
}
emit BitcoinAddressRemoved(_address);
}
/**
* @notice Get whether bitcoin address is a key holder.
* @param _address The bitcoin address to be checked.
* */
function isBitcoinAddressOwner(string memory _address) public view returns (bool) {
return isBitcoinAddressAdded[_address].added;
}
/**
* @notice Get array of bitcoin key holders.
* */
function getBitcoinAddresses() public view returns (string[] memory) {
return bitcoinAddresses;
}
/**
* @notice Set flag bitcoinRequired to true/false.
* @param _required The new value of the bitcoinRequired flag.
* */
function changeBitcoinRequirement(
uint256 _required
) public onlyOwner validRequirement(bitcoinAddresses.length, _required) {
bitcoinRequired = _required;
emit BitcoinRequirementChanged(_required);
}
/**
* @notice Add rBTC and bitcoin addresses to the key holders.
* @param _ethereumAddress the rBTC addresses to be added.
* @param _bitcoinAddress the bitcoin addresses to be added.
* */
function addEthereumAndBitcoinAddresses(
address[] memory _ethereumAddress,
string[] memory _bitcoinAddress
) public onlyOwner {
for (uint256 i = 0; i < _ethereumAddress.length; i++) {
_addEthereumAddress(_ethereumAddress[i]);
}
for (uint256 i = 0; i < _bitcoinAddress.length; i++) {
_addBitcoinAddress(_bitcoinAddress[i]);
}
}
/**
* @notice Remove rBTC and bitcoin addresses to the key holders.
* @param _ethereumAddress The rBTC addresses to be removed.
* @param _bitcoinAddress The bitcoin addresses to be removed.
* */
function removeEthereumAndBitcoinAddresses(
address[] memory _ethereumAddress,
string[] memory _bitcoinAddress
) public onlyOwner {
for (uint256 i = 0; i < _ethereumAddress.length; i++) {
_removeEthereumAddress(_ethereumAddress[i]);
}
for (uint256 i = 0; i < _bitcoinAddress.length; i++) {
_removeBitcoinAddress(_bitcoinAddress[i]);
}
}
}