From 398e98d84711f1ca1c305a7a98f61cb43af88b82 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 24 Dec 2022 12:44:47 +0100 Subject: [PATCH] feat: add legacy token serialization for backwards compatibility with old clients --- cashu/wallet/cli.py | 53 ++++++++++++++++++++++++++++-------------- cashu/wallet/wallet.py | 7 +++++- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index 11b5467..c8beccc 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -185,17 +185,21 @@ async def invoice(ctx, amount: int, hash: str): @coro async def balance(ctx, verbose): wallet: Wallet = ctx.obj["WALLET"] - # keyset_balances = wallet.balance_per_keyset() - # if len(keyset_balances) > 1: - # print(f"You have balances in {len(keyset_balances)} keysets:") - # print("") - # for k, v in keyset_balances.items(): - # print( - # f"Keyset: {k or 'undefined'} Balance: {v['balance']} sat (available: {v['available']} sat)" - # ) - # print("") + if verbose: + # show balances per keyset + keyset_balances = wallet.balance_per_keyset() + if len(keyset_balances) > 1: + print(f"You have balances in {len(keyset_balances)} keysets:") + print("") + for k, v in keyset_balances.items(): + print( + f"Keyset: {k or 'undefined'} Balance: {v['balance']} sat (available: {v['available']} sat)" + ) + print("") + mint_balances = await wallet.balance_per_minturl() if len(mint_balances) > 1: + # show balances per mint print(f"You have balances in {len(mint_balances)} mints:") print("") for k, v in mint_balances.items(): @@ -203,6 +207,7 @@ async def balance(ctx, verbose): f"Mint: {k or 'undefined'} Balance: {v['balance']} sat (available: {v['available']} sat)" ) print("") + if verbose: print( f"Balance: {wallet.balance} sat (available: {wallet.available_balance} sat in {len([p for p in wallet.proofs if not p.reserved])} tokens)" @@ -214,9 +219,16 @@ async def balance(ctx, verbose): @cli.command("send", help="Send tokens.") @click.argument("amount", type=int) @click.option("--lock", "-l", default=None, help="Lock tokens (P2SH).", type=str) +@click.option( + "--legacy", + default=False, + is_flag=True, + help="Print legacy token without mint information.", + type=bool, +) @click.pass_context @coro -async def send(ctx, amount: int, lock: str): +async def send(ctx, amount: int, lock: str, legacy: bool): if lock and len(lock) < 22: print("Error: lock has to be at least 22 characters long.") return @@ -236,14 +248,19 @@ async def send(ctx, amount: int, lock: str): ) print(token) - # print("") - # print("Legacy:") - # token = await wallet.serialize_proofs( - # send_proofs, - # hide_secrets=True if lock and not p2sh else False, - # include_mints=False, - # ) - # print(token) + if legacy: + print("") + print( + "Legacy token without mint information for older clients. This token can only be be received by wallets who use the mint the token is issued from:" + ) + print("") + token = await wallet.serialize_proofs( + send_proofs, + hide_secrets=True if lock and not p2sh else False, + legacy=True, + ) + print(token) + wallet.status() diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index 60f0806..205d689 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -482,7 +482,7 @@ class Wallet(LedgerAPI): return status["paid"] async def serialize_proofs( - self, proofs: List[Proof], hide_secrets=False, include_mints=False + self, proofs: List[Proof], hide_secrets=False, include_mints=False, legacy=False ): """ Produces sharable token with proofs and mint information. @@ -493,6 +493,11 @@ class Wallet(LedgerAPI): else: proofs_serialized = [p.to_dict() for p in proofs] + if legacy: + return base64.urlsafe_b64encode( + json.dumps(proofs_serialized).encode() + ).decode() + token = dict(tokens=proofs_serialized) # add mint information to the token, if requested