From c0c454968d0971cca46b9b44cdfd18392d45bbc2 Mon Sep 17 00:00:00 2001 From: korayakpinar Date: Fri, 10 Oct 2025 15:40:09 +0300 Subject: [PATCH 1/2] feat: added prefix check for the BLS private key input when it comes as a string --- pyproject.toml | 5 ++++- staking-cli/src/__init__.py | 0 staking-cli/src/helpers.py | 3 +++ staking-cli/tests/__init__.py | 0 staking-cli/tests/conftest.py | 7 +++++++ staking-cli/tests/test_prefix.py | 18 ++++++++++++++++++ 6 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 staking-cli/src/__init__.py create mode 100644 staking-cli/tests/__init__.py create mode 100644 staking-cli/tests/conftest.py create mode 100644 staking-cli/tests/test_prefix.py diff --git a/pyproject.toml b/pyproject.toml index 9626fb3..973e7a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,10 +46,13 @@ omit = ["src/staking_sdk_py/__about__.py"] [tool.coverage.paths] staking_sdk_py = ["src/staking_sdk_py", "*/staking-sdk-py/src/staking_sdk_py"] -tests = ["tests", "*/staking-sdk-py/tests"] +tests = ["*/staking-sdk-py/tests"] [tool.coverage.report] exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"] [tool.hatch.build.targets.wheel] packages = ["src/staking_sdk_py"] + +[tool.pytest.ini_options] +testpaths = ["staking-cli/tests"] diff --git a/staking-cli/src/__init__.py b/staking-cli/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/staking-cli/src/helpers.py b/staking-cli/src/helpers.py index dd0e763..b78198d 100644 --- a/staking-cli/src/helpers.py +++ b/staking-cli/src/helpers.py @@ -30,6 +30,9 @@ def is_valid_amount(amount: int, register=False) -> bool: def is_valid_bls_private_key(private_key: Union[int, str]) -> bool: '''Validates a BLS12-381 private key.''' if isinstance(private_key, str): + if not private_key.startswith("0x"): + return False + try: # Convert hex string (with '0x' prefix) to integer key_int = int(private_key, 16) diff --git a/staking-cli/tests/__init__.py b/staking-cli/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/staking-cli/tests/conftest.py b/staking-cli/tests/conftest.py new file mode 100644 index 0000000..f56cd21 --- /dev/null +++ b/staking-cli/tests/conftest.py @@ -0,0 +1,7 @@ +import sys +from pathlib import Path + +# Add the staking-cli directory to the Python path +staking_cli_dir = Path(__file__).parent.parent +sys.path.insert(0, str(staking_cli_dir)) + diff --git a/staking-cli/tests/test_prefix.py b/staking-cli/tests/test_prefix.py new file mode 100644 index 0000000..663b5a8 --- /dev/null +++ b/staking-cli/tests/test_prefix.py @@ -0,0 +1,18 @@ +import pytest +from src.helpers import is_valid_bls_private_key + +BLS_PRIV_NO_PREFIX = "1f8b8a9d3c4e5f112233445566778899aabbccddeeff00112233445566778899" +BLS_PRIV_WITH_PREFIX = "0x" + BLS_PRIV_NO_PREFIX + +def test_accepts_0x_prefixed_hex(): + assert is_valid_bls_private_key(BLS_PRIV_WITH_PREFIX) is True + +def test_rejects_hex_without_prefix(): + assert is_valid_bls_private_key(BLS_PRIV_NO_PREFIX) is False + +@pytest.mark.parametrize("bad", ["0X" + BLS_PRIV_NO_PREFIX, "0xGG", None, []]) +def test_rejects_invalid_inputs(bad): + assert is_valid_bls_private_key(bad) is False + +def test_accepts_int(): + assert is_valid_bls_private_key(int(BLS_PRIV_NO_PREFIX, 16)) is True \ No newline at end of file From b078b22573df095331e0624e0d0e5188ae002b84 Mon Sep 17 00:00:00 2001 From: korayakpinar Date: Fri, 10 Oct 2025 15:45:54 +0300 Subject: [PATCH 2/2] chore: changed the error message to include warning for prefix in BLS key validation --- staking-cli/src/add_validator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/staking-cli/src/add_validator.py b/staking-cli/src/add_validator.py index 591f179..ec0b0bf 100644 --- a/staking-cli/src/add_validator.py +++ b/staking-cli/src/add_validator.py @@ -118,7 +118,7 @@ def register_validator_cli(config: dict, secp_privkey: str, bls_privkey: str, au # Input validation try: if not is_valid_bls_private_key(bls_privkey): - log.error("Key validation failed! Verify bls key") + log.error("Key validation failed! Verify bls key and don't forget to add 0x prefix") return if not is_valid_secp256k1_private_key(secp_privkey): log.error("Key validation failed! Verify secp key")