eth-address-index/python/eth_address_declarator/declarator.py
2021-02-19 13:50:13 +01:00

85 lines
2.5 KiB
Python

# Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
# SPDX-License-Identifier: GPL-3.0-or-later
# File-version: 1
# Description: Python interface to abi and bin files for faucet contracts
# standard imports
import logging
import json
import os
import hashlib
logg = logging.getLogger(__name__)
moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class AddressDeclarator:
__abi = None
__bytecode = None
__address = None
def __init__(self, w3, address, signer_address=None):
abi = AddressDeclarator.abi()
AddressDeclarator.bytecode()
self.__address = address
self.contract = w3.eth.contract(abi=abi, address=address)
self.w3 = w3
if signer_address != None:
self.signer_address = signer_address
else:
if type(self.w3.eth.defaultAccount).__name__ == 'Empty':
self.w3.eth.defaultAccount = self.w3.eth.accounts[0]
self.signer_address = self.w3.eth.defaultAccount
@staticmethod
def abi():
if AddressDeclarator.__abi == None:
f = open(os.path.join(datadir, 'AddressDeclarator.json'), 'r')
AddressDeclarator.__abi = json.load(f)
f.close()
return AddressDeclarator.__abi
@staticmethod
def bytecode():
if AddressDeclarator.__bytecode == None:
f = open(os.path.join(datadir, 'AddressDeclarator.bin'))
AddressDeclarator.__bytecode = f.read()
f.close()
return AddressDeclarator.__bytecode
# def token_from_symbol(self, symbol):
# return self.contract.functions.tokenSymbolIndex(symbol).call()
# def
#
# def endorsed_tokens(self, endorser_address):
# tokens = []
# for i in range(self.contract.functions.endorserTokenCount(endorser_address).call()):
# tidx = self.contract.functions.endorser(endorser_address, i).call()
# t = self.contract.functions.tokens(tidx).call()
# tokens.append(t)
# return tokens
#
# def declared(self, declarator_address):
# addresses = []
# for i
#
#
# def add(self, token_address, data):
# self.contract.functions.add(token_address, data).transact({'from': self.signer_address})
#
def to_declarator_key(declarator_address_hex, declaration_address_hex):
h = hashlib.new('sha256')
h.update(bytes.fromhex(token_address_hex[2:]))
h.update(bytes.fromhex(endorser_address_hex[2:]))
return h.digest()