Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug fix] Fix import json #1759

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bittensor/keyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def serialized_keypair_to_keyfile_data(keypair: "bittensor.Keypair") -> bytes:
json_data = {
"accountId": "0x" + keypair.public_key.hex() if keypair.public_key else None,
"publicKey": "0x" + keypair.public_key.hex() if keypair.public_key else None,
"privateKey": "0x" + keypair.private_key.hex() if keypair.private_key else None,
"secretPhrase": keypair.mnemonic if keypair.mnemonic else None,
"secretSeed": (
"0x"
Expand Down Expand Up @@ -90,6 +91,7 @@ def deserialize_keypair_from_keyfile_data(keyfile_data: bytes) -> "bittensor.Key
keyfile_dict = {
"accountId": None,
"publicKey": None,
"privateKey": None,
"secretPhrase": None,
"secretSeed": None,
"ss58Address": string_value,
Expand All @@ -104,11 +106,17 @@ def deserialize_keypair_from_keyfile_data(keyfile_data: bytes) -> "bittensor.Key
if "secretSeed" in keyfile_dict and keyfile_dict["secretSeed"] is not None:
return bittensor.Keypair.create_from_seed(keyfile_dict["secretSeed"])

if "secretPhrase" in keyfile_dict and keyfile_dict["secretPhrase"] is not None:
elif "secretPhrase" in keyfile_dict and keyfile_dict["secretPhrase"] is not None:
return bittensor.Keypair.create_from_mnemonic(
mnemonic=keyfile_dict["secretPhrase"]
)

elif keyfile_dict.get("privateKey", None) is not None:
# May have the above dict keys also, but we want to preserve the first two
return bittensor.Keypair.create_from_private_key(
keyfile_dict["privateKey"], ss58_format=bittensor.__ss58_format__
)

if "ss58Address" in keyfile_dict and keyfile_dict["ss58Address"] is not None:
return bittensor.Keypair(ss58_address=keyfile_dict["ss58Address"])

Expand Down
14 changes: 12 additions & 2 deletions tests/unit_tests/test_keyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,18 @@ def test_legacy_coldkey(keyfile_setup_teardown):
assert keyfile.keyfile_data == keyfile_data
keyfile.encrypt(password="this is the fake password")
keyfile.decrypt(password="this is the fake password")
keypair_bytes = b'{"accountId": "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f", "publicKey": "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f", "secretPhrase": null, "secretSeed": null, "ss58Address": "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm"}'
assert keyfile.keyfile_data == keypair_bytes
expected_decryption = {
"accountId": "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f",
"publicKey": "0x32939b6abc4d81f02dff04d2b8d1d01cc8e71c5e4c7492e4fa6a238cdca3512f",
"privateKey": None,
"secretPhrase": None,
"secretSeed": None,
"ss58Address": "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm",
}
for key, value in expected_decryption.items():
value_str = f'"{value}"' if value is not None else "null"
assert f'"{key}": {value_str}'.encode() in keyfile.keyfile_data

assert (
keyfile.get_keypair().ss58_address
== "5DD26kC2kxajmwfbbZmVmxhrY9VeeyR1Gpzy9i8wxLUg6zxm"
Expand Down