okota/solidity/AccountsIndexAddressDeclarator.sol

119 lines
2.9 KiB
Solidity
Raw Normal View History

2021-10-04 16:32:39 +02:00
pragma solidity >0.6.11;
// SPDX-License-Identifier: GPL-3.0-or-later
2021-10-20 14:49:52 +02:00
// TODO: inherit accounts index contract
2021-10-04 16:32:39 +02:00
contract AccountsIndexAddressDeclarator {
address public tokenAddress;
2021-11-13 07:13:27 +01:00
bytes32 public tokenAddressHash;
2021-10-04 16:32:39 +02:00
address public addressDeclaratorAddress;
mapping(address => uint256) entryIndex;
2021-10-20 14:49:52 +02:00
mapping(address => bool) writers;
address[] entries;
2021-10-04 16:32:39 +02:00
address public owner;
address newOwner;
2021-11-13 07:13:27 +01:00
event AddressAdded(address indexed addedAccount, uint256 indexed accountIndex); // AddressIndex
2021-10-04 16:32:39 +02:00
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
constructor(address _tokenAddress, address _addressDeclaratorAddress) public {
bytes memory _tokenAddressPadded;
owner = msg.sender;
addressDeclaratorAddress = _addressDeclaratorAddress;
tokenAddress = _tokenAddress;
_tokenAddressPadded = abi.encode(tokenAddress);
tokenAddressHash = sha256(_tokenAddressPadded);
2021-10-20 14:49:52 +02:00
entries.push(address(0));
2021-10-04 16:32:39 +02:00
}
function add(address _account) external returns (bool) {
bool ok;
bytes memory r;
uint256 oldEntryIndex;
2021-10-20 14:49:52 +02:00
require(writers[msg.sender]);
require(entryIndex[_account] == 0);
2021-10-04 16:32:39 +02:00
(ok, r) = addressDeclaratorAddress.call(abi.encodeWithSignature("addDeclaration(address,bytes32)", _account, tokenAddressHash));
require(ok);
require(r[31] == 0x01);
2021-11-13 07:13:27 +01:00
oldEntryIndex = entries.length;
2021-10-04 16:32:39 +02:00
entryIndex[_account] = oldEntryIndex;
2021-10-20 14:49:52 +02:00
entries.push(_account);
2021-10-04 16:32:39 +02:00
emit AddressAdded(_account, oldEntryIndex);
return true;
}
2021-10-20 14:49:52 +02:00
// Implements AccountsIndex
2021-10-04 16:32:39 +02:00
function have(address _account) external view returns (bool) {
return entryIndex[_account] > 0;
}
2021-10-20 14:49:52 +02:00
// Implements AccountsIndex
function entry(uint256 _idx) public returns (address) {
return entries[_idx+1];
}
// Implements AccountsIndex
function entryCount() public returns (uint256) {
return entries.length - 1;
}
2021-11-13 07:13:27 +01:00
// Implements Writer
2021-10-20 14:49:52 +02:00
function addWriter(address _writer) public returns (bool) {
require(owner == msg.sender);
writers[_writer] = true;
return true;
}
2021-11-13 07:13:27 +01:00
// Implements Writer
2021-10-20 14:49:52 +02:00
function deleteWriter(address _writer) public returns (bool) {
require(owner == msg.sender);
delete writers[_writer];
return true;
}
// Implements EIP173
function transferOwnership(address _newOwner) public returns (bool) {
require(msg.sender == owner);
newOwner = _newOwner;
return true;
}
// Implements OwnedAccepter
function acceptOwnership() public returns (bool) {
address oldOwner;
require(msg.sender == newOwner);
oldOwner = owner;
owner = newOwner;
newOwner = address(0);
emit OwnershipTransferred(oldOwner, owner);
return true;
}
2021-11-13 07:13:27 +01:00
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0xcbdb05c7) { // AccountsIndex
return true;
}
if (_sum == 0x01ffc9a7) { // EIP165
return true;
}
if (_sum == 0x9493f8b2) { // EIP173
return true;
}
if (_sum == 0x37a47be4) { // OwnedAccepter
return true;
}
if (_sum == 0x80c84bd6) { // Writer
return true;
}
return false;
}
2021-10-04 16:32:39 +02:00
}