-
Notifications
You must be signed in to change notification settings - Fork 6
python ss58 conversion #143
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
Open
thewhaleking
wants to merge
3
commits into
feat/thewhaleking/distribute-runtime
Choose a base branch
from
feat/thewhaleking/python-ss58-conversion
base: feat/thewhaleking/distribute-runtime
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from typing import Union, TYPE_CHECKING | ||
from typing import Union, TYPE_CHECKING, Any | ||
|
||
from bt_decode import AxonInfo, PrometheusInfo, decode_list | ||
from scalecodec import ScaleBytes | ||
from scalecodec import ScaleBytes, ss58_encode | ||
|
||
from async_substrate_interface.utils import hex_to_bytes | ||
from async_substrate_interface.types import ScaleObj | ||
|
@@ -81,6 +81,7 @@ def decode_query_map( | |
value_type, | ||
key_hashers, | ||
ignore_decoding_errors, | ||
decode_ss58: bool = False, | ||
): | ||
def concat_hash_len(key_hasher: str) -> int: | ||
""" | ||
|
@@ -120,12 +121,19 @@ def concat_hash_len(key_hasher: str) -> int: | |
) | ||
middl_index = len(all_decoded) // 2 | ||
decoded_keys = all_decoded[:middl_index] | ||
decoded_values = [ScaleObj(x) for x in all_decoded[middl_index:]] | ||
for dk, dv in zip(decoded_keys, decoded_values): | ||
decoded_values = all_decoded[middl_index:] | ||
for (kts, vts), (dk, dv) in zip( | ||
zip(pre_decoded_key_types, pre_decoded_value_types), | ||
zip(decoded_keys, decoded_values), | ||
): | ||
Comment on lines
+125
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for kts, vts, dk, dv in zip(
pre_decoded_key_types,
pre_decoded_value_types,
decoded_keys,
decoded_values,
): wouldn't it be the same logic with a more elegant look? |
||
try: | ||
# strip key_hashers to use as item key | ||
if len(param_types) - len(params) == 1: | ||
item_key = dk[1] | ||
if decode_ss58: | ||
if kts[kts.index(", ") + 2 : kts.index(")")] == "scale_info::0": | ||
item_key = ss58_encode(bytes(item_key[0]), runtime.ss58_format) | ||
|
||
else: | ||
item_key = tuple( | ||
dk[key + 1] for key in range(len(params), len(param_types) + 1, 2) | ||
|
@@ -135,9 +143,17 @@ def concat_hash_len(key_hasher: str) -> int: | |
if not ignore_decoding_errors: | ||
raise | ||
item_key = None | ||
|
||
item_value = dv | ||
result.append([item_key, item_value]) | ||
if decode_ss58: | ||
try: | ||
value_type_str_int = int(vts.split("::")[1]) | ||
decoded_type_str = runtime.type_id_to_name[value_type_str_int] | ||
item_value = convert_account_ids( | ||
dv, decoded_type_str, runtime.ss58_format | ||
) | ||
except (ValueError, KeyError): | ||
pass | ||
result.append([item_key, ScaleObj(item_value)]) | ||
return result | ||
|
||
|
||
|
@@ -154,3 +170,68 @@ def legacy_scale_decode( | |
obj.decode(check_remaining=runtime.config.get("strict_scale_decode")) | ||
|
||
return obj.value | ||
|
||
|
||
def is_accountid32(value: Any) -> bool: | ||
return ( | ||
isinstance(value, tuple) | ||
and len(value) == 32 | ||
and all(isinstance(b, int) and 0 <= b <= 255 for b in value) | ||
) | ||
|
||
|
||
def convert_account_ids(value: Any, type_str: str, ss58_format=42) -> Any: | ||
if "AccountId32" not in type_str: | ||
return value | ||
|
||
# Option<T> | ||
if type_str.startswith("Option<") and value is not None: | ||
inner_type = type_str[7:-1] | ||
return convert_account_ids(value, inner_type) | ||
# Vec<T> | ||
if type_str.startswith("Vec<") and isinstance(value, (list, tuple)): | ||
inner_type = type_str[4:-1] | ||
return tuple(convert_account_ids(v, inner_type) for v in value) | ||
|
||
# Vec<Vec<T>> | ||
if type_str.startswith("Vec<Vec<") and isinstance(value, (list, tuple)): | ||
inner_type = type_str[8:-2] | ||
return tuple( | ||
tuple(convert_account_ids(v2, inner_type) for v2 in v1) for v1 in value | ||
) | ||
|
||
# Tuple | ||
if type_str.startswith("(") and isinstance(value, (list, tuple)): | ||
inner_parts = split_tuple_type(type_str) | ||
return tuple(convert_account_ids(v, t) for v, t in zip(value, inner_parts)) | ||
|
||
# AccountId32 | ||
if type_str == "AccountId32" and is_accountid32(value[0]): | ||
return ss58_encode(bytes(value[0]), ss58_format=ss58_format) | ||
|
||
# Fallback | ||
return value | ||
|
||
|
||
def split_tuple_type(type_str: str) -> list[str]: | ||
""" | ||
Splits a type string like '(AccountId32, Vec<StakeInfo>)' into ['AccountId32', 'Vec<StakeInfo>'] | ||
Handles nested generics. | ||
""" | ||
s = type_str[1:-1] | ||
parts = [] | ||
depth = 0 | ||
current = "" | ||
for char in s: | ||
if char == "," and depth == 0: | ||
parts.append(current.strip()) | ||
current = "" | ||
else: | ||
if char == "<": | ||
depth += 1 | ||
elif char == ">": | ||
depth -= 1 | ||
current += char | ||
if current: | ||
parts.append(current.strip()) | ||
return parts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hacky logic :D