From 140d87e0717a022d9704dbfc3f6ae50ac2751881 Mon Sep 17 00:00:00 2001 From: Kieran Date: Sat, 4 Feb 2023 13:30:05 +0000 Subject: [PATCH] verify description hash --- src/Element/Timeline.tsx | 14 ++++++------- src/Element/Zap.tsx | 44 ++++++++++++++++++++++------------------ src/Number.ts | 2 +- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/Element/Timeline.tsx b/src/Element/Timeline.tsx index 5fbb2e4b..dc55b3d9 100644 --- a/src/Element/Timeline.tsx +++ b/src/Element/Timeline.tsx @@ -46,19 +46,19 @@ export default function Timeline({ subject, postsOnly = false, method, ignoreMod function eventElement(e: TaggedRawEvent) { switch (e.kind) { case EventKind.SetMetadata: { - return + return } case EventKind.TextNote: { return } - case EventKind.ZapReceipt: { - const zap = parseZap(e) - return zap.e ? null : + case EventKind.ZapReceipt: { + const zap = parseZap(e) + return zap.e ? null : } case EventKind.Reaction: case EventKind.Repost: { let eRef = e.tags.find(a => a[0] === "e")?.at(1); - return a.id === eRef)}/> + return a.id === eRef)} /> } } } @@ -66,12 +66,12 @@ export default function Timeline({ subject, postsOnly = false, method, ignoreMod return (
{latestFeed.length > 1 && (
showLatest()}> - +   Show latest {latestFeed.length - 1} notes
)} {mainFeed.map(eventElement)} - +
); } diff --git a/src/Element/Zap.tsx b/src/Element/Zap.tsx index e0ca66f5..adfc7288 100644 --- a/src/Element/Zap.tsx +++ b/src/Element/Zap.tsx @@ -4,6 +4,7 @@ import { useSelector } from "react-redux"; // @ts-expect-error import { decode as invoiceDecode } from "light-bolt11-decoder"; import { bytesToHex } from "@noble/hashes/utils"; +import { sha256 } from "Util"; //import { sha256 } from "Util"; import { formatShort } from "Number"; @@ -30,21 +31,24 @@ function getInvoice(zap: TaggedRawEvent) { return { amount, hash: hash ? bytesToHex(hash) : undefined }; } -function getZapper(zap: TaggedRawEvent, dhash: string) { +interface Zapper { + pubkey?: HexKey, + isValid: boolean +} + +function getZapper(zap: TaggedRawEvent, dhash: string): Zapper { const zapRequest = findTag(zap, 'description') if (zapRequest) { const rawEvent: TaggedRawEvent = JSON.parse(zapRequest); if (Array.isArray(rawEvent)) { // old format, ignored - const rawDescriptionTag = rawEvent.find(a => a[0] === 'application/nostr') - const rawDescription = rawDescriptionTag && rawDescriptionTag[1] - const request = typeof rawDescription === 'string' ? JSON.parse(rawDescription) : rawDescription - return request?.pubkey + return { isValid: false }; } - //const metaHash = sha256(zapRequest); + const metaHash = sha256(zapRequest); const ev = new Event(rawEvent) - return ev.PubKey + return { pubkey: ev.PubKey, isValid: dhash === metaHash }; } + return { isValid: false } } interface ParsedZap { @@ -59,7 +63,7 @@ interface ParsedZap { export function parseZap(zap: TaggedRawEvent): ParsedZap { const { amount, hash } = getInvoice(zap) - const zapper = hash && getZapper(zap, hash) + const zapper = hash ? getZapper(zap, hash) : { isValid: false }; const e = findTag(zap, 'e') const p = findTag(zap, 'p')! return { @@ -67,9 +71,9 @@ export function parseZap(zap: TaggedRawEvent): ParsedZap { e, p, amount: Number(amount) / 1000, - zapper, + zapper: zapper.pubkey, content: zap.content, - valid: true, + valid: zapper.isValid, } } @@ -87,12 +91,12 @@ const Zap = ({ zap, showZapped = true }: { zap: ParsedZap, showZapped?: boolean
- +
) : null @@ -102,8 +106,8 @@ interface ZapsSummaryProps { zaps: ParsedZap[] } export const ZapsSummary = ({ zaps }: ZapsSummaryProps) => { const sortedZaps = useMemo(() => { - const pub = [...zaps.filter(z => z.zapper)] - const priv = [...zaps.filter(z => !z.zapper)] + const pub = [...zaps.filter(z => z.zapper)] + const priv = [...zaps.filter(z => !z.zapper)] pub.sort((a, b) => b.amount - a.amount) return pub.concat(priv) }, [zaps]) @@ -127,8 +131,8 @@ export const ZapsSummary = ({ zaps }: ZapsSummaryProps) => {
- {content && ( -