snort/packages/app/src/Element/Zap.tsx

172 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-02-03 21:38:14 +00:00
import "./Zap.css";
2023-02-04 10:28:13 +00:00
import { useMemo } from "react";
2023-02-18 22:42:47 +00:00
import { FormattedMessage, useIntl } from "react-intl";
2023-03-05 16:58:34 +00:00
import { HexKey, TaggedRawEvent } from "@snort/nostr";
2023-03-02 15:23:53 +00:00
2023-03-05 16:58:34 +00:00
import { decodeInvoice, InvoiceDetails, sha256, unwrap } from "Util";
2023-02-03 21:38:14 +00:00
import { formatShort } from "Number";
import Text from "Element/Text";
import ProfileImage from "Element/ProfileImage";
2023-03-09 10:13:10 +00:00
import { findTag } from "Util";
2023-03-29 12:10:22 +00:00
import { UserCache } from "Cache/UserCache";
2023-04-14 11:57:48 +00:00
import useLogin from "Hooks/useLogin";
2023-02-03 21:38:14 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-03-05 16:58:34 +00:00
function getInvoice(zap: TaggedRawEvent): InvoiceDetails | undefined {
const bolt11 = findTag(zap, "bolt11");
2023-02-16 21:53:44 +00:00
if (!bolt11) {
2023-03-05 16:58:34 +00:00
throw new Error("Invalid zap, missing bolt11 tag");
2023-02-16 21:53:44 +00:00
}
2023-03-05 16:58:34 +00:00
return decodeInvoice(bolt11);
2023-02-04 13:30:05 +00:00
}
export function parseZap(zapReceipt: TaggedRawEvent, refNote?: TaggedRawEvent): ParsedZap {
2023-03-05 16:58:34 +00:00
let innerZapJson = findTag(zapReceipt, "description");
if (innerZapJson) {
2023-02-14 14:53:00 +00:00
try {
2023-03-05 18:38:14 +00:00
const invoice = getInvoice(zapReceipt);
2023-03-05 16:58:34 +00:00
if (innerZapJson.startsWith("%")) {
innerZapJson = decodeURIComponent(innerZapJson);
2023-02-14 14:53:00 +00:00
}
2023-03-05 16:58:34 +00:00
const zapRequest: TaggedRawEvent = JSON.parse(innerZapJson);
if (Array.isArray(zapRequest)) {
2023-02-14 14:53:00 +00:00
// old format, ignored
2023-03-05 16:58:34 +00:00
throw new Error("deprecated zap format");
2023-02-14 14:53:00 +00:00
}
const isForwardedZap = refNote?.tags.some(a => a[0] === "zap") ?? false;
2023-04-11 11:50:58 +00:00
const anonZap = zapRequest.tags.find(a => a[0] === "anon");
2023-03-05 16:58:34 +00:00
const metaHash = sha256(innerZapJson);
2023-04-05 15:10:14 +00:00
const pollOpt = zapRequest.tags.find(a => a[0] === "poll_option")?.[1];
2023-03-05 16:58:34 +00:00
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: [],
2023-04-05 15:10:14 +00:00
pollOption: pollOpt ? Number(pollOpt) : undefined,
2023-03-05 15:36:12 +00:00
};
2023-03-05 16:58:34 +00:00
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) {
2023-03-05 17:54:55 +00:00
ret.valid = false;
ret.errors.push("zap service pubkey doesn't match");
}
2023-03-05 16:58:34 +00:00
return ret;
2023-02-14 14:53:00 +00:00
} catch (e) {
2023-03-09 09:57:49 +00:00
// ignored: console.debug("Invalid zap", zapReceipt, e);
2023-02-03 21:38:14 +00:00
}
}
2023-03-05 16:58:34 +00:00
return {
id: zapReceipt.id,
zapService: zapReceipt.pubkey,
amount: 0,
valid: false,
anonZap: false,
errors: ["invalid zap, parsing failed"],
};
2023-02-03 21:38:14 +00:00
}
2023-02-08 21:10:26 +00:00
export interface ParsedZap {
id: HexKey;
2023-03-05 16:58:34 +00:00
event?: HexKey;
receiver?: HexKey;
amount: number;
2023-03-05 16:58:34 +00:00
content?: string;
sender?: HexKey;
valid: boolean;
2023-02-16 21:53:44 +00:00
zapService: HexKey;
2023-02-18 21:27:06 +00:00
anonZap: boolean;
2023-03-05 16:58:34 +00:00
errors: Array<string>;
2023-04-05 15:10:14 +00:00
pollOption?: number;
2023-02-03 21:38:14 +00:00
}
2023-02-09 12:26:54 +00:00
const Zap = ({ zap, showZapped = true }: { zap: ParsedZap; showZapped?: boolean }) => {
2023-03-05 16:58:34 +00:00
const { amount, content, sender, valid, receiver } = zap;
2023-04-14 11:33:19 +00:00
const pubKey = useLogin().publicKey;
2023-02-03 21:38:14 +00:00
2023-03-05 16:58:34 +00:00
return valid && sender ? (
2023-02-03 23:35:57 +00:00
<div className="zap note card">
<div className="header">
2023-05-10 12:29:07 +00:00
<ProfileImage pubkey={sender} />
{receiver !== pubKey && showZapped && <ProfileImage pubkey={unwrap(receiver)} />}
2023-02-03 21:38:14 +00:00
<div className="amount">
2023-02-08 21:10:26 +00:00
<span className="amount-number">
2023-03-05 16:58:34 +00:00
<FormattedMessage {...messages.Sats} values={{ n: formatShort(amount ?? 0) }} />
2023-02-08 21:10:26 +00:00
</span>
2023-02-03 21:38:14 +00:00
</div>
</div>
2023-03-05 16:58:34 +00:00
{(content?.length ?? 0) > 0 && sender && (
2023-02-08 21:10:26 +00:00
<div className="body">
2023-03-05 16:58:34 +00:00
<Text creator={sender} content={unwrap(content)} tags={[]} />
2023-02-08 21:10:26 +00:00
</div>
)}
2023-02-03 21:38:14 +00:00
</div>
) : null;
};
2023-02-03 21:38:14 +00:00
interface ZapsSummaryProps {
zaps: ParsedZap[];
}
2023-02-03 21:38:14 +00:00
export const ZapsSummary = ({ zaps }: ZapsSummaryProps) => {
2023-02-18 22:42:47 +00:00
const { formatMessage } = useIntl();
2023-02-04 10:28:13 +00:00
const sortedZaps = useMemo(() => {
2023-03-05 16:58:34 +00:00
const pub = [...zaps.filter(z => z.sender && z.valid)];
const priv = [...zaps.filter(z => !z.sender && z.valid)];
pub.sort((a, b) => b.amount - a.amount);
return pub.concat(priv);
}, [zaps]);
2023-02-04 10:28:13 +00:00
if (zaps.length === 0) {
return null;
2023-02-04 10:28:13 +00:00
}
const [topZap, ...restZaps] = sortedZaps;
2023-03-05 16:58:34 +00:00
const { sender, amount, anonZap } = topZap;
2023-02-03 21:38:14 +00:00
return (
<div className="zaps-summary">
2023-02-04 10:28:13 +00:00
{amount && (
2023-02-03 21:38:14 +00:00
<div className={`top-zap`}>
<div className="summary">
2023-03-05 16:58:34 +00:00
{sender && (
2023-02-18 22:42:47 +00:00
<ProfileImage
2023-03-05 16:58:34 +00:00
pubkey={anonZap ? "" : sender}
2023-02-18 22:42:47 +00:00
overrideUsername={anonZap ? formatMessage({ defaultMessage: "Anonymous" }) : undefined}
/>
)}
2023-04-05 01:35:44 +00:00
{restZaps.length > 0 ? (
2023-04-04 16:17:28 +00:00
<FormattedMessage {...messages.Others} values={{ n: restZaps.length }} />
2023-04-05 01:35:44 +00:00
) : (
<FormattedMessage {...messages.Zapped} />
)}{" "}
2023-02-09 12:26:54 +00:00
<FormattedMessage {...messages.OthersZapped} values={{ n: restZaps.length }} />
2023-02-03 21:38:14 +00:00
</div>
</div>
)}
</div>
);
};
2023-02-03 21:38:14 +00:00
export default Zap;