Simplify new implementation

This commit is contained in:
nolash 2020-12-29 17:33:00 +01:00
parent 0b76e366b5
commit 4400240b3e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 77 additions and 47 deletions

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
[{"inputs":[{"internalType":"bytes32[]","name":"_descriptions","type":"bytes32[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"},{"internalType":"bytes32","name":"_proof","type":"bytes32"}],"name":"addDeclaration","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_declarator","type":"address"},{"internalType":"address","name":"_target","type":"address"}],"name":"declaration","outputs":[{"internalType":"bytes32[]","name":"","type":"bytes32[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_target","type":"address"},{"internalType":"uint256","name":"_idx","type":"uint256"}],"name":"declarator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"declaratorCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
[{"inputs":[{"internalType":"bytes32","name":"_initialDescription","type":"bytes32"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"},{"internalType":"bytes32","name":"_proof","type":"bytes32"}],"name":"addDeclaration","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"contents","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_declarator","type":"address"},{"internalType":"address","name":"_subject","type":"address"}],"name":"declaration","outputs":[{"internalType":"bytes32[]","name":"","type":"bytes32[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"declarator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"declaratorCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceID","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]

View File

@ -4,48 +4,74 @@ pragma solidity >=0.6.12;
contract AddressDeclarator {
mapping( bytes32 => uint256[] )
//mapping( address => uint256[] ) declaratorItemsIndex;
//mapping( address => uint256 ) public declaratorCount;
//mapping( address => mapping ( address => declaratorItem ) ) declarationByDeclaratorIndex;
bytes32[][] contents;
// EIP 173
address public owner;
// constructor(bytes32[] memory _descriptions) {
// for (uint i; i < _descriptions.length; i++) {
// addDeclaration(msg.sender, _descriptions[i]);
// }
// }
//
// function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) {
// declaratorItem storage item;
//
// item = declarationByDeclaratorIndex[msg.sender][_subject];
// if (item.signer == address(0)) {
// item.signer = msg.sender;
// declaratorItemsIndex[_subject].push(declaratorItems.length);
// declaratorItems.push(item);
// declaratorCount[_subject]++;
// }
// item.content.push(_proof);
//
// return true;
// }
//
// function declarator(address _target, uint256 _idx) public view returns ( address ) {
// uint256 idx;
// declaratorItem storage item;
//
// idx = declaratorItemsIndex[_target][_idx];
// item = declaratorItems[idx];
//
// return item.signer;
// }
//
// function declaration(address _declarator, address _target) public view returns ( bytes32[] memory ) {
// declaratorItem storage item;
//
// item = declarationByDeclaratorIndex[_declarator][_target];
//
// return item.content;
// }
mapping( bytes32 => uint256 ) declarations;
mapping( address => address[] ) public declarator;
bytes32[][] public contents;
constructor(bytes32 _initialDescription) {
bytes32[] memory foundation;
owner = msg.sender;
contents.push(foundation);
contents[contents.length-1].push(blockhash(block.number));
addDeclaration(msg.sender, _initialDescription);
}
// EIP 172
function transferOwnership() public {
revert("owner cannot be changed");
}
// EIP-165
function supportsInterface(bytes4 interfaceID) public view returns ( bool ) {
return false;
}
function toReference(address _declarator, address _subject) private pure returns ( bytes32 ) {
bytes32 k;
bytes memory signMaterial = new bytes(40);
bytes memory addrBytes = abi.encodePacked(_declarator);
for (uint256 i = 0; i < 20; i++) {
signMaterial[i] = addrBytes[i];
}
addrBytes = abi.encodePacked(_subject);
for (uint256 i = 0; i < 20; i++) {
signMaterial[i+20] = addrBytes[i];
}
k = sha256(signMaterial);
return k;
}
function declaratorCount(address _subject) public view returns ( uint256 ) {
return declarator[_subject].length;
}
function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) {
bytes32 k;
bytes32[] memory declarationContents;
uint256 declarationsIndex;
k = toReference(msg.sender, _subject);
declarationsIndex = declarations[k];
if (declarationsIndex == 0) { // This also works for the constructor :)
declarator[_subject].push(msg.sender);
contents.push(declarationContents); //= contents[declarationsIndex],
}
declarationsIndex = contents.length-1;
declarations[k] = declarationsIndex;
contents[declarationsIndex].push(_proof);
return true;
}
function declaration(address _declarator, address _subject) public view returns ( bytes32[] memory ) {
bytes32 k;
uint256 i;
k = toReference(_declarator, _subject);
i = declarations[k];
return contents[i];
}
}

View File

@ -3,6 +3,11 @@ all:
solc AddressDeclarator.sol --bin | awk 'NR>3' > AddressDeclarator.bin
truncate -s -1 AddressDeclarator.bin
old:
solc TokenEndorser.sol --abi | awk 'NR>3' > TokenEndorser.json
solc TokenEndorser.sol --bin | awk 'NR>3' > TokenEndorser.bin
truncate -s -1 TokenEndorser.bin
test: all
python test.py

View File

@ -39,7 +39,7 @@ declarations = [
]
# Deployment is a self-signed declaration
tx_hash = c.constructor(declarations[0]).transact({'from': w3.eth.accounts[0]})
tx_hash = c.constructor(declarations[0][0]).transact({'from': w3.eth.accounts[0]})
r = w3.eth.getTransactionReceipt(tx_hash)
logg.debug('contract {}'.format(r.contractAddress))
@ -53,7 +53,6 @@ assert r == w3.eth.accounts[0]
r = c.functions.declaration(w3.eth.accounts[0], w3.eth.accounts[0]).call()
assert r[0].hex() == declarations[0][0][2:]
assert r[1].hex() == declarations[0][1][2:]
# Add first declaration for 0 by 2