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

View File

@ -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

View File

@ -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') ])

View File

@ -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()