mirror of
git://holbrook.no/eth-address-index
synced 2024-11-19 23:56:47 +01:00
185 lines
5.6 KiB
Python
185 lines
5.6 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
|
|
|
|
# external imports
|
|
from hexathon import (
|
|
strip_0x,
|
|
add_0x,
|
|
)
|
|
from chainlib.eth.tx import (
|
|
TxFormat,
|
|
TxFactory,
|
|
)
|
|
from chainlib.eth.contract import (
|
|
ABIContractEncoder,
|
|
ABIContractType,
|
|
abi_decode_single,
|
|
)
|
|
from chainlib.jsonrpc import jsonrpc_template
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
moddir = os.path.dirname(__file__)
|
|
datadir = os.path.join(moddir, 'data')
|
|
|
|
|
|
def to_declarator_key(declarator_address_hex, declaration_address_hex):
|
|
h = hashlib.new('sha256')
|
|
h.update(bytes.fromhex(strip_0x(declaration_address_hex)))
|
|
h.update(bytes.fromhex(strip_0x(declarator_address_hex)))
|
|
return h.digest()
|
|
|
|
|
|
class AddressDeclarator(TxFactory):
|
|
|
|
__abi = None
|
|
__bytecode = None
|
|
|
|
@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
|
|
|
|
|
|
@staticmethod
|
|
def gas(code=None):
|
|
return 2000000
|
|
|
|
|
|
def constructor(self, sender_address, initial_description):
|
|
code = AddressDeclarator.bytecode()
|
|
enc = ABIContractEncoder()
|
|
initial_description_hex = add_0x(initial_description)
|
|
enc.bytes32(initial_description_hex)
|
|
code += enc.get()
|
|
tx = self.template(sender_address, None, use_nonce=True)
|
|
tx = self.set_code(tx, code)
|
|
return self.build(tx)
|
|
|
|
|
|
def add_declaration(self, contract_address, sender_address, subject_address, proof, tx_format=TxFormat.JSONRPC):
|
|
enc = ABIContractEncoder()
|
|
enc.method('addDeclaration')
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
enc.typ(ABIContractType.BYTES32)
|
|
enc.address(subject_address)
|
|
enc.bytes32(proof)
|
|
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 declarator_count(self, contract_address, subject_address, sender_address=ZERO_ADDRESS):
|
|
o = jsonrpc_template()
|
|
o['method'] = 'eth_call'
|
|
enc = ABIContractEncoder()
|
|
enc.method('declaratorCount')
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
enc.address(subject_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 declaration(self, contract_address, declarator_address, subject_address, sender_address=ZERO_ADDRESS):
|
|
o = jsonrpc_template()
|
|
o['method'] = 'eth_call'
|
|
enc = ABIContractEncoder()
|
|
enc.method('declaration')
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
enc.address(declarator_address)
|
|
enc.address(subject_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 declaration_address_at(self, contract_address, declarator_address, idx, sender_address=ZERO_ADDRESS):
|
|
o = jsonrpc_template()
|
|
o['method'] = 'eth_call'
|
|
enc = ABIContractEncoder()
|
|
enc.method('declarationAddressAt')
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
enc.typ(ABIContractType.UINT256)
|
|
enc.address(declarator_address)
|
|
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
|
|
|
|
|
|
def declarator_address_at(self, contract_address, subject_address, idx, sender_address=ZERO_ADDRESS):
|
|
o = jsonrpc_template()
|
|
o['method'] = 'eth_call'
|
|
enc = ABIContractEncoder()
|
|
enc.method('declaratorAddressAt')
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
enc.typ(ABIContractType.UINT256)
|
|
enc.address(subject_address)
|
|
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_declarator_count(self, v):
|
|
return abi_decode_single(ABIContractType.UINT256, v)
|
|
|
|
|
|
@classmethod
|
|
def parse_declaration(self, v):
|
|
cursor = 0
|
|
v = strip_0x(v)
|
|
position = int.from_bytes(bytes.fromhex(v[cursor:cursor+64]), 'big')
|
|
cursor += (position * 2)
|
|
length = int.from_bytes(bytes.fromhex(v[cursor:cursor+64]), 'big')
|
|
cursor += 64
|
|
r = []
|
|
for i in range(length):
|
|
r.append(v[cursor:cursor+64])
|
|
cursor += 64
|
|
return r
|
|
|
|
|
|
@classmethod
|
|
def parse_declaration_address_at(self, v):
|
|
return abi_decode_single(ABIContractType.ADDRESS, v)
|
|
|
|
|
|
@classmethod
|
|
def parse_declarator_address_at(self, v):
|
|
return abi_decode_single(ABIContractType.ADDRESS, v)
|