Move zap parsing to @snort/system
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@snort/system",
|
||||
"version": "1.0.8",
|
||||
"version": "1.0.9",
|
||||
"description": "Snort nostr system package",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
@ -25,7 +25,7 @@
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@snort/shared": "^1.0.1",
|
||||
"@snort/shared": "^1.0.2",
|
||||
"@noble/curves": "^1.0.0",
|
||||
"@scure/base": "^1.1.1",
|
||||
"@stablelib/xchacha20": "^1.0.1",
|
||||
|
@ -3,7 +3,7 @@ import debug from "debug";
|
||||
import { unixNowMs, FeedCache } from "@snort/shared";
|
||||
import { EventKind, HexKey, SystemInterface, TaggedRawEvent, PubkeyReplaceableNoteStore, RequestBuilder } from ".";
|
||||
import { ProfileCacheExpire } from "./Const";
|
||||
import { mapEventToProfile, MetadataCache } from "./cache";
|
||||
import { mapEventToProfile, MetadataCache } from "./Cache";
|
||||
|
||||
const MetadataRelays = [
|
||||
"wss://purplepag.es"
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { FeedCache } from "@snort/shared";
|
||||
import { Connection } from "Connection";
|
||||
import { RelayMetrics } from "cache";
|
||||
import { RelayMetrics } from "Cache";
|
||||
|
||||
export class RelayMetricHandler {
|
||||
readonly #cache: FeedCache<RelayMetrics>;
|
||||
|
92
packages/system/src/Zaps.ts
Normal file
92
packages/system/src/Zaps.ts
Normal file
@ -0,0 +1,92 @@
|
||||
import { FeedCache } from "@snort/shared";
|
||||
import { sha256, decodeInvoice, InvoiceDetails } from "@snort/shared";
|
||||
import { HexKey, NostrEvent } from "Nostr";
|
||||
import { findTag } from "./Utils";
|
||||
import { MetadataCache } from "./Cache";
|
||||
|
||||
function getInvoice(zap: NostrEvent): InvoiceDetails | undefined {
|
||||
const bolt11 = findTag(zap, "bolt11");
|
||||
if (!bolt11) {
|
||||
throw new Error("Invalid zap, missing bolt11 tag");
|
||||
}
|
||||
return decodeInvoice(bolt11);
|
||||
}
|
||||
|
||||
export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCache>, refNote?: NostrEvent): ParsedZap {
|
||||
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");
|
||||
}
|
||||
const isForwardedZap = refNote?.tags.some(a => a[0] === "zap") ?? false;
|
||||
const anonZap = zapRequest.tags.find(a => a[0] === "anon");
|
||||
const metaHash = sha256(innerZapJson);
|
||||
const pollOpt = zapRequest.tags.find(a => a[0] === "poll_option")?.[1];
|
||||
const ret: ParsedZap = {
|
||||
id: zapReceipt.id,
|
||||
zapService: zapReceipt.pubkey,
|
||||
amount: (invoice?.amount ?? 0) / 1000,
|
||||
event: findTag(zapRequest, "e"),
|
||||
sender: zapRequest.pubkey,
|
||||
receiver: findTag(zapRequest, "p"),
|
||||
valid: true,
|
||||
anonZap: anonZap !== undefined,
|
||||
content: zapRequest.content,
|
||||
errors: [],
|
||||
pollOption: pollOpt ? Number(pollOpt) : undefined,
|
||||
};
|
||||
if (invoice?.descriptionHash !== metaHash) {
|
||||
ret.valid = false;
|
||||
ret.errors.push("description_hash does not match zap request");
|
||||
}
|
||||
if (findTag(zapRequest, "p") !== findTag(zapReceipt, "p")) {
|
||||
ret.valid = false;
|
||||
ret.errors.push("p tags dont match");
|
||||
}
|
||||
if (ret.event && ret.event !== findTag(zapReceipt, "e")) {
|
||||
ret.valid = false;
|
||||
ret.errors.push("e tags dont match");
|
||||
}
|
||||
if (findTag(zapRequest, "amount") === invoice?.amount) {
|
||||
ret.valid = false;
|
||||
ret.errors.push("amount tag does not match invoice amount");
|
||||
}
|
||||
if (userCache.getFromCache(ret.receiver)?.zapService !== ret.zapService && !isForwardedZap) {
|
||||
ret.valid = false;
|
||||
ret.errors.push("zap service pubkey doesn't match");
|
||||
}
|
||||
return ret;
|
||||
} catch (e) {
|
||||
// ignored: console.debug("Invalid zap", zapReceipt, e);
|
||||
}
|
||||
}
|
||||
return {
|
||||
id: zapReceipt.id,
|
||||
zapService: zapReceipt.pubkey,
|
||||
amount: 0,
|
||||
valid: false,
|
||||
anonZap: false,
|
||||
errors: ["invalid zap, parsing failed"],
|
||||
};
|
||||
}
|
||||
|
||||
export interface ParsedZap {
|
||||
id: HexKey;
|
||||
event?: HexKey;
|
||||
receiver?: HexKey;
|
||||
amount: number;
|
||||
content?: string;
|
||||
sender?: HexKey;
|
||||
valid: boolean;
|
||||
zapService: HexKey;
|
||||
anonZap: boolean;
|
||||
errors: Array<string>;
|
||||
pollOption?: number;
|
||||
}
|
@ -18,14 +18,15 @@ export * from "./EventPublisher";
|
||||
export * from "./EventBuilder";
|
||||
export * from "./NostrLink";
|
||||
export * from "./ProfileCache";
|
||||
export * from "./Zaps";
|
||||
|
||||
export * from "./impl/nip4";
|
||||
export * from "./impl/nip44";
|
||||
|
||||
export * from "./cache";
|
||||
export * from "./cache/UserRelayCache";
|
||||
export * from "./cache/UserCache";
|
||||
export * from "./cache/RelayMetricCache";
|
||||
export * from "./Cache";
|
||||
export * from "./Cache/UserRelayCache";
|
||||
export * from "./Cache/UserCache";
|
||||
export * from "./Cache/RelayMetricCache";
|
||||
|
||||
export interface SystemInterface {
|
||||
/**
|
||||
|
@ -13,6 +13,6 @@
|
||||
"outDir": "dist",
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"files": ["src/index.ts"]
|
||||
"include": ["./src/**/*.ts"],
|
||||
"files": ["./src/index.ts"]
|
||||
}
|
||||
|
Reference in New Issue
Block a user