Skip to content

Commit

Permalink
[chore] Import/Export WalletData (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
John-peterson-coinbase authored Sep 25, 2024
1 parent bdabb2d commit 6d18e53
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ install-deps:

.PHONY: docs
docs:
sphinx-apidoc -o docs/ ./cdp/
sphinx-apidoc -f -o docs/ ./cdp/
2 changes: 2 additions & 0 deletions cdp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 44 additions & 3 deletions cdp/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"]:
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
73 changes: 73 additions & 0 deletions cdp/wallet_data.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions docs/cdp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------

Expand Down

0 comments on commit 6d18e53

Please sign in to comment.