From cd44a27f1ffcabb29c318f9be71ff6576bce29ce Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 18:16:41 +0100 Subject: [PATCH 1/7] ask for mint url --- cashu/wallet/cli.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index 2330200..c6a0de5 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -303,19 +303,26 @@ async def receive(ctx, token: str, lock: str): # LNbits token link parsing # can extract minut URL from LNbits token links like: # https://lnbits.server/cashu/wallet?mint_id=aMintId&recv_token=W3siaWQiOiJHY2... - url = None - if len(token.split("&recv_token=")) == 2: - # extract URL params - params = urllib.parse.parse_qs(token.split("?")[1]) - # extract URL - if "mint_id" in params: - url = ( - token.split("?")[0].split("/wallet")[0] - + "/api/v1/" - + params["mint_id"][0] - ) - # extract token - token = params["recv_token"][0] + + def parse_lnbits_link(token): + url = None + if len(token.split("&recv_token=")) == 2: + # extract URL params + params = urllib.parse.parse_qs(token.split("?")[1]) + # extract URL + if "mint_id" in params: + url = ( + token.split("?")[0].split("/wallet")[0] + + "/api/v1/" + + params["mint_id"][0] + ) + # extract token + token = params["recv_token"][0] + return token, url + else: + return token, None + + token, url = parse_lnbits_link(token) # assume W3siaWQiOiJH.. token # trows an error if the desirialization with the old format doesn't work @@ -325,7 +332,12 @@ async def receive(ctx, token: str, lock: str): include_mints=False, ) - # if it was an LNbits link + # if it was not an lnbits link + if url is None: + url = input(f"Enter mint URL (default: {MINT_URL}: ") + if url == "": + url = MINT_URL + # and add url and keyset id to token from link extraction above if url: token_object: TokenJson = await wallet._make_token( From e230b873fe5317ec4a4f2d4789c78496538336d7 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 18:18:31 +0100 Subject: [PATCH 2/7] simplify code --- cashu/wallet/cli.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index c6a0de5..ca1d064 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -334,9 +334,7 @@ async def receive(ctx, token: str, lock: str): # if it was not an lnbits link if url is None: - url = input(f"Enter mint URL (default: {MINT_URL}: ") - if url == "": - url = MINT_URL + url = input(f"Enter mint URL (default: {MINT_URL}: ") or MINT_URL # and add url and keyset id to token from link extraction above if url: From 639fab547efd4854c524e3bd8cedc445395d57e5 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:30:53 +0100 Subject: [PATCH 3/7] update text --- cashu/wallet/cli.py | 5 ++++- cashu/wallet/cli_helpers.py | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index ca1d064..bb99eb8 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -334,7 +334,10 @@ async def receive(ctx, token: str, lock: str): # if it was not an lnbits link if url is None: - url = input(f"Enter mint URL (default: {MINT_URL}: ") or MINT_URL + url = ( + input(f"Enter mint URL (or press enter for default: {MINT_URL}: ") + or MINT_URL + ) # and add url and keyset id to token from link extraction above if url: diff --git a/cashu/wallet/cli_helpers.py b/cashu/wallet/cli_helpers.py index 3217631..4a6a0fb 100644 --- a/cashu/wallet/cli_helpers.py +++ b/cashu/wallet/cli_helpers.py @@ -87,7 +87,7 @@ async def print_mint_balances(ctx, wallet, show_mints=False): print("") for i, (k, v) in enumerate(mint_balances.items()): print( - f"Mint {i+1}: {k} - Balance: {v['available']} sat (pending: {v['balance']-v['available']} sat)" + f"Mint {i+1}: Balance: {v['available']} sat (pending: {v['balance']-v['available']} sat) URL: {k}" ) print("") @@ -103,10 +103,10 @@ async def get_mint_wallet(ctx): await print_mint_balances(ctx, wallet, show_mints=True) - mint_nr = input( - f"Which mint do you want to use? [1-{len(mint_balances)}, default: 1] " + mint_nr = ( + input(f"Select mint [1-{len(mint_balances)}, press enter for default 1]: ") + or "1" ) - mint_nr = "1" if mint_nr == "" else mint_nr if not mint_nr.isdigit(): raise Exception("invalid input.") mint_nr = int(mint_nr) From 2adccfa86100cc3e5073bda323d7166ad8e2672c Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:32:16 +0100 Subject: [PATCH 4/7] fix text --- cashu/wallet/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index bb99eb8..a093a6f 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -335,7 +335,7 @@ async def receive(ctx, token: str, lock: str): # if it was not an lnbits link if url is None: url = ( - input(f"Enter mint URL (or press enter for default: {MINT_URL}: ") + input(f"Enter mint URL (press enter for default {MINT_URL}): ") or MINT_URL ) From 645d5c9eae117406b2b3b1ef3761622638e160c1 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:40:19 +0100 Subject: [PATCH 5/7] refactor --- cashu/wallet/cli.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index a093a6f..ce1a18e 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -304,15 +304,15 @@ async def receive(ctx, token: str, lock: str): # can extract minut URL from LNbits token links like: # https://lnbits.server/cashu/wallet?mint_id=aMintId&recv_token=W3siaWQiOiJHY2... - def parse_lnbits_link(token): - url = None - if len(token.split("&recv_token=")) == 2: + def parse_lnbits_link(link): + url, token = "", "" + if len(link.split("&recv_token=")) == 2: # extract URL params - params = urllib.parse.parse_qs(token.split("?")[1]) + params = urllib.parse.parse_qs(link.split("?")[1]) # extract URL if "mint_id" in params: url = ( - token.split("?")[0].split("/wallet")[0] + link.split("?")[0].split("/wallet")[0] + "/api/v1/" + params["mint_id"][0] ) @@ -320,7 +320,7 @@ async def receive(ctx, token: str, lock: str): token = params["recv_token"][0] return token, url else: - return token, None + return link, "" token, url = parse_lnbits_link(token) @@ -333,22 +333,19 @@ async def receive(ctx, token: str, lock: str): ) # if it was not an lnbits link - if url is None: + if not url: url = ( input(f"Enter mint URL (press enter for default {MINT_URL}): ") or MINT_URL ) - # and add url and keyset id to token from link extraction above - if url: - 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) + # 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) except: pass From f682e95b8fc37fb083368a3fa2f4cabb2f08624f Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:41:31 +0100 Subject: [PATCH 6/7] refactor --- cashu/wallet/cli.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index ce1a18e..aaddcd7 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -333,11 +333,9 @@ async def receive(ctx, token: str, lock: str): ) # if it was not an lnbits link - if not url: - url = ( - input(f"Enter mint URL (press enter for default {MINT_URL}): ") - or MINT_URL - ) + 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) From 1969abae5f60584efa15eb4368ac1ce6e4fec16b Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 27 Dec 2022 19:47:46 +0100 Subject: [PATCH 7/7] reorder help --- cashu/wallet/cli.py | 50 ++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index aaddcd7..927f506 100644 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -521,6 +521,31 @@ async def invoices(ctx): print("No invoices found.") +@cli.command("wallets", help="List of all available wallets.") +@click.pass_context +@coro +async def wallets(ctx): + # list all directories + wallets = [d for d in listdir(CASHU_DIR) if isdir(join(CASHU_DIR, d))] + try: + wallets.remove("mint") + except ValueError: + pass + for w in wallets: + wallet = Wallet(ctx.obj["HOST"], os.path.join(CASHU_DIR, w)) + try: + await init_wallet(wallet) + if wallet.proofs and len(wallet.proofs): + active_wallet = False + if w == ctx.obj["WALLET_NAME"]: + active_wallet = True + print( + f"Wallet: {w}\tBalance: {sum_proofs(wallet.proofs)} sat (available: {sum_proofs([p for p in wallet.proofs if not p.reserved])} sat){' *' if active_wallet else ''}" + ) + except: + pass + + @cli.command("nsend", help="Send tokens via nostr.") @click.argument("amount", type=int) @click.argument( @@ -614,31 +639,6 @@ async def nreceive(ctx, verbose: bool): t.start() -@cli.command("wallets", help="List of all available wallets.") -@click.pass_context -@coro -async def wallets(ctx): - # list all directories - wallets = [d for d in listdir(CASHU_DIR) if isdir(join(CASHU_DIR, d))] - try: - wallets.remove("mint") - except ValueError: - pass - for w in wallets: - wallet = Wallet(ctx.obj["HOST"], os.path.join(CASHU_DIR, w)) - try: - await init_wallet(wallet) - if wallet.proofs and len(wallet.proofs): - active_wallet = False - if w == ctx.obj["WALLET_NAME"]: - active_wallet = True - print( - f"Wallet: {w}\tBalance: {sum_proofs(wallet.proofs)} sat (available: {sum_proofs([p for p in wallet.proofs if not p.reserved])} sat){' *' if active_wallet else ''}" - ) - except: - pass - - @cli.command("info", help="Information about Cashu wallet.") @click.pass_context @coro