diff --git a/funga/error.py b/funga/error.py index 2724ee6..1fdd62d 100644 --- a/funga/error.py +++ b/funga/error.py @@ -21,4 +21,9 @@ class SignerError(Exception): return self.jsonrpc_error +class DecryptError(Exception): + pass + +class KeyfileError(Exception): + pass diff --git a/funga/eth/keystore/keyfile.py b/funga/eth/keystore/keyfile.py index ded22a2..a20061b 100644 --- a/funga/eth/keystore/keyfile.py +++ b/funga/eth/keystore/keyfile.py @@ -12,6 +12,10 @@ from Crypto.Util import Counter import sha3 # local imports +from funga.error import ( + DecryptError, + KeyfileError, + ) from funga.eth.encoding import private_key_to_address logg = logging.getLogger(__name__) @@ -127,17 +131,28 @@ def from_dict(o, passphrase=''): # check mac calculated_mac = to_mac(decryption_key[16:], ciphertext_bytes) - assert control_mac == calculated_mac - + if control_mac != calculated_mac: + raise DecryptError('mac mismatch when decrypting passphrase') + m = getattr(Ciphers, 'decrypt_{}'.format(cipher.replace('-', '_'))) - pk = m(ciphertext_bytes, decryption_key[:16], iv) + + try: + pk = m(ciphertext_bytes, decryption_key[:16], iv) + except AssertionError as e: + raise DecryptError('could not decrypt keyfile: {}'.format(e)) + logg.debug('bar') + return pk def from_file(filepath, passphrase=''): f = open(filepath, 'r') - o = json.load(f) + try: + o = json.load(f) + except json.decoder.JSONDecodeError as e: + f.close() + raise KeyfileError(e) f.close() return from_dict(o, passphrase) diff --git a/funga/eth/runnable/keyfile.py b/funga/eth/runnable/keyfile.py index c391f7f..327f771 100644 --- a/funga/eth/runnable/keyfile.py +++ b/funga/eth/runnable/keyfile.py @@ -11,11 +11,12 @@ import coincurve from hexathon import strip_0x # local imports -from crypto_dev_signer.keystore.keyfile import ( +from funga.error import DecryptError +from funga.eth.keystore.keyfile import ( from_file, to_dict, ) -from crypto_dev_signer.encoding import ( +from funga.eth.encoding import ( private_key_to_address, private_key_from_bytes, ) @@ -60,7 +61,7 @@ def main(): passphrase = getpass.getpass('decryption phrase: ') try: r = from_file(args.d, passphrase).hex() - except AssertionError: + except DecryptError: sys.stderr.write('Invalid passphrase\n') sys.exit(1) if not secret: diff --git a/funga/keystore.py b/funga/keystore.py index c51a8ff..0c3df27 100644 --- a/funga/keystore.py +++ b/funga/keystore.py @@ -23,6 +23,14 @@ class Keystore: raise NotImplementedError + def lock(self, address=None): + raise NotImplementedError + + + def unlock(self, address=None): + raise NotImplementedError + + def new(self, password=None): self.private_key_generator(password=password) diff --git a/setup.py b/setup.py index c3650be..bf83b5a 100644 --- a/setup.py +++ b/setup.py @@ -38,12 +38,12 @@ setup( author="Louis Holbrook", author_email="dev@holbrook.no", packages=[ + 'funga', 'funga.eth.signer', 'funga.eth', - 'funga.cli', - 'funga.keystore', - 'funga.runnable', - 'funga', + 'funga.eth.cli', + 'funga.eth.keystore', + 'funga.eth.runnable', ], install_requires=requirements, extras_require={