aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/nostr.ts34
1 files changed, 26 insertions, 8 deletions
diff --git a/routes/nostr.ts b/routes/nostr.ts
index 3b28076a..023b656f 100644
--- a/routes/nostr.ts
+++ b/routes/nostr.ts
@@ -1,5 +1,6 @@
import config from "$config";
import { db, g, s } from "$lib/db";
+import { holderBlocked } from "$lib/names";
import ln from "$lib/ln";
import {
EX,
@@ -15,11 +16,12 @@ import {
import { parseContent } from "$lib/notes";
import { scan } from "$lib/strfry";
import { bail, fail, fields, getUser } from "$lib/utils";
-import got from "got";
+import { safeGot } from "$lib/safe-fetch";
+import { bech32 } from "bech32";
import type { Event } from "nostr-tools";
import { decode } from "nostr-tools/nip19";
import type { ProfilePointer } from "nostr-tools/nip19";
-import { getZapEndpoint, makeZapRequest } from "nostr-tools/nip57";
+import { makeZapRequest } from "nostr-tools/nip57";
// Amount and payer of a zap receipt. NIP-57 kind 9735: amount comes from
// decoding the paid bolt11, payer from the embedded 9734 zap request in the
@@ -351,6 +353,7 @@ export default {
// v3 registrar first: a claimed name's NIP-05 belongs to its wallet key.
// Legacy accounts answer as before when the registrar doesn't know it.
try {
+ if (await holderBlocked(name)) throw new Error("blocked holder");
const r = await fetch(
`${process.env.NAMES_URL || "https://names.coinos.io"}/.well-known/nostr.json?name=${encodeURIComponent(name)}`,
{ signal: AbortSignal.timeout(3000) },
@@ -408,13 +411,28 @@ export default {
const { event } = req.body;
const amount = event.tags.find((t) => t[0] === "amount")[1];
const pubkey = event.tags.find((t) => t[0] === "p")[1];
- const content = JSON.stringify(await getProfile(pubkey));
- const callback = await getZapEndpoint({ content } as Event);
- if (!callback || callback === "null") fail("Lightning address not found");
+ // The recipient's profile is whatever they published, so its lightning
+ // address and the callback it leads to are fetched through the SSRF
+ // guard (nostr-tools' getZapEndpoint and got would fetch them as-is).
+ const { lud06, lud16 } = (await getProfile(pubkey)) || {};
+ let lnurl: string;
+ if (typeof lud16 === "string" && /^[^@\s/?#]+@[^@\s/?#]+$/.test(lud16)) {
+ const [name, domain] = lud16.split("@");
+ lnurl = `https://${domain}/.well-known/lnurlp/${encodeURIComponent(name)}`;
+ } else if (typeof lud06 === "string" && lud06) {
+ lnurl = Buffer.from(
+ bech32.fromWords(bech32.decode(lud06, 2000).words),
+ ).toString();
+ } else fail("Lightning address not found");
- const encodedEvent = encodeURI(JSON.stringify(event));
- const url = `${callback}?amount=${amount}&nostr=${encodedEvent}`;
- const json = await got(url).json();
+ const pay = await safeGot(lnurl);
+ if (!pay?.allowsNostr || !pay.nostrPubkey || typeof pay.callback !== "string")
+ fail("Lightning address not found");
+
+ const u = new URL(pay.callback);
+ u.searchParams.set("amount", String(amount));
+ u.searchParams.set("nostr", JSON.stringify(event));
+ const json = await safeGot(u.toString());
res.send(json);
} catch (e) {