eth-accounts-index

Accounts index evm contract tooling with permissioned writes
Info | Log | Files | Refs

registry.py (2083B)


      1 # standard imports
      2 import os
      3 import json
      4 
      5 import pathlib
      6 # external imports
      7 from chainlib.eth.tx import (
      8     TxFormat,
      9 )
     10 from chainlib.eth.contract import (
     11     ABIContractEncoder,
     12     ABIContractType,
     13 )
     14 
     15 # local imports
     16 from .interface import AccountsIndex
     17 
     18 moddir = os.path.dirname(__file__)
     19 datadir = os.path.join(moddir, 'data')
     20 
     21 
     22 class AccountRegistry(AccountsIndex):
     23     __abi = None
     24     __bytecode = None
     25 
     26     @staticmethod
     27     def abi():
     28         if AccountRegistry.__abi is None:
     29             with open(os.path.join(datadir, 'AccountsIndex.json'), 'r') as f:
     30                 AccountRegistry.__abi = json.load(f)
     31         return AccountRegistry.__abi
     32 
     33     @staticmethod
     34     def bytecode():
     35         if AccountRegistry.__bytecode is None:
     36             AccountRegistry.__bytecode = pathlib.Path(
     37                 os.path.join(datadir, 'AccountsIndex.bin')
     38             ).read_text()
     39         return AccountRegistry.__bytecode
     40 
     41     @staticmethod
     42     def gas(code=None):
     43         return 1200000
     44 
     45     def constructor(self, sender_address):
     46         code = AccountRegistry.bytecode()
     47         tx = self.template(sender_address, None, use_nonce=True)
     48         tx = self.set_code(tx, code)
     49         return self.build(tx)
     50 
     51     def __single_address_method(self, method, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
     52         enc = ABIContractEncoder()
     53         enc.method(method)
     54         enc.typ(ABIContractType.ADDRESS)
     55         enc.address(address)
     56         data = enc.get()
     57         tx = self.template(sender_address, contract_address, use_nonce=True)
     58         tx = self.set_code(tx, data)
     59         tx = self.finalize(tx, tx_format)
     60         return tx
     61 
     62     def add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
     63         return self.__single_address_method('addWriter', contract_address, sender_address, address, tx_format)
     64 
     65     def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
     66         return self.__single_address_method('deleteWriter', contract_address, sender_address, address, tx_format)