mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-21 02:54:20 +01:00
refactor
This commit is contained in:
@@ -19,7 +19,7 @@ from typing import Dict, List
|
||||
import click
|
||||
from loguru import logger
|
||||
|
||||
from cashu.core.base import Proof, TokenJson, TokenMintJson
|
||||
from cashu.core.base import Proof
|
||||
from cashu.core.helpers import sum_proofs
|
||||
from cashu.core.migrations import migrate_databases
|
||||
from cashu.core.settings import (
|
||||
@@ -53,6 +53,8 @@ from .cli_helpers import (
|
||||
print_mint_balances,
|
||||
redeem_multimint,
|
||||
verify_mints,
|
||||
token_from_lnbits_link,
|
||||
proofs_to_token,
|
||||
)
|
||||
|
||||
|
||||
@@ -300,51 +302,16 @@ async def receive(ctx, token: str, lock: str):
|
||||
# backwards compatibility: tokens without mint information
|
||||
# supports tokens of the form W3siaWQiOiJH
|
||||
|
||||
# LNbits token link parsing
|
||||
# can extract minut URL from LNbits token links like:
|
||||
# https://lnbits.server/cashu/wallet?mint_id=aMintId&recv_token=W3siaWQiOiJHY2...
|
||||
|
||||
def parse_lnbits_link(link):
|
||||
url, token = "", ""
|
||||
if len(link.split("&recv_token=")) == 2:
|
||||
# extract URL params
|
||||
params = urllib.parse.parse_qs(link.split("?")[1])
|
||||
# extract URL
|
||||
if "mint_id" in params:
|
||||
url = (
|
||||
link.split("?")[0].split("/wallet")[0]
|
||||
+ "/api/v1/"
|
||||
+ params["mint_id"][0]
|
||||
)
|
||||
# extract token
|
||||
token = params["recv_token"][0]
|
||||
return token, url
|
||||
else:
|
||||
return link, ""
|
||||
|
||||
token, url = parse_lnbits_link(token)
|
||||
# if it's an lnbits https:// link with a token as an argument, speacial treatment
|
||||
token, url = token_from_lnbits_link(token)
|
||||
|
||||
# assume W3siaWQiOiJH.. token
|
||||
# trows an error if the desirialization with the old format doesn't work
|
||||
# next line trows an error if the desirialization with the old format doesn't
|
||||
# work and we can assume it's the new format
|
||||
proofs = [Proof(**p) for p in json.loads(base64.urlsafe_b64decode(token))]
|
||||
token = await wallet.serialize_proofs(
|
||||
proofs,
|
||||
include_mints=False,
|
||||
)
|
||||
|
||||
# if it was not an lnbits link
|
||||
url = url or (
|
||||
input(f"Enter mint URL (press enter for default {MINT_URL}): ") or MINT_URL
|
||||
)
|
||||
|
||||
# and add url and keyset id to token
|
||||
token_object: TokenJson = await wallet._make_token(proofs, include_mints=False)
|
||||
token_object.mints = {}
|
||||
keysets = list(set([p.id for p in proofs]))
|
||||
assert keysets is not None, "no keysets"
|
||||
token_object.mints[url] = TokenMintJson(url=url, ks=keysets) # type: ignore
|
||||
token = await wallet._serialize_token_base64(token_object)
|
||||
|
||||
# we take the proofs parsed from the old format token and produce a new format token with it
|
||||
token = await proofs_to_token(wallet, proofs, url)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user