diff options
| author | Adam Soltys <[email protected]> | 2025-02-26 11:59:58 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-02-26 11:59:58 -0800 |
| commit | 1d541fd97210389f006e65d46ab783afbe82cea8 (patch) | |
| tree | 66781ea2992fa99198ed1e450cdb9b68acfbc4d9 /routes | |
| parent | a14b987a392bc700975b048f37838da08e4ed255 (diff) | |
| download | coinos-server-1d541fd97210389f006e65d46ab783afbe82cea8.tar.gz coinos-server-1d541fd97210389f006e65d46ab783afbe82cea8.zip | |
payment types
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/ecash.ts | 8 | ||||
| -rw-r--r-- | routes/lnurl.ts | 15 | ||||
| -rw-r--r-- | routes/payments.ts | 13 | ||||
| -rw-r--r-- | routes/users.ts | 16 |
4 files changed, 37 insertions, 15 deletions
diff --git a/routes/ecash.ts b/routes/ecash.ts index e5039aa4..e4898358 100644 --- a/routes/ecash.ts +++ b/routes/ecash.ts @@ -1,12 +1,14 @@ import { db, g, s } from "$lib/db"; import { check, claim, get, mint } from "$lib/ecash"; import { err, l } from "$lib/logging"; -import { credit, debit, types } from "$lib/payments"; +import { credit, debit } from "$lib/payments"; import { emit } from "$lib/sockets"; import { bail, fail } from "$lib/utils"; import { getEncodedToken } from "@cashu/cashu-ts"; import { v4 } from "uuid"; -const { ecash: type } = types; + +import { PaymentType } from "$lib/types"; +const { ecash: type } = PaymentType; Error.stackTraceLimit = 100; // Set this to the desired limit @@ -97,7 +99,7 @@ export default { try { amount = Math.round(amount / 1000); const ref = preimage; - const { lightning: type } = types; + const { lightning: type } = PaymentType; if (user.username !== "mint") fail("unauthorized"); const { id: uid, currency } = user; const ourfee = await db.debit( diff --git a/routes/lnurl.ts b/routes/lnurl.ts index 05d369fc..294e6d7c 100644 --- a/routes/lnurl.ts +++ b/routes/lnurl.ts @@ -4,12 +4,13 @@ import ln from "$lib/ln"; import { err, warn } from "$lib/logging"; import { serverPubkey } from "$lib/nostr"; import { credit } from "$lib/payments"; -import { types } from "$lib/payments"; import { bail, fail, getInvoice, getUser } from "$lib/utils"; import { bech32 } from "bech32"; import got from "got"; import { v4 } from "uuid"; +import { PaymentType } from "$lib/types"; + const { URL } = process.env; const host = URL.split("/").at(-1); @@ -124,7 +125,7 @@ export default { invoice: { amount: Math.round(amount / 1000), memo: metadata, - type: types.lightning, + type: PaymentType.lightning, }, user, }); @@ -158,7 +159,13 @@ export default { let p; if (invoice) { const { hash } = invoice; - p = await credit({ hash, amount, memo: id, ref: id, type: types.fund }); + p = await credit({ + hash, + amount, + memo: id, + ref: id, + type: PaymentType.fund, + }); } else { await ln.xpay({ invstring: pr.replace(/\s/g, "").toLowerCase(), @@ -173,7 +180,7 @@ export default { confirmed: true, rate: rates.USD, currency: "USD", - type: types.fund, + type: PaymentType.fund, ref: id, created: Date.now(), }; diff --git a/routes/payments.ts b/routes/payments.ts index 9282723a..e5af18ee 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -13,7 +13,6 @@ import { sendInternal, sendLightning, sendOnchain, - types, } from "$lib/payments"; import { emit } from "$lib/sockets"; import { @@ -30,6 +29,8 @@ import rpc from "@coinos/rpc"; import got from "got"; import { v4 } from "uuid"; +import { PaymentType } from "$lib/types"; + import ln from "$lib/ln"; const { URL } = process.env; @@ -77,7 +78,7 @@ export default { sender: user, }); } else if (fund) { - p = await debit({ hash, amount, memo: fund, user, type: types.fund }); + p = await debit({ hash, amount, memo: fund, user, type: PaymentType.fund }); await db.incrBy(`fund:${fund}`, amount); await db.lPush(`fund:${fund}:payments`, p.id); l("funded fund", fund); @@ -115,7 +116,7 @@ export default { return p; } if (p.created < start || p.created > end) return; - if (p.type === types.internal) p.with = await getUser(p.ref, fields); + if (p.type === PaymentType.internal) p.with = await getUser(p.ref, fields); return p; }), ) @@ -164,7 +165,7 @@ export default { params: { hash }, } = req; const p = await getPayment(hash); - if (p?.type === types.internal) p.with = await getUser(p.ref, fields); + if (p?.type === PaymentType.internal) p.with = await getUser(p.ref, fields); res.send(p); } catch (e) { console.log(e); @@ -285,7 +286,7 @@ export default { amount, memo: id, ref: id, - type: types.fund, + type: PaymentType.fund, }); await db.lPush(`fund:${id}:payments`, payment.id); @@ -311,7 +312,7 @@ export default { for (const { address, amount, asset, category, vout } of details) { if (!address) continue; - if (type === types.liquid && asset !== config.liquid.btc) continue; + if (type === PaymentType.liquid && asset !== config.liquid.btc) continue; if (category === "send") { const p = await getPayment(txid); diff --git a/routes/users.ts b/routes/users.ts index 106cfd4a..515e64f0 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -6,7 +6,7 @@ import { db, g, s } from "$lib/db"; import { err, l, warn } from "$lib/logging"; import { mail, templates } from "$lib/mail"; import { getNostrUser, getProfile, serverPubkey } from "$lib/nostr"; -import { reconcile, types } from "$lib/payments"; +import { reconcile } from "$lib/payments"; import register from "$lib/register"; import { emit } from "$lib/sockets"; import upload from "$lib/upload"; @@ -19,6 +19,8 @@ import { getPublicKey, nip19, verifyEvent } from "nostr-tools"; import { authenticator } from "otplib"; import { v4 } from "uuid"; +import type { Payment, PaymentType } from "$lib/types"; + export default { upload, @@ -465,7 +467,7 @@ export default { await Promise.all( payments.reverse().map(async (id) => await g(`payment:${id}`)), ) - ).filter((p) => p && p.type === types.internal && p.ref)) { + ).filter((p) => p && p.type === PaymentType.internal && p.ref)) { if (ref === id) continue; const i = contacts.findIndex((c) => c && c.id === ref); if (~i) contacts.splice(i, 1); @@ -817,6 +819,16 @@ export default { async app(req, res) { const { pubkey } = req.params; const app = await g(`app:${pubkey}`); + + let payments = (await db.lRange(`${pubkey}:payments`, 0, -1)) || []; + payments = await Promise.all(payments.map((hash) => g(`payment:${hash}`))); + + await Promise.all( + payments.map(async (p: Payment) => (p.user = await g(`user:${p.uid}`))), + ); + + app.payments = payments.filter((p) => p); + res.send(app); }, |
