eth-address-index/python/eth_address_declarator/declarator.py

85 lines
2.5 KiB
Python
Raw Normal View History

2020-12-29 19:44:14 +01:00
# 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')
2020-12-29 21:19:12 +01:00
class AddressDeclarator:
2020-12-29 19:44:14 +01:00
__abi = None
__bytecode = None
__address = None
def __init__(self, w3, address, signer_address=None):
2020-12-29 21:19:12 +01:00
abi = AddressDeclarator.abi()
AddressDeclarator.bytecode()
2020-12-29 19:44:14 +01:00
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():
2020-12-29 21:19:12 +01:00
if AddressDeclarator.__abi == None:
2021-02-19 13:50:13 +01:00
f = open(os.path.join(datadir, 'AddressDeclarator.json'), 'r')
AddressDeclarator.__abi = json.load(f)
2020-12-29 19:44:14 +01:00
f.close()
2020-12-29 21:19:12 +01:00
return AddressDeclarator.__abi
2020-12-29 19:44:14 +01:00
@staticmethod
def bytecode():
2020-12-29 21:19:12 +01:00
if AddressDeclarator.__bytecode == None:
2021-02-19 13:50:13 +01:00
f = open(os.path.join(datadir, 'AddressDeclarator.bin'))
AddressDeclarator.__bytecode = f.read()
2020-12-29 19:44:14 +01:00
f.close()
2020-12-29 21:19:12 +01:00
return AddressDeclarator.__bytecode
2020-12-29 19:44:14 +01:00
# 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})
#
2021-02-16 15:38:32 +01:00
def to_declarator_key(declarator_address_hex, declaration_address_hex):
2020-12-29 19:44:14 +01:00
h = hashlib.new('sha256')
h.update(bytes.fromhex(token_address_hex[2:]))
h.update(bytes.fromhex(endorser_address_hex[2:]))
return h.digest()