2023-03-25 13:52:59 +01:00
|
|
|
pragma solidity >=0.8.0;
|
2020-11-13 18:33:39 +01:00
|
|
|
|
2023-03-25 13:52:59 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// File-Version: 3
|
2020-11-13 18:33:39 +01:00
|
|
|
|
2023-03-25 13:52:59 +01:00
|
|
|
contract AccountsIndex {
|
2023-02-12 08:51:58 +01:00
|
|
|
uint256 constant blockedField = 1 << 128;
|
|
|
|
address[] entryList;
|
2023-03-25 13:52:59 +01:00
|
|
|
mapping(address => uint256) entryIndex;
|
|
|
|
|
|
|
|
// Implements Writer
|
2024-04-15 12:23:34 +02:00
|
|
|
mapping(address => bool) public writers;
|
2023-03-25 13:52:59 +01:00
|
|
|
|
|
|
|
// Implements ERC173
|
2021-04-30 11:36:56 +02:00
|
|
|
address public owner;
|
2020-11-13 18:33:39 +01:00
|
|
|
|
2023-03-25 13:52:59 +01:00
|
|
|
// Implements AccountsIndex
|
|
|
|
event AddressAdded(address _account); // AccountsIndex
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndexMutable
|
2023-03-25 13:52:59 +01:00
|
|
|
event AddressActive(address indexed _account, bool _active);
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndexMutable
|
2023-03-25 13:52:59 +01:00
|
|
|
event AddressRemoved(address _account);
|
|
|
|
// Implements ERC173
|
2021-04-30 11:36:56 +02:00
|
|
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
|
2023-03-25 13:52:59 +01:00
|
|
|
// Implements Writer
|
|
|
|
event WriterAdded(address _account); // AccountsIndex
|
|
|
|
// Implements Writer
|
|
|
|
event WriterDeleted(address _account);
|
2020-11-13 18:33:39 +01:00
|
|
|
|
2023-03-17 22:35:12 +01:00
|
|
|
constructor() {
|
2020-11-13 18:33:39 +01:00
|
|
|
owner = msg.sender;
|
2023-02-12 08:51:58 +01:00
|
|
|
entryList.push(address(0));
|
2023-02-12 07:07:14 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndex
|
2023-03-17 22:35:12 +01:00
|
|
|
function entryCount() external view returns (uint256) {
|
2023-02-12 08:51:58 +01:00
|
|
|
return entryList.length - 1;
|
2020-11-13 18:33:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements Writer
|
2020-12-07 16:41:48 +01:00
|
|
|
function addWriter(address _writer) public returns (bool) {
|
2023-03-26 09:12:50 +02:00
|
|
|
require(owner == msg.sender, 'ERR_AXX');
|
2024-04-15 12:23:34 +02:00
|
|
|
writers[_writer] = true;
|
2023-03-25 13:52:59 +01:00
|
|
|
emit WriterAdded(_writer);
|
2020-12-07 16:41:48 +01:00
|
|
|
return true;
|
2020-11-13 18:33:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements Writer
|
2020-12-07 16:41:48 +01:00
|
|
|
function deleteWriter(address _writer) public returns (bool) {
|
2023-03-26 09:12:50 +02:00
|
|
|
require(owner == msg.sender, 'ERR_AXX');
|
2024-04-15 12:23:34 +02:00
|
|
|
delete writers[_writer];
|
2023-03-25 13:52:59 +01:00
|
|
|
emit WriterDeleted(_writer);
|
2020-12-07 16:41:48 +01:00
|
|
|
return true;
|
2020-11-13 18:33:39 +01:00
|
|
|
}
|
|
|
|
|
2024-04-15 12:23:34 +02:00
|
|
|
// Implements Writer
|
|
|
|
function isWriter(address _writer) public view returns (bool) {
|
|
|
|
return writers[_writer] || _writer == owner;
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
// Implements AccountsIndex
|
2020-12-07 16:41:48 +01:00
|
|
|
function add(address _account) external returns (bool) {
|
2023-02-12 06:47:47 +01:00
|
|
|
uint256 i;
|
2023-02-12 08:51:58 +01:00
|
|
|
uint256 _entry;
|
2023-02-12 06:47:47 +01:00
|
|
|
|
2024-04-15 12:23:34 +02:00
|
|
|
require(isWriter(msg.sender));
|
2021-04-30 11:36:56 +02:00
|
|
|
require(entryIndex[_account] == 0);
|
2023-02-12 08:51:58 +01:00
|
|
|
require(entryList.length < (1 << 64));
|
|
|
|
i = entryList.length;
|
|
|
|
entryList.push(_account);
|
|
|
|
_entry = uint64(i);
|
|
|
|
_entry |= block.timestamp << 64;
|
|
|
|
entryIndex[_account] = _entry;
|
2023-03-25 13:52:59 +01:00
|
|
|
emit AddressAdded(_account);
|
2020-12-07 16:41:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndexMutable
|
2023-02-12 08:51:58 +01:00
|
|
|
function remove(address _account) external returns (bool) {
|
|
|
|
uint256 i;
|
|
|
|
uint256 l;
|
|
|
|
|
2024-04-15 12:23:34 +02:00
|
|
|
require(isWriter(msg.sender), 'ERR_AXX');
|
2023-03-26 09:12:50 +02:00
|
|
|
require(this.have(_account), 'ERR_ACL');
|
2023-02-12 08:51:58 +01:00
|
|
|
|
|
|
|
l = entryList.length - 1;
|
|
|
|
|
|
|
|
i = entryIndex[_account];
|
|
|
|
if (i < l) {
|
|
|
|
entryList[i] = entryList[l];
|
|
|
|
}
|
|
|
|
entryList.pop();
|
|
|
|
entryIndex[_account] = 0;
|
2023-03-25 13:52:59 +01:00
|
|
|
emit AddressRemoved(_account);
|
2023-02-12 08:51:58 +01:00
|
|
|
return true;
|
2023-02-12 06:47:47 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndexMutable
|
2023-02-12 06:47:47 +01:00
|
|
|
function activate(address _account) external returns (bool) {
|
2024-04-15 12:23:34 +02:00
|
|
|
require(isWriter(msg.sender), 'ERR_AXX');
|
2023-03-26 09:12:50 +02:00
|
|
|
require(entryIndex[_account] > 0, 'ERR_NOT_FOUND');
|
|
|
|
require(entryIndex[_account] & blockedField == blockedField, 'ERR_NOT_BLOCKED');
|
2023-02-12 08:51:58 +01:00
|
|
|
entryIndex[_account] >>= 129;
|
2023-03-25 13:52:59 +01:00
|
|
|
emit AddressActive(_account, true);
|
2023-03-17 22:35:12 +01:00
|
|
|
return true;
|
2023-02-12 06:47:47 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndexMutable
|
2023-02-12 08:51:58 +01:00
|
|
|
function deactivate(address _account) external returns (bool) {
|
2024-04-15 12:23:34 +02:00
|
|
|
require(isWriter(msg.sender));
|
2023-03-26 09:12:50 +02:00
|
|
|
require(entryIndex[_account] > 0, 'ERR_NOT_FOUND');
|
|
|
|
require(entryIndex[_account] & blockedField == 0, 'ERR_NOT_ACTIVE');
|
2023-02-12 08:51:58 +01:00
|
|
|
entryIndex[_account] <<= 129;
|
|
|
|
entryIndex[_account] |= blockedField;
|
2023-03-25 13:52:59 +01:00
|
|
|
emit AddressActive(_account, false);
|
2023-02-12 08:51:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements AccountsIndex
|
2023-03-17 22:35:12 +01:00
|
|
|
function entry(uint256 _i) external view returns (address) {
|
2023-02-12 08:51:58 +01:00
|
|
|
return entryList[_i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements AccountsIndex
|
2023-03-17 22:35:12 +01:00
|
|
|
function time(address _account) external view returns (uint256) {
|
2023-02-12 08:51:58 +01:00
|
|
|
require(entryIndex[_account] > 0);
|
|
|
|
return entryIndex[_account] >> 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
// Implements AccountsIndex
|
2023-02-05 05:54:50 +01:00
|
|
|
function have(address _account) external view returns (bool) {
|
|
|
|
return entryIndex[_account] > 0;
|
2021-04-30 11:36:56 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 14:15:50 +01:00
|
|
|
// Implements AccountsIndexMutable
|
2023-02-12 06:47:47 +01:00
|
|
|
function isActive(address _account) external view returns (bool) {
|
2023-02-12 07:07:14 +01:00
|
|
|
return this.have(_account) && entryIndex[_account] & blockedField != blockedField;
|
2023-02-12 06:47:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
// Implements EIP173
|
2021-04-30 11:36:56 +02:00
|
|
|
function transferOwnership(address _newOwner) public returns (bool) {
|
2023-02-12 06:47:47 +01:00
|
|
|
address oldOwner;
|
2023-03-26 09:12:50 +02:00
|
|
|
require(msg.sender == owner, 'ERR_AXX');
|
2021-04-30 11:36:56 +02:00
|
|
|
|
2023-02-12 06:47:47 +01:00
|
|
|
oldOwner = owner;
|
|
|
|
owner = _newOwner;
|
2021-04-30 11:36:56 +02:00
|
|
|
|
|
|
|
emit OwnershipTransferred(oldOwner, owner);
|
2023-03-17 22:35:12 +01:00
|
|
|
return true;
|
2021-04-30 11:36:56 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
// Implements EIP165
|
2021-04-30 11:36:56 +02:00
|
|
|
function supportsInterface(bytes4 _sum) public pure returns (bool) {
|
2023-03-25 14:15:50 +01:00
|
|
|
if (_sum == 0xb7bca625) { // AccountsIndex
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_sum == 0x9479f0ae) { // AccountsIndexMutable
|
2021-04-30 11:36:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_sum == 0x01ffc9a7) { // EIP165
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_sum == 0x9493f8b2) { // EIP173
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-25 13:52:59 +01:00
|
|
|
if (_sum == 0xabe1f1f5) { // Writer
|
2021-10-24 15:25:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-30 11:36:56 +02:00
|
|
|
return false;
|
2020-11-13 18:33:39 +01:00
|
|
|
}
|
|
|
|
}
|