From 41e41ad2209ef157dd32a5a83c021b5807a4ffd4 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 11 Oct 2021 20:08:18 +0200 Subject: [PATCH] Add keystore dir parsing for signer for eth target --- cic/__init__.py | 1 + cic/cmd/export.py | 21 ++++++++++++++++----- cic/data/config/config.ini | 1 + cic/ext/eth/__init__.py | 14 +++++++++++--- cic/ext/eth/signer.py | 33 +++++++++++++++++++++++++++++++++ tests/eth/base_eth.py | 12 +++++++++--- 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 cic/ext/eth/signer.py diff --git a/cic/__init__.py b/cic/__init__.py index 2073594..9c13049 100644 --- a/cic/__init__.py +++ b/cic/__init__.py @@ -1 +1,2 @@ from .proof import Proof +from .processor import Processor diff --git a/cic/cmd/export.py b/cic/cmd/export.py index ccd0dfe..8739809 100644 --- a/cic/cmd/export.py +++ b/cic/cmd/export.py @@ -1,9 +1,13 @@ # standard imports import logging import importlib +import os # local imports -from cic import Proof +from cic import ( + Proof, + Processor, + ) from cic.meta import Meta from cic.attachment import Attachment from cic.network import Network @@ -14,7 +18,7 @@ logg = logging.getLogger(__name__) def process_args(argparser): argparser.add_argument('-d', '--directory', type=str, dest='directory', default='.', help='directory') - argparser.add_argument('-y', '--signer', type=str, dest='signer', help='target-specific signer to use for export') + argparser.add_argument('-y', '--signer', type=str, dest='y', help='target-specific signer to use for export') argparser.add_argument('target', type=str, help='target network type') @@ -27,6 +31,7 @@ def init_writers_from_config(config): 'meta': None, 'attachment': None, 'proof': None, + 'ext': None, } for v in w.keys(): k = 'CIC_CORE_{}_WRITER'.format(v.upper()) @@ -36,7 +41,7 @@ def init_writers_from_config(config): w[v] = o return w - + def execute(config, eargs): modname = 'cic.ext.{}'.format(eargs.target) @@ -56,7 +61,13 @@ def execute(config, eargs): ca.load() cn.load() + signer = cmd_mod.parse_signer(eargs.y) + ref = cn.resource(eargs.target) chain_spec = cn.chain_spec(eargs.target) - logg.debug('found reference {} for target {}'.format(ref, eargs.target)) - getattr(cmd_mod, 'new')(chain_spec, ref, cp, signer_hint=eargs.signer) + logg.debug('found reference {} chain spec {} for target {}'.format(ref['contents'], chain_spec, eargs.target)) + c = getattr(cmd_mod, 'new')(chain_spec, ref['contents'], cp, signer_hint=signer) + c.apply_token(ct) + + p = Processor(proof=cp, attachment=ca, metadata=cm, extensions=[eargs.target]) + c.process(writer=writers['ext']) diff --git a/cic/data/config/config.ini b/cic/data/config/config.ini index 41feedb..9a77060 100644 --- a/cic/data/config/config.ini +++ b/cic/data/config/config.ini @@ -2,3 +2,4 @@ meta_writer = cic.output.KVWriter attachment_writer = cic.output.KVWriter proof_writer = cic.output.KVWriter +ext_writer = cic.output.KVWriter diff --git a/cic/ext/eth/__init__.py b/cic/ext/eth/__init__.py index fd74418..467a53f 100644 --- a/cic/ext/eth/__init__.py +++ b/cic/ext/eth/__init__.py @@ -22,6 +22,9 @@ from eth_token_index import TokenUniqueSymbolIndex from eth_address_declarator import Declarator from eth_address_declarator.declarator import AddressDeclarator +# local imports +from .signer import parse_signer + logg = logging.getLogger(__name__) @@ -48,12 +51,16 @@ class CICEth: self.tx_format = TxFormat.RLP_SIGNED + def apply_token(self, token): + return self.prepare_token(token.name, token.symbol, token.precision, token.code) + + def prepare_token(self, name, symbol, precision, code, extra=[], extra_types=[], positions=None): self.token_details = { 'name': name, 'symbol': symbol, 'precision': precision, - 'code': code, + 'code': code or '', 'extra': extra, 'extra_types': extra_types, 'positions': positions, @@ -143,7 +150,8 @@ class CICEth: code = self.token_details['code'] + enc.get() - signer_address = self.resources['token']['key_address'] + logg.debug('resource {}'.format(self.resources)) + signer_address = self.resources['token']['key_account'] nonce_oracle = None if self.rpc != None: nonce_oracle = RPCNonceOracle(signer_address, conn=self.rpc) @@ -254,4 +262,4 @@ class CICEth: def new(chain_spec, resources, proof, signer_hint=None): - return CICEth(chain_spec, resources, proof, signer=None) + return CICEth(chain_spec, resources, proof, signer=signer_hint) diff --git a/cic/ext/eth/signer.py b/cic/ext/eth/signer.py new file mode 100644 index 0000000..259ced1 --- /dev/null +++ b/cic/ext/eth/signer.py @@ -0,0 +1,33 @@ +# standard imports +import stat +import os +import logging + +# external imports +from funga.eth.keystore.dict import DictKeystore +from funga.eth.signer import EIP155Signer + +# local imports +from cic.keystore import KeystoreDirectory + +logg = logging.getLogger(__name__) + + +class EthKeystoreDirectory(DictKeystore, KeystoreDirectory): + pass + + +def parse_signer(signer_hint): + + if signer_hint == None: + logg.info('signer hint missing') + return None + st = os.stat(signer_hint) + if stat.S_ISDIR(st.st_mode): + logg.debug('signer hint is directory') + keystore = EthKeystoreDirectory() + keystore.process_dir(signer_hint) + signer = EIP155Signer(keystore) + + return signer + diff --git a/tests/eth/base_eth.py b/tests/eth/base_eth.py index ba6009b..0940d92 100644 --- a/tests/eth/base_eth.py +++ b/tests/eth/base_eth.py @@ -72,17 +72,23 @@ class TestCICEthBase(EthTesterCase): self.resources = { 'token': { 'reference': self.token_address, - 'key_address': addresses[0], + 'key_account': addresses[0], }, 'token_index': { 'reference': self.token_index_address, - 'key_address': addresses[1], + 'key_account': addresses[1], }, 'address_declarator': { 'reference': self.address_declarator_address, - 'key_address': addresses[2], + 'key_account': addresses[2], }, } + self.chain_spec_dict = { + 'arch': 'evm', + 'fork': 'byzantine', + 'network_id': 42, + 'common_name': 'foo', + } proof_dir = os.path.join(test_data_dir, 'proof') attach = Attachment(path=proof_dir) attach.load()