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.
This commit is contained in:
vbuterin 2020-11-05 14:34:01 +08:00 committed by GitHub
parent 9310de0ff3
commit 0bfb657894
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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