From 0c3777dd1799337f2cb6ee1a5545120958834eb4 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Thu, 9 Nov 2023 20:42:05 -0300 Subject: [PATCH] ensure that mint keys are loaded before calling appropriate api methods (#356) --- cashu/wallet/wallet.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index 115b59dd..7c20ff81 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -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 @@ -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. @@ -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]: @@ -421,6 +436,7 @@ async def mint( return promises @async_set_httpx_client + @async_ensure_mint_loaded async def split( self, proofs: List[Proof], @@ -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. @@ -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) @@ -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: @@ -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]]: