-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAltheaDB.sol
More file actions
480 lines (419 loc) · 15.3 KB
/
Copy pathAltheaDB.sol
File metadata and controls
480 lines (419 loc) · 15.3 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.21; // Force solidity compliance
/// Identity struct
struct Identity {
uint128 mesh_ip;
uint256 wg_key;
address eth_addr;
}
/// Identity struct for an exit, including a list of allowed region codes
/// This is to protect a user from connecting to an exit that does not allow
/// their region despite providing the best connection metrics otherwise
/// The payment types specification prevents clients from moving to exits
/// that do not accept their tokens as payment
struct ExitIdentity {
uint128 mesh_ip;
uint256 wg_key;
address eth_addr;
uint16 registration_port;
uint16 wg_exit_listen_port;
uint256[] allowed_regions;
uint256[] payment_types;
}
contract AltheaDB {
/// The admin address that is allowed to update who is on the
/// user admin and exit admin lists. This could be an individual
/// account or a multisig
address public immutable state_admin;
/// A list of addresses allowed to add and remove users from the list of users
address[] public state_UserAdmins;
/// A list of addresses allowed to add and remove exits from the list of exits
address[] public state_ExitAdmins;
// List of regsitered clients
Identity[] public state_registeredUsers;
// List of regsitered exits
ExitIdentity[] public state_registeredExits;
// Mappings for duplicate checking
mapping(uint128 => bool) public state_registeredIps;
mapping(uint256 => bool) public state_registeredKey;
mapping(address => bool) public state_registeredAddr;
event UserRegisteredEvent(Identity indexed _user);
event UserRemovedEvent(Identity indexed _user);
event ExitRegisteredEvent(ExitIdentity indexed _user);
event ExitRemovedEvent(ExitIdentity indexed _user);
event UserAdminAddedEvent(address indexed _admin);
event UserAdminRemovedEvent(address indexed _admin);
event ExitAdminAddedEvent(address indexed _admin);
event ExitAdminRemovedEvent(address indexed _admin);
constructor(address _admin) {
state_admin = _admin;
}
// start utility function
// Used to convert an exit identity struct to an identity struct essentially just dropping the
// region codes component. This is mostly used for comparison and duplicate checking as we want
// to ignore the region codes when adding and removing an exit to avoid having identical exits
// with different region codes.
function exitIdToId(
ExitIdentity memory input
) public pure returns (Identity memory) {
return
Identity({
mesh_ip: input.mesh_ip,
wg_key: input.wg_key,
eth_addr: input.eth_addr
});
}
function getNullIdentity() public pure returns (Identity memory) {
return Identity({mesh_ip: 0, wg_key: 0, eth_addr: address(0)});
}
function getNullExitIdentity() public pure returns (ExitIdentity memory) {
uint256[] memory empty_array;
return
ExitIdentity({
mesh_ip: 0,
wg_key: 0,
eth_addr: address(0),
registration_port: 0,
wg_exit_listen_port: 0,
allowed_regions: empty_array,
payment_types: empty_array
});
}
function isNullIdentity(
Identity calldata input
) public pure returns (bool) {
return identitiesAreEqual(input, getNullIdentity());
}
function identitiesAreEqual(
Identity memory a,
Identity memory b
) public pure returns (bool) {
if (a.mesh_ip != b.mesh_ip) {
return false;
}
if (a.wg_key != b.wg_key) {
return false;
}
if (a.eth_addr != b.eth_addr) {
return false;
}
return true;
}
function isUserAdmin(address potential_admin) public view returns (bool) {
for (uint256 i = 0; i < state_UserAdmins.length; i++) {
if (potential_admin == state_UserAdmins[i]) {
return true;
}
}
return false;
}
function isExitAdmin(address potential_admin) public view returns (bool) {
for (uint256 i = 0; i < state_ExitAdmins.length; i++) {
if (potential_admin == state_ExitAdmins[i]) {
return true;
}
}
return false;
}
/// Deletes an entry of the provided array
function deleteArrayEntry(uint index, Identity[] storage array) private {
require(index < array.length);
// copy the last element into the index that we want to delete
// in the case that we want to delete the last element, just skip this
if (index != array.length - 1) {
array[index] = array[array.length - 1];
}
// drop the new duplicated end element effectively deleting the originally
// specified index
array.pop();
}
/// Deletes an entry of the provided array
function deleteArrayEntry(
uint index,
ExitIdentity[] storage array
) private {
require(index < array.length);
// copy the last element into the index that we want to delete
// in the case that we want to delete the last element, just skip this
if (index != array.length - 1) {
array[index] = array[array.length - 1];
}
// drop the new duplicated end element effectively deleting the originally
// specified index
array.pop();
}
/// Deletes an entry of the provided array
function deleteArrayEntry(uint index, address[] storage array) private {
require(index < array.length);
// copy the last element into the index that we want to delete
// in the case that we want to delete the last element, just skip this
if (index != array.length - 1) {
array[index] = array[array.length - 1];
}
// drop the new duplicated end element effectively deleting the originally
// specified index
array.pop();
}
function getIndexOfId(
Identity memory id,
Identity[] memory array
) private pure returns (uint256) {
for (uint256 i = 0; i < array.length; i++) {
if (identitiesAreEqual(array[i], id)) {
return i;
}
}
revert("id not found");
}
function getIndexOfId(
ExitIdentity memory id,
ExitIdentity[] memory array
) private pure returns (uint256) {
for (uint256 i = 0; i < array.length; i++) {
if (identitiesAreEqual(exitIdToId(array[i]), exitIdToId(id))) {
return i;
}
}
revert("id not found");
}
function getIndexOfAdmin(
address admin,
address[] memory array
) private pure returns (uint256) {
for (uint256 i = 0; i < array.length; i++) {
if (admin == array[i]) {
return i;
}
}
revert("admin not found");
}
/// Checks both the exit and the client lists for any entry with any
/// sort of duplicate ID component
function checkForAnyDuplicates(
Identity memory entry
) public view returns (bool) {
if (state_registeredIps[entry.mesh_ip] == true) {
return true;
}
if (state_registeredKey[entry.wg_key] == true) {
return true;
}
if (state_registeredAddr[entry.eth_addr] == true) {
return true;
}
return false;
}
// start user and exit management functions
// Add a new registered user
function addRegisteredUser(Identity calldata entry) public {
if (isUserAdmin(msg.sender)) {
// if any client or exit currently registered has overlapping data, do not allow the
// registration to continue
if (checkForAnyDuplicates(entry)) {
revert("duplicate");
}
state_registeredIps[entry.mesh_ip] = true;
state_registeredKey[entry.wg_key] = true;
state_registeredAddr[entry.eth_addr] = true;
state_registeredUsers.push(entry);
emit UserRegisteredEvent(entry);
} else {
revert("unauthorized");
}
}
// Utility function that registers users in bulk within a single transaction
function addRegisteredUsersBulk(Identity[] calldata users) public {
for (uint256 i = 0; i < users.length; i++) {
addRegisteredUser(users[i]);
}
}
// Remove a new registered user
function removeRegisteredUser(Identity calldata entry) public {
if (isUserAdmin(msg.sender)) {
uint256 index = getIndexOfId(entry, state_registeredUsers);
deleteArrayEntry(index, state_registeredUsers);
state_registeredAddr[entry.eth_addr] = false;
state_registeredIps[entry.mesh_ip] = false;
state_registeredKey[entry.wg_key] = false;
emit UserRemovedEvent(entry);
} else {
revert("unauthorized");
}
}
// Utility function that removes exits in bulk within a single transaction
function removeRegisteredUsersBulk(Identity[] calldata users) public {
for (uint256 i = 0; i < users.length; i++) {
removeRegisteredUser(users[i]);
}
}
// Add a new registered exit
function addRegisteredExit(ExitIdentity calldata entry) public {
if (isExitAdmin(msg.sender)) {
// if any client or exit currently registered has overlapping data, do not allow the
// registration to continue
if (checkForAnyDuplicates(exitIdToId(entry))) {
revert("duplicate");
}
state_registeredIps[entry.mesh_ip] = true;
state_registeredKey[entry.wg_key] = true;
state_registeredAddr[entry.eth_addr] = true;
state_registeredExits.push(entry);
emit ExitRegisteredEvent(entry);
} else {
revert("unauthorized");
}
}
// Utility function that registers exits in bulk within a single transaction
function addRegisteredExitsBulk(ExitIdentity[] calldata exits) public {
for (uint256 i = 0; i < exits.length; i++) {
addRegisteredExit(exits[i]);
}
}
// Remove a new registered exit
function removeRegisteredExit(ExitIdentity calldata entry) public {
if (isExitAdmin(msg.sender)) {
uint256 index = getIndexOfId(entry, state_registeredExits);
deleteArrayEntry(index, state_registeredExits);
state_registeredAddr[entry.eth_addr] = false;
state_registeredIps[entry.mesh_ip] = false;
state_registeredKey[entry.wg_key] = false;
emit ExitRemovedEvent(entry);
} else {
revert("unauthorized");
}
}
// Utility function that removes exits in bulk within a single transaction
function removeRegisteredExitsBulk(ExitIdentity[] calldata exits) public {
for (uint256 i = 0; i < exits.length; i++) {
removeRegisteredExit(exits[i]);
}
}
// start user query functions
function getAllRegisteredUsers() public view returns (Identity[] memory) {
return state_registeredUsers;
}
function getAllRegisteredExits()
public
view
returns (ExitIdentity[] memory)
{
return state_registeredExits;
}
function getRegisteredClientWithWgKey(
uint256 wg_key
) public view returns (Identity memory) {
for (uint256 i = 0; i < state_registeredUsers.length; i++) {
if (state_registeredUsers[i].wg_key == wg_key) {
return state_registeredUsers[i];
}
}
return getNullIdentity();
}
function getRegisteredClientWithMeshIp(
uint128 mesh_ip
) public view returns (Identity memory) {
for (uint256 i = 0; i < state_registeredUsers.length; i++) {
if (state_registeredUsers[i].mesh_ip == mesh_ip) {
return state_registeredUsers[i];
}
}
return getNullIdentity();
}
function getRegisteredClientWithEthAddr(
address eth_addr
) public view returns (Identity memory) {
for (uint256 i = 0; i < state_registeredUsers.length; i++) {
if (state_registeredUsers[i].eth_addr == eth_addr) {
return state_registeredUsers[i];
}
}
return getNullIdentity();
}
function getRegisteredExitWithWgKey(
uint256 wg_key
) public view returns (ExitIdentity memory) {
for (uint256 i = 0; i < state_registeredExits.length; i++) {
if (state_registeredExits[i].wg_key == wg_key) {
return state_registeredExits[i];
}
}
return getNullExitIdentity();
}
function getRegisteredExitWithMeshIp(
uint128 mesh_ip
) public view returns (ExitIdentity memory) {
for (uint256 i = 0; i < state_registeredExits.length; i++) {
if (state_registeredExits[i].mesh_ip == mesh_ip) {
return state_registeredExits[i];
}
}
return getNullExitIdentity();
}
function getRegisteredExitWithEthAddr(
address eth_addr
) public view returns (ExitIdentity memory) {
for (uint256 i = 0; i < state_registeredExits.length; i++) {
if (state_registeredExits[i].eth_addr == eth_addr) {
return state_registeredExits[i];
}
}
return getNullExitIdentity();
}
// start admin management functions
// Add a new user admin
function addUserAdmin(address entry) public {
if (state_admin == msg.sender) {
if (isUserAdmin(entry)) {
revert("duplicate");
}
state_UserAdmins.push(entry);
emit UserAdminAddedEvent(entry);
} else {
revert("unauthorized");
}
}
// Remove a user admin
function removeUserAdmin(address entry) public {
if (state_admin == msg.sender) {
uint256 index = getIndexOfAdmin(entry, state_UserAdmins);
deleteArrayEntry(index, state_UserAdmins);
emit UserAdminAddedEvent(entry);
} else {
revert("unauthorized");
}
}
// Add a new exit admin
function addExitAdmin(address entry) public {
if (state_admin == msg.sender) {
if (isExitAdmin(entry)) {
revert("duplicate");
}
state_ExitAdmins.push(entry);
emit ExitAdminAddedEvent(entry);
} else {
revert("unauthorized");
}
}
// Remove a exit admin
function removeExitAdmin(address entry) public {
if (state_admin == msg.sender) {
uint256 index = getIndexOfAdmin(entry, state_ExitAdmins);
deleteArrayEntry(index, state_ExitAdmins);
emit UserAdminAddedEvent(entry);
} else {
revert("unauthorized");
}
}
// Query user admin list
function getUserAdminList() public view returns (address[] memory) {
return state_UserAdmins;
}
// Query exit admin list
function getExitAdminList() public view returns (address[] memory) {
return state_ExitAdmins;
}
// Query contract admin
function getStateAdminList() public view returns (address) {
return state_admin;
}
}