From 4b4d79c3599d770b43405474034c7e68a4c6ea36 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Thu, 5 Oct 2023 03:21:35 +0200 Subject: [PATCH] upgrade seems to work --- cashu/core/base.py | 3 ++ cashu/wallet/wallet.py | 64 ++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/cashu/core/base.py b/cashu/core/base.py index c5b52f67..aec84c36 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -324,6 +324,7 @@ def __init__( valid_to=None, first_seen=None, active=None, + deprecated_id=False, ): self.valid_from = valid_from self.valid_to = valid_to @@ -336,6 +337,8 @@ def __init__( self.id = derive_keyset_id(self.public_keys) # BEGIN BACKWARDS COMPATIBILITY < 0.14.0 self.id_deprecated = derive_keyset_id_deprecated(self.public_keys) + if deprecated_id: + self.id = self.id_deprecated # END BACKWARDS COMPATIBILITY < 0.14.0 def serialize(self): diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index 64b2c39d..81e939c3 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -5,7 +5,7 @@ import time import uuid from itertools import groupby -from typing import Callable, Dict, List, Optional, Tuple, Union +from typing import Dict, List, Optional, Tuple, Union import requests from bip32 import BIP32 @@ -122,15 +122,11 @@ async def _init_s(self): @staticmethod def raise_on_error( resp: Response, - call_404: Optional[Callable] = None, - call_args: List = [], - call_kwargs: Dict = {}, ) -> None: """Raises an exception if the response from the mint contains an error. Args: resp_dict (Response): Response dict (previously JSON) from mint - call_instead (Callable): Function to call instead of raising an exception Raises: Exception: if the response contains an error @@ -141,11 +137,6 @@ def raise_on_error( error_message = f"Mint Error: {resp_dict['detail']}" if "code" in resp_dict: error_message += f" (Code: {resp_dict['code']})" - # BEGIN BACKWARDS COMPATIBILITY < 0.14.0 - # if the error is a 404, we assume that the mint is not upgraded yet - if call_404 and resp.status_code == 404: - return call_404(*call_args, **call_kwargs) - # END BACKWARDS COMPATIBILITY < 0.14.0 raise Exception(error_message) # raise for status if no error resp.raise_for_status() @@ -172,8 +163,8 @@ async def _load_mint_keys(self, keyset_id: str = "") -> WalletKeyset: # get current keyset keyset = await self._get_keys(self.url) - assert keyset.public_keys - assert keyset.id + assert keyset.public_keys, f"did not receive keys from mint: {keyset}" + assert keyset.id, f"did not receive keyset id from mint: {keyset}" assert len(keyset.public_keys) > 0, "did not receive keys from mint." # check if current keyset is in db @@ -261,11 +252,13 @@ async def _get_keys(self, url: str) -> WalletKeyset: resp = self.s.get( url + "/v1/keys", ) - self.raise_on_error( - resp, - call_404=self._get_keys_deprecated, # backwards compatibility < 0.14.0 - call_args=[url], - ) + # BEGIN backwards compatibility < 0.14.0 + # assume the mint has not upgraded yet if we get a 404 + if resp.status_code == 404: + ret = await self._get_keys_deprecated(url) + return ret + # END backwards compatibility < 0.14.0 + self.raise_on_error(resp) keys_dict: dict = resp.json() assert len(keys_dict), Exception("did not receive any keys") keys = KeysResponse.parse_obj(keys_dict) @@ -295,11 +288,16 @@ async def _get_keys_of_keyset(self, url: str, keyset_id: str) -> WalletKeyset: resp = self.s.get( url + f"/v1/keys/{keyset_id_urlsafe}", ) - self.raise_on_error( - resp, - call_404=self._get_keys_of_keyset_deprecated, # backwards compatibility < 0.14.0 - call_args=[url, keyset_id], - ) + # BEGIN backwards compatibility < 0.14.0 + # assume the mint has not upgraded yet if we get a 404 + if resp.status_code == 404: + ret = await self._get_keys_of_keyset_deprecated(url, keyset_id) + return ret + # END backwards compatibility < 0.14.0 + r = self.raise_on_error(resp) + if r: + return r # backwards compatibility < 0.14.0 + keys_dict = resp.json() assert len(keys_dict), Exception("did not receive any keys") keys = KeysResponse.parse_obj(keys_dict) @@ -326,11 +324,16 @@ async def _get_keyset_ids(self, url: str) -> List[str]: resp = self.s.get( url + "/v1/keysets", ) - self.raise_on_error( - resp, - call_404=self._get_keyset_ids_deprecated, # backwards compatibility < 0.14.0 - call_args=[url], - ) + # BEGIN backwards compatibility < 0.14.0 + # assume the mint has not upgraded yet if we get a 404 + if resp.status_code == 404: + ret = await self._get_keyset_ids_deprecated(url) + return ret + # END backwards compatibility < 0.14.0 + r = self.raise_on_error(resp) + if r: + return r # backwards compatibility < 0.14.0 + keysets_dict = resp.json() keysets = KeysetsResponse.parse_obj(keysets_dict) assert len(keysets.keysets), Exception("did not receive any keysets") @@ -883,9 +886,10 @@ async def _construct_proofs( proofs: List[Proof] = [] for promise, secret, r, path in zip(promises, secrets, rs, derivation_paths): logger.trace(f"Creating proof with keyset {self.keyset_id} = {promise.id}") - assert ( - self.keyset_id == promise.id - ), "our keyset id does not match promise id." + assert self.keyset_id == promise.id, ( + f"our keyset id does not match promise id: {self.keyset_id} !=" + f" {promise.id}" + ) C_ = PublicKey(bytes.fromhex(promise.C_), raw=True) C = b_dhke.step3_alice(C_, r, self.public_keys[promise.amount])