Restructure import method chain

This commit is contained in:
nolash 2020-09-21 19:10:20 +02:00
parent 5a45512f3b
commit 2678231f39
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 31 additions and 16 deletions

View File

@ -1 +1,7 @@
# third-party imports
from eth_keys import KeyAPI
from eth_keys.backends import NativeECCBackend
keyapi = KeyAPI(NativeECCBackend)
from .postgres import ReferenceKeystore from .postgres import ReferenceKeystore

View File

@ -1,11 +1,30 @@
# standard imports
import os
# local imports
from . import keyapi
class Keystore: class Keystore:
def get(self, address, password=None): def get(self, address, password=None):
raise NotImplementedError raise NotImplementedError
def new(self, password=None): 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): def import_key(self, pk, password=None):
raise NotImplementedError raise NotImplementedError
def insert_key(self, pk, password=None):
raise NotImplementedError

View File

@ -1,21 +1,17 @@
# standard imports # standard imports
import logging import logging
import base64 import base64
import os
# third-party imports # third-party imports
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
import psycopg2 import psycopg2
from psycopg2 import sql from psycopg2 import sql
from eth_keys import KeyAPI
from eth_keys.backends import NativeECCBackend
import sha3 import sha3
# local imports # local imports
from crypto_dev_signer.common import strip_hex_prefix
from .interface import Keystore from .interface import Keystore
from crypto_dev_signer.common import strip_hex_prefix
keyapi = KeyAPI(NativeECCBackend) from . import keyapi
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger(__file__) logg = logging.getLogger(__file__)
@ -52,18 +48,11 @@ class ReferenceKeystore(Keystore):
return self._decrypt(k, password) 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): def import_key(self, pk, password=None):
pubk = keyapi.private_key_to_public_key(pk) pubk = keyapi.private_key_to_public_key(pk)
address_hex = pubk.to_checksum_address() address_hex = pubk.to_checksum_address()
address_hex_clean = strip_hex_prefix(address_hex) address_hex_clean = strip_hex_prefix(address_hex)
logg.debug('importing address {}'.format(address_hex_clean))
logg.debug('inserting address {}'.format(address_hex_clean))
c = self._encrypt(pk.to_bytes(), password) c = self._encrypt(pk.to_bytes(), password)
s = sql.SQL('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (%s, %s)') 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') ]) self.cur.execute(s, [ address_hex_clean, c.decode('utf-8') ])

View File

@ -130,8 +130,9 @@ def start_server():
try: try:
(rpc_id, r) = process_input(j) (rpc_id, r) = process_input(j)
csock.send(json.dumps(jsonrpc_ok(rpc_id, r)).encode('utf-8')) 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 # 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.send(json.dumps(jsonrpc_error(j['id'], JSONRPCServerError)).encode('utf-8'))
csock.close() csock.close()