2021-10-11 20:08:18 +02:00
|
|
|
# standard imports
|
|
|
|
import stat
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from funga.eth.keystore.dict import DictKeystore
|
|
|
|
from funga.eth.signer import EIP155Signer
|
2021-10-11 21:18:51 +02:00
|
|
|
from chainlib.eth.cli import Rpc
|
|
|
|
from chainlib.cli import Wallet
|
2021-10-11 20:08:18 +02:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic.keystore import KeystoreDirectory
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class EthKeystoreDirectory(DictKeystore, KeystoreDirectory):
|
2021-11-29 15:39:16 +01:00
|
|
|
"""Combination of DictKeystore and KeystoreDirectory
|
|
|
|
|
|
|
|
TODO: Move to funga
|
|
|
|
"""
|
2022-03-01 08:01:56 +01:00
|
|
|
|
|
|
|
|
2021-10-11 20:08:18 +02:00
|
|
|
|
|
|
|
|
2021-10-11 21:18:51 +02:00
|
|
|
def parse_adapter(config, signer_hint):
|
2021-11-29 15:39:16 +01:00
|
|
|
"""Determine and instantiate signer and rpc from configuration.
|
2022-03-01 08:01:56 +01:00
|
|
|
|
2021-11-29 15:39:16 +01:00
|
|
|
If either could not be determined, None is returned.
|
|
|
|
|
|
|
|
:param config: Configuration object implementing the get() method
|
|
|
|
:type config: dict, object with get()
|
|
|
|
:param signer_hint: Signer resource description (e.g. keystore directory)
|
|
|
|
:type signer_hint: str
|
|
|
|
:rtype: tuple; chainlib.connection.RPCConnection, funga.signer.Signer
|
|
|
|
:return: RPC interface, signer interface
|
|
|
|
"""
|
2021-10-11 21:18:51 +02:00
|
|
|
keystore = None
|
2022-03-01 08:01:56 +01:00
|
|
|
if signer_hint is None:
|
|
|
|
logg.info("signer hint missing")
|
2021-10-11 20:08:18 +02:00
|
|
|
return None
|
|
|
|
st = os.stat(signer_hint)
|
|
|
|
if stat.S_ISDIR(st.st_mode):
|
2022-03-01 08:01:56 +01:00
|
|
|
logg.debug("signer hint is directory")
|
2021-10-11 20:08:18 +02:00
|
|
|
keystore = EthKeystoreDirectory()
|
|
|
|
keystore.process_dir(signer_hint)
|
|
|
|
|
2021-10-11 21:18:51 +02:00
|
|
|
w = Wallet(EIP155Signer, keystore=keystore)
|
|
|
|
signer = EIP155Signer(keystore)
|
|
|
|
rpc = Rpc(wallet=w)
|
|
|
|
rpc.connect_by_config(config)
|
|
|
|
|
|
|
|
return (rpc.conn, signer)
|