eth-address-index/solidity/AddressDeclarator.sol

127 lines
3.8 KiB
Solidity
Raw Normal View History

pragma solidity >=0.8.0;
2020-12-29 16:08:49 +01:00
2023-03-24 11:38:00 +01:00
// SPDX-License-Identifier: AGPL-3.0-or-later
2020-12-29 16:08:49 +01:00
contract AddressDeclarator {
2020-12-29 19:44:14 +01:00
mapping( address => address[] ) declarationIndex;
mapping( bytes32 => uint256 ) declarationContentIndex; // the _latest_ content pointer for the declarator to subject mapping
2020-12-29 19:44:14 +01:00
mapping( address => address[] ) declarator;
mapping( address => address[] ) declaratorReverse;
2020-12-29 17:33:00 +01:00
bytes32[][] contents;
// Implements Declarator
event DeclarationAdded(address indexed _declarator, address indexed _subject, bytes32 indexed _topic, bytes32 _proof);
2021-04-30 17:13:58 +02:00
2023-03-24 09:55:59 +01:00
constructor(bytes32 _initial) {
addDeclaration(address(this), _initial);
2020-12-29 17:33:00 +01:00
}
2023-03-24 09:55:59 +01:00
// Create a digest reference for a declarator and subject pair
2020-12-29 17:33:00 +01:00
function toReference(address _declarator, address _subject) private pure returns ( bytes32 ) {
bytes32 k;
bytes memory addrMaterial = new bytes(40);
2020-12-29 17:33:00 +01:00
bytes memory addrBytes = abi.encodePacked(_declarator);
for (uint256 i = 0; i < 20; i++) {
addrMaterial[i] = addrBytes[i];
2020-12-29 17:33:00 +01:00
}
addrBytes = abi.encodePacked(_subject);
for (uint256 i = 0; i < 20; i++) {
addrMaterial[i+20] = addrBytes[i];
2020-12-29 17:33:00 +01:00
}
k = sha256(addrMaterial);
2020-12-29 17:33:00 +01:00
return k;
}
// Create a digest reference for a declarator and subject pair on top of the given digest
function toReference(address _declarator, address _subject, bytes32 _digest) private pure returns ( bytes32[2] memory ) {
bytes32 k;
bytes32[2] memory ks;
bytes memory signMaterial = new bytes(64);
k = toReference(_declarator, _subject);
for (uint256 i = 0; i < 32; i++) {
signMaterial[i] = k[i];
}
for (uint256 i = 0; i < 32; i++) {
signMaterial[i+32] = _digest[i];
}
ks[0] = k;
ks[1] = sha256(signMaterial);
return ks;
}
2021-04-30 17:13:58 +02:00
// Implements Declarator
2020-12-29 17:33:00 +01:00
function declaratorCount(address _subject) public view returns ( uint256 ) {
return declarator[_subject].length;
}
2021-04-30 17:13:58 +02:00
// Implements Declarator
2020-12-29 19:44:14 +01:00
function declaratorAddressAt(address _subject, uint256 _idx) public view returns ( address ) {
return declarator[_subject][_idx];
}
2023-03-24 09:55:59 +01:00
// Implements Declarator
function addDeclaration(address _subject, bytes32 _proof, bytes32 _topic) public returns ( bool ) {
bytes32[2] memory ks;
bytes32[] memory declarationContents;
uint256 idx;
ks = toReference(tx.origin, _subject, _topic);
idx = declarationContentIndex[ks[1]];
if (idx == 0) { // This also works for the constructor :)
declarator[_subject].push(tx.origin);
contents.push(declarationContents);
declarationIndex[tx.origin].push(_subject);
idx = contents.length-1;
}
declarationContentIndex[ks[1]] = idx;
contents[idx].push(_proof);
return true;
}
2021-04-30 17:13:58 +02:00
// Implements Declarator
2020-12-29 17:33:00 +01:00
function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) {
return addDeclaration(_subject, _proof, bytes32(0));
2020-12-29 17:33:00 +01:00
}
2021-04-30 17:13:58 +02:00
// Implements Declarator
2020-12-29 17:33:00 +01:00
function declaration(address _declarator, address _subject) public view returns ( bytes32[] memory ) {
return declaration(_declarator, _subject, bytes32(0));
2020-12-29 19:44:14 +01:00
}
2023-03-24 09:55:59 +01:00
// Implements Declarator
function declaration(address _declarator, address _subject, bytes32 _topic) public view returns ( bytes32[] memory ) {
bytes32[2] memory k;
uint256 idx;
k = toReference(_declarator, _subject, _topic);
idx = declarationContentIndex[k[1]];
return contents[idx];
}
2021-04-30 17:13:58 +02:00
// Implements Declarator
2020-12-29 19:44:14 +01:00
function declarationCount(address _declarator) public view returns ( uint256 ) {
return declarationIndex[_declarator].length;
}
2021-04-30 17:13:58 +02:00
// Implements Declarator
2020-12-29 19:44:14 +01:00
function declarationAddressAt(address _declarator, uint256 _idx) public view returns ( address ) {
return declarationIndex[_declarator][_idx];
2020-12-29 17:33:00 +01:00
}
2021-04-30 17:22:32 +02:00
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0x21b7493b) { // Implements Declarator
2021-04-30 17:22:32 +02:00
return true;
}
if (_sum == 0x01ffc9a7) { // EIP165
return true;
}
return false;
2021-04-30 17:22:32 +02:00
}
2020-12-29 16:08:49 +01:00
}