Skip to content

Commit ba7d95c

Browse files
authored
Merge pull request #557 from opentensor/fix/thewhaleking/handle-hex-identity-decoding
2 parents 53ee0a4 + 795ad76 commit ba7d95c

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

bittensor_cli/src/bittensor/utils.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -622,20 +622,23 @@ def decode_hex_identity_dict(info_dictionary) -> dict[str, Any]:
622622
623623
Examples:
624624
input_dict = {
625-
... "name": {"value": "0x6a6f686e"},
626-
... "additional": [
627-
... [{"data": "0x64617461"}]
628-
... ]
629-
... }
625+
"name": {"value": "0x6a6f686e"},
626+
"additional": [
627+
{"data1": "0x64617461"},
628+
("data2", "0x64617461")
629+
]
630+
}
630631
decode_hex_identity_dict(input_dict)
631-
{'name': 'john', 'additional': [('data', 'data')]}
632+
{'name': 'john', 'additional': [('data1', 'data'), ('data2', 'data')]}
632633
"""
633634

634-
def get_decoded(data: str) -> str:
635+
def get_decoded(data: Optional[str]) -> str:
635636
"""Decodes a hex-encoded string."""
637+
if data is None:
638+
return ""
636639
try:
637640
return hex_to_bytes(data).decode()
638-
except UnicodeDecodeError:
641+
except (UnicodeDecodeError, ValueError):
639642
print(f"Could not decode: {key}: {item}")
640643

641644
for key, value in info_dictionary.items():
@@ -651,12 +654,14 @@ def get_decoded(data: str) -> str:
651654
if key == "additional":
652655
additional = []
653656
for item in value:
654-
additional.append(
655-
tuple(
656-
get_decoded(data=next(iter(sub_item.values())))
657-
for sub_item in item
658-
)
659-
)
657+
if isinstance(item, dict):
658+
for k, v in item.items():
659+
additional.append((k, get_decoded(v)))
660+
else:
661+
if isinstance(item, (tuple, list)) and len(item) == 2:
662+
k_, v = item
663+
k = k_ if k_ is not None else ""
664+
additional.append((k, get_decoded(v)))
660665
info_dictionary[key] = additional
661666

662667
return info_dictionary

tests/unit_tests/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from bittensor_cli.src.bittensor import utils
2+
import pytest
3+
4+
5+
@pytest.mark.parametrize(
6+
"input_dict,expected_result",
7+
[
8+
(
9+
{
10+
"name": {"value": "0x6a6f686e"},
11+
"additional": [{"data1": "0x64617461"}, ("data2", "0x64617461")],
12+
},
13+
{"name": "john", "additional": [("data1", "data"), ("data2", "data")]},
14+
),
15+
(
16+
{"name": {"value": "0x6a6f686e"}, "additional": [("data2", "0x64617461")]},
17+
{"name": "john", "additional": [("data2", "data")]},
18+
),
19+
(
20+
{
21+
"name": {"value": "0x6a6f686e"},
22+
"additional": [(None, None)],
23+
},
24+
{"name": "john", "additional": [("", "")]},
25+
),
26+
],
27+
)
28+
def test_decode_hex_identity_dict(input_dict, expected_result):
29+
assert utils.decode_hex_identity_dict(input_dict) == expected_result

0 commit comments

Comments
 (0)