From 0bfb657894c550cdfaf8eed0233c18275e67934b Mon Sep 17 00:00:00 2001 From: vbuterin Date: Thu, 5 Nov 2020 14:34:01 +0800 Subject: [PATCH 1/3] Proposed tweak to from_json and from_file This feels to me like it would be more intuitive behavior; the `from_json` behaves the same way for Keystore and KeystoreCrypto, and it exposes a way for people to use the library with json that is not saved in a file anywhere. --- eth2deposit/key_handling/keystore.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/eth2deposit/key_handling/keystore.py b/eth2deposit/key_handling/keystore.py index a11052f..a977900 100644 --- a/eth2deposit/key_handling/keystore.py +++ b/eth2deposit/key_handling/keystore.py @@ -98,9 +98,8 @@ class Keystore(BytesDataclass): f.write(self.as_json()) @classmethod - def from_json(cls, path: str) -> 'Keystore': - with open(path, 'r') as f: - json_dict = json.load(f) + def from_json(cls, json_dict: Dict[Any, Any]) -> 'Keystore': + crypto = KeystoreCrypto.from_json(json_dict['crypto']) path = json_dict['path'] uuid = json_dict['uuid'] @@ -108,6 +107,11 @@ class Keystore(BytesDataclass): description = json_dict.get('description', '') pubkey = json_dict.get('pubkey', '') return cls(crypto=crypto, description=description, pubkey=pubkey, path=path, uuid=uuid, version=version) + + @classmethod + def from_file(cls, path: str) -> 'KeyStore': + with open(path, 'r') as f: + return cls.from_json(json.load(f)) @staticmethod def _process_password(password: str) -> bytes: From 59a12e927e14b720f141e838b3305463f74d9ff9 Mon Sep 17 00:00:00 2001 From: vbuterin Date: Thu, 5 Nov 2020 14:35:25 +0800 Subject: [PATCH 2/3] Fixed saving the path --- eth2deposit/key_handling/keystore.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/eth2deposit/key_handling/keystore.py b/eth2deposit/key_handling/keystore.py index a977900..6699462 100644 --- a/eth2deposit/key_handling/keystore.py +++ b/eth2deposit/key_handling/keystore.py @@ -98,8 +98,7 @@ class Keystore(BytesDataclass): f.write(self.as_json()) @classmethod - def from_json(cls, json_dict: Dict[Any, Any]) -> 'Keystore': - + def from_json(cls, json_dict: Dict[Any, Any], path='': str) -> 'Keystore': crypto = KeystoreCrypto.from_json(json_dict['crypto']) path = json_dict['path'] uuid = json_dict['uuid'] @@ -111,7 +110,7 @@ class Keystore(BytesDataclass): @classmethod def from_file(cls, path: str) -> 'KeyStore': with open(path, 'r') as f: - return cls.from_json(json.load(f)) + return cls.from_json(json.load(f), path) @staticmethod def _process_password(password: str) -> bytes: From b6f7ca93c3e8439ed5a40f3cb79725b577b1c354 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 5 Nov 2020 17:13:32 +0800 Subject: [PATCH 3/3] Fix --- eth2deposit/credentials.py | 2 +- eth2deposit/key_handling/keystore.py | 8 ++++---- tests/test_cli/helpers.py | 2 +- tests/test_key_handling/test_keystore.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/eth2deposit/credentials.py b/eth2deposit/credentials.py index d9a4a0b..f8a98a7 100644 --- a/eth2deposit/credentials.py +++ b/eth2deposit/credentials.py @@ -107,7 +107,7 @@ class Credential: return filefolder def verify_keystore(self, keystore_filefolder: str, password: str) -> bool: - saved_keystore = Keystore.from_json(keystore_filefolder) + saved_keystore = Keystore.from_file(keystore_filefolder) secret_bytes = saved_keystore.decrypt(password) return self.signing_sk == int.from_bytes(secret_bytes, 'big') diff --git a/eth2deposit/key_handling/keystore.py b/eth2deposit/key_handling/keystore.py index 6699462..8075bba 100644 --- a/eth2deposit/key_handling/keystore.py +++ b/eth2deposit/key_handling/keystore.py @@ -98,7 +98,7 @@ class Keystore(BytesDataclass): f.write(self.as_json()) @classmethod - def from_json(cls, json_dict: Dict[Any, Any], path='': str) -> 'Keystore': + def from_json(cls, json_dict: Dict[Any, Any]) -> 'Keystore': crypto = KeystoreCrypto.from_json(json_dict['crypto']) path = json_dict['path'] uuid = json_dict['uuid'] @@ -106,11 +106,11 @@ class Keystore(BytesDataclass): description = json_dict.get('description', '') pubkey = json_dict.get('pubkey', '') return cls(crypto=crypto, description=description, pubkey=pubkey, path=path, uuid=uuid, version=version) - + @classmethod - def from_file(cls, path: str) -> 'KeyStore': + def from_file(cls, path: str) -> 'Keystore': with open(path, 'r') as f: - return cls.from_json(json.load(f), path) + return cls.from_json(json.load(f)) @staticmethod def _process_password(password: str) -> bytes: diff --git a/tests/test_cli/helpers.py b/tests/test_cli/helpers.py index c199c75..01f9204 100644 --- a/tests/test_cli/helpers.py +++ b/tests/test_cli/helpers.py @@ -17,5 +17,5 @@ def clean_key_folder(my_folder_path: str) -> None: def get_uuid(key_file: str) -> str: - keystore = Keystore.from_json(key_file) + keystore = Keystore.from_file(key_file) return keystore.uuid diff --git a/tests/test_key_handling/test_keystore.py b/tests/test_key_handling/test_keystore.py index e15463d..add35b8 100644 --- a/tests/test_key_handling/test_keystore.py +++ b/tests/test_key_handling/test_keystore.py @@ -13,7 +13,7 @@ test_vector_secret = bytes.fromhex('000000000019d6689c085ae165831e934ff763ae46a2 test_vector_folder = os.path.join(os.getcwd(), 'tests', 'test_key_handling', 'keystore_test_vectors') _, _, test_vector_files = next(os.walk(test_vector_folder)) # type: ignore -test_vector_keystores = [Keystore.from_json(os.path.join(test_vector_folder, f)) for f in test_vector_files] +test_vector_keystores = [Keystore.from_file(os.path.join(test_vector_folder, f)) for f in test_vector_files] def test_json_serialization() -> None: