From 88d9515ba6fed68ecdc4343c89e79d20f145c570 Mon Sep 17 00:00:00 2001 From: Danny Morabito Date: Sat, 21 Dec 2024 22:21:28 +0100 Subject: [PATCH] allow purchasing time with lightning directly not just cashu (fix #9) --- package.json | 1 + src/lib/style/form.css | 2 +- src/routes/settings/BuyTime.svelte | 65 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 200d5e8..e02c12a 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "dependencies": { "@arx/utils": "git+ssh://git@git.arx-ccn.com:222/Arx/ts-utils#03163e9f4a07b011a28a8f97c90852ecfc806ddd", + "@cashu/cashu-ts": "^2.1.0", "@gandlaf21/bc-ur": "^1.1.12", "@nostr-dev-kit/ndk": "^2.10.7", "@nostr-dev-kit/ndk-cache-dexie": "^2.5.8", diff --git a/src/lib/style/form.css b/src/lib/style/form.css index ee8ca1c..baf5b92 100644 --- a/src/lib/style/form.css +++ b/src/lib/style/form.css @@ -37,7 +37,7 @@ button, .button { } -input[type="text"], input[type="password"] { +input[type="text"], input[type="number"], input[type="password"] { background: var(--editor-bg); border: 1px solid var(--input-border); color: var(--text-primary); diff --git a/src/routes/settings/BuyTime.svelte b/src/routes/settings/BuyTime.svelte index 4035784..47b790f 100644 --- a/src/routes/settings/BuyTime.svelte +++ b/src/routes/settings/BuyTime.svelte @@ -3,6 +3,10 @@ import { TokenInfoWithMailSubscriptionDuration } from '@arx/utils'; import TimeCountdown from '../../components/TimeCountdown.svelte'; import { ndk } from '$lib/stores.svelte'; + import { CashuMint, CashuWallet, getEncodedTokenV4, MintQuoteState } from '@cashu/cashu-ts'; + import type { MintQuoteResponse } from '@cashu/cashu-ts'; + import Dialog from '../../components/Dialog.svelte'; + import * as qr from 'qrcode'; let { onReloadNeeded } = $props<{ onReloadNeeded: () => void; @@ -35,6 +39,44 @@ } } + let buyingWithLightning = $state(false); + let lightningAmount = $state(21); + let invoice = $state(''); + let invoiceQR = $state(''); + let mintQuote = $state(null); + + const mint = new CashuMint('https://mint.minibits.cash/Bitcoin'); + const wallet = new CashuWallet(mint); + + async function buyWithLightning() { + if (tokenInfo) return; + if(!mintQuote) return; + const mintQuoteChecked = await wallet.checkMintQuote(mintQuote.quote); + if (mintQuoteChecked.state === MintQuoteState.PAID) { + const proofs = await wallet.mintProofs(lightningAmount, mintQuote.quote); + cashuTokenForBuy = getEncodedTokenV4({ mint: mint.mintUrl, proofs: proofs }); + buyingWithLightning = false; + invoice = ''; + invoiceQR = ''; + mintQuote = null; + try { + tokenInfo = new TokenInfoWithMailSubscriptionDuration(cashuTokenForBuy); + tokenInfoError = ''; + } catch (e) { + tokenInfoError = (e as Error).message; + } + buyTime(); + } else + alert('Invoice not paid yet') + } + + async function generateInvoice() { + await wallet.loadMint(); + mintQuote = await wallet.createMintQuote(lightningAmount); + invoice = mintQuote.request; + invoiceQR = await qr.toDataURL(invoice); + } + $effect(() => { try { tokenInfo = new TokenInfoWithMailSubscriptionDuration(cashuTokenForBuy); @@ -45,6 +87,28 @@ }); + +
+

Buy time with lightning

+ +
+ + {#if !invoice} +

Enter the amount of time you want to buy (min 21 sats, 210 sats = 1 day):

+ + + + + {:else} +
+ Lightning Invoice + +

After payment, click the button below to add the time to your account.

+ +
+ {/if} +
+ {#if cashuTokenForBuy !== ''} {#if tokenInfoError}

{tokenInfoError}

@@ -56,6 +120,7 @@ {/if} +

Buy for: