From a99d22f94423235f89211a334d734440e64cc424 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Thu, 21 Nov 2024 15:37:18 -0800 Subject: [PATCH 1/4] first pass implementing wallet address private key export as a hex string --- cdp/wallet_address.py | 10 ++++++++++ tests/test_wallet_address.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cdp/wallet_address.py b/cdp/wallet_address.py index 19bfb83..b1dd614 100644 --- a/cdp/wallet_address.py +++ b/cdp/wallet_address.py @@ -72,6 +72,16 @@ def can_sign(self) -> bool: """ return self.key is not None + def export(self) -> str: + local_account = self.key + + if local_account is None: + raise ValueError("Account is unavailable") + + key_bytes = local_account.key + + return key_bytes.hex() + def transfer( self, amount: Number | Decimal | str, diff --git a/tests/test_wallet_address.py b/tests/test_wallet_address.py index a04d6f2..fcc4464 100644 --- a/tests/test_wallet_address.py +++ b/tests/test_wallet_address.py @@ -81,6 +81,24 @@ def test_key_setter_raises_error_when_already_set(wallet_address_factory): wallet_address_with_key.key = new_key +def test_export(wallet_address_factory): + """Test export method success for a WalletAddress.""" + wallet_address_with_key = wallet_address_factory(True) + + key_hex = wallet_address_with_key.export() + + assert key_hex is not None + assert key_hex is not "" + + +def test_export_raises_error_when_local_account_is_none(wallet_address_factory): + """Test export method failure for a WalletAddress with no LocalAccount.""" + wallet_address_without_key = wallet_address_factory() + + with pytest.raises(ValueError, match="Account is unavailable"): + wallet_address_without_key.export() + + @patch("cdp.wallet_address.Transfer") @patch("cdp.Cdp.api_clients") @patch("cdp.Cdp.use_server_signer", True) From 62acebe4d5fa8ce3aafb340e3ab56efb6a466693 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Mon, 25 Nov 2024 00:27:22 -0800 Subject: [PATCH 2/4] formatting --- cdp/wallet_address.py | 9 +++++++++ tests/test_wallet_address.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cdp/wallet_address.py b/cdp/wallet_address.py index b1dd614..d2f99e2 100644 --- a/cdp/wallet_address.py +++ b/cdp/wallet_address.py @@ -73,6 +73,15 @@ def can_sign(self) -> bool: return self.key is not None def export(self) -> str: + """Export the wallet address's private key as a hex string. + + Returns: + str: The wallet address's private key as a hex string. + + Raises: + ValueError: If the wallet address does not have a private key. + + """ local_account = self.key if local_account is None: diff --git a/tests/test_wallet_address.py b/tests/test_wallet_address.py index fcc4464..acd22a8 100644 --- a/tests/test_wallet_address.py +++ b/tests/test_wallet_address.py @@ -88,7 +88,7 @@ def test_export(wallet_address_factory): key_hex = wallet_address_with_key.export() assert key_hex is not None - assert key_hex is not "" + assert key_hex != "" def test_export_raises_error_when_local_account_is_none(wallet_address_factory): From 6db3413f2655a43a72b92fa09196bb8a0028c362 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Mon, 25 Nov 2024 10:06:07 -0800 Subject: [PATCH 3/4] implementing feedback --- cdp/wallet_address.py | 2 +- tests/test_wallet_address.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cdp/wallet_address.py b/cdp/wallet_address.py index d2f99e2..4ea6bdb 100644 --- a/cdp/wallet_address.py +++ b/cdp/wallet_address.py @@ -85,7 +85,7 @@ def export(self) -> str: local_account = self.key if local_account is None: - raise ValueError("Account is unavailable") + raise ValueError("Private key is unavailable") key_bytes = local_account.key diff --git a/tests/test_wallet_address.py b/tests/test_wallet_address.py index acd22a8..883782c 100644 --- a/tests/test_wallet_address.py +++ b/tests/test_wallet_address.py @@ -89,13 +89,14 @@ def test_export(wallet_address_factory): assert key_hex is not None assert key_hex != "" + assert key_hex.startswith("0x") def test_export_raises_error_when_local_account_is_none(wallet_address_factory): """Test export method failure for a WalletAddress with no LocalAccount.""" wallet_address_without_key = wallet_address_factory() - with pytest.raises(ValueError, match="Account is unavailable"): + with pytest.raises(ValueError, match="Private key is unavailable"): wallet_address_without_key.export() From f812601b57c6f71f024118393a4aa8ca0df9ce73 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Mon, 25 Nov 2024 10:08:08 -0800 Subject: [PATCH 4/4] adding additional check for key bytes --- cdp/wallet_address.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cdp/wallet_address.py b/cdp/wallet_address.py index 4ea6bdb..4f0efa8 100644 --- a/cdp/wallet_address.py +++ b/cdp/wallet_address.py @@ -83,11 +83,12 @@ def export(self) -> str: """ local_account = self.key - if local_account is None: raise ValueError("Private key is unavailable") key_bytes = local_account.key + if key_bytes is None: + raise ValueError("Private key is empty") return key_bytes.hex()