eth-address-index

signed metadata declarations for ethereum addresses
Log | Files | Refs

commit e3d4d6f47808aff79f4ed3ec5105a92d450029fb
parent 6d673a0d8b58fe5cffc2b7338aed97a7c67c9461
Author: nolash <dev@holbrook.no>
Date:   Fri, 30 Apr 2021 17:13:58 +0200

Split interface to separate file

Diffstat:
Mpython/eth_address_declarator/__init__.py | 2+-
Mpython/eth_address_declarator/declarator.py | 111+++----------------------------------------------------------------------------
Apython/eth_address_declarator/interface.py | 131+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/tests/test_addressdeclarator.py | 29+++++++++++++++--------------
Msolidity/AddressDeclarator.sol | 9+++++++++
5 files changed, 160 insertions(+), 122 deletions(-)

diff --git a/python/eth_address_declarator/__init__.py b/python/eth_address_declarator/__init__.py @@ -1 +1 @@ -from .declarator import AddressDeclarator +from .interface import Declarator diff --git a/python/eth_address_declarator/declarator.py b/python/eth_address_declarator/declarator.py @@ -26,6 +26,9 @@ from chainlib.eth.contract import ( from chainlib.jsonrpc import jsonrpc_template from chainlib.eth.constant import ZERO_ADDRESS +# local imports +from eth_address_declarator import Declarator + logg = logging.getLogger(__name__) moddir = os.path.dirname(__file__) @@ -39,7 +42,7 @@ def to_declarator_key(declarator_address_hex, declaration_address_hex): return h.digest() -class AddressDeclarator(TxFactory): +class AddressDeclarator(Declarator): __abi = None __bytecode = None @@ -76,109 +79,3 @@ class AddressDeclarator(TxFactory): tx = self.template(sender_address, None, use_nonce=True) tx = self.set_code(tx, code) return self.build(tx) - - - def add_declaration(self, contract_address, sender_address, subject_address, proof, tx_format=TxFormat.JSONRPC): - enc = ABIContractEncoder() - enc.method('addDeclaration') - enc.typ(ABIContractType.ADDRESS) - enc.typ(ABIContractType.BYTES32) - enc.address(subject_address) - enc.bytes32(proof) - data = enc.get() - tx = self.template(sender_address, contract_address, use_nonce=True) - tx = self.set_code(tx, data) - tx = self.finalize(tx, tx_format) - return tx - - - def declarator_count(self, contract_address, subject_address, sender_address=ZERO_ADDRESS): - o = jsonrpc_template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('declaratorCount') - enc.typ(ABIContractType.ADDRESS) - enc.address(subject_address) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - return o - - - def declaration(self, contract_address, declarator_address, subject_address, sender_address=ZERO_ADDRESS): - o = jsonrpc_template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('declaration') - enc.typ(ABIContractType.ADDRESS) - enc.typ(ABIContractType.ADDRESS) - enc.address(declarator_address) - enc.address(subject_address) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - return o - - - def declaration_address_at(self, contract_address, declarator_address, idx, sender_address=ZERO_ADDRESS): - o = jsonrpc_template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('declarationAddressAt') - enc.typ(ABIContractType.ADDRESS) - enc.typ(ABIContractType.UINT256) - enc.address(declarator_address) - enc.uint256(idx) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - return o - - - def declarator_address_at(self, contract_address, subject_address, idx, sender_address=ZERO_ADDRESS): - o = jsonrpc_template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('declaratorAddressAt') - enc.typ(ABIContractType.ADDRESS) - enc.typ(ABIContractType.UINT256) - enc.address(subject_address) - enc.uint256(idx) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - return o - - - @classmethod - def parse_declarator_count(self, v): - return abi_decode_single(ABIContractType.UINT256, v) - - - @classmethod - def parse_declaration(self, v): - cursor = 0 - v = strip_0x(v) - position = int.from_bytes(bytes.fromhex(v[cursor:cursor+64]), 'big') - cursor += (position * 2) - length = int.from_bytes(bytes.fromhex(v[cursor:cursor+64]), 'big') - cursor += 64 - r = [] - for i in range(length): - r.append(v[cursor:cursor+64]) - cursor += 64 - return r - - - @classmethod - def parse_declaration_address_at(self, v): - return abi_decode_single(ABIContractType.ADDRESS, v) - - - @classmethod - def parse_declarator_address_at(self, v): - return abi_decode_single(ABIContractType.ADDRESS, v) diff --git a/python/eth_address_declarator/interface.py b/python/eth_address_declarator/interface.py @@ -0,0 +1,131 @@ +# standard imports +import logging +import json +import os + +# external imports +from hexathon import ( + strip_0x, + add_0x, + ) +from chainlib.eth.tx import ( + TxFormat, + TxFactory, + ) +from chainlib.eth.contract import ( + ABIContractEncoder, + ABIContractType, + abi_decode_single, + ) +from chainlib.jsonrpc import jsonrpc_template +from chainlib.eth.constant import ZERO_ADDRESS + +logg = logging.getLogger(__name__) + + +class Declarator(TxFactory): + + def add_declaration(self, contract_address, sender_address, subject_address, proof, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('addDeclaration') + enc.typ(ABIContractType.ADDRESS) + enc.typ(ABIContractType.BYTES32) + enc.address(subject_address) + enc.bytes32(proof) + data = enc.get() + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def declarator_count(self, contract_address, subject_address, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('declaratorCount') + enc.typ(ABIContractType.ADDRESS) + enc.address(subject_address) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + return o + + + def declaration(self, contract_address, declarator_address, subject_address, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('declaration') + enc.typ(ABIContractType.ADDRESS) + enc.typ(ABIContractType.ADDRESS) + enc.address(declarator_address) + enc.address(subject_address) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + return o + + + def declaration_address_at(self, contract_address, declarator_address, idx, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('declarationAddressAt') + enc.typ(ABIContractType.ADDRESS) + enc.typ(ABIContractType.UINT256) + enc.address(declarator_address) + enc.uint256(idx) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + return o + + + def declarator_address_at(self, contract_address, subject_address, idx, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('declaratorAddressAt') + enc.typ(ABIContractType.ADDRESS) + enc.typ(ABIContractType.UINT256) + enc.address(subject_address) + enc.uint256(idx) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + return o + + + @classmethod + def parse_declarator_count(self, v): + return abi_decode_single(ABIContractType.UINT256, v) + + + @classmethod + def parse_declaration(self, v): + cursor = 0 + v = strip_0x(v) + position = int.from_bytes(bytes.fromhex(v[cursor:cursor+64]), 'big') + cursor += (position * 2) + length = int.from_bytes(bytes.fromhex(v[cursor:cursor+64]), 'big') + cursor += 64 + r = [] + for i in range(length): + r.append(v[cursor:cursor+64]) + cursor += 64 + return r + + + @classmethod + def parse_declaration_address_at(self, v): + return abi_decode_single(ABIContractType.ADDRESS, v) + + + @classmethod + def parse_declarator_address_at(self, v): + return abi_decode_single(ABIContractType.ADDRESS, v) diff --git a/python/tests/test_addressdeclarator.py b/python/tests/test_addressdeclarator.py @@ -17,7 +17,8 @@ from giftable_erc20_token import GiftableToken from hexathon import add_0x # local imports -from eth_address_declarator import AddressDeclarator +from eth_address_declarator.declarator import AddressDeclarator +from eth_address_declarator import Declarator logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -74,17 +75,17 @@ class Test(EthTesterCase): d = add_0x(os.urandom(32).hex()) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.foo_token_address, d) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.bar_token_address, d) self.rpc.do(o) @@ -103,20 +104,20 @@ class Test(EthTesterCase): d_two = add_0x(os.urandom(32).hex()) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) self.rpc.do(o) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d_two) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d) self.rpc.do(o) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.bar_token_address, d) self.rpc.do(o) @@ -132,17 +133,17 @@ class Test(EthTesterCase): d = add_0x(os.urandom(32).hex()) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d) self.rpc.do(o) @@ -163,17 +164,17 @@ class Test(EthTesterCase): d = '0x' + os.urandom(32).hex() nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d) self.rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = AddressDeclarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d) self.rpc.do(o) diff --git a/solidity/AddressDeclarator.sol b/solidity/AddressDeclarator.sol @@ -6,6 +6,7 @@ contract AddressDeclarator { // EIP 173 address public owner; + address newOwner; mapping( address => address[] ) declarationIndex; mapping( bytes32 => uint256 ) declarationContentIndex; @@ -13,6 +14,8 @@ contract AddressDeclarator { mapping( address => address[] ) declaratorReverse; bytes32[][] public contents; + event DeclarationAdded(address _declarator, address _subject, bytes32 _proof); + constructor(bytes32 _initialDescription) public { bytes32[] memory foundation; @@ -48,14 +51,17 @@ contract AddressDeclarator { return k; } + // Implements Declarator function declaratorCount(address _subject) public view returns ( uint256 ) { return declarator[_subject].length; } + // Implements Declarator function declaratorAddressAt(address _subject, uint256 _idx) public view returns ( address ) { return declarator[_subject][_idx]; } + // Implements Declarator function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) { bytes32 k; bytes32[] memory declarationContents; @@ -74,6 +80,7 @@ contract AddressDeclarator { return true; } + // Implements Declarator function declaration(address _declarator, address _subject) public view returns ( bytes32[] memory ) { bytes32 k; uint256 idx; @@ -82,10 +89,12 @@ contract AddressDeclarator { return contents[idx]; } + // Implements Declarator function declarationCount(address _declarator) public view returns ( uint256 ) { return declarationIndex[_declarator].length; } + // Implements Declarator function declarationAddressAt(address _declarator, uint256 _idx) public view returns ( address ) { return declarationIndex[_declarator][_idx]; }