commit fef79c4a32e3749d21160fc653f4fe42123c3e73
parent 5a45512f3b115f272c2e8b07f1cde4f4bd4d6bb1
Author: Louis Holbrook <accounts-gitlab@holbrook.no>
Date: Mon, 21 Sep 2020 17:12:07 +0000
Merge branch 'lash/import-prvate-key-type' into 'master'
Restructure import method chain for signer
See merge request nolash/crypto-dev-signer!2
Diffstat:
4 files changed, 31 insertions(+), 16 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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()