diff --git a/Makefile b/Makefile index bafa3ab..f44df2e 100644 --- a/Makefile +++ b/Makefile @@ -28,4 +28,4 @@ install-deps: .PHONY: docs docs: - sphinx-apidoc -o docs/ ./cdp/ + sphinx-apidoc -f -o docs/ ./cdp/ diff --git a/cdp/__init__.py b/cdp/__init__.py index 59e9448..b2ef04a 100644 --- a/cdp/__init__.py +++ b/cdp/__init__.py @@ -10,11 +10,13 @@ from cdp.transaction import Transaction from cdp.transfer import Transfer from cdp.wallet import Wallet +from cdp.wallet_data import WalletData __all__ = [ "__version__", "Cdp", "Wallet", + "WalletData", "Asset", "Transfer", "Address", diff --git a/cdp/wallet.py b/cdp/wallet.py index e5e81a8..41c2414 100644 --- a/cdp/wallet.py +++ b/cdp/wallet.py @@ -28,6 +28,7 @@ from cdp.faucet_transaction import FaucetTransaction from cdp.trade import Trade from cdp.wallet_address import WalletAddress +from cdp.wallet_data import WalletData class Wallet: @@ -176,8 +177,8 @@ def reload(self) -> None: self._model = model return - @staticmethod - def fetch(wallet_id: str) -> "Wallet": + @classmethod + def fetch(cls, wallet_id: str) -> "Wallet": """Fetch a wallet by its ID. Args: @@ -192,7 +193,7 @@ def fetch(wallet_id: str) -> "Wallet": """ model = Cdp.api_clients.wallets.get_wallet(wallet_id) - return Wallet(model, "") + return cls(model, "") @classmethod def list(cls) -> Iterator["Wallet"]: @@ -218,6 +219,31 @@ def list(cls) -> Iterator["Wallet"]: page = response.next_page + @classmethod + def import_data(cls, data: WalletData) -> "Wallet": + """Import a wallet from previously exported wallet data. + + Args: + data (WalletData): The wallet data to import. + + Returns: + Wallet: The imported wallet. + + Raises: + Exception: If there's an error getting the wallet. + + """ + if not isinstance(data, WalletData): + raise ValueError("Data must be a WalletData instance") + + model = Cdp.api_clients.wallets.get_wallet(data.wallet_id) + + wallet = cls(model, data.seed) + + wallet._set_addresses() + + return wallet + def create_address(self) -> "WalletAddress": """Create a new address for the wallet. @@ -372,6 +398,21 @@ def default_address(self) -> WalletAddress | None: else None ) + def export_data(self) -> WalletData: + """Export the wallet's data. + + Returns: + WalletData: The wallet's data. + + Raises: + ValueError: If the wallet does not have a seed loaded. + + """ + if self._master is None or self._seed is None: + raise ValueError("Wallet does not have seed loaded") + + return WalletData(self.id, self._seed) + def save_seed(self, file_path: str, encrypt: bool | None = False) -> None: """Save the wallet seed to a file. diff --git a/cdp/wallet_data.py b/cdp/wallet_data.py new file mode 100644 index 0000000..dc4816e --- /dev/null +++ b/cdp/wallet_data.py @@ -0,0 +1,73 @@ +class WalletData: + """A class representing wallet data required to recreate a wallet.""" + + def __init__(self, wallet_id: str, seed: str) -> None: + """Initialize the WalletData class. + + Args: + wallet_id (str): The ID of the wallet. + seed (str): The seed of the wallet. + + """ + self._wallet_id = wallet_id + self._seed = seed + + @property + def wallet_id(self) -> str: + """Get the ID of the wallet. + + Returns: + str: The ID of the wallet. + + """ + return self._wallet_id + + @property + def seed(self) -> str: + """Get the seed of the wallet. + + Returns: + str: The seed of the wallet. + + """ + return self._seed + + def to_dict(self) -> dict[str, str]: + """Convert the wallet data to a dictionary. + + Returns: + dict[str, str]: The dictionary representation of the wallet data. + + """ + return {"wallet_id": self.wallet_id, "seed": self.seed} + + @classmethod + def from_dict(cls, data: dict[str, str]) -> "WalletData": + """Create a WalletData class instance from the given dictionary. + + Args: + data (dict[str, str]): The data to create the WalletData object from. + + Returns: + WalletData: The wallet data. + + """ + return cls(data["wallet_id"], data["seed"]) + + def __str__(self) -> str: + """Return a string representation of the WalletData object. + + Returns: + str: A string representation of the wallet data. + + """ + return f"WalletData: (wallet_id: {self.wallet_id}, seed: {self.seed})" + + def __repr__(self) -> str: + """Return a string representation of the WalletData object. + + Returns: + str: A string representation of the wallet data. + + """ + return str(self) diff --git a/docs/cdp.rst b/docs/cdp.rst index 9812bca..e0c4e6b 100644 --- a/docs/cdp.rst +++ b/docs/cdp.rst @@ -132,6 +132,14 @@ cdp.wallet\_address module :undoc-members: :show-inheritance: +cdp.wallet\_data module +----------------------- + +.. automodule:: cdp.wallet_data + :members: + :undoc-members: + :show-inheritance: + Module contents ---------------