2021-03-31 16:07:23 +02:00
|
|
|
# standard imports
|
2020-11-13 23:19:48 +01:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
# external imports
|
|
|
|
from chainlib.eth.tx import (
|
|
|
|
TxFactory,
|
|
|
|
TxFormat,
|
|
|
|
)
|
|
|
|
from chainlib.eth.contract import (
|
|
|
|
ABIContractEncoder,
|
|
|
|
ABIContractDecoder,
|
|
|
|
ABIContractType,
|
|
|
|
abi_decode_single,
|
|
|
|
)
|
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
from chainlib.jsonrpc import (
|
|
|
|
jsonrpc_template,
|
|
|
|
)
|
|
|
|
from chainlib.eth.error import RequestMismatchException
|
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2020-11-16 18:25:50 +01:00
|
|
|
moddir = os.path.dirname(__file__)
|
|
|
|
datadir = os.path.join(moddir, 'data')
|
|
|
|
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
class AccountRegistry(TxFactory):
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2020-11-16 18:25:50 +01:00
|
|
|
__abi = None
|
|
|
|
__bytecode = None
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2020-11-16 18:25:50 +01:00
|
|
|
@staticmethod
|
|
|
|
def abi():
|
|
|
|
if AccountRegistry.__abi == None:
|
2021-01-06 17:56:44 +01:00
|
|
|
f = open(os.path.join(datadir, 'AccountsIndex.json'), 'r')
|
2020-11-16 18:25:50 +01:00
|
|
|
AccountRegistry.__abi = json.load(f)
|
|
|
|
f.close()
|
|
|
|
return AccountRegistry.__abi
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def bytecode():
|
|
|
|
if AccountRegistry.__bytecode == None:
|
2020-12-07 16:41:48 +01:00
|
|
|
f = open(os.path.join(datadir, 'AccountsIndex.bin'))
|
2020-11-16 18:25:50 +01:00
|
|
|
AccountRegistry.__bytecode = f.read()
|
|
|
|
f.close()
|
|
|
|
return AccountRegistry.__bytecode
|
|
|
|
|
|
|
|
|
2021-03-31 16:07:23 +02:00
|
|
|
@staticmethod
|
|
|
|
def gas(code=None):
|
|
|
|
return 700000
|
|
|
|
|
|
|
|
|
|
|
|
def constructor(self, sender_address):
|
|
|
|
code = AccountRegistry.bytecode()
|
|
|
|
tx = self.template(sender_address, None, use_nonce=True)
|
|
|
|
tx = self.set_code(tx, code)
|
|
|
|
return self.build(tx)
|
|
|
|
|
|
|
|
|
|
|
|
def add(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('add', contract_address, sender_address, address, tx_format)
|
|
|
|
|
|
|
|
|
|
|
|
def add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('addWriter', contract_address, sender_address, address, tx_format)
|
|
|
|
|
|
|
|
|
|
|
|
def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('deleteWriter', contract_address, sender_address, address, tx_format)
|
|
|
|
|
|
|
|
|
|
|
|
def __single_address_method(self, method, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method(method)
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
enc.address(address)
|
|
|
|
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 have(self, contract_address, address, sender_address=ZERO_ADDRESS):
|
|
|
|
o = jsonrpc_template()
|
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('have')
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
enc.address(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 count(self, contract_address, sender_address=ZERO_ADDRESS):
|
|
|
|
o = jsonrpc_template()
|
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('count')
|
|
|
|
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 account(self, contract_address, idx, sender_address=ZERO_ADDRESS):
|
|
|
|
o = jsonrpc_template()
|
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('accounts')
|
|
|
|
enc.typ(ABIContractType.UINT256)
|
|
|
|
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_account(self, v):
|
|
|
|
return abi_decode_single(ABIContractType.ADDRESS, v)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parse_have(self, v):
|
|
|
|
return abi_decode_single(ABIContractType.BOOLEAN, v)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parse_add_request(self, v):
|
|
|
|
v = strip_0x(v)
|
|
|
|
cursor = 0
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('add')
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
r = enc.get()
|
|
|
|
l = len(r)
|
|
|
|
m = v[:l]
|
|
|
|
if m != r:
|
|
|
|
logg.error('method mismatch, expected {}, got {}'.format(r, m))
|
|
|
|
raise RequestMismatchException(v)
|
|
|
|
cursor += l
|
|
|
|
|
|
|
|
dec = ABIContractDecoder()
|
|
|
|
dec.typ(ABIContractType.ADDRESS)
|
|
|
|
dec.val(v[cursor:cursor+64])
|
|
|
|
r = dec.decode()
|
|
|
|
return r
|