2020-10-17 02:44:25 +02:00
# standard imports
2021-03-17 15:34:51 +01:00
import re
2020-10-17 02:44:25 +02:00
import os
import sys
2020-08-08 11:33:15 +02:00
import json
import logging
2020-10-17 02:44:25 +02:00
import argparse
2021-03-17 15:34:51 +01:00
from urllib . parse import urlparse
2020-08-08 11:33:15 +02:00
2021-03-31 12:13:04 +02:00
# external imports
2020-10-17 02:44:25 +02:00
import confini
2020-08-08 11:33:15 +02:00
from jsonrpc . exceptions import *
2020-10-17 02:44:25 +02:00
# local imports
2020-08-08 11:33:15 +02:00
from crypto_dev_signer . eth . signer import ReferenceSigner
2021-03-25 19:14:55 +01:00
from crypto_dev_signer . keystore . reference import ReferenceKeystore
2021-09-06 21:34:09 +02:00
from crypto_dev_signer . cli . handle import SignRequestHandler
2020-08-08 11:33:15 +02:00
2021-01-25 18:22:06 +01:00
logging . basicConfig ( level = logging . WARNING )
2020-08-08 11:33:15 +02:00
logg = logging . getLogger ( )
2021-01-25 18:22:06 +01:00
config_dir = ' . '
2020-10-17 02:44:25 +02:00
2020-08-08 11:33:15 +02:00
db = None
signer = None
2021-02-28 08:39:15 +01:00
session = None
2020-08-08 11:33:15 +02:00
chainId = 8995
2020-10-17 02:44:25 +02:00
socket_path = ' /run/crypto-dev-signer/jsonrpc.ipc '
argparser = argparse . ArgumentParser ( )
argparser . add_argument ( ' -c ' , type = str , default = config_dir , help = ' config file ' )
2020-10-17 14:47:01 +02:00
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 ' )
2020-10-17 02:44:25 +02:00
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 )
2020-10-17 14:47:01 +02:00
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 ) )
2020-10-17 02:44:25 +02:00
if args . i :
chainId = args . i
if args . s :
2021-03-17 15:34:51 +01:00
socket_url = urlparse ( args . s )
2020-10-17 02:44:25 +02:00
elif config . get ( ' SIGNER_SOCKET_PATH ' ) :
2021-03-17 15:34:51 +01:00
socket_url = urlparse ( config . get ( ' SIGNER_SOCKET_PATH ' ) )
2020-08-08 11:33:15 +02:00
2020-10-17 14:47:01 +02:00
2020-10-17 11:06:52 +02:00
# 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 ' ) ,
)
2020-08-08 11:33:15 +02:00
2020-10-17 14:47:01 +02:00
logg . info ( ' using dsn {} ' . format ( dsn ) )
2021-05-02 18:06:27 +02:00
logg . info ( ' using socket {} ' . format ( config . get ( ' SIGNER_SOCKET_PATH ' ) ) )
2020-10-17 14:47:01 +02:00
2021-03-17 15:34:51 +01:00
re_http = r ' ^http '
2021-09-06 15:47:03 +02:00
re_tcp = r ' ^tcp '
2021-03-17 15:34:51 +01:00
re_unix = r ' ^ipc '
2020-10-17 14:47:01 +02:00
2021-09-06 21:34:09 +02:00
class MissingSecretError ( Exception ) :
pass
2020-08-08 11:33:15 +02:00
2021-09-06 21:34:09 +02:00
def main ( ) :
2020-08-08 11:33:15 +02:00
2020-10-17 02:44:25 +02:00
secret_hex = config . get ( ' SIGNER_SECRET ' )
if secret_hex == None :
raise MissingSecretError ( ' please provide a valid hex value for the SIGNER_SECRET configuration variable ' )
2020-08-08 12:27:05 +02:00
2020-08-08 11:33:15 +02:00
secret = bytes . fromhex ( secret_hex )
kw = {
' symmetric_key ' : secret ,
}
2021-09-06 21:34:09 +02:00
SignRequestHandler . keystore = ReferenceKeystore ( dsn , * * kw )
SignRequestHandler . signer = ReferenceSigner ( SignRequestHandler . keystore )
2020-08-08 11:33:15 +02:00
arg = None
try :
arg = json . loads ( sys . argv [ 1 ] )
except :
2021-09-06 15:47:03 +02:00
logg . info ( ' no json rpc command detected, starting socket server {} ' . format ( socket_url ) )
2021-03-17 15:34:51 +01:00
scheme = ' ipc '
if socket_url . scheme != ' ' :
scheme = socket_url . scheme
2021-09-06 15:47:03 +02:00
if re . match ( re_tcp , socket_url . scheme ) :
2021-09-06 21:34:09 +02:00
from crypto_dev_signer . cli . socket import start_server_tcp
2021-03-17 15:34:51 +01:00
socket_spec = socket_url . netloc . split ( ' : ' )
2021-01-25 18:22:06 +01:00
host = socket_spec [ 0 ]
port = int ( socket_spec [ 1 ] )
2021-01-27 13:33:52 +01:00
start_server_tcp ( ( host , port ) )
2021-09-06 15:47:03 +02:00
elif re . match ( re_http , socket_url . scheme ) :
2021-09-06 21:34:09 +02:00
from crypto_dev_signer . cli . http import start_server_http
2021-09-06 15:47:03 +02:00
socket_spec = socket_url . netloc . split ( ' : ' )
host = socket_spec [ 0 ]
port = int ( socket_spec [ 1 ] )
start_server_http ( ( host , port ) )
2021-01-25 18:22:06 +01:00
else :
2021-09-06 21:34:09 +02:00
from crypto_dev_signer . cli . socket import start_server_unix
2021-03-17 15:34:51 +01:00
start_server_unix ( socket_url . path )
2020-08-08 11:33:15 +02:00
sys . exit ( 0 )
( rpc_id , response ) = process_input ( arg )
r = jsonrpc_ok ( rpc_id , response )
sys . stdout . write ( json . dumps ( r ) )
2020-10-26 09:02:29 +01:00
if __name__ == ' __main__ ' :
main ( )