Merge pull request #151 from ethereum/vbuterin-patch-1

Proposed tweak to from_json and from_file
This commit is contained in:
Hsiao-Wei Wang 2020-11-13 01:22:14 +08:00 committed by GitHub
commit bd0a130f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 6 deletions

View File

@ -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')

View File

@ -98,9 +98,7 @@ 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']
@ -109,6 +107,11 @@ class Keystore(BytesDataclass):
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:
"""

View File

@ -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

View File

@ -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: