From 2678231f39ae4f533d770922f8f27879f395cf0e Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 21 Sep 2020 19:10:20 +0200 Subject: [PATCH] Restructure import method chain --- crypto_dev_signer/keystore/__init__.py | 6 ++++++ crypto_dev_signer/keystore/interface.py | 21 ++++++++++++++++++++- crypto_dev_signer/keystore/postgres.py | 17 +++-------------- scripts/crypto-dev-daemon | 3 ++- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/crypto_dev_signer/keystore/__init__.py b/crypto_dev_signer/keystore/__init__.py index 1032f3f..44f35f0 100644 --- a/crypto_dev_signer/keystore/__init__.py +++ b/crypto_dev_signer/keystore/__init__.py @@ -1 +1,7 @@ +# third-party imports +from eth_keys import KeyAPI +from eth_keys.backends import NativeECCBackend + +keyapi = KeyAPI(NativeECCBackend) + from .postgres import ReferenceKeystore diff --git a/crypto_dev_signer/keystore/interface.py b/crypto_dev_signer/keystore/interface.py index 2525388..c349347 100644 --- a/crypto_dev_signer/keystore/interface.py +++ b/crypto_dev_signer/keystore/interface.py @@ -1,11 +1,30 @@ +# standard imports +import os + +# local imports +from . import keyapi + + class Keystore: def get(self, address, password=None): raise NotImplementedError + def new(self, password=None): - raise NotImplementedError + b = os.urandom(32) + return self.import_raw_key(b, password) + + + def import_raw_key(self, b, password=None): + pk = keyapi.PrivateKey(b) + return self.import_key(pk, password) + def import_key(self, pk, password=None): raise NotImplementedError + + def insert_key(self, pk, password=None): + raise NotImplementedError + diff --git a/crypto_dev_signer/keystore/postgres.py b/crypto_dev_signer/keystore/postgres.py index 32498af..069b464 100644 --- a/crypto_dev_signer/keystore/postgres.py +++ b/crypto_dev_signer/keystore/postgres.py @@ -1,21 +1,17 @@ # standard imports import logging import base64 -import os # third-party imports from cryptography.fernet import Fernet import psycopg2 from psycopg2 import sql -from eth_keys import KeyAPI -from eth_keys.backends import NativeECCBackend import sha3 # local imports -from crypto_dev_signer.common import strip_hex_prefix from .interface import Keystore - -keyapi = KeyAPI(NativeECCBackend) +from crypto_dev_signer.common import strip_hex_prefix +from . import keyapi logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger(__file__) @@ -52,18 +48,11 @@ class ReferenceKeystore(Keystore): return self._decrypt(k, password) - def new(self, password=None): - b = os.urandom(32) - pk = keyapi.PrivateKey(b) - return self.import_key(pk, password) - - def import_key(self, pk, password=None): pubk = keyapi.private_key_to_public_key(pk) address_hex = pubk.to_checksum_address() address_hex_clean = strip_hex_prefix(address_hex) - - logg.debug('inserting address {}'.format(address_hex_clean)) + logg.debug('importing address {}'.format(address_hex_clean)) c = self._encrypt(pk.to_bytes(), password) s = sql.SQL('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (%s, %s)') self.cur.execute(s, [ address_hex_clean, c.decode('utf-8') ]) diff --git a/scripts/crypto-dev-daemon b/scripts/crypto-dev-daemon index a8ca9fc..d547a98 100755 --- a/scripts/crypto-dev-daemon +++ b/scripts/crypto-dev-daemon @@ -130,8 +130,9 @@ def start_server(): try: (rpc_id, r) = process_input(j) csock.send(json.dumps(jsonrpc_ok(rpc_id, r)).encode('utf-8')) - except: + except Exception as e: # TODO: handle cases to give better error context to caller + logg.error('error {}'.format(e)) csock.send(json.dumps(jsonrpc_error(j['id'], JSONRPCServerError)).encode('utf-8')) csock.close()