snort/packages/system/src/zaps.ts

108 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-06-21 15:08:48 +00:00
import { FeedCache } from "@snort/shared";
2023-10-11 09:03:52 +00:00
import { decodeInvoice, InvoiceDetails } from "@snort/shared";
import { NostrEvent } from "./nostr";
2023-06-21 16:56:43 +00:00
import { findTag } from "./utils";
2023-06-21 15:48:35 +00:00
import { MetadataCache } from "./cache";
2023-10-11 09:03:52 +00:00
import { EventExt } from "./event-ext";
import { NostrLink } from "./nostr-link";
import debug from "debug";
2023-06-21 15:08:48 +00:00
2023-10-11 09:03:52 +00:00
const Log = debug("zaps");
2023-09-11 13:27:11 +00:00
const ParsedZapCache = new Map<string, ParsedZap>();
2023-06-21 15:08:48 +00:00
function getInvoice(zap: NostrEvent): InvoiceDetails | undefined {
2023-07-22 18:37:46 +00:00
const bolt11 = findTag(zap, "bolt11");
if (!bolt11) {
throw new Error("Invalid zap, missing bolt11 tag");
}
return decodeInvoice(bolt11);
2023-06-21 15:08:48 +00:00
}
export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCache>, refNote?: NostrEvent): ParsedZap {
2023-09-11 13:27:11 +00:00
const existing = ParsedZapCache.get(zapReceipt.id);
2023-09-12 14:02:16 +00:00
if (existing) {
2023-09-11 13:27:11 +00:00
return existing;
}
2023-07-22 18:37:46 +00:00
let innerZapJson = findTag(zapReceipt, "description");
if (innerZapJson) {
try {
const invoice = getInvoice(zapReceipt);
if (innerZapJson.startsWith("%")) {
innerZapJson = decodeURIComponent(innerZapJson);
}
const zapRequest: NostrEvent = JSON.parse(innerZapJson);
if (Array.isArray(zapRequest)) {
// old format, ignored
throw new Error("deprecated zap format");
}
2023-10-11 09:03:52 +00:00
const zapRequestThread = EventExt.extractThread(zapRequest);
const requestContext = zapRequestThread?.root;
2023-07-22 18:37:46 +00:00
const isForwardedZap = refNote?.tags.some(a => a[0] === "zap") ?? false;
const anonZap = zapRequest.tags.find(a => a[0] === "anon");
const pollOpt = zapRequest.tags.find(a => a[0] === "poll_option")?.[1];
const ret: ParsedZap = {
2023-06-21 15:08:48 +00:00
id: zapReceipt.id,
zapService: zapReceipt.pubkey,
2023-07-22 18:37:46 +00:00
amount: (invoice?.amount ?? 0) / 1000,
2023-10-11 09:03:52 +00:00
event: requestContext ? NostrLink.fromThreadTag(requestContext) : undefined,
2023-07-22 18:37:46 +00:00
sender: zapRequest.pubkey,
receiver: findTag(zapRequest, "p"),
valid: true,
anonZap: anonZap !== undefined,
content: zapRequest.content,
errors: [],
pollOption: pollOpt ? Number(pollOpt) : undefined,
};
if (findTag(zapRequest, "p") !== findTag(zapReceipt, "p")) {
ret.valid = false;
ret.errors.push("p tags dont match");
}
if (findTag(zapRequest, "amount") === invoice?.amount) {
ret.valid = false;
ret.errors.push("amount tag does not match invoice amount");
}
2023-11-03 01:07:06 +00:00
/*if (userCache.getFromCache(ret.receiver)?.zapService !== ret.zapService && !isForwardedZap) {
2023-07-22 18:37:46 +00:00
ret.valid = false;
ret.errors.push("zap service pubkey doesn't match");
2023-11-03 01:07:06 +00:00
}*/
2023-10-11 09:03:52 +00:00
if (!ret.valid) {
Log("Invalid zap %O", ret);
}
2023-07-22 18:37:46 +00:00
return ret;
2023-10-11 09:03:52 +00:00
} catch {
// ignored
2023-07-22 18:37:46 +00:00
}
}
2023-09-11 13:27:11 +00:00
const ret = {
2023-07-22 18:37:46 +00:00
id: zapReceipt.id,
zapService: zapReceipt.pubkey,
amount: 0,
valid: false,
anonZap: false,
errors: ["invalid zap, parsing failed"],
};
2023-10-11 09:03:52 +00:00
if (!ret.valid) {
Log("Invalid zap %O", ret);
}
2023-09-11 13:27:11 +00:00
ParsedZapCache.set(ret.id, ret);
return ret;
2023-06-21 15:08:48 +00:00
}
export interface ParsedZap {
2023-10-11 09:03:52 +00:00
id: string;
2023-07-22 18:37:46 +00:00
amount: number;
2023-10-11 09:03:52 +00:00
zapService: string;
2023-07-22 18:37:46 +00:00
valid: boolean;
errors: Array<string>;
2023-10-11 09:03:52 +00:00
anonZap: boolean;
event?: NostrLink;
receiver?: string;
content?: string;
sender?: string;
2023-07-22 18:37:46 +00:00
pollOption?: number;
2023-06-21 15:08:48 +00:00
}