mirror of
git://holbrook.no/eth-contract-registry
synced 2026-05-16 17:45:20 +02:00
Rename package
This commit is contained in:
1
python/eth_contract_registry/__init__.py
Normal file
1
python/eth_contract_registry/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .registry import Registry
|
||||
1
python/eth_contract_registry/data/Registry.bin
Normal file
1
python/eth_contract_registry/data/Registry.bin
Normal file
File diff suppressed because one or more lines are too long
1
python/eth_contract_registry/data/Registry.json
Normal file
1
python/eth_contract_registry/data/Registry.json
Normal file
@@ -0,0 +1 @@
|
||||
[{"inputs":[{"internalType":"bytes32[]","name":"_identifiers","type":"bytes32[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"}],"name":"addressOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"}],"name":"chainOf","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_chain","type":"bytes32"}],"name":"configSumOf","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"identifiers","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"},{"internalType":"address","name":"_address","type":"address"},{"internalType":"bytes32","name":"_chainDescriptor","type":"bytes32"},{"internalType":"bytes32","name":"_chainConfig","type":"bytes32"}],"name":"set","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
|
||||
32
python/eth_contract_registry/encoding.py
Normal file
32
python/eth_contract_registry/encoding.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# external imports
|
||||
from hexathon import strip_0x
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def to_text(b):
|
||||
b = b[:b.find(0)]
|
||||
# TODO: why was this part of this method previously?
|
||||
# if len(b) % 2 > 0:
|
||||
# b = b'\x00' + b
|
||||
return b.decode('utf-8')
|
||||
|
||||
|
||||
def from_text(txt):
|
||||
return '0x{:0<64s}'.format(txt.encode('utf-8').hex())
|
||||
|
||||
|
||||
def from_identifier(b):
|
||||
return to_text(b)
|
||||
|
||||
|
||||
def from_identifier_hex(hx):
|
||||
b = bytes.fromhex(strip_0x(hx))
|
||||
return from_identifier(b)
|
||||
|
||||
|
||||
def to_identifier(txt):
|
||||
return from_text(txt)
|
||||
1
python/eth_contract_registry/pytest/__init__.py
Normal file
1
python/eth_contract_registry/pytest/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .fixtures_registry import *
|
||||
74
python/eth_contract_registry/pytest/fixtures_registry.py
Normal file
74
python/eth_contract_registry/pytest/fixtures_registry.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# standard imports
|
||||
import json
|
||||
import logging
|
||||
import hashlib
|
||||
|
||||
# external imports
|
||||
from hexathon import add_0x
|
||||
import pytest
|
||||
from chainlib.connection import RPCConnection
|
||||
from chainlib.eth.tx import receipt
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
|
||||
# local imports
|
||||
from contract_registry.registry import Registry
|
||||
from contract_registry.encoding import to_identifier
|
||||
|
||||
#logg = logging.getLogger(__name__)
|
||||
logg = logging.getLogger()
|
||||
|
||||
valid_identifiers = [
|
||||
'ContractRegistry',
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def roles(
|
||||
eth_accounts,
|
||||
):
|
||||
return {
|
||||
'DEFAULT': eth_accounts[0],
|
||||
'CONTRACT_DEPLOYER': eth_accounts[1],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def registry(
|
||||
default_chain_spec,
|
||||
default_chain_config,
|
||||
init_eth_tester,
|
||||
eth_rpc,
|
||||
eth_accounts,
|
||||
eth_signer,
|
||||
roles,
|
||||
):
|
||||
|
||||
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc)
|
||||
|
||||
builder = Registry(signer=eth_signer, nonce_oracle=nonce_oracle)
|
||||
logg.info('registering identifiers {} in contract registry'.format(valid_identifiers))
|
||||
(tx_hash_hex, o) = builder.constructor(roles['CONTRACT_DEPLOYER'], valid_identifiers)
|
||||
r = eth_rpc.do(o)
|
||||
|
||||
o = receipt(r)
|
||||
rcpt = eth_rpc.do(o)
|
||||
assert rcpt['status'] == 1
|
||||
|
||||
registry_address = rcpt['contract_address']
|
||||
|
||||
c = Registry(signer=eth_signer, nonce_oracle=nonce_oracle)
|
||||
|
||||
chain_spec_identifier = to_identifier(str(default_chain_spec))
|
||||
|
||||
h = hashlib.new('sha256')
|
||||
j = json.dumps(default_chain_config)
|
||||
h.update(j.encode('utf-8'))
|
||||
z = h.digest()
|
||||
chain_config_digest = add_0x(z.hex())
|
||||
(tx_hash_hex, o) = c.set(registry_address, roles['CONTRACT_DEPLOYER'], 'ContractRegistry', registry_address, chain_spec_identifier, chain_config_digest)
|
||||
r = eth_rpc.do(o)
|
||||
o = receipt(tx_hash_hex)
|
||||
r = eth_rpc.do(o)
|
||||
assert r['status'] == 1
|
||||
|
||||
return registry_address
|
||||
150
python/eth_contract_registry/registry.py
Normal file
150
python/eth_contract_registry/registry.py
Normal file
@@ -0,0 +1,150 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
# third-party imports
|
||||
from chainlib.eth.contract import (
|
||||
ABIContractEncoder,
|
||||
ABIContractType,
|
||||
abi_decode_single,
|
||||
)
|
||||
from chainlib.chain import ChainSpec
|
||||
from chainlib.eth.constant import (
|
||||
ZERO_ADDRESS,
|
||||
ZERO_CONTENT,
|
||||
MAX_UINT,
|
||||
)
|
||||
from chainlib.eth.rpc import (
|
||||
jsonrpc_template,
|
||||
)
|
||||
from hexathon import (
|
||||
even,
|
||||
add_0x,
|
||||
)
|
||||
from chainlib.eth.tx import TxFactory
|
||||
|
||||
# local imports
|
||||
from .encoding import to_identifier
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
moddir = os.path.dirname(__file__)
|
||||
datadir = os.path.join(moddir, 'data')
|
||||
|
||||
|
||||
class Registry(TxFactory):
|
||||
|
||||
default_chain_spec = None
|
||||
__chains_registry = {}
|
||||
|
||||
__abi = None
|
||||
__bytecode = None
|
||||
|
||||
|
||||
|
||||
@staticmethod
|
||||
def abi():
|
||||
if Registry.__abi == None:
|
||||
f = open(os.path.join(datadir, 'Registry.json'), 'r')
|
||||
Registry.__abi = json.load(f)
|
||||
f.close()
|
||||
return Registry.__abi
|
||||
|
||||
|
||||
@staticmethod
|
||||
def bytecode():
|
||||
if Registry.__bytecode == None:
|
||||
f = open(os.path.join(datadir, 'Registry.bin'))
|
||||
Registry.__bytecode = f.read()
|
||||
f.close()
|
||||
return Registry.__bytecode
|
||||
|
||||
|
||||
@staticmethod
|
||||
def gas(code=None):
|
||||
return 1500000
|
||||
|
||||
def constructor(self, sender_address, identifier_strings=[]):
|
||||
# TODO: handle arrays in chainlib encode instead
|
||||
enc = ABIContractEncoder()
|
||||
enc.uint256(32)
|
||||
enc.uint256(len(identifier_strings))
|
||||
for s in identifier_strings:
|
||||
enc.bytes32(to_identifier(s))
|
||||
data = enc.get_contents()
|
||||
|
||||
tx = self.template(sender_address, None, use_nonce=True)
|
||||
tx = self.set_code(tx, Registry.bytecode() + data)
|
||||
logg.debug('bytecode {}\ndata {}\ntx {}'.format(Registry.bytecode(), data, tx))
|
||||
return self.build(tx)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def address(address=None):
|
||||
if address != None:
|
||||
Registry.__address = address
|
||||
return Registry.__address
|
||||
|
||||
|
||||
@staticmethod
|
||||
def load_for(chain_spec):
|
||||
chain_str = str(chain_spec)
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def address_of(self, contract_address, identifier_string, sender_address=ZERO_ADDRESS):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_call'
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('addressOf')
|
||||
enc.typ(ABIContractType.BYTES32)
|
||||
identifier = to_identifier(identifier_string)
|
||||
enc.bytes32(identifier)
|
||||
data = add_0x(enc.encode())
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
return o
|
||||
|
||||
|
||||
@classmethod
|
||||
def parse_address_of(self, v):
|
||||
return abi_decode_single(ABIContractType.ADDRESS, v)
|
||||
|
||||
|
||||
def set(self, contract_address, sender_address, identifier_string, address, chain_spec, chain_config_hash):
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('set')
|
||||
enc.typ(ABIContractType.BYTES32)
|
||||
enc.typ(ABIContractType.ADDRESS)
|
||||
enc.typ(ABIContractType.BYTES32)
|
||||
enc.typ(ABIContractType.BYTES32)
|
||||
identifier = to_identifier(identifier_string)
|
||||
enc.bytes32(identifier)
|
||||
enc.address(address)
|
||||
chain_str = str(chain_spec)
|
||||
h = hashlib.new('sha256')
|
||||
h.update(chain_str.encode('utf-8'))
|
||||
chain_description_hash_bytes = h.digest()
|
||||
enc.bytes32(chain_description_hash_bytes.hex())
|
||||
enc.bytes32(chain_config_hash)
|
||||
data = enc.encode()
|
||||
tx = self.template(sender_address, contract_address, use_nonce=True)
|
||||
tx = self.set_code(tx, data)
|
||||
return self.build(tx)
|
||||
|
||||
|
||||
def identifier(self, contract_address, idx, sender_address=ZERO_ADDRESS):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_call'
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('identifiers')
|
||||
enc.typ(ABIContractType.UINT256)
|
||||
enc.uint256(idx)
|
||||
data = add_0x(enc.encode())
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
return o
|
||||
101
python/eth_contract_registry/runnable/deploy.py
Normal file
101
python/eth_contract_registry/runnable/deploy.py
Normal file
@@ -0,0 +1,101 @@
|
||||
"""Deploys contract registry
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# standard imports
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
# third-party imports
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
|
||||
from crypto_dev_signer.keystore.dict import DictKeystore
|
||||
from crypto_dev_signer.eth.helper import EthTxExecutor
|
||||
from chainlib.chain import ChainSpec
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
from chainlib.eth.gas import RPCGasOracle
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.tx import receipt
|
||||
|
||||
# local imports
|
||||
from contract_registry import Registry
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
script_dir = os.path.dirname(__file__)
|
||||
data_dir = os.path.join(script_dir, '..', 'data')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
|
||||
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
|
||||
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('--identifier', type=str, action='append', help='Add contract identifier')
|
||||
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
|
||||
args = argparser.parse_args()
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
block_last = args.w
|
||||
block_all = args.ww
|
||||
|
||||
passphrase_env = 'ETH_PASSPHRASE'
|
||||
if args.env_prefix != None:
|
||||
passphrase_env = args.env_prefix + '_' + passphrase_env
|
||||
passphrase = os.environ.get(passphrase_env)
|
||||
if passphrase == None:
|
||||
logg.warning('no passphrase given')
|
||||
passphrase=''
|
||||
|
||||
signer_address = None
|
||||
keystore = DictKeystore()
|
||||
if args.y != None:
|
||||
logg.debug('loading keystore file {}'.format(args.y))
|
||||
signer_address = keystore.import_keystore_file(args.y, password=passphrase)
|
||||
logg.debug('now have key for signer address {}'.format(signer_address))
|
||||
signer = EIP155Signer(keystore)
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
chain_id = chain_spec.network_id()
|
||||
|
||||
rpc = EthHTTPConnection(args.p)
|
||||
nonce_oracle = RPCNonceOracle(signer_address, rpc)
|
||||
gas_oracle = RPCGasOracle(rpc, code_callback=Registry.gas)
|
||||
identifiers = args.identifier
|
||||
if 'ContractRegistry' not in identifiers:
|
||||
identifiers = ['ContractRegistry'] + identifiers
|
||||
logg.debug('adding identifiers {}'.format(identifiers))
|
||||
|
||||
def main():
|
||||
c = Registry(signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, chain_id=chain_id)
|
||||
(tx_hash_hex, o) = c.constructor(signer_address, identifiers)
|
||||
rpc.do(o)
|
||||
if block_last:
|
||||
r = rpc.wait(tx_hash_hex)
|
||||
if r['status'] == 0:
|
||||
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
|
||||
sys.exit(1)
|
||||
# TODO: pass through translator for keys (evm tester uses underscore instead of camelcase)
|
||||
address = r['contractAddress']
|
||||
|
||||
print(address)
|
||||
else:
|
||||
print(tx_hash_hex)
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
103
python/eth_contract_registry/runnable/set.py
Normal file
103
python/eth_contract_registry/runnable/set.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""Set identifier value on contract registry
|
||||
|
||||
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
|
||||
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
|
||||
"""
|
||||
|
||||
# standard imports
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
# third-party imports
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
|
||||
from crypto_dev_signer.keystore.dict import DictKeystore
|
||||
from crypto_dev_signer.eth.helper import EthTxExecutor
|
||||
from chainlib.chain import ChainSpec
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
from chainlib.eth.gas import RPCGasOracle
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.tx import receipt
|
||||
from chainlib.eth.constant import ZERO_CONTENT
|
||||
|
||||
# local imports
|
||||
from contract_registry import Registry
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
script_dir = os.path.dirname(__file__)
|
||||
data_dir = os.path.join(script_dir, '..', 'data')
|
||||
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
|
||||
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
|
||||
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
|
||||
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
|
||||
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
|
||||
argparser.add_argument('-a', '--contract-address', dest='a', type=str, help='Alias for -r')
|
||||
argparser.add_argument('-r', '--registry', dest='r', type=str, help='Contract registry address')
|
||||
argparser.add_argument('-v', action='store_true', help='Be verbose')
|
||||
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
|
||||
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
|
||||
argparser.add_argument('identifier', type=str, help='Contract registry identifier')
|
||||
argparser.add_argument('address', type=str, help='Address to set for identifier')
|
||||
args = argparser.parse_args()
|
||||
|
||||
if args.vv:
|
||||
logg.setLevel(logging.DEBUG)
|
||||
elif args.v:
|
||||
logg.setLevel(logging.INFO)
|
||||
|
||||
block_last = args.w
|
||||
block_all = args.ww
|
||||
|
||||
passphrase_env = 'ETH_PASSPHRASE'
|
||||
if args.env_prefix != None:
|
||||
passphrase_env = args.env_prefix + '_' + passphrase_env
|
||||
passphrase = os.environ.get(passphrase_env)
|
||||
if passphrase == None:
|
||||
logg.warning('no passphrase given')
|
||||
passphrase=''
|
||||
|
||||
signer_address = None
|
||||
keystore = DictKeystore()
|
||||
if args.y != None:
|
||||
logg.debug('loading keystore file {}'.format(args.y))
|
||||
signer_address = keystore.import_keystore_file(args.y, password=passphrase)
|
||||
logg.debug('now have key for signer address {}'.format(signer_address))
|
||||
signer = EIP155Signer(keystore)
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(args.i)
|
||||
chain_id = chain_spec.network_id()
|
||||
|
||||
rpc = EthHTTPConnection(args.p)
|
||||
nonce_oracle = RPCNonceOracle(signer_address, rpc)
|
||||
gas_oracle = RPCGasOracle(rpc, code_callback=Registry.gas)
|
||||
registry_address = args.r
|
||||
if registry_address == None:
|
||||
registry_address = args.a
|
||||
if registry_address == None:
|
||||
raise ValueError('Registry address must be set')
|
||||
identifier = args.identifier
|
||||
address = args.address
|
||||
|
||||
|
||||
def main():
|
||||
c = Registry(signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, chain_id=chain_id)
|
||||
(tx_hash_hex, o) = c.set(registry_address, signer_address, identifier, address, chain_spec, ZERO_CONTENT)
|
||||
rpc.do(o)
|
||||
if block_last:
|
||||
r = rpc.wait(tx_hash_hex)
|
||||
if r['status'] == 0:
|
||||
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
|
||||
sys.exit(1)
|
||||
|
||||
print(tx_hash_hex)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
0
python/eth_contract_registry/unittest/base.py
Normal file
0
python/eth_contract_registry/unittest/base.py
Normal file
Reference in New Issue
Block a user