2023-10-11 09:03:52 +00:00
|
|
|
import debug from "debug";
|
2024-02-05 10:01:12 +00:00
|
|
|
import { LRUCache } from "lru-cache";
|
2024-04-22 13:38:14 +00:00
|
|
|
import { decodeInvoice, InvoiceDetails } from "@snort/shared";
|
|
|
|
import { NostrEvent } from "../nostr";
|
|
|
|
import { findTag } from "../utils";
|
|
|
|
import { NostrLink } from "../nostr-link";
|
|
|
|
import { Nip10 } from "./nip10";
|
2023-06-21 15:08:48 +00:00
|
|
|
|
2023-10-11 09:03:52 +00:00
|
|
|
const Log = debug("zaps");
|
2024-02-05 09:20:13 +00:00
|
|
|
const ParsedZapCache = new LRUCache<string, ParsedZap>({ max: 1000 });
|
2023-09-11 13:27:11 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-14 12:55:46 +00:00
|
|
|
export function parseZap(zapReceipt: 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");
|
|
|
|
}
|
2024-04-22 13:38:14 +00:00
|
|
|
const zapRequestThread = Nip10.parseThread(zapRequest);
|
2023-10-11 09:03:52 +00:00
|
|
|
const requestContext = zapRequestThread?.root;
|
|
|
|
|
2023-07-22 18:37:46 +00:00
|
|
|
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,
|
2024-04-22 13:38:14 +00:00
|
|
|
event: requestContext,
|
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,
|
2023-11-14 13:55:15 +00:00
|
|
|
targetEvents: NostrLink.fromTags(zapRequest.tags),
|
2023-12-07 14:41:57 +00:00
|
|
|
created_at: zapRequest.created_at,
|
2023-07-22 18:37:46 +00:00
|
|
|
};
|
|
|
|
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-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-11-14 13:55:15 +00:00
|
|
|
targetEvents: [],
|
2023-12-07 14:41:57 +00:00
|
|
|
created_at: zapReceipt.created_at,
|
2023-11-14 13:55:15 +00:00
|
|
|
} as ParsedZap;
|
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-12-07 14:41:57 +00:00
|
|
|
created_at: number;
|
2023-11-14 13:55:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of targets this zap is zapping
|
|
|
|
*/
|
|
|
|
targetEvents: Array<NostrLink>;
|
2023-06-21 15:08:48 +00:00
|
|
|
}
|