feat: zap type

This commit is contained in:
Kieran 2023-02-18 20:36:42 +00:00
parent 05a363491f
commit b5d7e2c58a
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 51 additions and 1 deletions

View File

@ -36,6 +36,13 @@ interface LNURLSuccessAction {
url?: string;
}
enum ZapType {
PublicZap = 1,
AnonZap = 2,
PrivateZap = 3,
NonZap = 4,
}
export interface LNURLTipProps {
onClose?: () => void;
svc?: string;
@ -82,6 +89,7 @@ export default function LNURLTip(props: LNURLTipProps) {
const [comment, setComment] = useState<string>();
const [error, setError] = useState<string>();
const [success, setSuccess] = useState<LNURLSuccessAction>();
const [zapType, setZapType] = useState(ZapType.PublicZap);
const webln = useWebln(show);
const { formatMessage } = useIntl();
const publisher = useEventPublisher();
@ -99,6 +107,7 @@ export default function LNURLTip(props: LNURLTipProps) {
setAmount(500);
setComment(undefined);
setSuccess(undefined);
setZapType(ZapType.PublicZap);
}
}, [show, service]);
@ -161,9 +170,17 @@ export default function LNURLTip(props: LNURLTipProps) {
if (comment && payService?.commentAllowed) {
query.set("comment", comment);
}
if (payService.nostrPubkey && author) {
if (payService.nostrPubkey && author && zapType !== ZapType.NonZap) {
const ev = await publisher.zap(author, note, comment);
if (ev) {
// replace sig for anon-zap
if (zapType === ZapType.AnonZap) {
const randomKey = publisher.newKey();
console.debug("Generated new key for zap: ", randomKey);
ev.PubKey = randomKey.publicKey;
ev.Id = "";
await ev.Sign(randomKey.privateKey);
}
query.set("nostr", JSON.stringify(ev.ToObject()));
}
}
@ -263,6 +280,7 @@ export default function LNURLTip(props: LNURLTipProps) {
/>
)}
</div>
{zapTypeSelector()}
{(amount ?? 0) > 0 && (
<button type="button" className="zap-action" onClick={() => loadInvoice()}>
<div className="zap-action-container">
@ -279,6 +297,29 @@ export default function LNURLTip(props: LNURLTipProps) {
);
}
function zapTypeSelector() {
if (!payService || !payService.nostrPubkey) return;
const makeTab = (t: ZapType, n: string) => (
<div className={`tab${zapType === t ? " active" : ""}`} onClick={() => setZapType(t)}>
{n}
</div>
);
return (
<>
<h3>
<FormattedMessage defaultMessage="Zap Type" />
</h3>
<div className="tabs mt10">
{makeTab(ZapType.PublicZap, "Public")}
{/*makeTab(ZapType.PrivateZap, "Private")*/}
{makeTab(ZapType.AnonZap, "Anon")}
{makeTab(ZapType.NonZap, "Non-Zap")}
</div>
</>
);
}
function payInvoice() {
if (success) return null;
const pr = invoice?.pr;

View File

@ -1,4 +1,5 @@
import { useSelector } from "react-redux";
import * as secp from "@noble/secp256k1";
import { TaggedRawEvent } from "@snort/nostr";
import { EventKind, Tag, Event as NEvent, System, RelaySettings } from "@snort/nostr";
@ -382,6 +383,14 @@ export default function useEventPublisher() {
}
}
},
newKey: () => {
const privKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
const pubKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
return {
privateKey: privKey,
publicKey: pubKey,
};
},
};
}