eth-address-index

signed metadata declarations for ethereum addresses
Log | Files | Refs

commit 60a1c3402afa242122f5ae025eafc96b67e9ddd9
parent a60e9383a53ec8870e3aa429c307e8e3fe84cdf4
Author: nolash <dev@holbrook.no>
Date:   Tue, 22 Dec 2020 07:52:09 +0100

Add interface, contract token symbol external call

Diffstat:
Apython/eth_token_endorser/__init__.py | 1+
Mpython/eth_token_endorser/data/TokenEndorser.bin | 4++--
Mpython/eth_token_endorser/data/TokenEndorser.json | 2+-
Apython/eth_token_endorser/token.py | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/tests/test_deploy.py | 5+++--
Apython/tests/test_inteface.py | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msolidity/TokenEndorser.bin | 2++
Msolidity/TokenEndorser.json | 1+
Msolidity/TokenEndorser.sol | 13+++++++++----
9 files changed, 203 insertions(+), 9 deletions(-)

diff --git a/python/eth_token_endorser/__init__.py b/python/eth_token_endorser/__init__.py @@ -0,0 +1 @@ +from .token import TokenEndorser diff --git a/python/eth_token_endorser/data/TokenEndorser.bin b/python/eth_token_endorser/data/TokenEndorser.bin @@ -1 +1 @@ -608060405234801561001057600080fd5b506001600081905550600460009080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610bc48061008c6000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80632d580ef61461005c578063427f91a61461008c5780634f64b2be146100bc578063c82d6828146100ec578063e6088e831461011c575b600080fd5b6100766004803603810190610071919061079a565b61014c565b604051610083919061095e565b60405180910390f35b6100a660048036038101906100a19190610771565b610561565b6040516100b39190610994565b60405180910390f35b6100d660048036038101906100d19190610864565b610579565b6040516100e39190610943565b60405180910390f35b610106600480360381019061010191906107d6565b6105b8565b6040516101139190610994565b60405180910390f35b61013660048036038101906101319190610812565b6105e9565b6040516101439190610979565b60405180910390f35b600061015783610601565b50600080602867ffffffffffffffff81111561019c577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040519080825280601f01601f1916602001820160405280156101ce5781602001600182028036833780820191505090505b5090506000856040516020016101e49190610911565b604051602081830303815290604052905060005b60148110156102c05781818151811061023a577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b83828151811061027e577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535080806102b890610aa0565b9150506101f8565b50336040516020016102d29190610911565b604051602081830303815290604052905060005b60148110156103ba57818181518110610328577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b8360148361034191906109c5565b81518110610378577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535080806103b290610aa0565b9150506102e6565b506002826040516103cb919061092c565b602060405180830381855afa1580156103e8573d6000803e3d6000fd5b5050506040513d601f19601f8201168201806040525081019061040b919061083b565b92506000801b60016000858152602001908152602001600020541461042f57600080fd5b846001600085815260200190815260200160002081905550600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600260008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490806001815401808255809150506001900390600052602060002001600090919091909150556000543373ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff167fb8a4220b7c147fc6e04861db14024976c551727749a09806414670fac57e53408860405161054c9190610979565b60405180910390a46001935050505092915050565b60026020528060005260406000206000915090505481565b6004818154811061058957600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600360205281600052604060002081815481106105d457600080fd5b90600052602060002001600091509150505481565b60016020528060005260406000206000915090505481565b600080600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205411156106535760009050610718565b6004829080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600054600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555060008081548092919061070e90610aa0565b9190505550600190505b919050565b60008135905061072c81610b49565b92915050565b60008135905061074181610b60565b92915050565b60008151905061075681610b60565b92915050565b60008135905061076b81610b77565b92915050565b60006020828403121561078357600080fd5b60006107918482850161071d565b91505092915050565b600080604083850312156107ad57600080fd5b60006107bb8582860161071d565b92505060206107cc85828601610732565b9150509250929050565b600080604083850312156107e957600080fd5b60006107f78582860161071d565b92505060206108088582860161075c565b9150509250929050565b60006020828403121561082457600080fd5b600061083284828501610732565b91505092915050565b60006020828403121561084d57600080fd5b600061085b84828501610747565b91505092915050565b60006020828403121561087657600080fd5b60006108848482850161075c565b91505092915050565b61089681610a1b565b82525050565b6108ad6108a882610a1b565b610ae9565b82525050565b6108bc81610a2d565b82525050565b6108cb81610a39565b82525050565b60006108dc826109af565b6108e681856109ba565b93506108f6818560208601610a6d565b80840191505092915050565b61090b81610a63565b82525050565b600061091d828461089c565b60148201915081905092915050565b600061093882846108d1565b915081905092915050565b6000602082019050610958600083018461088d565b92915050565b600060208201905061097360008301846108b3565b92915050565b600060208201905061098e60008301846108c2565b92915050565b60006020820190506109a96000830184610902565b92915050565b600081519050919050565b600081905092915050565b60006109d082610a63565b91506109db83610a63565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610a1057610a0f610b0d565b5b828201905092915050565b6000610a2682610a43565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b83811015610a8b578082015181840152602081019050610a70565b83811115610a9a576000848401525b50505050565b6000610aab82610a63565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821415610ade57610add610b0d565b5b600182019050919050565b6000610af482610afb565b9050919050565b6000610b0682610b3c565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008160601b9050919050565b610b5281610a1b565b8114610b5d57600080fd5b50565b610b6981610a39565b8114610b7457600080fd5b50565b610b8081610a63565b8114610b8b57600080fd5b5056fea2646970667358221220bdfe2b23cf9bc8fac35e8b06e83bc20d08b1c9b009c2e1d7c2ccf4932a53601e64736f6c63430008000033 -\ No newline at end of file +608060405234801561001057600080fd5b506001600081905550600660009080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506111398061008c6000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c8063427f91a61161005b578063427f91a6146101125780634f64b2be146101425780639367d97514610172578063e6088e83146101a25761007d565b80630f1244d3146100825780632d580ef6146100b25780633fac3587146100e2575b600080fd5b61009c60048036038101906100979190610c5a565b6101d2565b6040516100a99190610e03565b60405180910390f35b6100cc60048036038101906100c79190610b90565b61021b565b6040516100d99190610e1e565b60405180910390f35b6100fc60048036038101906100f79190610bcc565b610685565b6040516101099190610e54565b60405180910390f35b61012c60048036038101906101279190610b67565b6106b6565b6040516101399190610e54565b60405180910390f35b61015c60048036038101906101579190610cdc565b6106ce565b6040516101699190610e03565b60405180910390f35b61018c60048036038101906101879190610b67565b61070d565b6040516101999190610e54565b60405180910390f35b6101bc60048036038101906101b79190610c08565b610725565b6040516101c99190610e39565b60405180910390f35b6005818051602081018201805184825260208301602085012081835280955050505050506000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60006102268361073d565b50600080602867ffffffffffffffff81111561026b577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040519080825280601f01601f19166020018201604052801561029d5781602001600182028036833780820191505090505b5090506000856040516020016102b39190610dba565b604051602081830303815290604052905060005b601481101561038f57818181518110610309577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b83828151811061034d577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061038790610fe6565b9150506102c7565b50336040516020016103a19190610dba565b604051602081830303815290604052905060005b6014811015610489578181815181106103f7577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b836014836104109190610efc565b81518110610447577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061048190610fe6565b9150506103b5565b5060028260405161049a9190610dd5565b602060405180830381855afa1580156104b7573d6000803e3d6000fd5b5050506040513d601f19601f820116820180604052508101906104da9190610c31565b92506000801b6001600085815260200190815260200160002054146104fe57600080fd5b846001600085815260200190815260200160002081905550600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600260008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549080600181540180825580915050600190039060005260206000200160009091909190915055600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600081548092919061060b90610fe6565b91905055506000543373ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff167fb8a4220b7c147fc6e04861db14024976c551727749a09806414670fac57e5340886040516106709190610e39565b60405180910390a46001935050505092915050565b600360205281600052604060002081815481106106a157600080fd5b90600052602060002001600091509150505481565b60026020528060005260406000206000915090505481565b600681815481106106de57600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60046020528060005260406000206000915090505481565b60016020528060005260406000206000915090505481565b600080600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054111561078f5760009050610a3e565b6000808373ffffffffffffffffffffffffffffffffffffffff166040516024016040516020818303038152906040527f95d89b41000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516108399190610dd5565b6000604051808303816000865af19150503d8060008114610876576040519150601f19603f3d011682016040523d82523d6000602084013e61087b565b606091505b50915091508161088a57600080fd5b6000818060200190518101906108a09190610c9b565b9050600073ffffffffffffffffffffffffffffffffffffffff166005826040516108ca9190610dec565b908152602001604051809103902060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161461091957600080fd5b6006859080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600054600260008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550846005826040516109d39190610dec565b908152602001604051809103902060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600080815480929190610a3190610fe6565b9190505550600193505050505b919050565b6000610a56610a5184610ea0565b610e6f565b905082815260208101848484011115610a6e57600080fd5b610a79848285610fa4565b509392505050565b6000610a94610a8f84610ea0565b610e6f565b905082815260208101848484011115610aac57600080fd5b610ab7848285610fb3565b509392505050565b600081359050610ace816110be565b92915050565b600081359050610ae3816110d5565b92915050565b600081519050610af8816110d5565b92915050565b600082601f830112610b0f57600080fd5b8135610b1f848260208601610a43565b91505092915050565b600082601f830112610b3957600080fd5b8151610b49848260208601610a81565b91505092915050565b600081359050610b61816110ec565b92915050565b600060208284031215610b7957600080fd5b6000610b8784828501610abf565b91505092915050565b60008060408385031215610ba357600080fd5b6000610bb185828601610abf565b9250506020610bc285828601610ad4565b9150509250929050565b60008060408385031215610bdf57600080fd5b6000610bed85828601610abf565b9250506020610bfe85828601610b52565b9150509250929050565b600060208284031215610c1a57600080fd5b6000610c2884828501610ad4565b91505092915050565b600060208284031215610c4357600080fd5b6000610c5184828501610ae9565b91505092915050565b600060208284031215610c6c57600080fd5b600082013567ffffffffffffffff811115610c8657600080fd5b610c9284828501610afe565b91505092915050565b600060208284031215610cad57600080fd5b600082015167ffffffffffffffff811115610cc757600080fd5b610cd384828501610b28565b91505092915050565b600060208284031215610cee57600080fd5b6000610cfc84828501610b52565b91505092915050565b610d0e81610f52565b82525050565b610d25610d2082610f52565b61102f565b82525050565b610d3481610f64565b82525050565b610d4381610f70565b82525050565b6000610d5482610ed0565b610d5e8185610ee6565b9350610d6e818560208601610fb3565b80840191505092915050565b6000610d8582610edb565b610d8f8185610ef1565b9350610d9f818560208601610fb3565b80840191505092915050565b610db481610f9a565b82525050565b6000610dc68284610d14565b60148201915081905092915050565b6000610de18284610d49565b915081905092915050565b6000610df88284610d7a565b915081905092915050565b6000602082019050610e186000830184610d05565b92915050565b6000602082019050610e336000830184610d2b565b92915050565b6000602082019050610e4e6000830184610d3a565b92915050565b6000602082019050610e696000830184610dab565b92915050565b6000604051905081810181811067ffffffffffffffff82111715610e9657610e95611082565b5b8060405250919050565b600067ffffffffffffffff821115610ebb57610eba611082565b5b601f19601f8301169050602081019050919050565b600081519050919050565b600081519050919050565b600081905092915050565b600081905092915050565b6000610f0782610f9a565b9150610f1283610f9a565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610f4757610f46611053565b5b828201905092915050565b6000610f5d82610f7a565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b82818337600083830152505050565b60005b83811015610fd1578082015181840152602081019050610fb6565b83811115610fe0576000848401525b50505050565b6000610ff182610f9a565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82141561102457611023611053565b5b600182019050919050565b600061103a82611041565b9050919050565b600061104c826110b1565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60008160601b9050919050565b6110c781610f52565b81146110d257600080fd5b50565b6110de81610f70565b81146110e957600080fd5b50565b6110f581610f9a565b811461110057600080fd5b5056fea2646970667358221220b8f2c7741375a75c920a4152b00634fd6b771f728ba3117ab25bf6d17dc721f764736f6c63430008000033 +\ No newline at end of file diff --git a/python/eth_token_endorser/data/TokenEndorser.json b/python/eth_token_endorser/data/TokenEndorser.json @@ -1 +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"}] +[{"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":"endorser","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"endorserTokenCount","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":"string","name":"","type":"string"}],"name":"tokenSymbolIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}] diff --git a/python/eth_token_endorser/token.py b/python/eth_token_endorser/token.py @@ -0,0 +1,79 @@ +# 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, 'TokenEndorser.json'), 'r') + TokenEndorser.__abi = json.load(f) + f.close() + return TokenEndorser.__abi + + + @staticmethod + def bytecode(): + if TokenEndorser.__bytecode == None: + f = open(os.path.join(datadir, 'TokenEndorser.bin')) + TokenEndorser.__bytecode = f.read() + f.close() + return TokenEndorser.__bytecode + + + def token_from_symbol(self, symbol): + return self.contract.functions.tokenSymbolIndex(symbol).call() + + + 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 add(self, token_address, data): + self.contract.functions.add(token_address, data).transact({'from': self.signer_address}) + + +def to_endorsement_key(token_address_hex, endorser_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() + + diff --git a/python/tests/test_deploy.py b/python/tests/test_deploy.py @@ -89,8 +89,8 @@ class Test(unittest.TestCase): 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): + def test_endorsement(self): c = self.w3.eth.contract(abi=self.abi, address=self.address) d = '0x' + os.urandom(32).hex() @@ -105,7 +105,6 @@ class Test(unittest.TestCase): assert d[2:] == c.functions.endorsement(z.hex()).call().hex() - #another_token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).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 @@ -127,6 +126,8 @@ class Test(unittest.TestCase): 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__': diff --git a/python/tests/test_inteface.py b/python/tests/test_inteface.py @@ -0,0 +1,105 @@ +import os +import unittest +import json +import logging + +import web3 +import eth_tester +import eth_abi + +from eth_token_endorser import TokenEndorser + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +logging.getLogger('web3').setLevel(logging.WARNING) +logging.getLogger('eth.vm').setLevel(logging.WARNING) + +testdir = os.path.dirname(__file__) + + +class Test(unittest.TestCase): + + contract = None + + def setUp(self): + eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({ + 'gas_limit': 9000000, + }) + + # create store of used accounts + f = open(os.path.join(testdir, '../eth_token_endorser/data/TokenEndorser.bin'), 'r') + bytecode = f.read() + f.close() + + f = open(os.path.join(testdir, '../eth_token_endorser/data/TokenEndorser.json'), 'r') + self.abi = json.load(f) + f.close() + + + backend = eth_tester.PyEVMBackend(eth_params) + self.eth_tester = eth_tester.EthereumTester(backend) + provider = web3.Web3.EthereumTesterProvider(self.eth_tester) + self.w3 = web3.Web3(provider) + c = self.w3.eth.contract(abi=self.abi, bytecode=bytecode) + tx_hash = c.constructor().transact({'from': self.w3.eth.accounts[0]}) + + r = self.w3.eth.getTransactionReceipt(tx_hash) + + self.address = r.contractAddress + + + # create token + f = open(os.path.join(testdir, '../eth_token_endorser/data/GiftableToken.bin'), 'r') + bytecode = f.read() + f.close() + + f = open(os.path.join(testdir, '../eth_token_endorser/data/GiftableToken.json'), 'r') + self.abi_token = json.load(f) + f.close() + + t = self.w3.eth.contract(abi=self.abi_token, bytecode=bytecode) + tx_hash = t.constructor('Foo Token', 'FOO', 18).transact({'from': self.w3.eth.accounts[0]}) + + r = self.w3.eth.getTransactionReceipt(tx_hash) + + self.address_token_one = r.contractAddress + + t = self.w3.eth.contract(abi=self.abi_token, bytecode=bytecode) + tx_hash = t.constructor('Bar Token', 'BAR', 18).transact({'from': self.w3.eth.accounts[0]}) + + r = self.w3.eth.getTransactionReceipt(tx_hash) + + 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_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) + + +if __name__ == '__main__': + unittest.main() diff --git a/solidity/TokenEndorser.bin b/solidity/TokenEndorser.bin @@ -0,0 +1 @@ +608060405234801561001057600080fd5b506001600081905550600660009080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506111398061008c6000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c8063427f91a61161005b578063427f91a6146101125780634f64b2be146101425780639367d97514610172578063e6088e83146101a25761007d565b80630f1244d3146100825780632d580ef6146100b25780633fac3587146100e2575b600080fd5b61009c60048036038101906100979190610c5a565b6101d2565b6040516100a99190610e03565b60405180910390f35b6100cc60048036038101906100c79190610b90565b61021b565b6040516100d99190610e1e565b60405180910390f35b6100fc60048036038101906100f79190610bcc565b610685565b6040516101099190610e54565b60405180910390f35b61012c60048036038101906101279190610b67565b6106b6565b6040516101399190610e54565b60405180910390f35b61015c60048036038101906101579190610cdc565b6106ce565b6040516101699190610e03565b60405180910390f35b61018c60048036038101906101879190610b67565b61070d565b6040516101999190610e54565b60405180910390f35b6101bc60048036038101906101b79190610c08565b610725565b6040516101c99190610e39565b60405180910390f35b6005818051602081018201805184825260208301602085012081835280955050505050506000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60006102268361073d565b50600080602867ffffffffffffffff81111561026b577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040519080825280601f01601f19166020018201604052801561029d5781602001600182028036833780820191505090505b5090506000856040516020016102b39190610dba565b604051602081830303815290604052905060005b601481101561038f57818181518110610309577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b83828151811061034d577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061038790610fe6565b9150506102c7565b50336040516020016103a19190610dba565b604051602081830303815290604052905060005b6014811015610489578181815181106103f7577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b836014836104109190610efc565b81518110610447577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061048190610fe6565b9150506103b5565b5060028260405161049a9190610dd5565b602060405180830381855afa1580156104b7573d6000803e3d6000fd5b5050506040513d601f19601f820116820180604052508101906104da9190610c31565b92506000801b6001600085815260200190815260200160002054146104fe57600080fd5b846001600085815260200190815260200160002081905550600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600260008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549080600181540180825580915050600190039060005260206000200160009091909190915055600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600081548092919061060b90610fe6565b91905055506000543373ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff167fb8a4220b7c147fc6e04861db14024976c551727749a09806414670fac57e5340886040516106709190610e39565b60405180910390a46001935050505092915050565b600360205281600052604060002081815481106106a157600080fd5b90600052602060002001600091509150505481565b60026020528060005260406000206000915090505481565b600681815481106106de57600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60046020528060005260406000206000915090505481565b60016020528060005260406000206000915090505481565b600080600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054111561078f5760009050610a3e565b6000808373ffffffffffffffffffffffffffffffffffffffff166040516024016040516020818303038152906040527f95d89b41000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516108399190610dd5565b6000604051808303816000865af19150503d8060008114610876576040519150601f19603f3d011682016040523d82523d6000602084013e61087b565b606091505b50915091508161088a57600080fd5b6000818060200190518101906108a09190610c9b565b9050600073ffffffffffffffffffffffffffffffffffffffff166005826040516108ca9190610dec565b908152602001604051809103902060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161461091957600080fd5b6006859080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600054600260008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550846005826040516109d39190610dec565b908152602001604051809103902060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600080815480929190610a3190610fe6565b9190505550600193505050505b919050565b6000610a56610a5184610ea0565b610e6f565b905082815260208101848484011115610a6e57600080fd5b610a79848285610fa4565b509392505050565b6000610a94610a8f84610ea0565b610e6f565b905082815260208101848484011115610aac57600080fd5b610ab7848285610fb3565b509392505050565b600081359050610ace816110be565b92915050565b600081359050610ae3816110d5565b92915050565b600081519050610af8816110d5565b92915050565b600082601f830112610b0f57600080fd5b8135610b1f848260208601610a43565b91505092915050565b600082601f830112610b3957600080fd5b8151610b49848260208601610a81565b91505092915050565b600081359050610b61816110ec565b92915050565b600060208284031215610b7957600080fd5b6000610b8784828501610abf565b91505092915050565b60008060408385031215610ba357600080fd5b6000610bb185828601610abf565b9250506020610bc285828601610ad4565b9150509250929050565b60008060408385031215610bdf57600080fd5b6000610bed85828601610abf565b9250506020610bfe85828601610b52565b9150509250929050565b600060208284031215610c1a57600080fd5b6000610c2884828501610ad4565b91505092915050565b600060208284031215610c4357600080fd5b6000610c5184828501610ae9565b91505092915050565b600060208284031215610c6c57600080fd5b600082013567ffffffffffffffff811115610c8657600080fd5b610c9284828501610afe565b91505092915050565b600060208284031215610cad57600080fd5b600082015167ffffffffffffffff811115610cc757600080fd5b610cd384828501610b28565b91505092915050565b600060208284031215610cee57600080fd5b6000610cfc84828501610b52565b91505092915050565b610d0e81610f52565b82525050565b610d25610d2082610f52565b61102f565b82525050565b610d3481610f64565b82525050565b610d4381610f70565b82525050565b6000610d5482610ed0565b610d5e8185610ee6565b9350610d6e818560208601610fb3565b80840191505092915050565b6000610d8582610edb565b610d8f8185610ef1565b9350610d9f818560208601610fb3565b80840191505092915050565b610db481610f9a565b82525050565b6000610dc68284610d14565b60148201915081905092915050565b6000610de18284610d49565b915081905092915050565b6000610df88284610d7a565b915081905092915050565b6000602082019050610e186000830184610d05565b92915050565b6000602082019050610e336000830184610d2b565b92915050565b6000602082019050610e4e6000830184610d3a565b92915050565b6000602082019050610e696000830184610dab565b92915050565b6000604051905081810181811067ffffffffffffffff82111715610e9657610e95611082565b5b8060405250919050565b600067ffffffffffffffff821115610ebb57610eba611082565b5b601f19601f8301169050602081019050919050565b600081519050919050565b600081519050919050565b600081905092915050565b600081905092915050565b6000610f0782610f9a565b9150610f1283610f9a565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610f4757610f46611053565b5b828201905092915050565b6000610f5d82610f7a565b9050919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b82818337600083830152505050565b60005b83811015610fd1578082015181840152602081019050610fb6565b83811115610fe0576000848401525b50505050565b6000610ff182610f9a565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82141561102457611023611053565b5b600182019050919050565b600061103a82611041565b9050919050565b600061104c826110b1565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60008160601b9050919050565b6110c781610f52565b81146110d257600080fd5b50565b6110de81610f70565b81146110e957600080fd5b50565b6110f581610f9a565b811461110057600080fd5b5056fea2646970667358221220b8f2c7741375a75c920a4152b00634fd6b771f728ba3117ab25bf6d17dc721f764736f6c63430008000033 +\ No newline at end of file diff --git a/solidity/TokenEndorser.json b/solidity/TokenEndorser.json @@ -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":"endorser","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"endorserTokenCount","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":"string","name":"","type":"string"}],"name":"tokenSymbolIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}] diff --git a/solidity/TokenEndorser.sol b/solidity/TokenEndorser.sol @@ -7,7 +7,8 @@ contract TokenEndorsement { uint256 count; mapping ( bytes32 => bytes32 ) public endorsement; mapping ( address => uint256 ) public tokenIndex; - mapping ( address => uint256[] ) public endorsers; + mapping ( address => uint256[] ) public endorser; + mapping ( address => uint256 ) public endorserTokenCount; mapping ( string => address ) public tokenSymbolIndex; address[] public tokens; @@ -22,8 +23,11 @@ contract TokenEndorsement { if (tokenIndex[_token] > 0) { return false; } - string memory token_symbol = _token.call(abi.encodeWithSignature('symbol()')); - require(tokenSymbolIndex[token_symbol] = address(0)); + (bool _ok, bytes memory _r) = _token.call(abi.encodeWithSignature('symbol()')); + require(_ok); + + string memory token_symbol = abi.decode(_r, (string)); + require(tokenSymbolIndex[token_symbol] == address(0)); tokens.push(_token); tokenIndex[_token] = count; @@ -47,7 +51,8 @@ contract TokenEndorsement { k = sha256(signMaterial); require(endorsement[k] == bytes32(0x00)); endorsement[k] = _data; - endorsers[msg.sender].push(tokenIndex[_token]); + endorser[msg.sender].push(tokenIndex[_token]); + endorserTokenCount[msg.sender]++; emit EndorsementAdded(_token, msg.sender, count, _data); return true; }