Split interface to separate file

This commit is contained in:
nolash 2021-04-30 17:13:58 +02:00
parent 6d673a0d8b
commit e3d4d6f478
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 160 additions and 122 deletions

View File

@ -1 +1 @@
from .declarator import AddressDeclarator from .interface import Declarator

View File

@ -26,6 +26,9 @@ from chainlib.eth.contract import (
from chainlib.jsonrpc import jsonrpc_template from chainlib.jsonrpc import jsonrpc_template
from chainlib.eth.constant import ZERO_ADDRESS from chainlib.eth.constant import ZERO_ADDRESS
# local imports
from eth_address_declarator import Declarator
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
moddir = os.path.dirname(__file__) moddir = os.path.dirname(__file__)
@ -39,7 +42,7 @@ def to_declarator_key(declarator_address_hex, declaration_address_hex):
return h.digest() return h.digest()
class AddressDeclarator(TxFactory): class AddressDeclarator(Declarator):
__abi = None __abi = None
__bytecode = None __bytecode = None
@ -76,109 +79,3 @@ class AddressDeclarator(TxFactory):
tx = self.template(sender_address, None, use_nonce=True) tx = self.template(sender_address, None, use_nonce=True)
tx = self.set_code(tx, code) tx = self.set_code(tx, code)
return self.build(tx) 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)

View File

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

View File

@ -17,7 +17,8 @@ from giftable_erc20_token import GiftableToken
from hexathon import add_0x from hexathon import add_0x
# local imports # 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) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
@ -74,17 +75,17 @@ class Test(EthTesterCase):
d = add_0x(os.urandom(32).hex()) d = add_0x(os.urandom(32).hex())
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.foo_token_address, d)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.bar_token_address, d)
self.rpc.do(o) self.rpc.do(o)
@ -103,20 +104,20 @@ class Test(EthTesterCase):
d_two = add_0x(os.urandom(32).hex()) d_two = add_0x(os.urandom(32).hex())
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
self.rpc.do(o) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d_two)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d)
self.rpc.do(o) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.bar_token_address, d)
self.rpc.do(o) self.rpc.do(o)
@ -132,17 +133,17 @@ class Test(EthTesterCase):
d = add_0x(os.urandom(32).hex()) d = add_0x(os.urandom(32).hex())
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d)
self.rpc.do(o) self.rpc.do(o)
@ -163,17 +164,17 @@ class Test(EthTesterCase):
d = '0x' + os.urandom(32).hex() d = '0x' + os.urandom(32).hex()
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d)
self.rpc.do(o) self.rpc.do(o)
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 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) (tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d)
self.rpc.do(o) self.rpc.do(o)

View File

@ -6,6 +6,7 @@ contract AddressDeclarator {
// EIP 173 // EIP 173
address public owner; address public owner;
address newOwner;
mapping( address => address[] ) declarationIndex; mapping( address => address[] ) declarationIndex;
mapping( bytes32 => uint256 ) declarationContentIndex; mapping( bytes32 => uint256 ) declarationContentIndex;
@ -13,6 +14,8 @@ contract AddressDeclarator {
mapping( address => address[] ) declaratorReverse; mapping( address => address[] ) declaratorReverse;
bytes32[][] public contents; bytes32[][] public contents;
event DeclarationAdded(address _declarator, address _subject, bytes32 _proof);
constructor(bytes32 _initialDescription) public { constructor(bytes32 _initialDescription) public {
bytes32[] memory foundation; bytes32[] memory foundation;
@ -48,14 +51,17 @@ contract AddressDeclarator {
return k; return k;
} }
// Implements Declarator
function declaratorCount(address _subject) public view returns ( uint256 ) { function declaratorCount(address _subject) public view returns ( uint256 ) {
return declarator[_subject].length; return declarator[_subject].length;
} }
// Implements Declarator
function declaratorAddressAt(address _subject, uint256 _idx) public view returns ( address ) { function declaratorAddressAt(address _subject, uint256 _idx) public view returns ( address ) {
return declarator[_subject][_idx]; return declarator[_subject][_idx];
} }
// Implements Declarator
function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) { function addDeclaration(address _subject, bytes32 _proof) public returns ( bool ) {
bytes32 k; bytes32 k;
bytes32[] memory declarationContents; bytes32[] memory declarationContents;
@ -74,6 +80,7 @@ contract AddressDeclarator {
return true; return true;
} }
// Implements Declarator
function declaration(address _declarator, address _subject) public view returns ( bytes32[] memory ) { function declaration(address _declarator, address _subject) public view returns ( bytes32[] memory ) {
bytes32 k; bytes32 k;
uint256 idx; uint256 idx;
@ -82,10 +89,12 @@ contract AddressDeclarator {
return contents[idx]; return contents[idx];
} }
// Implements Declarator
function declarationCount(address _declarator) public view returns ( uint256 ) { function declarationCount(address _declarator) public view returns ( uint256 ) {
return declarationIndex[_declarator].length; return declarationIndex[_declarator].length;
} }
// Implements Declarator
function declarationAddressAt(address _declarator, uint256 _idx) public view returns ( address ) { function declarationAddressAt(address _declarator, uint256 _idx) public view returns ( address ) {
return declarationIndex[_declarator][_idx]; return declarationIndex[_declarator][_idx];
} }