Initial commit

This commit is contained in:
nolash 2020-12-21 23:00:45 +01:00
commit ffb4fdee12
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
11 changed files with 254 additions and 0 deletions

1
python/MANIFEST.in Normal file
View File

@ -0,0 +1 @@
include **/data/TokenEndorser.json **/data/TokenEndorser.bin

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,62 @@
"""Deploys token endorsement contract
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import os
import json
import argparse
import logging
# third-party imports
import web3
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data')
argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
argparser.add_argument('-o', '--owner', dest='o', type=str, help='Accounts index owner')
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
args = argparser.parse_args()
if args.v:
logg.setLevel(logging.DEBUG)
def main():
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
f = open(os.path.join(args.abi_dir, 'TokenEndorser.json'), 'r')
abi = json.load(f)
f.close()
f = open(os.path.join(args.abi_dir, 'TokenEndorser.bin'), 'r')
bytecode = f.read()
f.close()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('owner address {}'.format(w3.eth.defaultAccount))
c = w3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = c.constructor().transact()
rcpt = w3.eth.getTransactionReceipt(tx_hash)
print(rcpt.contractAddress)
if __name__ == '__main__':
main()

47
python/setup.cfg Normal file
View File

@ -0,0 +1,47 @@
[metadata]
name = eth-token-endorser
version = 0.0.1
description = Ethereum token address to multihash metadata mapper
author = Louis Holbrook
author_email = dev@holbrook.no
url = https://gitlab.com/nolash/simple-multisig
keywords =
ethereum
classifiers =
Programming Language :: Python :: 3
Operating System :: OS Independent
Development Status :: 3 - Alpha
Environment :: No Input/Output (Daemon)
Intended Audience :: Developers
License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Topic :: Internet
#Topic :: Blockchain :: EVM
license = GPL3
licence_files =
LICENSE
[options]
include_package_data = True
python_requires = >= 3.6
packages =
eth_token_endorser
eth_token_endorser.runnable
install_requires =
web3==5.12.2
tests_require =
eth-tester==0.5.0b2
py-evm==0.3.0a20
[options.extras_require]
testing =
eth-tester==0.5.0b2
py-evm==0.3.0a20
[options.package_data]
* =
data/TokenEndorser.json
data/TokenEndorser.bin
[options.entry_points]
console_scripts =
eth-token-endorser = eth_token_endorser.runnable.deploy:main

4
python/setup.py Normal file
View File

@ -0,0 +1,4 @@
from setuptools import setup
setup(
)

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