declarator.py (2069B)
1 # Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 2 # SPDX-License-Identifier: GPL-3.0-or-later 3 # File-version: 1 4 # Description: Python interface to abi and bin files for faucet contracts 5 6 # standard imports 7 import logging 8 import json 9 import os 10 import hashlib 11 12 # external imports 13 from hexathon import ( 14 strip_0x, 15 add_0x, 16 ) 17 from chainlib.eth.tx import ( 18 TxFormat, 19 TxFactory, 20 ) 21 from chainlib.eth.contract import ( 22 ABIContractEncoder, 23 ABIContractType, 24 abi_decode_single, 25 ) 26 from chainlib.eth.constant import ZERO_ADDRESS 27 28 # local imports 29 from eth_address_declarator import Declarator 30 31 logg = logging.getLogger(__name__) 32 33 moddir = os.path.dirname(__file__) 34 datadir = os.path.join(moddir, 'data') 35 36 37 def to_declarator_key(declarator_address_hex, declaration_address_hex): 38 h = hashlib.new('sha256') 39 h.update(bytes.fromhex(strip_0x(declaration_address_hex))) 40 h.update(bytes.fromhex(strip_0x(declarator_address_hex))) 41 return h.digest() 42 43 44 class AddressDeclarator(Declarator): 45 46 __abi = None 47 __bytecode = None 48 49 @staticmethod 50 def abi(): 51 if AddressDeclarator.__abi == None: 52 f = open(os.path.join(datadir, 'AddressDeclarator.json'), 'r') 53 AddressDeclarator.__abi = json.load(f) 54 f.close() 55 return AddressDeclarator.__abi 56 57 58 @staticmethod 59 def bytecode(): 60 if AddressDeclarator.__bytecode == None: 61 f = open(os.path.join(datadir, 'AddressDeclarator.bin')) 62 AddressDeclarator.__bytecode = f.read() 63 f.close() 64 return AddressDeclarator.__bytecode 65 66 67 @staticmethod 68 def gas(code=None): 69 return 2000000 70 71 72 def constructor(self, sender_address, description): 73 code = AddressDeclarator.bytecode() 74 enc = ABIContractEncoder() 75 enc.bytes32(description) 76 code += enc.get() 77 tx = self.template(sender_address, None, use_nonce=True) 78 tx = self.set_code(tx, code) 79 return self.build(tx)