aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-02-11 13:40:05 -0800
committerAdam Soltys <[email protected]>2025-02-11 13:40:05 -0800
commitde7435e34580e1a9b4b8d95b2a5013112414098f (patch)
tree97074cadf3a63512c2c5f554a3083924fd046b74
parent8add46c0975686ce0967c2c38514a31aad6ee8cb (diff)
downloadcoinos-server-de7435e34580e1a9b4b8d95b2a5013112414098f.tar.gz
coinos-server-de7435e34580e1a9b4b8d95b2a5013112414098f.zip
default app on register
-rw-r--r--index.ts2
-rw-r--r--lib/nostr.ts8
-rw-r--r--lib/nwc.ts3
-rw-r--r--lib/register.ts13
-rw-r--r--routes/nostr.ts9
-rw-r--r--routes/users.ts12
6 files changed, 27 insertions, 20 deletions
diff --git a/index.ts b/index.ts
index 31ca79fe..c392e187 100644
--- a/index.ts
+++ b/index.ts
@@ -128,7 +128,7 @@ app.post(
);
app.post("/printer", users.printer);
app.get("/challenge", users.challenge);
-app.post("/nostrLogin", users.nostrLogin);
+app.post("/nostrAuth", users.nostrAuth);
app.get("/app/:pubkey", auth, users.app);
app.get("/apps", auth, users.apps);
app.post("/app", auth, users.updateApp);
diff --git a/lib/nostr.ts b/lib/nostr.ts
index 77726f38..9f5a2698 100644
--- a/lib/nostr.ts
+++ b/lib/nostr.ts
@@ -3,7 +3,7 @@ import { db, g, s } from "$lib/db";
import { l, warn } from "$lib/logging";
import { scan } from "$lib/strfry";
import { fail, fields, getUser, pick } from "$lib/utils";
-import { finalizeEvent, getPublicKey, nip19 } from "nostr-tools";
+import { finalizeEvent, getPublicKey, nip19, verifyEvent } from "nostr-tools";
import type { Event } from "nostr-tools";
import { AbstractSimplePool } from "nostr-tools/abstract-pool";
import { Relay } from "nostr-tools/relay";
@@ -30,8 +30,8 @@ export const anon = (pubkey) => ({
followers: [],
});
-export async function send(ev, url = config.nostr) {
- if (!ev?.id) return;
+export async function publish(ev, url = config.nostr) {
+ if (!verifyEvent(ev)) fail("Invalid event");
const r = await Relay.connect(url);
await r.publish(ev);
r.close();
@@ -98,7 +98,7 @@ export async function handleZap(invoice, sender = undefined) {
l("sending receipt");
const relays = relays_tag.slice(1).filter((r) => r?.startsWith("ws"));
- relays.push(config.nostr);
+ relays.push(config.nostr);
relays.map(async (url) => {
try {
const r = await Relay.connect(url);
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 2774b0d8..3f346b87 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -2,7 +2,7 @@ import config from "$config";
import { db, g } from "$lib/db";
import { generate } from "$lib/invoices";
import ln from "$lib/ln";
-import { err } from "$lib/logging";
+import { err, l } from "$lib/logging";
import { handleZap, serverPubkey } from "$lib/nostr";
import { sendInternal, sendKeysend, sendLightning } from "$lib/payments";
import { getInvoice, sleep } from "$lib/utils";
@@ -228,6 +228,7 @@ const handle = (method, params, ev, app, user) =>
async make_invoice() {
const { amount, description, description_hash, expiry } = params;
+ l("nwc make_invoice", user.username);
const invoice = {
amount: Math.round(amount / 1000),
diff --git a/lib/register.ts b/lib/register.ts
index 6bd9df9f..e1a39d15 100644
--- a/lib/register.ts
+++ b/lib/register.ts
@@ -63,7 +63,6 @@ export default async (user, ip) => {
}
user.npub = nip19.npubEncode(pubkey);
- user.nwc = bytesToHex(randomBytes(32));
const account = JSON.stringify({
id,
@@ -71,6 +70,18 @@ export default async (user, ip) => {
name: "Spending",
});
+ const secret = bytesToHex(randomBytes(32));
+ const app = {
+ secret,
+ pubkey: getPublicKey(secret),
+ max_amount: 10000,
+ budget_renewal: "weekly",
+ name: username,
+ };
+
+ await s(app.pubkey, app);
+ await db.sAdd(`${uid}:apps`, app.pubkey);
+
db.multi()
.set(getPublicKey(user.nwc), user.id)
.set(`user:${id}`, JSON.stringify(user))
diff --git a/routes/nostr.ts b/routes/nostr.ts
index 5000ccc7..9c61bf16 100644
--- a/routes/nostr.ts
+++ b/routes/nostr.ts
@@ -8,6 +8,7 @@ import {
getNostrUser,
getProfile,
pool,
+ publish,
q,
serverPubkey,
} from "$lib/nostr";
@@ -16,7 +17,6 @@ import { scan } from "$lib/strfry";
import { bail, fail, fields, getUser } from "$lib/utils";
import got from "got";
import type { Event } from "nostr-tools";
-import { verifyEvent } from "nostr-tools";
import { decode } from "nostr-tools/nip19";
import type { ProfilePointer } from "nostr-tools/nip19";
import { getZapEndpoint, makeZapRequest } from "nostr-tools/nip57";
@@ -207,10 +207,8 @@ export default {
try {
const { event } = req.body;
const { pubkey } = req.user;
- // const { write: relays } = await getRelays(pubkey);
- // if (!relays.includes(config.nostr)) relays.push(config.nostr);
- if (!verifyEvent(event)) fail("Invalid event");
- await load(JSON.stringify(event));
+
+ await publish(event);
if (event.kind === 3) {
db.del(`${pubkey}:follows`);
@@ -219,6 +217,7 @@ export default {
res.send({});
} catch (e) {
+ console.log(e);
bail(res, e.message);
}
},
diff --git a/routes/users.ts b/routes/users.ts
index c0d99b7d..9f5905bf 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -179,16 +179,12 @@ export default {
else fail("Key in use by another account");
}
- const { challenge } = body;
const event = JSON.parse(body.event);
+ const challenge = event.tags.find((t) => t[0] === "challenge")[1];
const c = await g(`challenge:${challenge}`);
- if (!c) fail("Invalid or expired login challenge");
+ if (!c) fail("Invalid or expired challenge");
- if (
- !verifyEvent(event) ||
- event.content !== challenge ||
- event.pubkey !== pubkey
- )
+ if (!verifyEvent(event) || event.pubkey !== pubkey)
fail("Invalid signature or challenge mismatch.");
pubkey = pubkey.replace(/\s*/g, "");
@@ -332,7 +328,7 @@ export default {
res.send({ challenge: id });
},
- async nostrLogin(req, res) {
+ async nostrAuth(req, res) {
try {
const { event, challenge, twofa } = req.body;
const ip = req.headers["cf-connecting-ip"];