1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
import config from "$config";
import { db, g, s } from "$lib/db";
import ln from "$lib/ln";
import {
EX,
get,
getCount,
getNostrUser,
getProfile,
pool,
publish,
q,
serverPubkey,
} from "$lib/nostr";
import { parseContent } from "$lib/notes";
import { scan } from "$lib/strfry";
import { bail, fail, fields, getUser } from "$lib/utils";
import got from "got";
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";
// 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
// description tag. NIP-177 kind 9736 (BOLT12 zaps): amount is an explicit
// msat tag, and the receipt is payer-published with the signed 9737 intent in
// the description tag.
const parseZap = async (ev) => {
const { kind, tags, pubkey } = ev;
const description = tags.find((t) => t[0] === "description")?.[1];
let payer;
try {
payer = JSON.parse(description).pubkey;
} catch (e) {}
payer ||= pubkey;
let amount = 0;
if (kind === 9736) {
const msat = Number.parseInt(tags.find((t) => t[0] === "amount")?.[1]);
if (msat > 0) amount = Math.round(msat / 1000);
} else {
try {
const bolt11 = tags.find((t) => t[0] === "bolt11")?.[1];
const { amount_msat } = await ln.decode(bolt11);
if (amount_msat) amount = Math.round(amount_msat / 1000);
} catch (e) {}
}
return { amount, pubkey: payer };
};
export default {
async event(req, res) {
try {
let { id } = req.params;
const full = req.url.endsWith("full");
if (id.startsWith("nevent")) id = decode(id).data.id;
if (id.startsWith("note")) id = decode(id).data;
const k = `event:${id}${full ? ":full" : ""}`;
let event = await g(k);
if (event) return res.send(event);
event = await get({ ids: [id] });
if (!event) return res.code(404).send("Not found");
if (!full) return res.send(event);
const parts = parseContent(event);
let pubkeys = parts
.filter(
({ type }) => type.includes("nprofile") || type.includes("npub"),
)
.map(({ value }) => value.pubkey);
const zapEvents = await scan({ kinds: [9735, 9736], "#e": [id] });
const zaps = [];
for (const zapEvent of zapEvents) {
zaps.push(await parseZap(zapEvent));
}
pubkeys.push(event.pubkey);
pubkeys.push(...zaps.map((z) => z.pubkey).filter((p) => p));
pubkeys = [...new Set(pubkeys)];
const profiles = await scan({ kinds: [0], authors: pubkeys });
const found = profiles.map((p) => p.pubkey);
const missing = pubkeys.filter((p) => !found.includes(p));
const missingProfiles = (
await pool.querySync(config.relays, {
kinds: [0],
authors: missing,
})
)
.reduce((a, b) => {
a.set(
b.pubkey,
b.created_at > (a.get(b.pubkey)?.created_at || 0)
? b
: a.get(b.pubkey),
);
return a;
}, new Map())
.values();
profiles.push(...missingProfiles);
event.parts = parts;
event.names = profiles.reduce((a, b) => {
const { content } = b;
const { name } = JSON.parse(content);
a[b.pubkey] = name;
return a;
}, {});
event.author = profiles.find((p) => p.pubkey === event.pubkey);
event.zaps = zaps
.filter((z) => z.amount > 0)
.map(({ amount, pubkey }) => ({
amount,
user: profiles.find((p) => p.pubkey === pubkey),
}));
await db.set(k, JSON.stringify(event), { EX });
res.send(event);
} catch (e) {
bail(res, e.message);
}
},
async parse(req, res) {
const { event } = req.body;
const parts = parseContent(event);
const names = {};
for (const { type, value } of parts) {
if (type.includes("nprofile") || type.includes("npub")) {
const { name } = await getProfile(value.pubkey);
names[value.pubkey] = name;
}
}
res.send({ parts, names });
},
async thread(req, res) {
try {
const { id } = req.params;
const event = await get({ ids: [id] });
const rootId = event.tags.find(
(tag) => tag[0] === "e" && tag[3] === "root",
)?.[1];
let root;
if (rootId) root = await get({ ids: [rootId] });
else root = event;
const thread = [root, ...(await q({ kinds: [1], "#e": [root.id] }))];
for (const t of thread) {
const e = t as any;
e.author = await getProfile(e.pubkey);
e.parts = parseContent(e);
e.names = {};
for (const { type, value } of e.parts) {
if (type.includes("nprofile") || type.includes("npub")) {
const { name } = await getProfile(value.pubkey);
e.names[value.pubkey] = name;
}
}
}
res.send(thread);
} catch (e) {
bail(res, e.message);
}
},
async zaps(req, res) {
try {
let { id } = req.params;
if (id.startsWith("nevent")) id = decode(id).data.id;
if (id.startsWith("note")) id = decode(id).data;
// q() scans the local strfry and falls back to primal (the old code
// called a `sync` helper that never existed and threw ReferenceError
// whenever the local relay had no receipts)
const filter = { kinds: [9735, 9736], "#e": [id] };
const events = await q(filter);
if (!events.length) return res.send([]);
const zaps = [];
for (const zapEvent of events) {
const { amount, pubkey } = await parseZap(zapEvent);
let user;
if (pubkey) {
try {
user = await getNostrUser(pubkey);
zaps.push({ amount, user });
} catch (e) {}
}
}
res.send(zaps.filter((z) => z.amount > 0));
} catch (e) {
bail(res, e.message);
}
},
async publish(req, res) {
try {
const { event } = req.body;
const { pubkey } = req.user;
await publish(event);
if (event.kind === 3) {
db.del(`${pubkey}:follows`);
db.del(`${pubkey}:follows:n`);
}
res.send({});
} catch (e) {
bail(res, e.message);
}
},
async events(req, res) {
const {
params: { pubkey },
} = req;
try {
const events = await q({ kinds: [1], authors: [pubkey], limit: 20 });
for (const v of events) {
const e = v as any;
e.author = await getProfile(e.pubkey);
e.parts = parseContent(e);
e.names = {};
for (const { type, value } of e.parts) {
if (type.includes("nprofile") || type.includes("npub")) {
const { name } = await getProfile(value.pubkey);
e.names[value.pubkey] = name;
}
}
}
res.send(events);
} catch (e) {
bail(res, e.message);
}
},
async follows(req, res) {
const {
params: { pubkey },
query: { limit = 20, offset = 0, pubkeysOnly },
} = req;
try {
const k = `${pubkey}:follows${pubkeysOnly ? ":pubkeys" : ""}`;
let follows = await g(k);
if (follows?.length) return res.send(follows);
const event = await get({ authors: [pubkey], kinds: [3] });
if (!event) return res.send([]);
let pubkeys = event.tags
.filter((tag) => tag[0] === "p")
.map((tag) => tag[1]);
follows = [];
if (pubkeysOnly) follows = pubkeys;
else {
pubkeys = pubkeys.slice(offset, offset + limit);
follows = (
await Promise.allSettled(
pubkeys.map(async (pubkey) => ({
...(await getProfile(pubkey)),
pubkey,
})),
)
)
.filter((r) => r.status === "fulfilled")
.map((r) => r.value);
}
await db.set(k, JSON.stringify(follows), { EX });
res.send(follows);
} catch (e) {
console.log("follows fail", e);
bail(res, e.message);
}
},
async followers(req, res) {
const {
params: { pubkey },
query: { limit = 20, offset = 0 },
} = req;
try {
let followers = await g(`${pubkey}:followers`);
if (followers?.length) return res.send(followers);
const events = await q({ kinds: [3], "#p": [pubkey], limit });
if (!events.length) return res.send([]);
const pubkeys = events.map((e) => e.pubkey).slice(offset, offset + limit);
followers = (
await Promise.allSettled(
pubkeys.map(async (pubkey) => ({
...(await getProfile(pubkey)),
pubkey,
})),
)
)
.filter((r) => r.status === "fulfilled")
.map((r) => r.value);
await db.set(`${pubkey}:followers`, JSON.stringify(followers), { EX });
res.send(followers);
} catch (e) {
bail(res, e.message);
}
},
async count(req, res) {
try {
const { pubkey } = req.params;
// res.send({ followers: 0, follows: 0 });
res.send(await getCount(pubkey));
} catch (e) {
bail(res, e.message);
}
},
async identities(req, res) {
const {
query: { name },
} = req;
let names = {};
if (name) {
// 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 {
const r = await fetch(
`${process.env.NAMES_URL || "https://names.coinos.io"}/.well-known/nostr.json?name=${encodeURIComponent(name)}`,
{ signal: AbortSignal.timeout(3000) },
);
if (r.ok) {
const j = await r.json();
if (j?.names?.[name]) return res.send({ names: { [name]: j.names[name] } });
}
} catch {}
const u = await getUser(name, fields);
if (!u) return res.send({ names: {} }); // unknown name: empty per NIP-05, not a 500
names = { [name]: u.pubkey };
} else {
const records = await db.sMembers("nip5");
for (const s of records) {
const [name, pubkey] = s.split(":");
names[name] = pubkey;
}
}
res.send({ names });
},
async info(_, res) {
res.send({ pubkey: serverPubkey });
},
async profile(req, res) {
const { profile } = req.params;
const { data } = decode(profile);
const { pubkey, relays } = data as ProfilePointer;
const recipient = await getProfile(pubkey, relays);
recipient.relays = relays;
res.send(recipient);
},
async zapRequest(req, res) {
try {
const { amount, id } = req.body;
const { pubkey } = await get({ ids: [id] });
const event = await makeZapRequest({
profile: pubkey,
event: id,
amount: amount * 1000,
relays: ["wss://relay.coinos.io", "wss://relay.primal.net"],
comment: "",
});
res.send(event);
} catch (e) {}
},
async zap(req, res) {
try {
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");
const encodedEvent = encodeURI(JSON.stringify(event));
const url = `${callback}?amount=${amount}&nostr=${encodedEvent}`;
const json = await got(url).json();
res.send(json);
} catch (e) {
bail(res, e.message);
}
},
};
|