funga-eth/funga/eth/runnable/signer.py

126 lines
3.8 KiB
Python
Raw Normal View History

2021-10-10 18:14:34 +02:00
# standard imports
import re
import os
import sys
import json
import logging
import argparse
from urllib.parse import urlparse
# external imports
import confini
from jsonrpc.exceptions import *
# local imports
2021-10-15 22:40:22 +02:00
from funga.eth.signer import EIP155Signer
from funga.eth.keystore.sql import SQLKeystore
from funga.eth.cli.handle import SignRequestHandler
2021-10-10 18:14:34 +02:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
2021-10-15 22:40:22 +02:00
script_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(script_dir, '..', 'data')
config_dir = os.path.join(data_dir, 'config')
2021-10-10 18:14:34 +02:00
db = None
signer = None
session = None
chainId = 8995
socket_path = '/run/crypto-dev-signer/jsonrpc.ipc'
2021-10-15 22:40:22 +02:00
2021-10-10 18:14:34 +02:00
argparser = argparse.ArgumentParser()
argparser.add_argument('-c', type=str, default=config_dir, help='config file')
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('-i', type=int, help='default chain id for EIP155')
argparser.add_argument('-s', type=str, help='socket path')
argparser.add_argument('-v', action='store_true', help='be verbose')
argparser.add_argument('-vv', action='store_true', help='be more verbose')
args = argparser.parse_args()
if args.vv:
logging.getLogger().setLevel(logging.DEBUG)
elif args.v:
logging.getLogger().setLevel(logging.INFO)
config = confini.Config(args.c, args.env_prefix)
config.process()
config.censor('PASSWORD', 'DATABASE')
config.censor('SECRET', 'SIGNER')
logg.debug('config loaded from {}:\n{}'.format(config_dir, config))
if args.i:
chainId = args.i
if args.s:
socket_url = urlparse(args.s)
elif config.get('SIGNER_SOCKET_PATH'):
socket_url = urlparse(config.get('SIGNER_SOCKET_PATH'))
# connect to database
dsn = 'postgresql://{}:{}@{}:{}/{}'.format(
config.get('DATABASE_USER'),
config.get('DATABASE_PASSWORD'),
config.get('DATABASE_HOST'),
config.get('DATABASE_PORT'),
config.get('DATABASE_NAME'),
)
logg.info('using dsn {}'.format(dsn))
logg.info('using socket {}'.format(config.get('SIGNER_SOCKET_PATH')))
re_http = r'^http'
re_tcp = r'^tcp'
re_unix = r'^ipc'
class MissingSecretError(Exception):
pass
def main():
secret_hex = config.get('SIGNER_SECRET')
if secret_hex == None:
raise MissingSecretError('please provide a valid hex value for the SIGNER_SECRET configuration variable')
secret = bytes.fromhex(secret_hex)
kw = {
'symmetric_key': secret,
}
2021-10-15 22:40:22 +02:00
SignRequestHandler.keystore = SQLKeystore(dsn, **kw)
SignRequestHandler.signer = EIP155Signer(SignRequestHandler.keystore)
2021-10-10 18:14:34 +02:00
arg = None
try:
arg = json.loads(sys.argv[1])
except:
logg.info('no json rpc command detected, starting socket server {}'.format(socket_url))
scheme = 'ipc'
if socket_url.scheme != '':
scheme = socket_url.scheme
if re.match(re_tcp, socket_url.scheme):
2021-10-15 22:40:22 +02:00
from funga.eth.cli.socket import start_server_tcp
2021-10-10 18:14:34 +02:00
socket_spec = socket_url.netloc.split(':')
host = socket_spec[0]
port = int(socket_spec[1])
start_server_tcp((host, port))
elif re.match(re_http, socket_url.scheme):
2021-10-15 22:40:22 +02:00
from funga.eth.cli.http import start_server_http
2021-10-10 18:14:34 +02:00
socket_spec = socket_url.netloc.split(':')
host = socket_spec[0]
port = int(socket_spec[1])
start_server_http((host, port))
else:
2021-10-15 22:40:22 +02:00
from funga.eth.cli.socket import start_server_unix
2021-10-10 18:14:34 +02:00
start_server_unix(socket_url.path)
sys.exit(0)
(rpc_id, response) = process_input(arg)
r = jsonrpc_ok(rpc_id, response)
sys.stdout.write(json.dumps(r))
if __name__ == '__main__':
main()