File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,26 @@ def can_sign(self) -> bool:
72
72
"""
73
73
return self .key is not None
74
74
75
+ def export (self ) -> str :
76
+ """Export the wallet address's private key as a hex string.
77
+
78
+ Returns:
79
+ str: The wallet address's private key as a hex string.
80
+
81
+ Raises:
82
+ ValueError: If the wallet address does not have a private key.
83
+
84
+ """
85
+ local_account = self .key
86
+ if local_account is None :
87
+ raise ValueError ("Private key is unavailable" )
88
+
89
+ key_bytes = local_account .key
90
+ if key_bytes is None :
91
+ raise ValueError ("Private key is empty" )
92
+
93
+ return key_bytes .hex ()
94
+
75
95
def transfer (
76
96
self ,
77
97
amount : Number | Decimal | str ,
Original file line number Diff line number Diff line change @@ -81,6 +81,25 @@ def test_key_setter_raises_error_when_already_set(wallet_address_factory):
81
81
wallet_address_with_key .key = new_key
82
82
83
83
84
+ def test_export (wallet_address_factory ):
85
+ """Test export method success for a WalletAddress."""
86
+ wallet_address_with_key = wallet_address_factory (True )
87
+
88
+ key_hex = wallet_address_with_key .export ()
89
+
90
+ assert key_hex is not None
91
+ assert key_hex != ""
92
+ assert key_hex .startswith ("0x" )
93
+
94
+
95
+ def test_export_raises_error_when_local_account_is_none (wallet_address_factory ):
96
+ """Test export method failure for a WalletAddress with no LocalAccount."""
97
+ wallet_address_without_key = wallet_address_factory ()
98
+
99
+ with pytest .raises (ValueError , match = "Private key is unavailable" ):
100
+ wallet_address_without_key .export ()
101
+
102
+
84
103
@patch ("cdp.wallet_address.Transfer" )
85
104
@patch ("cdp.Cdp.api_clients" )
86
105
@patch ("cdp.Cdp.use_server_signer" , True )
You can’t perform that action at this time.
0 commit comments