eth-contract-registry/solidity/Registry.sol

87 lines
2.0 KiB
Solidity
Raw Normal View History

2023-03-25 13:56:46 +01:00
pragma solidity >=0.8.0;
// Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
2023-03-25 09:13:49 +01:00
// SPDX-License-Identifier: AGPL-3.0-or-later
2023-03-25 13:56:46 +01:00
// File-version: 4
2023-03-25 09:13:49 +01:00
// Description: Keyed smart contract registry
2023-03-25 09:13:49 +01:00
contract ContractRegistry {
2023-03-25 13:56:46 +01:00
mapping (bytes32 => address) entries;
2023-03-21 14:44:54 +01:00
// Implements ERC173
address public owner;
2023-03-25 13:56:46 +01:00
// Implements Registry
bytes32[] public identifier;
2023-03-21 14:44:54 +01:00
// Implements ERC173
event OwnershipTransferred(address indexed _previousOwner, address indexed _newOwner);
2023-03-25 15:38:19 +01:00
// Implements Registry
event AddressKey(bytes32 indexed _key, address _address);
2023-03-17 23:10:56 +01:00
constructor(bytes32[] memory _identifiers) {
owner = msg.sender;
for (uint i = 0; i < _identifiers.length; i++) {
identifier.push(_identifiers[i]);
}
}
2023-03-25 15:45:46 +01:00
// Assign address to identifier
2021-11-14 05:23:44 +01:00
function set(bytes32 _identifier, address _address) public returns (bool) {
require(msg.sender == owner);
require(entries[_identifier] == address(0));
2021-11-14 05:23:44 +01:00
require(_address != address(0));
bool found = false;
for (uint i = 0; i < identifier.length; i++) {
if (identifier[i] == _identifier) {
found = true;
}
}
require(found);
entries[_identifier] = _address;
2023-03-25 15:38:19 +01:00
emit AddressKey(_identifier, _address);
return true;
}
// Implements EIP 173
function transferOwnership(address _newOwner) public returns (bool) {
2023-03-21 14:44:54 +01:00
address _oldOwner;
require(msg.sender == owner, 'ERR_AXX');
2023-03-21 14:44:54 +01:00
_oldOwner = owner;
owner = _newOwner;
2023-03-21 14:44:54 +01:00
emit OwnershipTransferred(_oldOwner, _newOwner);
return true;
}
2023-03-25 13:56:46 +01:00
// Implements Registry
function addressOf(bytes32 _identifier) public view returns (address) {
return entries[_identifier];
}
2023-03-25 13:56:46 +01:00
// Implements Registry
2023-03-25 09:13:49 +01:00
function identifierCount() public view returns(uint256) {
return identifier.length;
2023-03-25 09:13:49 +01:00
}
2023-03-21 14:44:54 +01:00
// Implements ERC165
2021-04-30 13:11:22 +02:00
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0xeffbf671) { // Registry
2021-04-30 13:11:22 +02:00
return true;
}
2023-03-21 14:44:54 +01:00
if (_sum == 0x01ffc9a7) { // ERC165
2021-04-30 13:11:22 +02:00
return true;
}
2023-03-21 14:44:54 +01:00
if (_sum == 0x9493f8b2) { // ERC173
2021-04-30 13:11:22 +02:00
return true;
}
return false;
}
}