aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-08-18 13:09:41 -0700
committerAdam Soltys <[email protected]>2024-08-18 22:15:05 -0700
commitd66cba2d111bec3a2bd57a56233577f3fd417b18 (patch)
tree5cf0250363d11fb33414d8cae4f583e186247123 /src/lib
parent09d4867fe0ddc8cbc7bfa670911fe5c33a0a4f36 (diff)
downloadcoinos-ui-d66cba2d111bec3a2bd57a56233577f3fd417b18.tar.gz
coinos-ui-d66cba2d111bec3a2bd57a56233577f3fd417b18.zip
ecash
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/parse.js95
-rw-r--r--src/lib/socket.js10
-rw-r--r--src/lib/store.js2
-rw-r--r--src/lib/utils.js1
4 files changed, 104 insertions, 4 deletions
diff --git a/src/lib/parse.js b/src/lib/parse.js
new file mode 100644
index 00000000..fa4cb002
--- /dev/null
+++ b/src/lib/parse.js
@@ -0,0 +1,95 @@
+import { auth, get, isLiquid, post } from "$lib/utils";
+import bip21 from "bip21";
+import { validate } from "bitcoin-address-validation";
+import { PUBLIC_DOMAIN } from "$env/static/public";
+import { redirect } from "@sveltejs/kit";
+
+export default async (t, host, cookies) => {
+ if (!t) return;
+
+ if (t.startsWith("http")) redirect(307, t);
+ if (t.startsWith(host)) redirect(307, `http://${t}`);
+
+ let amount, id, user;
+
+ t = t.trim();
+ t.toLowerCase().startsWith("bitcoin:") &&
+ ({
+ address: t,
+ options: { amount },
+ } = bip21.decode(t));
+
+ if (t.startsWith("lightning:")) t = t.replace("lightning:", "");
+ if (t.endsWith(`@${PUBLIC_DOMAIN}`)) t = t.split("@")[0];
+ if (t.includes("@") && t.includes(".")) {
+ try {
+ t = await get(`/encode?address=${t}`);
+ } catch (e) {}
+ }
+
+ if (t.includes("/fund")) redirect(307, t.substring(t.indexOf("/fund")));
+
+ // lightning
+ if (t.toLowerCase().startsWith("lightning:"))
+ t = t.toLowerCase().replace("lightning:", "");
+ if (t.toLowerCase().startsWith("lnurl")) redirect(307, `/ln/${t}`);
+ if (t.includes(":")) t = t.split(":")[1];
+
+ if (t.toLowerCase().startsWith("ln")) {
+ try {
+ ({ id } = await get(`/invoice/${t}`));
+ } catch (e) {
+ redirect(307, `/send/lightning/${t}`);
+ }
+ }
+
+ // bitcoin
+ if (validate(t) || isLiquid(t)) {
+ try {
+ ({ id, user } = await get(`/invoice/${t}`));
+ } catch (e) {
+ let r = `/send/bitcoin/${t}`;
+ if (amount) r += "/" + amount;
+ redirect(307, r);
+ }
+
+ if (user) redirect(307, `/pay/${user.username}`);
+ else if (id) redirect(307, `/send/${id}`);
+ }
+
+ let claimed;
+ if (t.toLowerCase().startsWith("cashu")) {
+ try {
+ claimed = await post(`/claim`, { token: t }, auth(cookies));
+ claimed = { ok: true };
+ } catch (e) {
+ return e.message;
+ }
+ if (claimed.ok) {
+ redirect(307, `/${cookies.get("username")}/payments`);
+ }
+ }
+
+ // user
+ try {
+ user = await get(`/users/${t.split("/")[0]}`);
+ if (user.anon) user = null;
+ } catch (e) {}
+
+ if (user) redirect(307, `/pay/${t}`);
+
+ let fund;
+ try {
+ fund = await get(`/fund/${t}`);
+ } catch (e) {}
+
+ if (fund) redirect(307, `/send/fund/${t}`);
+
+ // invoice
+ let invoice;
+ try {
+ invoice = await get(`/invoice/${t}`);
+ } catch (e) {}
+
+ if (invoice) redirect(307, `/send/invoice/${invoice.id}`);
+};
diff --git a/src/lib/socket.js b/src/lib/socket.js
index f6a6893b..1b0c2649 100644
--- a/src/lib/socket.js
+++ b/src/lib/socket.js
@@ -32,9 +32,13 @@ export const messages = (data) => ({
async payment() {
let { amount, confirmed } = data;
- invalidate("app:user");
- invalidate("app:invoice");
- invalidate("app:payments");
+
+ // without setTimeout, invalidate seems to break redirections from actions
+ setTimeout(() => {
+ invalidate("app:user");
+ invalidate("app:invoice");
+ invalidate("app:payments");
+ }, 500);
if (amount > 0) {
success(`${confirmed ? "Received" : "Detected"} ${sat(amount)}!`);
diff --git a/src/lib/store.js b/src/lib/store.js
index 747afc4e..71d4aa40 100644
--- a/src/lib/store.js
+++ b/src/lib/store.js
@@ -56,4 +56,4 @@ export let request = writable();
export let requestRedirect = writable();
export let token = persistSession("token");
export let ndef = writable();
-export let showQr = persistLocal("showQr");
+export let showQr = persistLocal("showQr", true);
diff --git a/src/lib/utils.js b/src/lib/utils.js
index a897bed8..ac6e9216 100644
--- a/src/lib/utils.js
+++ b/src/lib/utils.js
@@ -288,6 +288,7 @@ export let types = {
lightning: "lightning",
internal: "internal",
fund: "fund",
+ ecash: "ecash"
};
export let ease = (t) =>