Skip to content

Commit 6d18e53

Browse files
[chore] Import/Export WalletData (#9)
1 parent bdabb2d commit 6d18e53

File tree

5 files changed

+128
-4
lines changed

5 files changed

+128
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ install-deps:
2828

2929
.PHONY: docs
3030
docs:
31-
sphinx-apidoc -o docs/ ./cdp/
31+
sphinx-apidoc -f -o docs/ ./cdp/

cdp/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
from cdp.transaction import Transaction
1111
from cdp.transfer import Transfer
1212
from cdp.wallet import Wallet
13+
from cdp.wallet_data import WalletData
1314

1415
__all__ = [
1516
"__version__",
1617
"Cdp",
1718
"Wallet",
19+
"WalletData",
1820
"Asset",
1921
"Transfer",
2022
"Address",

cdp/wallet.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from cdp.faucet_transaction import FaucetTransaction
2929
from cdp.trade import Trade
3030
from cdp.wallet_address import WalletAddress
31+
from cdp.wallet_data import WalletData
3132

3233

3334
class Wallet:
@@ -176,8 +177,8 @@ def reload(self) -> None:
176177
self._model = model
177178
return
178179

179-
@staticmethod
180-
def fetch(wallet_id: str) -> "Wallet":
180+
@classmethod
181+
def fetch(cls, wallet_id: str) -> "Wallet":
181182
"""Fetch a wallet by its ID.
182183
183184
Args:
@@ -192,7 +193,7 @@ def fetch(wallet_id: str) -> "Wallet":
192193
"""
193194
model = Cdp.api_clients.wallets.get_wallet(wallet_id)
194195

195-
return Wallet(model, "")
196+
return cls(model, "")
196197

197198
@classmethod
198199
def list(cls) -> Iterator["Wallet"]:
@@ -218,6 +219,31 @@ def list(cls) -> Iterator["Wallet"]:
218219

219220
page = response.next_page
220221

222+
@classmethod
223+
def import_data(cls, data: WalletData) -> "Wallet":
224+
"""Import a wallet from previously exported wallet data.
225+
226+
Args:
227+
data (WalletData): The wallet data to import.
228+
229+
Returns:
230+
Wallet: The imported wallet.
231+
232+
Raises:
233+
Exception: If there's an error getting the wallet.
234+
235+
"""
236+
if not isinstance(data, WalletData):
237+
raise ValueError("Data must be a WalletData instance")
238+
239+
model = Cdp.api_clients.wallets.get_wallet(data.wallet_id)
240+
241+
wallet = cls(model, data.seed)
242+
243+
wallet._set_addresses()
244+
245+
return wallet
246+
221247
def create_address(self) -> "WalletAddress":
222248
"""Create a new address for the wallet.
223249
@@ -372,6 +398,21 @@ def default_address(self) -> WalletAddress | None:
372398
else None
373399
)
374400

401+
def export_data(self) -> WalletData:
402+
"""Export the wallet's data.
403+
404+
Returns:
405+
WalletData: The wallet's data.
406+
407+
Raises:
408+
ValueError: If the wallet does not have a seed loaded.
409+
410+
"""
411+
if self._master is None or self._seed is None:
412+
raise ValueError("Wallet does not have seed loaded")
413+
414+
return WalletData(self.id, self._seed)
415+
375416
def save_seed(self, file_path: str, encrypt: bool | None = False) -> None:
376417
"""Save the wallet seed to a file.
377418

cdp/wallet_data.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class WalletData:
2+
"""A class representing wallet data required to recreate a wallet."""
3+
4+
def __init__(self, wallet_id: str, seed: str) -> None:
5+
"""Initialize the WalletData class.
6+
7+
Args:
8+
wallet_id (str): The ID of the wallet.
9+
seed (str): The seed of the wallet.
10+
11+
"""
12+
self._wallet_id = wallet_id
13+
self._seed = seed
14+
15+
@property
16+
def wallet_id(self) -> str:
17+
"""Get the ID of the wallet.
18+
19+
Returns:
20+
str: The ID of the wallet.
21+
22+
"""
23+
return self._wallet_id
24+
25+
@property
26+
def seed(self) -> str:
27+
"""Get the seed of the wallet.
28+
29+
Returns:
30+
str: The seed of the wallet.
31+
32+
"""
33+
return self._seed
34+
35+
def to_dict(self) -> dict[str, str]:
36+
"""Convert the wallet data to a dictionary.
37+
38+
Returns:
39+
dict[str, str]: The dictionary representation of the wallet data.
40+
41+
"""
42+
return {"wallet_id": self.wallet_id, "seed": self.seed}
43+
44+
@classmethod
45+
def from_dict(cls, data: dict[str, str]) -> "WalletData":
46+
"""Create a WalletData class instance from the given dictionary.
47+
48+
Args:
49+
data (dict[str, str]): The data to create the WalletData object from.
50+
51+
Returns:
52+
WalletData: The wallet data.
53+
54+
"""
55+
return cls(data["wallet_id"], data["seed"])
56+
57+
def __str__(self) -> str:
58+
"""Return a string representation of the WalletData object.
59+
60+
Returns:
61+
str: A string representation of the wallet data.
62+
63+
"""
64+
return f"WalletData: (wallet_id: {self.wallet_id}, seed: {self.seed})"
65+
66+
def __repr__(self) -> str:
67+
"""Return a string representation of the WalletData object.
68+
69+
Returns:
70+
str: A string representation of the wallet data.
71+
72+
"""
73+
return str(self)

docs/cdp.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ cdp.wallet\_address module
132132
:undoc-members:
133133
:show-inheritance:
134134

135+
cdp.wallet\_data module
136+
-----------------------
137+
138+
.. automodule:: cdp.wallet_data
139+
:members:
140+
:undoc-members:
141+
:show-inheritance:
142+
135143
Module contents
136144
---------------
137145

0 commit comments

Comments
 (0)