feat: add legacy token serialization for backwards compatibility with old clients

This commit is contained in:
callebtc
2022-12-24 12:44:47 +01:00
parent d9bca8e6e2
commit 398e98d847
2 changed files with 41 additions and 19 deletions

View File

@@ -185,17 +185,21 @@ async def invoice(ctx, amount: int, hash: str):
@coro @coro
async def balance(ctx, verbose): async def balance(ctx, verbose):
wallet: Wallet = ctx.obj["WALLET"] wallet: Wallet = ctx.obj["WALLET"]
# keyset_balances = wallet.balance_per_keyset() if verbose:
# if len(keyset_balances) > 1: # show balances per keyset
# print(f"You have balances in {len(keyset_balances)} keysets:") keyset_balances = wallet.balance_per_keyset()
# print("") if len(keyset_balances) > 1:
# for k, v in keyset_balances.items(): print(f"You have balances in {len(keyset_balances)} keysets:")
# print( print("")
# f"Keyset: {k or 'undefined'} Balance: {v['balance']} sat (available: {v['available']} sat)" for k, v in keyset_balances.items():
# ) print(
# print("") f"Keyset: {k or 'undefined'} Balance: {v['balance']} sat (available: {v['available']} sat)"
)
print("")
mint_balances = await wallet.balance_per_minturl() mint_balances = await wallet.balance_per_minturl()
if len(mint_balances) > 1: if len(mint_balances) > 1:
# show balances per mint
print(f"You have balances in {len(mint_balances)} mints:") print(f"You have balances in {len(mint_balances)} mints:")
print("") print("")
for k, v in mint_balances.items(): 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)" f"Mint: {k or 'undefined'} Balance: {v['balance']} sat (available: {v['available']} sat)"
) )
print("") print("")
if verbose: if verbose:
print( print(
f"Balance: {wallet.balance} sat (available: {wallet.available_balance} sat in {len([p for p in wallet.proofs if not p.reserved])} tokens)" 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.") @cli.command("send", help="Send tokens.")
@click.argument("amount", type=int) @click.argument("amount", type=int)
@click.option("--lock", "-l", default=None, help="Lock tokens (P2SH).", type=str) @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 @click.pass_context
@coro @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: if lock and len(lock) < 22:
print("Error: lock has to be at least 22 characters long.") print("Error: lock has to be at least 22 characters long.")
return return
@@ -236,14 +248,19 @@ async def send(ctx, amount: int, lock: str):
) )
print(token) print(token)
# print("") if legacy:
# print("Legacy:") print("")
# token = await wallet.serialize_proofs( print(
# send_proofs, "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:"
# hide_secrets=True if lock and not p2sh else False, )
# include_mints=False, print("")
# ) token = await wallet.serialize_proofs(
# print(token) send_proofs,
hide_secrets=True if lock and not p2sh else False,
legacy=True,
)
print(token)
wallet.status() wallet.status()

View File

@@ -482,7 +482,7 @@ class Wallet(LedgerAPI):
return status["paid"] return status["paid"]
async def serialize_proofs( 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. Produces sharable token with proofs and mint information.
@@ -493,6 +493,11 @@ class Wallet(LedgerAPI):
else: else:
proofs_serialized = [p.to_dict() for p in proofs] 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) token = dict(tokens=proofs_serialized)
# add mint information to the token, if requested # add mint information to the token, if requested