feat: tag anon zaps

This commit is contained in:
Kieran 2023-02-18 21:27:06 +00:00
parent b5d7e2c58a
commit 4c1513676e
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
4 changed files with 20 additions and 8 deletions

View File

@ -20,6 +20,7 @@ export interface ProfileImageProps {
defaultNip?: string;
verifyNip?: boolean;
linkToProfile?: boolean;
overrideUsername?: string;
}
export default function ProfileImage({
@ -32,6 +33,7 @@ export default function ProfileImage({
defaultNip,
verifyNip,
linkToProfile = true,
overrideUsername,
}: ProfileImageProps) {
const navigate = useNavigate();
const user = useUserProfile(pubkey);
@ -39,8 +41,8 @@ export default function ProfileImage({
const width = usePageWidth();
const name = useMemo(() => {
return getDisplayName(user, pubkey);
}, [user, pubkey]);
return overrideUsername ?? getDisplayName(user, pubkey);
}, [user, pubkey, overrideUsername]);
if (!pubkey && !link) {
link = "#";

View File

@ -110,7 +110,12 @@ const Reactions = ({ show, setShow, positive, negative, reposts, zaps }: Reactio
<ZapIcon width={17} height={20} />
<span className="zap-amount">{formatShort(z.amount)}</span>
</div>
<ProfileImage autoWidth={false} pubkey={z.zapper} subHeader={<>{z.content}</>} />
<ProfileImage
autoWidth={false}
pubkey={z.anonZap ? "" : z.zapper}
subHeader={<>{z.content}</>}
overrideUsername={z.anonZap ? formatMessage({ defaultMessage: "Anonymous" }) : undefined}
/>
</div>
)
);

View File

@ -4,7 +4,7 @@ import { useIntl, FormattedMessage } from "react-intl";
import { formatShort } from "Number";
import { bech32ToText } from "Util";
import { HexKey } from "@snort/nostr";
import { HexKey, Tag } from "@snort/nostr";
import Check from "Icons/Check";
import Zap from "Icons/Zap";
import Close from "Icons/Close";
@ -179,6 +179,7 @@ export default function LNURLTip(props: LNURLTipProps) {
console.debug("Generated new key for zap: ", randomKey);
ev.PubKey = randomKey.publicKey;
ev.Id = "";
ev.Tags.push(new Tag(["anon"], ev.Tags.length));
await ev.Sign(randomKey.privateKey);
}
query.set("nostr", JSON.stringify(ev.ToObject()));

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,
};
}