-
Notifications
You must be signed in to change notification settings - Fork 0
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
Support for instantiating address and bytes values from dictionary #80
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
@@ -29,9 +27,20 @@ def decode_top_level(self, data: bytes): | |||
def set_payload(self, value: Any): | |||
if isinstance(value, str): | |||
self.value = bytes(value, "utf-8") | |||
elif isinstance(value, dict): | |||
value = cast(Dict[str, str], value) |
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.
Maybe the cast is not necessary, since the linter sees an Any
type passed to _extract_value_from_dict
and is already forgiving?
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.
The cast is necessary since we have the elif isinstance(value, dict)
statement.
multiversx_sdk/abi/address_value.py
Outdated
if hex_address: | ||
return bytes.fromhex(hex_address) | ||
|
||
raise ValueError("cannot extract pubkey from dictionary") |
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.
Maybe specify what's needed, in parenthesis e.g. cannot extract pubkey from dictionary (missing "bech32", missing "hex")
or something similar.
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.
Added
@@ -42,10 +42,26 @@ def _check_pub_key_length(self, pubkey: bytes) -> None: | |||
raise ValueError(f"public key (address) has invalid length: {len(pubkey)}") | |||
|
|||
def set_payload(self, value: Any): | |||
pubkey = bytes(value) | |||
if isinstance(value, dict): | |||
value = cast(Dict[str, str], value) |
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.
If cast can be skipped and the linter is okay with it, let's skip it.
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.
The linter complains if we skip the cast
.
def _extract_pubkey_from_dict(self, value: Dict[str, str]) -> bytes: | ||
bech32_address = value.get("bech32", None) | ||
if bech32_address: | ||
return Address.new_from_bech32(bech32_address).get_public_key() |
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.
Here, we reference core
in abi
- maybe this is the first occasion? However, there should not be any issues with this.
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.
This is not the first time we reference core
in abi
. It is also used in abi.py
and biguint_value.py
.
multiversx_sdk/abi/address_value.py
Outdated
if hex_address: | ||
return bytes.fromhex(hex_address) | ||
|
||
raise ValueError("cannot extract pubkey from dictionary") |
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.
As above, some details in parenthesis would help the developers.
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.
Added
Previously, the
AddresValue
used to convert the value ofset_payload
tobytes
. Added support to set payload from adictionary
like bellow:or
BytesValue
can now be set from ahex
string, like so:Also now, an error is raised if someone tries to convert a
dictionary
to alist
usingconvert_native_value_to_list
.