Initial commit

This commit is contained in:
nolash
2020-12-21 23:00:45 +01:00
commit ffb4fdee12
11 changed files with 254 additions and 0 deletions

9
solidity/Makefile Normal file
View File

@@ -0,0 +1,9 @@
all:
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
.PHONY: test

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":true,"internalType":"address","name":"_adder","type":"address"},{"indexed":true,"internalType":"uint256","name":"_index","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"EndorsementAdded","type":"event"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"endorsement","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"endorsers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokenIndex","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"}]

View File

@@ -0,0 +1,49 @@
pragma solidity >=0.6.12;
// SPDX-License-Identifier: GPL-3.0-or-later
contract TokenEndorsement {
uint256 count;
mapping ( bytes32 => bytes32 ) public endorsement;
mapping ( address => uint256 ) public tokenIndex;
mapping ( address => uint256[] ) public endorsers;
address[] public tokens;
event EndorsementAdded(address indexed _token, address indexed _adder, uint256 indexed _index, bytes32 _data);
constructor() {
count = 1;
tokens.push(address(0x0));
}
function register(address _token) private returns (bool) {
if (tokenIndex[_token] > 0) {
return false;
}
tokens.push(_token);
tokenIndex[_token] = count;
count++;
return true;
}
function add(address _token, bytes32 _data) public returns (bool) {
register(_token);
bytes32 k;
bytes memory signMaterial = new bytes(40);
bytes memory addrBytes = abi.encodePacked(_token);
for (uint256 i = 0; i < 20; i++) {
signMaterial[i] = addrBytes[i];
}
addrBytes = abi.encodePacked(msg.sender);
for (uint256 i = 0; i < 20; i++) {
signMaterial[i+20] = addrBytes[i];
}
k = sha256(signMaterial);
require(endorsement[k] == bytes32(0x00));
endorsement[k] = _data;
endorsers[msg.sender].push(tokenIndex[_token]);
emit EndorsementAdded(_token, msg.sender, count, _data);
return true;
}
}

78
solidity/test.py Normal file
View File

@@ -0,0 +1,78 @@
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('TokenEndorser.bin', 'r')
bytecode = f.read()
f.close()
f = open('TokenEndorser.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 {} initial token {}'.format(r.contractAddress, token_address))
c = w3.eth.contract(abi=abi, address=r.contractAddress)
d = '0x' + os.urandom(32).hex()
# Initial token will fail in any case
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
fail = False
try:
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[0]})
except:
fail = True
pass
if not fail:
raise RuntimeError('expected fail on register same token to same address')
c.functions.add(token_address, d).transact({'from':w3.eth.accounts[1]})
h = hashlib.new('sha256')
h.update(bytes.fromhex(token_address[2:]))
h.update(bytes.fromhex(w3.eth.accounts[0][2:]))
z = h.digest()
assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
another_token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
c.functions.add(another_token_address, d).transact({'from':w3.eth.accounts[0]})
assert c.functions.endorsers(w3.eth.accounts[0], 0).call() == 1
assert c.functions.endorsers(w3.eth.accounts[1], 0).call() == 1
assert c.functions.endorsers(w3.eth.accounts[0], 1).call() == 2
assert c.functions.tokens(1).call() == token_address
assert c.functions.tokens(2).call() == another_token_address
assert c.functions.tokenIndex(token_address).call() == 1
assert c.functions.tokenIndex(another_token_address).call() == 2