Add EIP165 support

This commit is contained in:
nolash
2021-04-29 08:35:53 +02:00
parent 3241dfa9c5
commit 219c24b624
9 changed files with 144 additions and 9 deletions

View File

@@ -24,6 +24,7 @@ re_method = r'^[a-zA-Z0-9_]+$'
class ABIContractType(enum.Enum):
BYTES32 = 'bytes32'
BYTES4 = 'bytes4'
UINT256 = 'uint256'
ADDRESS = 'address'
STRING = 'string'
@@ -154,6 +155,12 @@ class ABIContractEncoder:
self.__log_latest(v)
def bytes4(self, v):
self.bytes_fixed(4, v)
self.types.append(ABIContractType.BYTES4)
self.__log_latest(v)
def string(self, v):
b = v.encode('utf-8')
l = len(b)
@@ -186,8 +193,7 @@ class ABIContractEncoder:
v = pad(b.hex(), mx)
else:
raise ValueError('invalid input {}'.format(typ))
self.contents.append(v)
self.contents.append(v.ljust(64, '0'))
def get_method(self):

38
chainlib/eth/eip165.py Normal file
View File

@@ -0,0 +1,38 @@
# standard imports
from chainlib.eth.constant import ZERO_ADDRESS
# external imports
from chainlib.jsonrpc import (
jsonrpc_template,
)
from hexathon import (
add_0x,
)
from chainlib.eth.contract import (
ABIContractEncoder,
ABIContractDecoder,
ABIContractType,
abi_decode_single,
)
from chainlib.eth.tx import TxFactory
class EIP165(TxFactory):
def supports_interface(self, contract_address, interface_sum, sender_address=ZERO_ADDRESS):
o = jsonrpc_template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
enc.method('supportsInterface')
enc.typ(ABIContractType.BYTES4)
enc.bytes4(interface_sum)
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_supports_interface(self, v):
return abi_decode_single(ABIContractType.BOOLEAN, v)

View File

@@ -168,6 +168,7 @@ def transaction(hsh):
o['params'].append(add_0x(hsh))
return o
def transaction_by_block(hsh, idx):
o = jsonrpc_template()
o['method'] = 'eth_getTransactionByBlockHashAndIndex'

View File

@@ -27,7 +27,7 @@ from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.encoding import private_key_to_address
logg = logging.getLogger()
logg = logging.getLogger().getChild(__name__)
test_pk = bytes.fromhex('5087503f0a9cc35b38665955eb830c63f778453dd11b8fa5bd04bc41fd2cc6d6')
@@ -99,8 +99,12 @@ class TestRPCConnection(RPCConnection):
def eth_getTransactionByBlock(self, p):
block = self.eth_getBlockByHash(p)
tx_hash = block['transactions'][p[1]]
tx = self.eth_getTransaction([tx_hash])
try:
tx_index = int(p[1], 16)
except TypeError:
tx_index = int(p[1])
tx_hash = block['transactions'][tx_index]
tx = self.eth_getTransactionByHash([tx_hash])
return tx
def eth_getBalance(self, p):
@@ -120,6 +124,14 @@ class TestRPCConnection(RPCConnection):
return tx
def eth_getTransactionByBlockHashAndIndex(self, p):
#logg.debug('p {}'.format(p))
#block = self.eth_getBlockByHash(p[0])
#tx = block.transactions[p[1]]
#return eth_getTransactionByHash(tx[0])
return self.eth_getTransactionByBlock(p)
def eth_getTransactionReceipt(self, p):
rcpt = self.backend.get_transaction_receipt(p[0])
if rcpt.get('block_number') == None:

View File

@@ -19,7 +19,10 @@ from .base import (
EthTesterSigner,
TestRPCConnection,
)
from chainlib.connection import RPCConnection
from chainlib.connection import (
RPCConnection,
ConnType,
)
from chainlib.eth.address import to_checksum_address
from chainlib.chain import ChainSpec
@@ -66,8 +69,10 @@ class EthTesterCase(unittest.TestCase):
def rpc_with_tester(chain_spec=self.chain_spec, url=None):
return self.rpc
RPCConnection.register_location('custom', self.chain_spec, tag='default', constructor=rpc_with_tester, exist_ok=True)
RPCConnection.register_location('custom', self.chain_spec, tag='signer', constructor=rpc_with_tester, exist_ok=True)
RPCConnection.register_constructor(ConnType.CUSTOM, rpc_with_tester, tag='default')
RPCConnection.register_constructor(ConnType.CUSTOM, rpc_with_tester, tag='signer')
RPCConnection.register_location('custom', self.chain_spec, tag='default', exist_ok=True)
RPCConnection.register_location('custom', self.chain_spec, tag='signer', exist_ok=True)