From 0a775e0c6e2adbcb959ed849b1a99c746ba76e9d Mon Sep 17 00:00:00 2001 From: Rohan Agarwal Date: Wed, 26 Feb 2025 20:01:29 -0700 Subject: [PATCH 1/4] fix --- cdp/user_operation.py | 5 +++++ tests/factories/api_key_factory.py | 4 +++- tests/test_api_key_utils.py | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cdp/user_operation.py b/cdp/user_operation.py index ded73a6..96be492 100644 --- a/cdp/user_operation.py +++ b/cdp/user_operation.py @@ -41,6 +41,11 @@ def __repr__(self) -> str: """Return a string representation of the Status.""" return str(self) + def __eq__(self, other): + if isinstance(other, str): + return self.value == other + return super().__eq__(other) + def __init__(self, model: UserOperationModel, smart_wallet_address: str) -> None: """Initialize the UserOperation class. diff --git a/tests/factories/api_key_factory.py b/tests/factories/api_key_factory.py index 1d54679..af80376 100644 --- a/tests/factories/api_key_factory.py +++ b/tests/factories/api_key_factory.py @@ -12,6 +12,7 @@ def dummy_key_factory(): - "ed25519-32": Returns a base64-encoded 32-byte Ed25519 private key. - "ed25519-64": Returns a base64-encoded 64-byte dummy Ed25519 key (the first 32 bytes will be used). """ + def _create_dummy(key_type: str = "ecdsa") -> str: if key_type == "ecdsa": return ( @@ -25,9 +26,10 @@ def _create_dummy(key_type: str = "ecdsa") -> str: return "BXyKC+eFINc/6ztE/3neSaPGgeiU9aDRpaDnAbaA/vyTrUNgtuh/1oX6Vp+OEObV3SLWF+OkF2EQNPtpl0pbfA==" elif key_type == "ed25519-64": # Create a 64-byte dummy by concatenating a 32-byte sequence with itself. - dummy_32 = b'\x01' * 32 + dummy_32 = b"\x01" * 32 dummy_64 = dummy_32 + dummy_32 return base64.b64encode(dummy_64).decode("utf-8") else: raise ValueError("Unsupported key type for dummy key creation") + return _create_dummy diff --git a/tests/test_api_key_utils.py b/tests/test_api_key_utils.py index 30f347a..21b178a 100644 --- a/tests/test_api_key_utils.py +++ b/tests/test_api_key_utils.py @@ -10,18 +10,21 @@ def test_parse_private_key_pem_ec(dummy_key_factory): parsed_key = _parse_private_key(dummy_key) assert isinstance(parsed_key, ec.EllipticCurvePrivateKey) + def test_parse_private_key_ed25519_32(dummy_key_factory): """Test that a base64-encoded 32-byte Ed25519 key is parsed correctly using a dummy key from the factory.""" dummy_key = dummy_key_factory("ed25519-32") parsed_key = _parse_private_key(dummy_key) assert isinstance(parsed_key, ed25519.Ed25519PrivateKey) + def test_parse_private_key_ed25519_64(dummy_key_factory): """Test that a base64-encoded 64-byte input is parsed correctly by taking the first 32 bytes using a dummy key from the factory.""" dummy_key = dummy_key_factory("ed25519-64") parsed_key = _parse_private_key(dummy_key) assert isinstance(parsed_key, ed25519.Ed25519PrivateKey) + def test_parse_private_key_invalid(): """Test that an invalid key string raises a ValueError.""" with pytest.raises(ValueError, match="Could not parse the private key"): From 1f58eddf737351d05d3c727eed6423c923903faa Mon Sep 17 00:00:00 2001 From: Rohan Agarwal Date: Wed, 26 Feb 2025 20:06:37 -0700 Subject: [PATCH 2/4] fix --- cdp/user_operation.py | 4 ++++ tests/test_user_operation.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cdp/user_operation.py b/cdp/user_operation.py index 96be492..4ecef8e 100644 --- a/cdp/user_operation.py +++ b/cdp/user_operation.py @@ -46,6 +46,10 @@ def __eq__(self, other): return self.value == other return super().__eq__(other) + def __hash__(self): + """Preserve hashability for dictionary keys.""" + return hash(self.name) + def __init__(self, model: UserOperationModel, smart_wallet_address: str) -> None: """Initialize the UserOperation class. diff --git a/tests/test_user_operation.py b/tests/test_user_operation.py index 2f8be8f..469299f 100644 --- a/tests/test_user_operation.py +++ b/tests/test_user_operation.py @@ -29,6 +29,21 @@ def test_user_operation_properties(user_operation_model_factory): assert user_operation.transaction_hash == model.transaction_hash +def test_user_operation_status_comparison(): + """Test the comparison of UserOperation statuses.""" + expected_statuses = { + UserOperation.Status.PENDING: "pending", + UserOperation.Status.SIGNED: "signed", + UserOperation.Status.BROADCAST: "broadcast", + UserOperation.Status.COMPLETE: "complete", + UserOperation.Status.FAILED: "failed", + UserOperation.Status.UNSPECIFIED: "unspecified", + } + + for status, _ in expected_statuses.items(): + assert status == expected_statuses[status] + + @patch("cdp.Cdp.api_clients") def test_create_user_operation(mock_api_clients, user_operation_model_factory): """Test the creation of a UserOperation object.""" From 8bb75d375981addea0a9a5ff51982c62e5043877 Mon Sep 17 00:00:00 2001 From: Rohan Agarwal Date: Wed, 26 Feb 2025 20:08:22 -0700 Subject: [PATCH 3/4] fix --- cdp/user_operation.py | 1 + tests/test_user_operation.py | 15 --------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/cdp/user_operation.py b/cdp/user_operation.py index 4ecef8e..a2de7a4 100644 --- a/cdp/user_operation.py +++ b/cdp/user_operation.py @@ -42,6 +42,7 @@ def __repr__(self) -> str: return str(self) def __eq__(self, other): + """Check if the status is equal to another object. Supports string comparison.""" if isinstance(other, str): return self.value == other return super().__eq__(other) diff --git a/tests/test_user_operation.py b/tests/test_user_operation.py index 469299f..2f8be8f 100644 --- a/tests/test_user_operation.py +++ b/tests/test_user_operation.py @@ -29,21 +29,6 @@ def test_user_operation_properties(user_operation_model_factory): assert user_operation.transaction_hash == model.transaction_hash -def test_user_operation_status_comparison(): - """Test the comparison of UserOperation statuses.""" - expected_statuses = { - UserOperation.Status.PENDING: "pending", - UserOperation.Status.SIGNED: "signed", - UserOperation.Status.BROADCAST: "broadcast", - UserOperation.Status.COMPLETE: "complete", - UserOperation.Status.FAILED: "failed", - UserOperation.Status.UNSPECIFIED: "unspecified", - } - - for status, _ in expected_statuses.items(): - assert status == expected_statuses[status] - - @patch("cdp.Cdp.api_clients") def test_create_user_operation(mock_api_clients, user_operation_model_factory): """Test the creation of a UserOperation object.""" From a7f6a98e524216cc6d0fb1303d4602a81bdad852 Mon Sep 17 00:00:00 2001 From: Rohan Agarwal Date: Wed, 26 Feb 2025 20:12:55 -0700 Subject: [PATCH 4/4] Fix --- cdp/user_operation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdp/user_operation.py b/cdp/user_operation.py index a2de7a4..e78b05b 100644 --- a/cdp/user_operation.py +++ b/cdp/user_operation.py @@ -48,7 +48,7 @@ def __eq__(self, other): return super().__eq__(other) def __hash__(self): - """Preserve hashability for dictionary keys.""" + """Return a hash value for the enum member to allow use as dictionary keys.""" return hash(self.name) def __init__(self, model: UserOperationModel, smart_wallet_address: str) -> None: