Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet: ensure that mint keys are loaded before calling appropriate api methods #356

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ async def wrapper(self, *args, **kwargs):
return wrapper


def async_ensure_mint_loaded(func):
"""Decorator that ensures that the mint is loaded before calling the wrapped
function. If the mint is not loaded, it will be loaded first.
"""

async def wrapper(self, *args, **kwargs):
if not self.keysets:
await self._load_mint()
return await func(self, *args, **kwargs)

return wrapper


class LedgerAPI(object):
keyset_id: str # holds current keyset id
keysets: Dict[str, WalletKeyset] # holds keysets
Expand Down Expand Up @@ -361,6 +374,7 @@ async def _get_info(self, url: str) -> GetInfoResponse:
return mint_info

@async_set_httpx_client
@async_ensure_mint_loaded
async def request_mint(self, amount) -> Invoice:
"""Requests a mint from the server and returns Lightning invoice.

Expand Down Expand Up @@ -389,6 +403,7 @@ async def request_mint(self, amount) -> Invoice:
)

@async_set_httpx_client
@async_ensure_mint_loaded
async def mint(
self, outputs: List[BlindedMessage], id: Optional[str] = None
) -> List[BlindedSignature]:
Expand Down Expand Up @@ -421,6 +436,7 @@ async def mint(
return promises

@async_set_httpx_client
@async_ensure_mint_loaded
async def split(
self,
proofs: List[Proof],
Expand Down Expand Up @@ -460,6 +476,7 @@ def _splitrequest_include_fields(proofs: List[Proof]):
return promises

@async_set_httpx_client
@async_ensure_mint_loaded
async def check_proof_state(self, proofs: List[Proof]):
"""
Checks whether the secrets in proofs are already spent or not and returns a list of booleans.
Expand All @@ -483,6 +500,7 @@ def _check_proof_state_include_fields(proofs):
return states

@async_set_httpx_client
@async_ensure_mint_loaded
async def check_fees(self, payment_request: str):
"""Checks whether the Lightning payment is internal."""
payload = CheckFeesRequest(pr=payment_request)
Expand All @@ -496,6 +514,7 @@ async def check_fees(self, payment_request: str):
return return_dict

@async_set_httpx_client
@async_ensure_mint_loaded
async def pay_lightning(
self, proofs: List[Proof], invoice: str, outputs: Optional[List[BlindedMessage]]
) -> GetMeltResponse:
Expand Down Expand Up @@ -526,6 +545,7 @@ def _meltrequest_include_fields(proofs: List[Proof]):
return GetMeltResponse.parse_obj(return_dict)

@async_set_httpx_client
@async_ensure_mint_loaded
async def restore_promises(
self, outputs: List[BlindedMessage]
) -> Tuple[List[BlindedMessage], List[BlindedSignature]]:
Expand Down
Loading