Skip to content
Snippets Groups Projects
Commit f5b5ebe6 authored by Jakub Ziebinski's avatar Jakub Ziebinski
Browse files

Fix get impacted accounts functions

parent d4cc6f6f
No related branches found
No related tags found
1 merge request!230Implementation of the python wax interface
......@@ -19,12 +19,19 @@ from .cpp_python_bridge import (
suggest_brain_key,
hbd,
hive,
minimize_required_signatures,
operation_get_impacted_accounts,
proto_operation_get_impacted_accounts,
proto_to_api,
proto_to_legacy_api,
proto_transaction_get_impacted_accounts,
serialize_proto_transaction,
deserialize_proto_transaction,
serialize_transaction,
deserialize_transaction,
serialize_witness_set_properties,
suggest_brain_key,
transaction_get_impacted_accounts,
validate_operation,
validate_proto_operation,
validate_proto_transaction,
......
......@@ -9,6 +9,9 @@ from wax._private.core.constants import (
PUBLIC_KEY_ADDRESS_PREFIX,
)
from wax._private.core.decimal_converter import DecimalConverter
from wax._private.core.format_recognizers.operation import is_hive_protocol_format
from wax._private.core.operation_converters.from_proto_to_cpp_string import from_proto_to_cpp_string
from wax._private.core.operation_converters.from_protocol_to_cpp_string import from_protocol_to_cpp_string
from wax._private.core.python_price_converter import convert_to_python_price
from wax._private.models.asset import (
Asset,
......@@ -21,11 +24,6 @@ from wax._private.models.asset import (
)
from wax._private.models.brain_key_data import BrainKeyData
from wax._private.models.manabar_data import ManabarData
from wax._private.models.operations import (
Operation,
prepare_operation_to_get_impacted_accounts,
prepare_operation_to_validate,
)
from wax._private.models.private_key_data import PrivateKeyData
from wax._private.result_tools import (
decode_impacted_account_names,
......@@ -50,8 +48,10 @@ from wax.cpp_python_bridge import ( # type: ignore[attr-defined]
get_public_key_from_signature,
is_valid_account_name,
operation_get_impacted_accounts,
proto_operation_get_impacted_accounts,
suggest_brain_key,
validate_operation,
validate_proto_operation,
)
from wax.interfaces import ChainConfig, IWaxBaseInterface, JsonTransaction, ProtoTransaction, TTimestamp
......@@ -59,6 +59,7 @@ if TYPE_CHECKING:
from decimal import Decimal
from wax._private.models.basic import AccountName, ChainId, PublicKey, SigDigest, Signature
from wax._private.models.operations import Operation
from wax.interfaces import ITransaction
......@@ -92,12 +93,14 @@ class WaxBaseApi(IWaxBaseInterface):
@staticmethod
def get_operation_impacted_accounts(operation: Operation) -> list[AccountName]:
validation_result = validate_operation(prepare_operation_to_validate(operation))
validate_wax_result(validation_result)
prepared_operation = prepare_operation_to_get_impacted_accounts(operation)
impacted_accounts = operation_get_impacted_accounts(prepared_operation)
if is_hive_protocol_format(operation):
converted = from_protocol_to_cpp_string(operation)
validate_wax_result(validate_operation(converted))
impacted_accounts = operation_get_impacted_accounts(converted)
else:
converted = from_proto_to_cpp_string(operation)
validate_wax_result(validate_proto_operation(converted))
impacted_accounts = proto_operation_get_impacted_accounts(converted)
return decode_impacted_account_names(impacted_accounts)
......
from __future__ import annotations
import json
from functools import singledispatch
from typing import TYPE_CHECKING, Any
from google.protobuf.message import Message
from typing_extensions import TypeIs
from wax._private.exceptions import InvalidOperationFormatError
if TYPE_CHECKING:
from wax._private.models.operations import Operation, ProtocolOperation
@singledispatch
def is_hive_protocol_format(operation: Operation) -> TypeIs[ProtocolOperation]:
raise InvalidOperationFormatError(f"Operation format {type(operation)} not recognized.")
@is_hive_protocol_format.register(str)
def _(operation: str) -> bool:
as_dict = json.loads(operation)
return "type" in as_dict
@is_hive_protocol_format.register(Message)
def _(operation: Message) -> bool: # noqa: ARG001
return False
@is_hive_protocol_format.register(dict)
def _(operation: dict[str, Any]) -> bool:
return "type" in operation
from __future__ import annotations
import json
from functools import singledispatch
from typing import TYPE_CHECKING, Any
from google.protobuf.json_format import MessageToJson
from google.protobuf.message import Message
from wax._private.core.encoders import to_cpp_string
from wax._private.exceptions import InvalidOperationFormatError
if TYPE_CHECKING:
from wax._private.models.operations import ProtoOperation
@singledispatch
def from_proto_to_cpp_string(operation: ProtoOperation) -> bytes:
raise InvalidOperationFormatError(
f"Operation in format {type(operation)} not recognized when converted from proto format to bytes."
)
@from_proto_to_cpp_string.register(str)
def _(operation: str) -> bytes:
return to_cpp_string(operation)
@from_proto_to_cpp_string.register(dict)
def _(operation: dict[str, Any]) -> bytes:
return to_cpp_string(json.dumps(operation))
@from_proto_to_cpp_string.register(Message)
def _(operation: Message) -> bytes:
return to_cpp_string(MessageToJson(operation))
from __future__ import annotations
import json
from functools import singledispatch
from typing import TYPE_CHECKING, Any
from wax._private.core.encoders import to_cpp_string
from wax._private.exceptions import InvalidOperationFormatError
if TYPE_CHECKING:
from wax._private.models.operations import ProtocolOperation
@singledispatch
def from_protocol_to_cpp_string(operation: ProtocolOperation) -> bytes:
raise InvalidOperationFormatError(
f"Operation in format {type(operation)} not recognized when converted from protocol format to bytes."
)
@from_protocol_to_cpp_string.register(str)
def _(operation: str) -> bytes:
return to_cpp_string(operation)
@from_protocol_to_cpp_string.register(dict)
def _(operation: dict[str, Any]) -> bytes:
return to_cpp_string(json.dumps(operation))
from __future__ import annotations
import json
from typing import Any, TypeAlias
from google.protobuf.json_format import MessageToJson
from google.protobuf.message import Message
from wax import api_to_proto, proto_to_api
from wax._private.exceptions import InvalidOperationFormatError, WaxValidationFailedError
from wax._private.result_tools import to_cpp_string, to_python_string, validate_wax_result
from wax.wax_result import python_result
WaxMetaOperation: TypeAlias = Message
Operation: TypeAlias = bytes | dict[str, Any] | str | python_result | Message
def prepare_operation_to_validate(operation: Operation) -> bytes:
if isinstance(operation, Message):
operation = proto_to_api(to_cpp_string(MessageToJson(operation)))
if isinstance(operation, bytes):
return operation
if isinstance(operation, str):
return to_cpp_string(operation)
if isinstance(operation, python_result):
validate_wax_result(operation)
return operation.result
return to_cpp_string(json.dumps(operation))
def prepare_operation_to_get_impacted_accounts(operation: Operation) -> bytes:
if isinstance(operation, bytes):
operation = to_python_string(operation)
if isinstance(operation, str):
operation = json.loads(operation)
if isinstance(operation, python_result):
validate_wax_result(operation)
operation = to_python_string(operation.result)
if isinstance(operation, Message):
return to_cpp_string(MessageToJson(operation))
assert isinstance(operation, dict), "Operation must be dict at this moment."
try:
formatted_operation = api_to_proto(to_cpp_string(json.dumps(operation)))
validate_wax_result(formatted_operation)
except WaxValidationFailedError as error:
raise InvalidOperationFormatError("Operation is not in the correct format.") from error
return formatted_operation.result
ProtoOperation: TypeAlias = dict[str, Any] | str | Message
ProtocolOperation: TypeAlias = dict[str, Any] | str
Operation: TypeAlias = ProtoOperation | ProtocolOperation
......@@ -24,9 +24,9 @@ from wax.cpp_python_bridge import ( # type: ignore[attr-defined]
get_tapos_data,
get_transaction_required_authorities,
proto_to_api,
proto_transaction_get_impacted_accounts,
python_ref_block_data,
serialize_proto_transaction,
transaction_get_impacted_accounts,
validate_proto_transaction,
)
from wax.interfaces import ITransaction, JsonTransaction, ProtoTransaction
......@@ -87,7 +87,7 @@ class Transaction(ITransaction):
@property
def impacted_accounts(self) -> list[AccountName]:
impacted_accounts = transaction_get_impacted_accounts(to_cpp_string(self.to_string()))
impacted_accounts = proto_transaction_get_impacted_accounts(to_cpp_string(self.to_string()))
return decode_impacted_account_names(impacted_accounts)
@property
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment