Improve hex prefix handling for keyfile and dictstore

This commit is contained in:
nolash 2021-04-09 16:05:16 +02:00
parent 2caa0ba755
commit 25ffb620a9
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 24 additions and 13 deletions

View File

@ -2,7 +2,10 @@
import logging
# external imports
from hexathon import strip_0x
from hexathon import (
strip_0x,
add_0x,
)
# local imports
#from . import keyapi
@ -20,12 +23,13 @@ class DictKeystore(Keystore):
def get(self, address, password=None):
address_key = strip_0x(address).lower()
if password != None:
logg.debug('password ignored as dictkeystore doesnt do encryption')
try:
return self.keys[address]
return self.keys[address_key]
except KeyError:
raise UnknownAccountError(address)
raise UnknownAccountError(address_key)
def list(self):
@ -34,7 +38,7 @@ class DictKeystore(Keystore):
def import_key(self, pk, password=None):
address_hex = private_key_to_address(pk)
address_hex_clean = strip_0x(address_hex)
address_hex_clean = strip_0x(address_hex).lower()
self.keys[address_hex_clean] = pk.secret
logg.debug('added key {}'.format(address_hex))
return address_hex_clean
return add_0x(address_hex)

View File

@ -6,6 +6,7 @@ import json
import uuid
# external imports
import coincurve
from Crypto.Cipher import AES
from Crypto.Util import Counter
import sha3
@ -72,7 +73,9 @@ class Ciphers:
return ciphertext
def to_dict(private_key, passphrase=''):
def to_dict(private_key_bytes, passphrase=''):
private_key = coincurve.PrivateKey(secret=private_key_bytes)
encryption_key = Hashes.from_scrypt(passphrase=passphrase)

View File

@ -10,7 +10,10 @@ from cryptography.fernet import Fernet
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
import sha3
from hexathon import strip_0x
from hexathon import (
strip_0x,
add_0x,
)
# local imports
from .interface import Keystore
@ -54,7 +57,7 @@ class ReferenceKeystore(Keystore):
def get(self, address, password=None):
safe_address = strip_0x(address)
safe_address = strip_0x(address).lower()
s = text('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = :a')
r = self.db_session.execute(s, {
'a': safe_address,
@ -64,14 +67,15 @@ class ReferenceKeystore(Keystore):
k = r.first()[0]
except TypeError:
self.db_session.rollback()
raise UnknownAccountError(address)
raise UnknownAccountError(safe_address)
self.db_session.commit()
return self._decrypt(k, password)
a = self._decrypt(k, password)
return a
def import_key(self, pk, password=None):
address_hex = private_key_to_address(pk)
address_hex_clean = strip_0x(address_hex)
address_hex_clean = strip_0x(address_hex).lower()
c = self._encrypt(pk.secret, password)
s = text('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (:a, :c)') #%s, %s)')
@ -82,7 +86,7 @@ class ReferenceKeystore(Keystore):
)
self.db_session.commit()
logg.info('added private key for address {}'.format(address_hex_clean))
return address_hex
return add_0x(address_hex)
def _encrypt(self, private_key, password):

View File

@ -62,7 +62,7 @@ def main():
else:
pk_bytes = os.urandom(32)
pk = coincurve.PrivateKey(secret=pk_bytes)
o = to_dict(pk, passphrase)
o = to_dict(pk_bytes, passphrase)
r = json.dumps(o)
print(r)