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

175 lines
5.2 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-02-03 23:45:04 +00:00
import { useSelector } from "react-redux";
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-02-03 23:45:04 +00:00
import { RootState } from "State/Store";
2023-03-05 15:36:12 +00:00
import { ZapperSpam } from "Const";
2023-02-03 21:38:14 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-02-03 21:38:14 +00:00
function findTag(e: TaggedRawEvent, tag: string) {
2023-02-09 12:26:54 +00:00
const maybeTag = e.tags.find(evTag => {
return evTag[0] === tag;
});
return maybeTag && maybeTag[1];
2023-02-03 21:38:14 +00:00
}
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
}
2023-03-05 16:58:34 +00:00
export function parseZap(zapReceipt: TaggedRawEvent): ParsedZap {
const invoice = getInvoice(zapReceipt);
let innerZapJson = findTag(zapReceipt, "description");
if (innerZapJson) {
2023-02-14 14:53:00 +00:00
try {
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
}
2023-03-05 16:58:34 +00:00
const anonZap = findTag(zapRequest, "anon");
const metaHash = sha256(innerZapJson);
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-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 (ZapperSpam.includes(zapReceipt.pubkey)) {
ret.valid = false;
ret.errors.push("zapper is banned");
}
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 (!ret.valid) {
console.debug("Invalid zap", ret);
}
return ret;
2023-02-14 14:53:00 +00:00
} catch (e) {
2023-03-05 16:58:34 +00:00
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-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;
const pubKey = useSelector((s: RootState) => s.login.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-03-05 16:58:34 +00:00
<ProfileImage autoWidth={false} pubkey={sender} />
{receiver !== pubKey && showZapped && <ProfileImage autoWidth={false} 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
autoWidth={false}
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-02-09 12:26:54 +00:00
{restZaps.length > 0 && <FormattedMessage {...messages.Others} values={{ n: restZaps.length }} />}{" "}
<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;