Skip to content

Commit

Permalink
upgrade seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 5, 2023
1 parent c91e799 commit 4b4d79c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
3 changes: 3 additions & 0 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
64 changes: 34 additions & 30 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 4b4d79c

Please sign in to comment.