Fix address declarator executables

This commit is contained in:
nolash 2020-12-29 19:44:14 +01:00
parent 4400240b3e
commit d659ae72f5
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
24 changed files with 627 additions and 92 deletions

View File

@ -0,0 +1 @@
from .declarator import AddressDeclarator

File diff suppressed because one or more lines are too long

View File

@ -0,0 +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":"_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"}]

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"TransferFrom","type":"event"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@ -0,0 +1,84 @@
# Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
# SPDX-License-Identifier: GPL-3.0-or-later
# File-version: 1
# Description: Python interface to abi and bin files for faucet contracts
# standard imports
import logging
import json
import os
import hashlib
logg = logging.getLogger(__name__)
moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class TokenEndorser:
__abi = None
__bytecode = None
__address = None
def __init__(self, w3, address, signer_address=None):
abi = TokenEndorser.abi()
TokenEndorser.bytecode()
self.__address = address
self.contract = w3.eth.contract(abi=abi, address=address)
self.w3 = w3
if signer_address != None:
self.signer_address = signer_address
else:
if type(self.w3.eth.defaultAccount).__name__ == 'Empty':
self.w3.eth.defaultAccount = self.w3.eth.accounts[0]
self.signer_address = self.w3.eth.defaultAccount
@staticmethod
def abi():
if TokenEndorser.__abi == None:
f = open(os.path.join(datadir, 'AccountDeclarator.json'), 'r')
AccountDeclarator.__abi = json.load(f)
f.close()
return TokenEndorser.__abi
@staticmethod
def bytecode():
if TokenEndorser.__bytecode == None:
f = open(os.path.join(datadir, 'AccountDeclarator.bin'))
AccountDeclarator.__bytecode = f.read()
f.close()
return TokenEndorser.__bytecode
# def token_from_symbol(self, symbol):
# return self.contract.functions.tokenSymbolIndex(symbol).call()
# def
#
# def endorsed_tokens(self, endorser_address):
# tokens = []
# for i in range(self.contract.functions.endorserTokenCount(endorser_address).call()):
# tidx = self.contract.functions.endorser(endorser_address, i).call()
# t = self.contract.functions.tokens(tidx).call()
# tokens.append(t)
# return tokens
#
# def declared(self, declarator_address):
# addresses = []
# for i
#
#
# def add(self, token_address, data):
# self.contract.functions.add(token_address, data).transact({'from': self.signer_address})
#
def to_endorsement_key(declarator_address_hex, declaration_address_hex):
h = hashlib.new('sha256')
h.update(bytes.fromhex(token_address_hex[2:]))
h.update(bytes.fromhex(endorser_address_hex[2:]))
return h.digest()

View File

@ -0,0 +1,59 @@
"""Adds a new Address declaration
.. 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('-r', '--contract-address', dest='r', type=str, help='Token endorsement contract address')
argparser.add_argument('-o', '--endorser-address', dest='o', type=str, help='Address to use to sign endorsement transaction')
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')
argparser.add_argument('address', type=str, help='Ethereum address to add declaration to')
argparser.add_argument('declaration', type=str, help='SHA256 sum of endorsement data to add')
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, 'AddressDeclarator.json'), 'r')
abi = json.load(f)
f.close()
w3.eth.defaultAccount = w3.eth.accounts[0]
if args.o != None:
w3.eth.defaultAccount = args.o
logg.debug('endorser address {}'.format(w3.eth.defaultAccount))
c = w3.eth.contract(abi=abi, address=args.r)
tx_hash = c.functions.addDeclaration(args.address, args.declaration).transact()
print(tx_hash.hex())
if __name__ == '__main__':
main()

View File

@ -0,0 +1,64 @@
"""Deploys address declaration 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 declarator 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')
argparser.add_argument('owner_description_digest', type=str, help='SHA256 of description metadata of contract deployer')
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, 'AddressDeclarator.json'), 'r')
abi = json.load(f)
f.close()
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.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)
logg.info('digest {}'.format(args.owner_description_digest))
tx_hash = c.constructor(args.owner_description_digest).transact()
rcpt = w3.eth.getTransactionReceipt(tx_hash)
print(rcpt.contractAddress)
if __name__ == '__main__':
main()

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

@ -1,10 +1,10 @@
[metadata]
name = eth-token-endorser
version = 0.0.2
description = Ethereum token address to multihash metadata mapper
name = eth-address-declarator
version = 0.0.1
description = Signed metadata declarations for ethereum addresses
author = Louis Holbrook
author_email = dev@holbrook.no
url = https://gitlab.com/nolash/simple-multisig
url = https://gitlab.com/nolash/eth-address-declarator
keywords =
ethereum
classifiers =
@ -24,8 +24,8 @@ licence_files =
include_package_data = True
python_requires = >= 3.6
packages =
eth_token_endorser
eth_token_endorser.runnable
eth_address_declarator
eth_address_declarator.runnable
install_requires =
web3==5.12.2
tests_require =
@ -39,10 +39,10 @@ testing =
[options.package_data]
* =
data/TokenEndorser.json
data/TokenEndorser.bin
data/AddressDeclarator.json
data/AddressDeclarator.bin
[options.entry_points]
console_scripts =
eth-token-endorser-deploy = eth_token_endorser.runnable.deploy:main
eth-token-endorser-add = eth_token_endorser.runnable.add:main
eth-address-declarator-deploy = eth_address_declarator.runnable.deploy:main
eth-address-declarator-add = eth_address_declarator.runnable.add:main

View File

@ -27,11 +27,13 @@ class Test(unittest.TestCase):
})
# create store of used accounts
f = open(os.path.join(testdir, '../eth_token_endorser/data/TokenEndorser.bin'), 'r')
#f = open(os.path.join(testdir, '../eth_token_index/data/TokenUniqueSymbolIndex.bin'), 'r')
f = open(os.path.join(testdir, '../../solidity/TokenUniqueSymbolIndex.bin'), 'r')
bytecode = f.read()
f.close()
f = open(os.path.join(testdir, '../eth_token_endorser/data/TokenEndorser.json'), 'r')
#f = open(os.path.join(testdir, '../eth_token_index/data/TokenUniqueSymbolIndex.json'), 'r')
f = open(os.path.join(testdir, '../../solidity/TokenUniqueSymbolIndex.json'), 'r')
self.abi = json.load(f)
f.close()
@ -71,6 +73,13 @@ class Test(unittest.TestCase):
self.address_token_two = r.contractAddress
t = self.w3.eth.contract(abi=self.abi_token, bytecode=bytecode)
tx_hash = t.constructor('Bar Token Duplicate', 'BAR', 18).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.address_token_three = r.contractAddress
def tearDown(self):
pass
@ -79,55 +88,36 @@ class Test(unittest.TestCase):
def test_basic(self):
c = self.w3.eth.contract(abi=self.abi, address=self.address)
d = '0x' + os.urandom(32).hex()
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
with self.assertRaises(Exception):
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[1]})
def test_endorsement(self):
c = self.w3.eth.contract(abi=self.abi, address=self.address)
d = '0x' + os.urandom(32).hex()
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
h = hashlib.new('sha256')
h.update(bytes.fromhex(self.address_token_one[2:]))
h.update(bytes.fromhex(self.w3.eth.accounts[0][2:]))
h.update('FOO'.encode('utf-8'))
z = h.digest()
assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
# owner text
with self.assertRaises(Exception):
c.functions.register(z.hex(), self.address_token_one).transact({'from': self.w3.eth.accounts[1]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
logg.debug('using identifier {}'.format(z.hex()))
# Register FOO symbol
c.functions.register(z.hex(), self.address_token_one).transact({'from': self.w3.eth.accounts[0]})
assert c.functions.endorsers(self.w3.eth.accounts[0], 0).call() == 1
assert c.functions.endorsers(self.w3.eth.accounts[1], 0).call() == 1
assert c.functions.endorsers(self.w3.eth.accounts[0], 1).call() == 2
# Raise on duplicate FOO symbol
with self.assertRaises(Exception):
c.functions.register(z.hex(), self.address_token_one).transact({'from': self.w3.eth.accounts[0]})
assert c.functions.tokens(1).call() == self.address_token_one
assert c.functions.tokens(2).call() == self.address_token_two
# Raise on mismatch between supplied symbol and token symbol reported by ERC20
with self.assertRaises(Exception):
c.functions.register(z.hex(), self.address_token_two).transact({'from': self.w3.eth.accounts[0]})
assert c.functions.tokenIndex(self.address_token_one).call() == 1
assert c.functions.tokenIndex(self.address_token_two).call() == 2
h = hashlib.new('sha256')
h.update('BAR'.encode('utf-8'))
z = h.digest()
# Register BAR symbol
c.functions.register(z.hex(), self.address_token_two).transact({'from': self.w3.eth.accounts[0]})
def test_symbol_index(self):
c = self.w3.eth.contract(abi=self.abi, address=self.address)
d = '0x' + os.urandom(32).hex()
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[1]})
self.assertEqual(c.functions.tokenSymbolIndex('FOO').call(), self.address_token_one);
self.assertEqual(c.functions.tokenSymbolIndex('BAR').call(), self.address_token_two);
# Raise on duplicate BAR symbol (with different token contract address)
with self.assertRaises(Exception):
c.functions.register(z.hex(), self.address_token_three).transact({'from': self.w3.eth.accounts[0]})
if __name__ == '__main__':

View File

@ -2,13 +2,12 @@ import os
import unittest
import json
import logging
import hashlib
import web3
import eth_tester
import eth_abi
from eth_token_endorser import TokenEndorser
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
@ -73,32 +72,62 @@ class Test(unittest.TestCase):
self.address_token_two = r.contractAddress
c = self.w3.eth.contract(abi=self.abi, address=self.address)
d = '0x' + os.urandom(32).hex()
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
def tearDown(self):
pass
def test_interface(self):
i = TokenEndorser(self.w3, self.address) #, self.w3.eth.accounts[1])
self.assertEqual(i.token_from_symbol('FOO'), self.address_token_one)
def test_basic(self):
c = self.w3.eth.contract(abi=self.abi, address=self.address)
def test_endorsed_tokens(self):
i = TokenEndorser(self.w3, self.address) #, self.w3.eth.accounts[1])
t = i.endorsed_tokens(self.w3.eth.accounts[0])
self.assertEqual(t[0], self.address_token_one)
self.assertEqual(t[1], self.address_token_two)
def test_add(self):
i = TokenEndorser(self.w3, self.address, self.w3.eth.accounts[1])
d = '0x' + os.urandom(32).hex()
i.add(self.address_token_one, d)
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
with self.assertRaises(Exception):
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[1]})
def test_endorsement(self):
c = self.w3.eth.contract(abi=self.abi, address=self.address)
d = '0x' + os.urandom(32).hex()
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
h = hashlib.new('sha256')
h.update(bytes.fromhex(self.address_token_one[2:]))
h.update(bytes.fromhex(self.w3.eth.accounts[0][2:]))
z = h.digest()
assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
assert c.functions.endorsers(self.w3.eth.accounts[0], 0).call() == 1
assert c.functions.endorsers(self.w3.eth.accounts[1], 0).call() == 1
assert c.functions.endorsers(self.w3.eth.accounts[0], 1).call() == 2
assert c.functions.tokens(1).call() == self.address_token_one
assert c.functions.tokens(2).call() == self.address_token_two
assert c.functions.tokenIndex(self.address_token_one).call() == 1
assert c.functions.tokenIndex(self.address_token_two).call() == 2
def test_symbol_index(self):
c = self.w3.eth.contract(abi=self.abi, address=self.address)
d = '0x' + os.urandom(32).hex()
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[1]})
self.assertEqual(c.functions.tokenSymbolIndex('FOO').call(), self.address_token_one);
self.assertEqual(c.functions.tokenSymbolIndex('BAR').call(), self.address_token_two);
if __name__ == '__main__':

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')