Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export wallet address #48

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cdp/wallet_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ 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:
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()
stat marked this conversation as resolved.
Show resolved Hide resolved

def transfer(
self,
amount: Number | Decimal | str,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_wallet_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ 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 != ""
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="Private key is unavailable"):
wallet_address_without_key.export()


@patch("cdp.wallet_address.Transfer")
@patch("cdp.Cdp.api_clients")
@patch("cdp.Cdp.use_server_signer", True)
Expand Down