From df71664f20312e81134ee90ad561f55d3020ee0a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 30 Aug 2024 20:04:29 -0700 Subject: typescript --- src/routes/(app)/ln/[lnurl]/+page.server.js | 75 ----------------------------- src/routes/(app)/ln/[lnurl]/+page.server.ts | 75 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 src/routes/(app)/ln/[lnurl]/+page.server.js create mode 100644 src/routes/(app)/ln/[lnurl]/+page.server.ts (limited to 'src/routes/(app)/ln') diff --git a/src/routes/(app)/ln/[lnurl]/+page.server.js b/src/routes/(app)/ln/[lnurl]/+page.server.js deleted file mode 100644 index 7f0536c1..00000000 --- a/src/routes/(app)/ln/[lnurl]/+page.server.js +++ /dev/null @@ -1,75 +0,0 @@ -import { fd, auth, get, post } from "$lib/utils"; -import { error, fail, redirect } from "@sveltejs/kit"; - -export async function load({ params, parent }) { - let { user } = await parent(); - if (!user) redirect(307, "/register"); - - let data; - - try { - let { lnurl } = params; - data = await get(`/decode?text=${lnurl}`); - } catch (e) { - error(500, e.message); - } - - if (!["payRequest", "withdrawRequest"].includes(data.tag)) - error(500, "We only support LNURLp and LNURLw at this time"); - - return data; -} - -export const actions = { - pay: async ({ cookies, fetch, request }) => { - let error; - - let { callback, amount, minSendable, maxSendable } = await fd(request); - minSendable = Math.round(minSendable / 1000); - maxSendable = Math.round(maxSendable / 1000); - - if (amount < minSendable) - error = `Amount must be at least ${minSendable} sats`; - if (amount > maxSendable) - error = `Amount must be at most ${maxSendable} sats`; - if (error) return fail(400, { error }); - - let { pr } = await fetch(`${callback}?amount=${amount * 1000}`).then((r) => - r.json() - ); - redirect(307, `/send/lightning/${pr}`); - }, - - withdraw: async ({ cookies, fetch, request }) => { - let error; - - let { - callback, - amount, - username, - currency, - minWithdrawable, - maxWithdrawable, - k1, - } = await fd(request); - - minWithdrawable = Math.round(minWithdrawable / 1000); - maxWithdrawable = Math.round(maxWithdrawable / 1000); - - if (amount < minWithdrawable) - error = `Amount must be at least ${minWithdrawable} sats`; - if (amount > maxWithdrawable) - error = `Amount must be at most ${maxWithdrawable} sats`; - if (error) return fail(400, { error }); - - let invoice = { amount, type: "lightning" }; - let user = { username, currency }; - - let { text: pr } = await post("/invoice", { invoice, user }, auth(cookies)); - - let c = callback.includes("?") ? "&" : "?"; - await fetch(`${callback}${c}k1=${k1}&pr=${pr}`).then((r) => r.json()); - - redirect(307, `/${username}/payments`); - }, -}; diff --git a/src/routes/(app)/ln/[lnurl]/+page.server.ts b/src/routes/(app)/ln/[lnurl]/+page.server.ts new file mode 100644 index 00000000..7f0536c1 --- /dev/null +++ b/src/routes/(app)/ln/[lnurl]/+page.server.ts @@ -0,0 +1,75 @@ +import { fd, auth, get, post } from "$lib/utils"; +import { error, fail, redirect } from "@sveltejs/kit"; + +export async function load({ params, parent }) { + let { user } = await parent(); + if (!user) redirect(307, "/register"); + + let data; + + try { + let { lnurl } = params; + data = await get(`/decode?text=${lnurl}`); + } catch (e) { + error(500, e.message); + } + + if (!["payRequest", "withdrawRequest"].includes(data.tag)) + error(500, "We only support LNURLp and LNURLw at this time"); + + return data; +} + +export const actions = { + pay: async ({ cookies, fetch, request }) => { + let error; + + let { callback, amount, minSendable, maxSendable } = await fd(request); + minSendable = Math.round(minSendable / 1000); + maxSendable = Math.round(maxSendable / 1000); + + if (amount < minSendable) + error = `Amount must be at least ${minSendable} sats`; + if (amount > maxSendable) + error = `Amount must be at most ${maxSendable} sats`; + if (error) return fail(400, { error }); + + let { pr } = await fetch(`${callback}?amount=${amount * 1000}`).then((r) => + r.json() + ); + redirect(307, `/send/lightning/${pr}`); + }, + + withdraw: async ({ cookies, fetch, request }) => { + let error; + + let { + callback, + amount, + username, + currency, + minWithdrawable, + maxWithdrawable, + k1, + } = await fd(request); + + minWithdrawable = Math.round(minWithdrawable / 1000); + maxWithdrawable = Math.round(maxWithdrawable / 1000); + + if (amount < minWithdrawable) + error = `Amount must be at least ${minWithdrawable} sats`; + if (amount > maxWithdrawable) + error = `Amount must be at most ${maxWithdrawable} sats`; + if (error) return fail(400, { error }); + + let invoice = { amount, type: "lightning" }; + let user = { username, currency }; + + let { text: pr } = await post("/invoice", { invoice, user }, auth(cookies)); + + let c = callback.includes("?") ? "&" : "?"; + await fetch(`${callback}${c}k1=${k1}&pr=${pr}`).then((r) => r.json()); + + redirect(307, `/${username}/payments`); + }, +}; -- cgit v1.2.3