commit f353930f449611fd31cf9ad507cfb91548f45232
parent 031bc5a618c3e44aaf8ef649e96eea93be237000
Author: nolash <dev@holbrook.no>
Date: Sun, 10 Oct 2021 13:46:50 +0200
Add lock, unlock, keyfile error detail
Diffstat:
5 files changed, 40 insertions(+), 11 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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={