Skip to content

Commit

Permalink
ensure that mint keys are loaded before calling appropriate api metho…
Browse files Browse the repository at this point in the history
…ds (#356)
  • Loading branch information
callebtc authored Nov 9, 2023
1 parent 84cbeb6 commit 0c3777d
Showing 1 changed file with 20 additions and 0 deletions.
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

0 comments on commit 0c3777d

Please sign in to comment.