From 5ab2b99424194bf204ecd72de794e469c8ea6ddc Mon Sep 17 00:00:00 2001 From: mroxso <24775431+mroxso@users.noreply.github.com> Date: Mon, 21 Apr 2025 14:13:44 +0200 Subject: [PATCH] fix: refactor zap request signing logic and add placeholders for id and sig (#90) Co-authored-by: highperfocused --- components/ZapButton.tsx | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/components/ZapButton.tsx b/components/ZapButton.tsx index 8423e23..590bbc7 100644 --- a/components/ZapButton.tsx +++ b/components/ZapButton.tsx @@ -20,9 +20,15 @@ import { useEffect, useState, useRef } from "react"; import QRCode from "react-qr-code"; import Link from "next/link"; import { Alert } from "./ui/alert"; +import { signEvent } from "@/utils/utils"; export default function ZapButton({ event }: { event: any }) { + let loginType = ''; + if (typeof window !== 'undefined') { + loginType = window.localStorage.getItem("loginType") ?? ''; + } + const { connectedRelays } = useNostr(); const { events, isLoading } = useNostrEvents({ @@ -219,6 +225,8 @@ export default function ZapButton({ event }: { event: any }) { ], created_at: Math.floor(Date.now() / 1000), pubkey: senderPubkey, + id: "", // Add placeholder for id + sig: "", // Add placeholder for sig }; let params = new URLSearchParams(); @@ -228,28 +236,7 @@ export default function ZapButton({ event }: { event: any }) { zapRequestEvent.tags.push(["lnurl", userData.lud06]); } - const signEvent = async () => { - if (window.nostr) { - return await window.nostr.signEvent(zapRequestEvent); - } else { - const nsecHex = window.localStorage.getItem("nsec"); - if (nsecHex) { - try { - const decoded = nip19.decode(nsecHex); - if (decoded.type === 'nsec') { - const privateKey = decoded.data as Uint8Array; - return finalizeEvent(zapRequestEvent, privateKey); - } - } catch (error) { - console.error("Error decoding private key:", error); - throw new Error("Invalid private key format"); - } - } - throw new Error("No private key available to sign the zap request"); - } - }; - - const signedZapRequest = await signEvent(); + const signedZapRequest = await signEvent(loginType, zapRequestEvent); params.append('nostr', JSON.stringify(signedZapRequest)); if (userData?.lud06) {