Remove eth_keys, web3 dependencies

This commit is contained in:
nolash
2021-03-17 15:34:51 +01:00
parent 3fb5745f98
commit b46ed3a9e6
15 changed files with 122 additions and 75 deletions

View File

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

View File

@@ -1,11 +1,14 @@
# standard imports
import logging
# external imports
from hexathon import strip_0x
# local imports
from . import keyapi
#from . import keyapi
from .interface import Keystore
from crypto_dev_signer.error import UnknownAccountError
from crypto_dev_signer.common import strip_hex_prefix
from crypto_dev_signer.encoding import private_key_to_address
logg = logging.getLogger()
@@ -25,9 +28,13 @@ class DictKeystore(Keystore):
raise UnknownAccountError(address)
def list(self):
return list(self.keys.keys())
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)
self.keys[address_hex_clean] = pk.to_bytes()
address_hex = private_key_to_address(pk)
address_hex_clean = strip_0x(address_hex)
self.keys[address_hex_clean] = pk.secret
logg.debug('added key {}'.format(address_hex))
return address_hex

View File

@@ -1,12 +1,13 @@
# standard imports
import os
# third-party imports
import eth_keyfile
import json
import logging
# local imports
from . import keyapi
from crypto_dev_signer.keystore import keyfile
import coincurve
logg = logging.getLogger(__name__)
class Keystore:
@@ -14,13 +15,17 @@ class Keystore:
raise NotImplementedError
def list(self):
raise NotImplementedError
def new(self, password=None):
b = os.urandom(32)
return self.import_raw_key(b, password)
def import_raw_key(self, b, password=None):
pk = keyapi.PrivateKey(b)
pk = coincurve.PrivateKey(secret=b)
return self.import_key(pk, password)
@@ -30,9 +35,16 @@ class Keystore:
def import_keystore_data(self, keystore_content, password=''):
#private_key = w3.eth.account.decrypt(keystore_content, password)
private_key = eth_keyfile.decode_keyfile_json(keystore_content, password.encode('utf-8'))
if type(keystore_content).__name__ == 'str':
keystore_content = json.loads(keystore_content)
elif type(keystore_content).__name__ == 'bytes':
logg.debug('bytes {}'.format(keystore_content))
keystore_content = json.loads(keystore_content.decode('utf-8'))
private_key = keyfile.from_dict(keystore_content, password.encode('utf-8'))
return self.import_raw_key(private_key, password)
def import_keystore_file(self, keystore_file, password=''):
keystore_content = eth_keyfile.load_keyfile(keystore_file)
return self.import_keystore_data(keystore_content, password)
private_key = keyfile.from_file(keystore_file)
#return self.import_keystore_data(keystore_content, password)
return self.import_raw_key(private_key, password)
#return kes

View File

@@ -2,7 +2,7 @@
import logging
import base64
# third-party imports
# external imports
from cryptography.fernet import Fernet
#import psycopg2
#from psycopg2 import sql
@@ -10,12 +10,13 @@ from cryptography.fernet import Fernet
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
import sha3
from hexathon import strip_0x
# local imports
from .interface import Keystore
from crypto_dev_signer.common import strip_hex_prefix
from . import keyapi
#from . import keyapi
from crypto_dev_signer.error import UnknownAccountError
from crypto_dev_signer.encoding import private_key_to_address
logg = logging.getLogger(__file__)
@@ -53,7 +54,7 @@ class ReferenceKeystore(Keystore):
def get(self, address, password=None):
safe_address = strip_hex_prefix(address)
safe_address = strip_0x(address)
s = text('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = :a')
r = self.db_session.execute(s, {
'a': safe_address,
@@ -69,10 +70,10 @@ class ReferenceKeystore(Keystore):
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)
c = self._encrypt(pk.to_bytes(), password)
address_hex = private_key_to_address(pk)
address_hex_clean = strip_0x(address_hex)
c = self._encrypt(pk.secret, password)
s = text('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (:a, :c)') #%s, %s)')
self.db_session.execute(s, {
'a': address_hex_clean,