diff options
| author | Adam Soltys <[email protected]> | 2025-02-07 22:23:44 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-02-07 22:23:44 -0800 |
| commit | e68c302e1082da29020d1efa932f986032ae5ce2 (patch) | |
| tree | 25a46c19ec2d528be4f2d8e4587765b1aaebd90a /routes | |
| parent | eae37f75a77236d7929d8ce1f573c0f5bd428c79 (diff) | |
| download | coinos-server-e68c302e1082da29020d1efa932f986032ae5ce2.tar.gz coinos-server-e68c302e1082da29020d1efa932f986032ae5ce2.zip | |
nwc apps endpoints
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/users.ts | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/routes/users.ts b/routes/users.ts index 16c2d407..ddadec5f 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -5,7 +5,7 @@ import { requirePin } from "$lib/auth"; import { db, g, s } from "$lib/db"; import { err, l, warn } from "$lib/logging"; import { mail, templates } from "$lib/mail"; -import { getNostrUser, getProfile } from "$lib/nostr"; +import { getNostrUser, getProfile, serverPubkey } from "$lib/nostr"; import { reconcile, types } from "$lib/payments"; import register from "$lib/register"; import { emit } from "$lib/sockets"; @@ -261,6 +261,7 @@ export default { } user.haspin = !!user.pin; + user.destination = user.destination.trim(); await s(`user:${user.pubkey}`, user.id); await s( `user:${user.username.toLowerCase().replace(/\s/g, "")}`, @@ -821,4 +822,68 @@ export default { res.code(500).send({ error: "Failed to generate LittleFS image" }); } }, + + async app(req, res) { + const { pubkey } = req.params; + const app = await g(pubkey); + res.send(app); + }, + + async apps(req, res) { + const { user } = req; + const pubkeys = await db.sMembers(`${user.id}:apps`); + const apps = await Promise.all(pubkeys.map((p) => g(p))); + + const { host } = new URL(process.env.URL); + const lud16 = `${user.username}@${host}`; + const relay = encodeURIComponent(config.nostr); + + await Promise.all( + apps.map(async (a) => { + if (a.secret) + a.nwc = `nostr+walletconnect://${serverPubkey}?relay=${relay}&secret=${a.secret}&lud16=${lud16}`; + + a.spent = (await db.lRange(`${a.pubkey}:payments`, 0, -1)).reduce( + (a, b) => a + (Math.abs(b.amount) + b.fee + b.ourfee), + 0, + ); + }), + ); + + res.send(apps); + }, + + async updateApp(req, res) { + try { + let { secret, pubkey, max_amount, budget_renewal, name } = req.body; + const { user } = req; + const uid = user.id; + let app = await g(pubkey); + if (app && uid !== app.uid) fail("Unauthorized"); + if (secret) pubkey = getPublicKey(secret); + app = { ...app, pubkey, max_amount, budget_renewal, name, uid, secret }; + await s(pubkey, app); + await db.sAdd(`${uid}:apps`, pubkey); + res.send({}); + } catch (e) { + console.log(e); + bail(res, e.message); + } + }, + + async deleteApp(req, res) { + try { + const { user } = req; + const uid = user.id; + const { pubkey } = req.body; + const app = await g(pubkey); + if (app && uid !== app.uid) fail("Unauthorized"); + await db.sRem(`${uid}:apps`, pubkey); + await db.del(pubkey); + res.send({}); + } catch (e) { + console.log(e); + bail(res, e.message); + } + }, }; |
