feat: tag anon zaps

This commit is contained in:
2023-02-18 21:27:06 +00:00
parent b5d7e2c58a
commit 4c1513676e
4 changed files with 20 additions and 8 deletions

View File

@ -38,6 +38,7 @@ function getInvoice(zap: TaggedRawEvent) {
interface Zapper {
pubkey?: HexKey;
isValid: boolean;
isAnon: boolean;
}
function getZapper(zap: TaggedRawEvent, dhash: string): Zapper {
@ -50,16 +51,17 @@ function getZapper(zap: TaggedRawEvent, dhash: string): Zapper {
const rawEvent: TaggedRawEvent = JSON.parse(zapRequest);
if (Array.isArray(rawEvent)) {
// old format, ignored
return { isValid: false };
return { isValid: false, isAnon: false };
}
const anonZap = rawEvent.tags.some(a => a[0] === "anon");
const metaHash = sha256(zapRequest);
const ev = new Event(rawEvent);
return { pubkey: ev.PubKey, isValid: dhash === metaHash };
return { pubkey: ev.PubKey, isValid: dhash === metaHash, isAnon: anonZap };
} catch (e) {
console.warn("Invalid zap", zapRequest);
}
}
return { isValid: false };
return { isValid: false, isAnon: false };
}
export interface ParsedZap {
@ -71,11 +73,12 @@ export interface ParsedZap {
zapper?: HexKey;
valid: boolean;
zapService: HexKey;
anonZap: boolean;
}
export function parseZap(zap: TaggedRawEvent): ParsedZap {
const { amount, hash } = getInvoice(zap);
const zapper = hash ? getZapper(zap, hash) : { isValid: false };
const zapper = hash ? getZapper(zap, hash) : ({ isValid: false } as Zapper);
const e = findTag(zap, "e");
const p = unwrap(findTag(zap, "p"));
return {
@ -87,6 +90,7 @@ export function parseZap(zap: TaggedRawEvent): ParsedZap {
content: zap.content,
valid: zapper.isValid,
zapService: zap.pubkey,
anonZap: zapper.isAnon,
};
}