From d4cc6f6f174679ced16afe876aaf49e288464c0b Mon Sep 17 00:00:00 2001 From: Jakub Ziebinski <ziebinskijakub@gmail.com> Date: Wed, 12 Feb 2025 13:11:43 +0100 Subject: [PATCH] Extract is_valid_account_name function --- python/cpp_python_bridge.pxd | 1 + python/cpp_python_bridge.pyx | 5 +++++ python/wax/_private/base_api.py | 5 +++++ python/wax/cpp_python_bridge.pyi | 1 + python/wax/interfaces.py | 13 +++++++++++++ 5 files changed, 25 insertions(+) diff --git a/python/cpp_python_bridge.pxd b/python/cpp_python_bridge.pxd index 08c5064f8..b37520040 100644 --- a/python/cpp_python_bridge.pxd +++ b/python/cpp_python_bridge.pxd @@ -194,3 +194,4 @@ cdef extern from "cpython_interface.hpp" namespace "cpp": result cpp_proto_to_api( string operation_or_tx ) result cpp_proto_to_legacy_api( string operation_or_tx ) result cpp_api_to_proto( string operation_or_tx ) + bool cpp_is_valid_account_name (string name ) diff --git a/python/cpp_python_bridge.pyx b/python/cpp_python_bridge.pyx index e32bc31a2..ea55625db 100644 --- a/python/cpp_python_bridge.pyx +++ b/python/cpp_python_bridge.pyx @@ -4,6 +4,7 @@ from typing import Callable from functools import wraps +from libcpp cimport bool from libcpp.string cimport string as cppstring from libcpp.set cimport set as cppset from libcpp.map cimport map as cppmap @@ -280,6 +281,10 @@ def estimate_hive_collateral(current_median_history: python_price, current_min_h response = obj.cpp_estimate_hive_collateral(_current_median_history, _current_min_history, _hbd_amount_to_get) return response.amount, response.precision, response.nai +def is_valid_account_name(account_name: bytes) -> bool: + cdef proto_protocol obj + return obj.cpp_is_valid_account_name(account_name) + def proto_operation_get_impacted_accounts(operation: bytes) -> vector[string]: cdef proto_protocol obj return obj.cpp_operation_get_impacted_accounts(operation) diff --git a/python/wax/_private/base_api.py b/python/wax/_private/base_api.py index 843769c15..2950c4f57 100644 --- a/python/wax/_private/base_api.py +++ b/python/wax/_private/base_api.py @@ -48,6 +48,7 @@ from wax.cpp_python_bridge import ( # type: ignore[attr-defined] generate_password_based_private_key, get_hive_protocol_config, get_public_key_from_signature, + is_valid_account_name, operation_get_impacted_accounts, suggest_brain_key, validate_operation, @@ -85,6 +86,10 @@ class WaxBaseApi(IWaxBaseInterface): def address_prefix(self) -> str: return self.config.get("HIVE_ADDRESS_PREFIX", PUBLIC_KEY_ADDRESS_PREFIX) + @staticmethod + def is_valid_account_name(account_name: AccountName) -> bool: + return is_valid_account_name(to_cpp_string(account_name)) + @staticmethod def get_operation_impacted_accounts(operation: Operation) -> list[AccountName]: validation_result = validate_operation(prepare_operation_to_validate(operation)) diff --git a/python/wax/cpp_python_bridge.pyi b/python/wax/cpp_python_bridge.pyi index 6a049397b..f8c27caa3 100644 --- a/python/wax/cpp_python_bridge.pyi +++ b/python/wax/cpp_python_bridge.pyi @@ -16,6 +16,7 @@ from .wax_result import ( python_minimize_required_signatures_data, ) +def is_valid_account_name(account_name: bytes) -> bool: ... def validate_operation(operation: bytes) -> python_result: ... def validate_transaction(transaction: bytes) -> python_result: ... def calculate_transaction_id(transaction: bytes) -> python_result: ... diff --git a/python/wax/interfaces.py b/python/wax/interfaces.py index ba65b6bf9..1f2c003a5 100644 --- a/python/wax/interfaces.py +++ b/python/wax/interfaces.py @@ -279,6 +279,19 @@ class IWaxBaseInterface(ABC): def address_prefix(self) -> str: """Returns the public key address prefix.""" + @staticmethod + @abstractmethod + def is_valid_account_name(account_name: AccountName) -> bool: + """ + Checks if the given account name is valid. + + Args: + account_name: Account name to be checked. + + Returns: + bool: True if the account name is valid, False otherwise. + """ + @staticmethod @abstractmethod def get_operation_impacted_accounts(operation: Operation) -> list[AccountName]: -- GitLab