Fix Tokenv4 handling of base64 keysets (#575)

* wip: handle tokenv4 if the keyset is base64

* wip

* Tokens now dataclasses

* Deserialize DLEQ from string

* add tokenv3/v4 base64 keyset ID tests

* fix cli
This commit is contained in:
callebtc
2024-07-10 14:12:03 +02:00
committed by GitHub
parent 8eea541b74
commit 26b94951fc
12 changed files with 326 additions and 185 deletions

View File

@@ -3,7 +3,7 @@ from typing import Optional
from loguru import logger
from ..core.base import TokenV3, TokenV4
from ..core.base import Token, TokenV3, TokenV4
from ..core.db import Database
from ..core.helpers import sum_proofs
from ..core.migrations import migrate_databases
@@ -34,7 +34,7 @@ async def list_mints(wallet: Wallet):
return mints
async def redeem_TokenV3_multimint(wallet: Wallet, token: TokenV3) -> Wallet:
async def redeem_TokenV3(wallet: Wallet, token: TokenV3) -> Wallet:
"""
Helper function to iterate thruogh a token with multiple mints and redeem them from
these mints one keyset at a time.
@@ -46,9 +46,7 @@ async def redeem_TokenV3_multimint(wallet: Wallet, token: TokenV3) -> Wallet:
token.unit = keysets[0].unit.name
for t in token.token:
assert t.mint, Exception(
"redeem_TokenV3_multimint: multimint redeem without URL"
)
assert t.mint, Exception("redeem_TokenV3: multimint redeem without URL")
mint_wallet = await Wallet.with_db(
t.mint,
os.path.join(settings.cashu_dir, wallet.name),
@@ -74,12 +72,23 @@ async def redeem_TokenV4(wallet: Wallet, token: TokenV4) -> Wallet:
return wallet
def deserialize_token_from_string(token: str) -> TokenV4:
# deserialize token
async def redeem_universal(wallet: Wallet, token: Token) -> Wallet:
if isinstance(token, TokenV3):
return await redeem_TokenV3(wallet, token)
if isinstance(token, TokenV4):
return await redeem_TokenV4(wallet, token)
raise Exception("Invalid token type")
def deserialize_token_from_string(token: str) -> Token:
# deserialize token
if token.startswith("cashuA"):
tokenV3Obj = TokenV3.deserialize(token)
return TokenV4.from_tokenv3(tokenV3Obj)
try:
return TokenV4.from_tokenv3(tokenV3Obj)
except ValueError as e:
logger.debug(f"Could not convert TokenV3 to TokenV4: {e}")
return tokenV3Obj
if token.startswith("cashuB"):
tokenObj = TokenV4.deserialize(token)
return tokenObj
@@ -89,14 +98,9 @@ def deserialize_token_from_string(token: str) -> TokenV4:
async def receive(
wallet: Wallet,
tokenObj: TokenV4,
token: Token,
) -> Wallet:
# redeem tokens with new wallet instances
mint_wallet = await redeem_TokenV4(
wallet,
tokenObj,
)
mint_wallet = await redeem_universal(wallet, token)
# reload main wallet so the balance updates
await wallet.load_proofs(reload=True)
return mint_wallet