Fix address declarator executables

This commit is contained in:
nolash
2020-12-29 19:44:14 +01:00
parent 4400240b3e
commit d659ae72f5
24 changed files with 627 additions and 92 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1 +1 @@
[{"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"}]
[{"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":"_declarator","type":"address"},{"internalType":"uint256","name":"_idx","type":"uint256"}],"name":"declarationAddressAt","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_declarator","type":"address"}],"name":"declarationCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"},{"internalType":"uint256","name":"_idx","type":"uint256"}],"name":"declaratorAddressAt","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

@@ -7,8 +7,10 @@ contract AddressDeclarator {
// EIP 173
address public owner;
mapping( bytes32 => uint256 ) declarations;
mapping( address => address[] ) public declarator;
mapping( address => address[] ) declarationIndex;
mapping( bytes32 => uint256 ) declarationContentIndex;
mapping( address => address[] ) declarator;
mapping( address => address[] ) declaratorReverse;
bytes32[][] public contents;
constructor(bytes32 _initialDescription) {
@@ -50,28 +52,41 @@ contract AddressDeclarator {
return declarator[_subject].length;
}
function declaratorAddressAt(address _subject, uint256 _idx) public view returns ( address ) {
return declarator[_subject][_idx];
}
function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) {
bytes32 k;
bytes32[] memory declarationContents;
uint256 declarationsIndex;
uint256 idx;
k = toReference(msg.sender, _subject);
declarationsIndex = declarations[k];
if (declarationsIndex == 0) { // This also works for the constructor :)
idx = declarationContentIndex[k];
if (idx == 0) { // This also works for the constructor :)
declarator[_subject].push(msg.sender);
contents.push(declarationContents); //= contents[declarationsIndex],
contents.push(declarationContents); //= contents[idx],
declarationIndex[msg.sender].push(_subject);
}
declarationsIndex = contents.length-1;
declarations[k] = declarationsIndex;
contents[declarationsIndex].push(_proof);
idx = contents.length-1;
declarationContentIndex[k] = idx;
contents[idx].push(_proof);
return true;
}
function declaration(address _declarator, address _subject) public view returns ( bytes32[] memory ) {
bytes32 k;
uint256 i;
uint256 idx;
k = toReference(_declarator, _subject);
i = declarations[k];
return contents[i];
idx = declarationContentIndex[k];
return contents[idx];
}
function declarationCount(address _declarator) public view returns ( uint256 ) {
return declarationIndex[_declarator].length;
}
function declarationAddressAt(address _declarator, uint256 _idx) public view returns ( address ) {
return declarationIndex[_declarator][_idx];
}
}

View File

@@ -2,6 +2,10 @@ all:
solc AddressDeclarator.sol --abi | awk 'NR>3' > AddressDeclarator.json
solc AddressDeclarator.sol --bin | awk 'NR>3' > AddressDeclarator.bin
truncate -s -1 AddressDeclarator.bin
solc TokenUniqueSymbolIndex.sol --abi | awk 'NR>3' > TokenUniqueSymbolIndex.json
solc TokenUniqueSymbolIndex.sol --bin | awk 'NR>3' > TokenUniqueSymbolIndex.bin
truncate -s -1 TokenUniqueSymbolIndex.bin
old:
solc TokenEndorser.sol --abi | awk 'NR>3' > TokenEndorser.json
@@ -9,6 +13,7 @@ old:
truncate -s -1 TokenEndorser.bin
test: all
python test.py
#python test.py
python test_tokenindex.py
.PHONY: test

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"bytes32","name":"_key","type":"bytes32"}],"name":"addressOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_key","type":"bytes32"},{"internalType":"address","name":"_token","type":"address"}],"name":"register","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"registry","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"registryCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]

View File

@@ -0,0 +1,55 @@
pragma solidity >=0.6.12;
// SPDX-License-Identifier: GPL-3.0-or-later
contract TokenUniqueSymbolIndex {
// EIP 173
address public owner;
mapping ( bytes32 => uint256 ) public registry;
address[] public tokens;
constructor() {
owner = msg.sender;
tokens.push(address(0));
}
// EIP 172
function transferOwnership() public {
revert("owner cannot be changed");
}
function addressOf(bytes32 _key) public view returns ( address ) {
uint256 idx;
idx = registry[_key];
return tokens[idx];
}
function register(bytes32 _key, address _token) public returns (bool) {
require(msg.sender == owner);
bytes memory token_symbol;
bytes32 token_symbol_key;
uint256 idx;
(bool _ok, bytes memory _r) = _token.call(abi.encodeWithSignature('symbol()'));
require(_ok);
token_symbol = abi.decode(_r, (bytes));
token_symbol_key = sha256(token_symbol);
require(_key == token_symbol_key);
idx = registry[token_symbol_key];
require(idx == 0);
registry[token_symbol_key] = tokens.length;
tokens.push(_token);
return true;
}
function registryCount() public view returns ( uint256 ) {
return tokens.length;
}
}

View File

@@ -35,6 +35,7 @@ c = w3.eth.contract(abi=abi, bytecode=bytecode)
declarations = [
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
]
@@ -48,7 +49,7 @@ c = w3.eth.contract(abi=abi, address=r.contractAddress)
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 1
r = c.functions.declarator(w3.eth.accounts[0], 0).call()
r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 0).call()
assert r == w3.eth.accounts[0]
r = c.functions.declaration(w3.eth.accounts[0], w3.eth.accounts[0]).call()
@@ -61,7 +62,7 @@ c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][0]).transact({'fr
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
r = c.functions.declarator(w3.eth.accounts[0], 1).call()
r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 1).call()
assert r == w3.eth.accounts[2]
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
@@ -89,9 +90,45 @@ assert r == 2
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
assert r == 1
r = c.functions.declarator(w3.eth.accounts[1], 0).call()
r = c.functions.declaratorAddressAt(w3.eth.accounts[1], 0).call()
assert r == w3.eth.accounts[2]
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[1]).call()
assert r[0].hex() == declarations[2][0][2:]
# Add declaration for 0 by 3
c.functions.addDeclaration(w3.eth.accounts[0], declarations[3][0]).transact({'from': w3.eth.accounts[3]})
# 0 declared itself and 1
r = c.functions.declarationCount(w3.eth.accounts[0]).call()
assert r == 1
# 0 was declared by itself, 1 and 3
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 3
# 1 declared noone
r = c.functions.declarationCount(w3.eth.accounts[1]).call()
assert r == 0
# 1 was declared by 2
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
assert r == 1
# 2 declared 0 and 1
r = c.functions.declarationCount(w3.eth.accounts[2]).call()
assert r == 2
# 2 was declared by noone
r = c.functions.declaratorCount(w3.eth.accounts[2]).call()
assert r == 0
# 3 declared 0
r = c.functions.declarationCount(w3.eth.accounts[3]).call()
assert r == 1
# 3 was declared by noone
r = c.functions.declaratorCount(w3.eth.accounts[3]).call()
assert r == 0

View File

@@ -0,0 +1,134 @@
import logging
import os
import json
import hashlib
import web3
import eth_tester
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
'gas_limit': 9000000,
})
backend = eth_tester.PyEVMBackend(eth_params)
instance = eth_tester.EthereumTester(backend)
provider = web3.Web3.EthereumTesterProvider(instance)
w3 = web3.Web3(provider)
f = open('AddressDeclarator.bin', 'r')
bytecode = f.read()
f.close()
f = open('AddressDeclarator.json', 'r')
abi = json.load(f)
f.close()
#token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
c = w3.eth.contract(abi=abi, bytecode=bytecode)
#tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
declarations = [
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
['0x' + os.urandom(32).hex(), '0x' + os.urandom(32).hex()],
]
# Deployment is a self-signed declaration
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))
c = w3.eth.contract(abi=abi, address=r.contractAddress)
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 1
r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 0).call()
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:]
# Add first declaration for 0 by 2
c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][0]).transact({'from': w3.eth.accounts[2]})
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
r = c.functions.declaratorAddressAt(w3.eth.accounts[0], 1).call()
assert r == w3.eth.accounts[2]
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
assert r[0].hex() == declarations[1][0][2:]
# Add second declaration for 0 by 2
c.functions.addDeclaration(w3.eth.accounts[0], declarations[1][1]).transact({'from': w3.eth.accounts[2]})
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[0]).call()
assert r[0].hex() == declarations[1][0][2:]
assert r[1].hex() == declarations[1][1][2:]
# Add first declaration for 1 by 2
c.functions.addDeclaration(w3.eth.accounts[1], declarations[2][0]).transact({'from': w3.eth.accounts[2]})
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 2
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
assert r == 1
r = c.functions.declaratorAddressAt(w3.eth.accounts[1], 0).call()
assert r == w3.eth.accounts[2]
r = c.functions.declaration(w3.eth.accounts[2], w3.eth.accounts[1]).call()
assert r[0].hex() == declarations[2][0][2:]
# Add declaration for 0 by 3
c.functions.addDeclaration(w3.eth.accounts[0], declarations[3][0]).transact({'from': w3.eth.accounts[3]})
# 0 declared itself and 1
r = c.functions.declarationCount(w3.eth.accounts[0]).call()
assert r == 1
# 0 was declared by itself, 1 and 3
r = c.functions.declaratorCount(w3.eth.accounts[0]).call()
assert r == 3
# 1 declared noone
r = c.functions.declarationCount(w3.eth.accounts[1]).call()
assert r == 0
# 1 was declared by 2
r = c.functions.declaratorCount(w3.eth.accounts[1]).call()
assert r == 1
# 2 declared 0 and 1
r = c.functions.declarationCount(w3.eth.accounts[2]).call()
assert r == 2
# 2 was declared by noone
r = c.functions.declaratorCount(w3.eth.accounts[2]).call()
assert r == 0
# 3 declared 0
r = c.functions.declarationCount(w3.eth.accounts[3]).call()
assert r == 1
# 3 was declared by noone
r = c.functions.declaratorCount(w3.eth.accounts[3]).call()
assert r == 0

View File

@@ -0,0 +1,54 @@
import logging
import os
import json
import hashlib
import web3
import eth_tester
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
'gas_limit': 9000000,
})
backend = eth_tester.PyEVMBackend(eth_params)
instance = eth_tester.EthereumTester(backend)
provider = web3.Web3.EthereumTesterProvider(instance)
w3 = web3.Web3(provider)
f = open('TokenUniqueSymbolIndex.bin', 'r')
bytecode = f.read()
f.close()
f = open('TokenUniqueSymbolIndex.json', 'r')
abi = json.load(f)
f.close()
token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
c = w3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = c.constructor().transact({'from': w3.eth.accounts[0]})
r = w3.eth.getTransactionReceipt(tx_hash)
logg.debug('contract {}'.format(r.contractAddress))
c = w3.eth.contract(abi=abi, address=r.contractAddress)
d = '0x' + os.urandom(32).hex()
# Initial token will fail in any case
h =hashlib.new('sha256')
h.update('FOO'.encode('utf-8'))
z = h.digest()
foo_symbol = z.hex()
fail = False
try:
c.functions.register(foo_symbol, token_address).transact({'from':w3.eth.accounts[0]})
except:
fail = True
pass
if not fail:
raise RuntimeError('expected fail on register same token to same address')