Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-stone committed Nov 27, 2024
1 parent d569b15 commit cb70de3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
### Fixed
- Fix bug in `Asset.from_model` where passed in asset ID was not used when creating a gwei or wei asset.

## [0.10.4] - 2024-11-25

### Added

- Wallet address key export

## [0.10.3] - 2024-11-07

### Added
Expand Down
20 changes: 20 additions & 0 deletions cdp/wallet_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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()

def transfer(
self,
amount: Number | Decimal | str,
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

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 @@ -83,6 +83,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

0 comments on commit cb70de3

Please sign in to comment.