From 29788ec616101498520d2c7b8039f2de657f993d Mon Sep 17 00:00:00 2001 From: Kyoto Date: Mon, 23 Dec 2024 13:38:07 +0100 Subject: [PATCH 1/8] feat: add wallet ID check function --- qubipy/rpc/rpc_client.py | 3 ++- qubipy/utils.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/qubipy/rpc/rpc_client.py b/qubipy/rpc/rpc_client.py index 2a47d46..7c6ef12 100644 --- a/qubipy/rpc/rpc_client.py +++ b/qubipy/rpc/rpc_client.py @@ -130,7 +130,8 @@ def get_balance(self, wallet_id: str | None = None) -> Dict[str, Any]: if not wallet_id: raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) - + + check_wallet_id(wallet_id) endpoint = WALLET_BALANCE.format(id = wallet_id) diff --git a/qubipy/utils.py b/qubipy/utils.py index 1242854..e3d3c39 100644 --- a/qubipy/utils.py +++ b/qubipy/utils.py @@ -57,3 +57,17 @@ def check_bytes(tx: bytes): if not isinstance(tx, (bytes, bytearray)): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_TX_BYTES) + +def check_wallet_id(wallet_id: str): + + """ + Validates that the provided wallet ID is in the correct format. + + Args: + wallet_id (str): The wallet ID to validate. Must be exactly 60 characters long. + + Raises: + QubiPy_Exceptions: If the wallet ID is not exactly 60 characters long. + """ + if len(wallet_id) != 60: + raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) From 30f4ab1b029edacd371287a05bfa11aaca4f737a Mon Sep 17 00:00:00 2001 From: Mazzya Date: Mon, 23 Dec 2024 15:10:34 +0100 Subject: [PATCH 2/8] feat: add upper function to the wallet id --- qubipy/rpc/rpc_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qubipy/rpc/rpc_client.py b/qubipy/rpc/rpc_client.py index 7c6ef12..3d5fb35 100644 --- a/qubipy/rpc/rpc_client.py +++ b/qubipy/rpc/rpc_client.py @@ -133,7 +133,7 @@ def get_balance(self, wallet_id: str | None = None) -> Dict[str, Any]: check_wallet_id(wallet_id) - endpoint = WALLET_BALANCE.format(id = wallet_id) + endpoint = WALLET_BALANCE.format(id = wallet_id.upper()) try: response = requests.get(f'{self.rpc_url}{endpoint}', headers=HEADERS, timeout=self.timeout) From 5ba02ac63910be6e453f35ba83790df1c1d3eced Mon Sep 17 00:00:00 2001 From: Mazzya Date: Mon, 23 Dec 2024 15:11:08 +0100 Subject: [PATCH 3/8] feat: add more verifications to the check wallet function --- qubipy/utils.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/qubipy/utils.py b/qubipy/utils.py index e3d3c39..a42cf62 100644 --- a/qubipy/utils.py +++ b/qubipy/utils.py @@ -58,16 +58,26 @@ def check_bytes(tx: bytes): if not isinstance(tx, (bytes, bytearray)): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_TX_BYTES) -def check_wallet_id(wallet_id: str): - - """ - Validates that the provided wallet ID is in the correct format. - - Args: - wallet_id (str): The wallet ID to validate. Must be exactly 60 characters long. - Raises: - QubiPy_Exceptions: If the wallet ID is not exactly 60 characters long. - """ - if len(wallet_id) != 60: - raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) +def check_wallet_id(wallet_id: str): + """ + Validates that the provided wallet ID is in the correct format. + + Args: + wallet_id (str): The wallet ID to validate. Must be exactly 60 characters long. + + Raises: + QubiPy_Exceptions: If the wallet ID is not a string, is not 60 characters long, + or contains non-alphabetic characters. + """ + + if not isinstance(wallet_id, str): + raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) + + + if len(wallet_id) != 60: + raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) + + + if not wallet_id.isalpha(): + raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) \ No newline at end of file From efe30db6d4497c8e0087652405baddfa10a02e53 Mon Sep 17 00:00:00 2001 From: Mazzya Date: Tue, 24 Dec 2024 14:50:43 +0100 Subject: [PATCH 4/8] docs: update version & add changelog --- CHANGELOG.md | 3 +++ README.md | 2 +- docs/index.md | 4 ++-- setup.py | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b3e606..bc6b331 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Change Log +## v0.2.6-beta - December XX, 2024 +* Added a function to verify and validate the wallet ID before making a call to the Qubic network + ## v0.2.5-beta - December 22, 2024 * Added new advanced code examples * Reorganized documentation structure diff --git a/README.md b/README.md index b625f37..7bfdc86 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Currently, QubiPy is in a very early development phase, so please take this into Please visit the [Change log](https://github.com/QubiPy-Labs/QubiPy/blob/main/docs/changelog.md) to see all changes. -![release](https://img.shields.io/badge/release-v0.2.5--beta-blue) +![release](https://img.shields.io/badge/release-v0.2.6--beta-blue) ![python](https://img.shields.io/badge/python-3.10_%7C_3.11_%7C_3.12-blue) ![Python Package](https://github.com/QubiPy-Labs/QubiPy/actions/workflows/python-package.yml/badge.svg) ![Code Quality](https://github.com/QubiPy-Labs/QubiPy/actions/workflows/pylint.yml/badge.svg) diff --git a/docs/index.md b/docs/index.md index 2883863..caa6de0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,11 +1,11 @@ # Welcome to the **QubiPy** official documentation, a Python Library for the QUBIC RPC API -!!! note "Beta Version: 0.2.5" +!!! note "Beta Version: 0.2.6" QubiPy is currently in beta. While functional, some features might change before the stable release. **QubiPy** is a Python library that provides RPC and Core client functionality. You can interact quickly and easily with the Qubic RPC API using the different methods offered by this library. -![release](https://img.shields.io/badge/release-v0.2.5--beta-blue) +![release](https://img.shields.io/badge/release-v0.2.6--beta-blue) ![python](https://img.shields.io/badge/python-3.10_%7C_3.11_%7C_3.12-blue) ![Python Package](https://github.com/QubiPy-Labs/QubiPy/actions/workflows/python-package.yml/badge.svg) ![Code Quality](https://github.com/QubiPy-Labs/QubiPy/actions/workflows/pylint.yml/badge.svg) diff --git a/setup.py b/setup.py index dcbf6ff..ceef3ad 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -__version__ = '0.2.5' +__version__ = '0.2.6' with open("README.md", "r", encoding="utf-8") as fh: From 23dcfb8a82f6823bd3ce58c1fdd29f0e5539cce3 Mon Sep 17 00:00:00 2001 From: Mazzya Date: Wed, 25 Dec 2024 13:00:52 +0100 Subject: [PATCH 5/8] docs: update changelog --- CHANGELOG.md | 4 +++- docs/changelog.md | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc6b331..9998e38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change Log ## v0.2.6-beta - December XX, 2024 -* Added a function to verify and validate the wallet ID before making a call to the Qubic network +* Added a check function to verify and validate the wallet ID before making a call to the Qubic network +* Optimized network calls by preventing invalid requests +* Added input validation to enhance security and reliability ## v0.2.5-beta - December 22, 2024 * Added new advanced code examples diff --git a/docs/changelog.md b/docs/changelog.md index 2b3e606..9998e38 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,4 +1,9 @@ # Change Log +## v0.2.6-beta - December XX, 2024 +* Added a check function to verify and validate the wallet ID before making a call to the Qubic network +* Optimized network calls by preventing invalid requests +* Added input validation to enhance security and reliability + ## v0.2.5-beta - December 22, 2024 * Added new advanced code examples * Reorganized documentation structure From 39a8e83f2ff47d39199ecdfa9097b773657eae45 Mon Sep 17 00:00:00 2001 From: Mazzya Date: Wed, 25 Dec 2024 14:27:24 +0100 Subject: [PATCH 6/8] refactor: simplify wallet ID validation with boolean expression --- qubipy/utils.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/qubipy/utils.py b/qubipy/utils.py index a42cf62..0fa1fc7 100644 --- a/qubipy/utils.py +++ b/qubipy/utils.py @@ -58,26 +58,15 @@ def check_bytes(tx: bytes): if not isinstance(tx, (bytes, bytearray)): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_TX_BYTES) + +def is_wallet_id_invalid(wallet_id: str) -> bool: + """ + Checks if the provided wallet ID is invalid. -def check_wallet_id(wallet_id: str): - """ - Validates that the provided wallet ID is in the correct format. - - Args: - wallet_id (str): The wallet ID to validate. Must be exactly 60 characters long. + Args: + wallet_id (str): The wallet ID to validate. Must be exactly 60 characters long. - Raises: - QubiPy_Exceptions: If the wallet ID is not a string, is not 60 characters long, - or contains non-alphabetic characters. - """ - - if not isinstance(wallet_id, str): - raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) - - - if len(wallet_id) != 60: - raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) - - - if not wallet_id.isalpha(): - raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) \ No newline at end of file + Returns: + bool: True if the wallet ID is invalid, False if valid + """ + return not isinstance(wallet_id, str) or len(wallet_id) != 60 or not wallet_id.isalpha() \ No newline at end of file From a1f2b336c2e2ac8b2f22ef29be880f7921b98730 Mon Sep 17 00:00:00 2001 From: Mazzya Date: Wed, 25 Dec 2024 14:27:55 +0100 Subject: [PATCH 7/8] refactor: add wallet validation to the rpc client --- qubipy/rpc/rpc_client.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/qubipy/rpc/rpc_client.py b/qubipy/rpc/rpc_client.py index 3d5fb35..55c33f9 100644 --- a/qubipy/rpc/rpc_client.py +++ b/qubipy/rpc/rpc_client.py @@ -356,7 +356,7 @@ def get_transfer_transactions_per_tick(self, identity: str | None = None, start_ QubiPy_Exceptions: If there is an issue with the API request (e.g., network error, invalid response, or timeout). """ - if not identity: + if not identity or is_wallet_id_invalid(identity): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) @@ -509,7 +509,7 @@ def get_issued_assets(self, identity: str | None = None) -> Dict[str, Any]: QubiPy_Exceptions: If there is an issue with the API request (e.g., network error, invalid response, or timeout). """ - if not identity: + if not identity or is_wallet_id_invalid(identity): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) endpoint = ISSUED_ASSETS.format(identity = identity) @@ -538,9 +538,10 @@ def get_owned_assets(self, identity: str | None = None) -> Dict[str, Any]: QubiPy_Exceptions: If there is an issue with the API request (e.g., network error, invalid response, or timeout). """ - if not identity: + if not identity or is_wallet_id_invalid(identity): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) + endpoint = OWNED_ASSETS.format(identity = identity) try: @@ -567,9 +568,10 @@ def get_possessed_assets(self, identity: str | None = None) -> Dict[str, Any]: QubiPy_Exceptions: If there is an issue with the API request (e.g., network error, invalid response, or timeout). """ - if not identity: + if not identity or is_wallet_id_invalid(identity): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) + endpoint = POSSESSED_ASSETS.format(identity = identity) try: From a6f7c0cca4100e1f3bcd7757d8a4b0e8475b962a Mon Sep 17 00:00:00 2001 From: Mazzya Date: Wed, 25 Dec 2024 14:42:20 +0100 Subject: [PATCH 8/8] refactor: add wallet validation to the rpc client --- qubipy/rpc/rpc_client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/qubipy/rpc/rpc_client.py b/qubipy/rpc/rpc_client.py index 55c33f9..85eb2be 100644 --- a/qubipy/rpc/rpc_client.py +++ b/qubipy/rpc/rpc_client.py @@ -128,10 +128,8 @@ def get_balance(self, wallet_id: str | None = None) -> Dict[str, Any]: QubiPy_Exceptions: If there is an issue with the API request (e.g., network error, invalid response, or timeout). """ - if not wallet_id: + if not wallet_id or is_wallet_id_invalid(wallet_id): raise QubiPy_Exceptions(QubiPy_Exceptions.INVALID_ADDRESS_ID) - - check_wallet_id(wallet_id) endpoint = WALLET_BALANCE.format(id = wallet_id.upper())