From 0bf630c39fe290ea2718eab7a0759d978c736ef9 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 26 Nov 2024 13:08:08 -0800 Subject: bip21 and socket fixes --- src/routes/(app)/+layout.server.ts | 11 +- src/routes/(app)/+layout.svelte | 12 +- src/routes/(app)/[username]/+layout.server.ts | 2 +- src/routes/(app)/[username]/receive/+page.svelte | 22 +-- src/routes/(app)/invoice/[id]/+layout@.svelte | 110 ------------ src/routes/(app)/invoice/[id]/+page.svelte | 24 ++- .../(app)/payments/[...page]/+page.server.ts | 2 +- src/routes/(app)/payments/[...page]/+page.svelte | 186 ++++++++++----------- .../(app)/send/bitcoin/[address]/+layout.server.ts | 18 +- .../[amount]/[...feeRate]/+page.server.ts | 5 +- .../[address]/[amount]/[...feeRate]/+page.svelte | 35 +--- src/routes/(app)/send/invoice/[id]/+page.svelte | 2 +- 12 files changed, 153 insertions(+), 276 deletions(-) delete mode 100644 src/routes/(app)/invoice/[id]/+layout@.svelte (limited to 'src/routes') diff --git a/src/routes/(app)/+layout.server.ts b/src/routes/(app)/+layout.server.ts index 3c5b3b3e..d490a684 100644 --- a/src/routes/(app)/+layout.server.ts +++ b/src/routes/(app)/+layout.server.ts @@ -3,7 +3,7 @@ import { error, redirect } from "@sveltejs/kit"; export async function load({ cookies, request, url, params }) { const { pathname } = url; - const { username } = params; + const { id, username } = params; const token = cookies.get("token"); let rate; let rates = { USD: 1 }; @@ -20,7 +20,14 @@ export async function load({ cookies, request, url, params }) { } let subject; - if (username) { + + if (url.pathname.startsWith("/invoice") && id) { + try { + ({ user: subject } = await get(`/invoice/${id}`)); + } catch (e) { + console.log("unable to fetch invoice", id, e); + } + } else if (username) { try { subject = await get(`/users/${username}`); } catch (e) { diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index aaaf543f..49e4bdb9 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -1,7 +1,7 @@ diff --git a/src/routes/(app)/[username]/+layout.server.ts b/src/routes/(app)/[username]/+layout.server.ts index 1dd163e9..bff8848c 100644 --- a/src/routes/(app)/[username]/+layout.server.ts +++ b/src/routes/(app)/[username]/+layout.server.ts @@ -15,7 +15,7 @@ export const load = async ({ const expires = new Date(); expires.setSeconds(expires.getSeconds() + 380 * 24 * 60 * 60); - cookies.set("username", username, { path: "/", expires }); + cookies.set("username", username, { path: "/", expires, httpOnly: false }); return { subject, user }; } catch (e) { diff --git a/src/routes/(app)/[username]/receive/+page.svelte b/src/routes/(app)/[username]/receive/+page.svelte index ddb17e71..89f3a1f3 100644 --- a/src/routes/(app)/[username]/receive/+page.svelte +++ b/src/routes/(app)/[username]/receive/+page.svelte @@ -38,16 +38,18 @@ tip; let setType = async (type) => { - invoice.type = type; - ({ id } = await post(`/invoice`, { - invoice, - user: { username, currency }, - })); - - goto(`/invoice/${id}?options=true`, { - invalidateAll: true, - noScroll: true, - }); + $showQr = true; + if (type === types.lightning && !amount) + goto(`/${username}/receive`, { invalidateAll: true, noScroll: true }); + else { + invoice.type = type; + ({ id } = await post(`/invoice`, { + invoice, + user: { username, currency }, + })); + + goto(`/invoice/${id}?options=true`, { invalidateAll: true, noScroll: true }); + } }; let setAmount = async (e) => { diff --git a/src/routes/(app)/invoice/[id]/+layout@.svelte b/src/routes/(app)/invoice/[id]/+layout@.svelte deleted file mode 100644 index 13b121d8..00000000 --- a/src/routes/(app)/invoice/[id]/+layout@.svelte +++ /dev/null @@ -1,110 +0,0 @@ - - -{#if browser && user && $passwordPrompt} - -{/if} - - - {#if subject} - coinos - {subject.username} - - {:else} - coinos - {/if} - - {#if subject?.profile} - - {:else} - - {/if} - - - - -
- - {#if !$loading} - {@render children?.()} - {/if} -
diff --git a/src/routes/(app)/invoice/[id]/+page.svelte b/src/routes/(app)/invoice/[id]/+page.svelte index 8c6e2b75..3ad20f68 100644 --- a/src/routes/(app)/invoice/[id]/+page.svelte +++ b/src/routes/(app)/invoice/[id]/+page.svelte @@ -56,13 +56,21 @@ }); let setType = async (type) => { - invoice.type = type; - ({ id } = await post(`/invoice`, { - invoice, - user: { username, currency }, - })); - - goto(`./${id}?options=true`, { invalidateAll: true, noScroll: true }); + $showQr = true; + if (type === types.lightning && !amount) + goto(`/${username}/receive`, { invalidateAll: true, noScroll: true }); + else { + invoice.type = type; + ({ id } = await post(`/invoice`, { + invoice, + user: { username, currency }, + })); + + goto(`/invoice/${id}?options=true`, { + invalidateAll: true, + noScroll: true, + }); + } }; let setAmount = async (e) => { @@ -74,7 +82,7 @@ if (typeof $amountPrompt === "undefined") $amountPrompt = true; invoice.amount = newAmount; invoice.tip = 0; - + ({ id } = await post(`/invoice`, { invoice, user: { username, currency }, diff --git a/src/routes/(app)/payments/[...page]/+page.server.ts b/src/routes/(app)/payments/[...page]/+page.server.ts index 268fd32b..05ed2d64 100644 --- a/src/routes/(app)/payments/[...page]/+page.server.ts +++ b/src/routes/(app)/payments/[...page]/+page.server.ts @@ -1,4 +1,4 @@ -import { get, auth } from "$lib/utils"; +import { auth, get } from "$lib/utils"; const limit = 25; diff --git a/src/routes/(app)/payments/[...page]/+page.svelte b/src/routes/(app)/payments/[...page]/+page.svelte index cda7c350..146a286d 100644 --- a/src/routes/(app)/payments/[...page]/+page.svelte +++ b/src/routes/(app)/payments/[...page]/+page.svelte @@ -11,8 +11,9 @@ import { goto, invalidate } from "$app/navigation"; import locales from "$lib/locales"; let { data } = $props(); + let { start, end, user, rates, payments, incoming, outgoing } = + $derived(data); - let { start, end, user, rates, payments } = $state(data); let locale = locales[user.language]; let change = ({ target: { value } }) => goto(value); @@ -55,19 +56,13 @@ ); let p = $state(), - incoming = $state(), - outgoing = $state(), pages = $state([]); - run(() => { - data && - ({ page: p, pages, start, end, incoming, outgoing, payments } = data); - }); - run(() => { - $page && ($newPayment = false); - }); - run(() => { - $newPayment && invalidate(`/users/${user.username}`); + $effect(() => { + if ($newPayment) { + $newPayment = false; + invalidate(`/users/${user.username}`); + } }); let path = $derived( @@ -204,104 +199,101 @@
{#each payments as p, i} -
goto(`/payment/${p.id}`)} - > -
-
-
- -
{s(Math.abs(p.amount))}
-
+
goto(`/payment/${p.id}`)} + > +
+
+
+ +
{s(Math.abs(p.amount))}
+
-
- {f(Math.abs(p.amount) * (p.rate / sats), p.currency)} +
+ {f(Math.abs(p.amount) * (p.rate / sats), p.currency)} - {#if p.tip} - -  +{Math.round((p.tip / Math.abs(p.amount)) * 100)}% - - {/if} -
+ {#if p.tip} + +  +{Math.round((p.tip / Math.abs(p.amount)) * 100)}% + + {/if}
+
-
- {#if p.type === types.pot} - -
-
- Pot -
- -
Pot
-
-
- {:else if p.with} -
-
- -
-
- {p.with.username} +
+ {#if p.type === types.pot} + +
+
+ Pot
-
- {:else} -
- {#if p.type === types.lightning} - - {:else if p.type === types.ecash} - - {:else if p.type === types.reconcile} - - {:else if p.type === types.bitcoin} - - {:else if p.type === types.liquid} -
- Liquid -
- {/if} +
Pot
+
+
+ {:else if p.with} +
+
+ +
+
+ {p.with.username} +
+
+ {:else} +
+ {#if p.type === types.lightning} + + {:else if p.type === types.ecash} + + {:else if p.type === types.reconcile} + + {:else if p.type === types.bitcoin} + + {:else if p.type === types.liquid}
- {p.amount > 0 - ? p.confirmed - ? "Received" - : "Pending" - : "Sent"} + Liquid
-
- {/if} -
+ {/if} -
-
- {format(new Date(p.created), "h:mm aaa", { locale })} -
-
- {format(new Date(p.created), "MMM d, yy", { locale })} +
+ {p.amount > 0 + ? p.confirmed + ? "Received" + : "Pending" + : "Sent"} +
+ {/if} +
+ +
+
+ {format(new Date(p.created), "h:mm aaa", { locale })} +
+
+ {format(new Date(p.created), "MMM d, yy", { locale })}
+
{:else}

{$t("payments.empty")}

{/each} diff --git a/src/routes/(app)/send/bitcoin/[address]/+layout.server.ts b/src/routes/(app)/send/bitcoin/[address]/+layout.server.ts index 68191311..a321ec43 100644 --- a/src/routes/(app)/send/bitcoin/[address]/+layout.server.ts +++ b/src/routes/(app)/send/bitcoin/[address]/+layout.server.ts @@ -4,20 +4,22 @@ import { redirect } from "@sveltejs/kit"; export async function load({ cookies, params, parent }) { const { user } = await parent(); const aid = cookies.get("aid"); - const { address } = params; + const { address, amount } = params; const { balance } = await get(`/account/${aid}`, auth(cookies)); + let invoice; try { - const invoice = await get(`/invoice/${address}`); - - if (invoice) { - const recipient = await get(`/users/${invoice.uid}`); - if (recipient?.id !== user.id) - redirect(307, `/pay/${recipient.username}`); - } + invoice = await get(`/invoice/${address}`); } catch (e) { // invoice not found } + if (invoice) { + const recipient = await get(`/users/${invoice.uid}`); + let r = `/pay/${recipient.username}`; + if (amount) r += `/${amount}`; + if (recipient?.id !== user.id) redirect(307, r); + } + return { balance }; } diff --git a/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.ts b/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.ts index 1122e689..9156bf7b 100644 --- a/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.ts +++ b/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.ts @@ -39,9 +39,10 @@ export const actions = { try { const body = await fd(request); p = await post("/bitcoin/send", body, auth(cookies)); - } catch (e: any) { + } catch (e) { console.log("problem sending bitcoin", e); - return fail(400, { address, amount, feeRate, message: e.message }); + const { message } = e as Error; + return fail(400, { address, amount, feeRate, message }); } redirect(307, `/sent/${p.id}`); diff --git a/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.svelte b/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.svelte index f37d4479..298280df 100644 --- a/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.svelte +++ b/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.svelte @@ -5,19 +5,10 @@ import { t } from "$lib/translations"; import { enhance } from "$app/forms"; import Toggle from "$comp/Toggle.svelte"; - import Icon from "$comp/Icon.svelte"; import Spinner from "$comp/Spinner.svelte"; import WalletPass from "$comp/WalletPass.svelte"; import { page } from "$app/stores"; - import { - toFiat, - f, - focus, - s, - sat, - closest, - network, - } from "$lib/utils"; + import { toFiat, f, focus, s, sat, closest, network } from "$lib/utils"; import { pin } from "$lib/store"; import { goto, invalidate } from "$app/navigation"; import { rate } from "$lib/store"; @@ -30,6 +21,8 @@ import { entropyToMnemonic, mnemonicToSeed } from "@scure/bip39"; import { wordlist } from "@scure/bip39/wordlists/english"; + import Amount from "$comp/Amount.svelte"; + let { data, form } = $props(); let { encode, decode } = hexUtil; @@ -130,34 +123,20 @@ {:else}
{address}
-
-

- {f(toFiat(amount, $rate), currency)} -

-

⚡️{s(amount)}

-
+

{$t("payments.networkFee")}

- {#each Object.keys(feeNames) as feeName} {feeNames[feeName]} — {fees[feeName]} sats/vb {/each} -
-

- {f(toFiat(fee, $rate), currency)} -

-

⚡️{s(fee)}

-
+
diff --git a/src/routes/(app)/send/invoice/[id]/+page.svelte b/src/routes/(app)/send/invoice/[id]/+page.svelte index f5259170..d2b3727c 100644 --- a/src/routes/(app)/send/invoice/[id]/+page.svelte +++ b/src/routes/(app)/send/invoice/[id]/+page.svelte @@ -103,7 +103,7 @@ -

+

{$t("payments.to")}

-- cgit v1.2.3