From d462a8d6798a72821631d8e056c4c91ebbd61c8d Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sun, 5 Oct 2025 19:27:58 +0000 Subject: [PATCH 1/6] Remove unneeded webln npm package This change removes the webln package as it serves no specific function that isn't already provided via native browser capability as it relates to WebLN. --- package-lock.json | 46 +++++++++++------------------------------- package.json | 2 +- src/hooks/useWallet.ts | 21 ++++++++++++------- src/hooks/useZaps.ts | 20 ++++++++++++++---- 4 files changed, 43 insertions(+), 46 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3e7b594..5aad45d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -63,7 +63,6 @@ "tailwind-merge": "^2.5.2", "tailwindcss-animate": "^1.0.7", "vaul": "^0.9.3", - "webln": "^0.3.2", "zod": "^3.25.71" }, "devDependencies": { @@ -78,6 +77,7 @@ "@types/react": "^18.3.1", "@types/react-dom": "^18.3.1", "@vitejs/plugin-react-swc": "^3.5.0", + "@webbtc/webln-types": "^3.0.0", "autoprefixer": "^10.4.20", "eslint": "^9.9.0", "eslint-plugin-react-hooks": "^5.1.0-rc.0", @@ -3416,15 +3416,6 @@ "license": "MIT", "peer": true }, - "node_modules/@types/chrome": { - "version": "0.0.74", - "resolved": "https://registry.npmjs.org/@types/chrome/-/chrome-0.0.74.tgz", - "integrity": "sha512-hzosS5CkQcIKCgxcsV2AzbJ36KNxG/Db2YEN/erEu7Boprg+KpMDLBQqKFmSo+JkQMGqRcicUyqCowJpuT+C6A==", - "license": "MIT", - "dependencies": { - "@types/filesystem": "*" - } - }, "node_modules/@types/d3-array": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.1.tgz", @@ -3494,21 +3485,6 @@ "integrity": "sha512-w28IoSUCJpidD/TGviZwwMJckNESJZXFu7NBZ5YJ4mEUnNraUn9Pm8HSZm/jDF1pDWYKspWE7oVphigUPRakIQ==", "license": "MIT" }, - "node_modules/@types/filesystem": { - "version": "0.0.36", - "resolved": "https://registry.npmjs.org/@types/filesystem/-/filesystem-0.0.36.tgz", - "integrity": "sha512-vPDXOZuannb9FZdxgHnqSwAG/jvdGM8Wq+6N4D/d80z+D4HWH+bItqsZaVRQykAn6WEVeEkLm2oQigyHtgb0RA==", - "license": "MIT", - "dependencies": { - "@types/filewriter": "*" - } - }, - "node_modules/@types/filewriter": { - "version": "0.0.33", - "resolved": "https://registry.npmjs.org/@types/filewriter/-/filewriter-0.0.33.tgz", - "integrity": "sha512-xFU8ZXTw4gd358lb2jw25nxY9QAgqn2+bKKjKOYfNCzN4DKCFetK7sPtrlpg66Ywe3vWY9FNxprZawAh9wfJ3g==", - "license": "MIT" - }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -3929,6 +3905,17 @@ "url": "https://opencollective.com/vitest" } }, + "node_modules/@webbtc/webln-types": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@webbtc/webln-types/-/webln-types-3.0.0.tgz", + "integrity": "sha512-aXfTHLKz5lysd+6xTeWl+qHNh/p3qVYbeLo+yDN5cUDmhie2ZoGvkppfWxzbGkcFBzb6dJyQ2/i2cbmDHas+zQ==", + "dev": true, + "license": "MIT", + "funding": { + "type": "lightning", + "url": "lightning:hello@getalby.com" + } + }, "node_modules/acorn": { "version": "8.15.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", @@ -8011,15 +7998,6 @@ "node": ">=12" } }, - "node_modules/webln": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/webln/-/webln-0.3.2.tgz", - "integrity": "sha512-YYT83aOCLup2AmqvJdKtdeBTaZpjC6/JDMe8o6x1kbTYWwiwrtWHyO//PAsPixF3jwFsAkj5DmiceB6w/QSe7Q==", - "license": "MIT", - "dependencies": { - "@types/chrome": "^0.0.74" - } - }, "node_modules/webpack-virtual-modules": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.6.2.tgz", diff --git a/package.json b/package.json index cfa97dd..a2b34ac 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,6 @@ "tailwind-merge": "^2.5.2", "tailwindcss-animate": "^1.0.7", "vaul": "^0.9.3", - "webln": "^0.3.2", "zod": "^3.25.71" }, "devDependencies": { @@ -80,6 +79,7 @@ "@types/react": "^18.3.1", "@types/react-dom": "^18.3.1", "@vitejs/plugin-react-swc": "^3.5.0", + "@webbtc/webln-types": "^3.0.0", "autoprefixer": "^10.4.20", "eslint": "^9.9.0", "eslint-plugin-react-hooks": "^5.1.0-rc.0", diff --git a/src/hooks/useWallet.ts b/src/hooks/useWallet.ts index 8030dd2..b1514da 100644 --- a/src/hooks/useWallet.ts +++ b/src/hooks/useWallet.ts @@ -1,7 +1,6 @@ import { useState, useEffect, useCallback, useMemo } from 'react'; import { useNWC } from '@/hooks/useNWCContext'; -import type { WebLNProvider } from 'webln'; -import { requestProvider } from 'webln'; +import type { WebLNProvider } from '@webbtc/webln-types'; export interface WalletStatus { hasWebLN: boolean; @@ -27,10 +26,15 @@ export function useWallet() { setIsDetecting(true); try { - const provider = await requestProvider(); - setWebln(provider); - setHasAttemptedDetection(true); - return provider; + // Check if native WebLN is available without enabling it yet + const webLn = (window as Window & { webln?: WebLNProvider }).webln; + if (webLn) { + setWebln(webLn); + setHasAttemptedDetection(true); + return webLn; + } else { + throw new Error('no WebLN provider'); + } } catch (error) { // Only log the error if it's not the common "no provider" error if (error instanceof Error && !error.message.includes('no WebLN provider')) { @@ -56,7 +60,10 @@ export function useWallet() { if (!webln) return false; try { - await webln.enable(); + // Test the connection with a getInfo call if available, otherwise just check if provider exists + if (webln.getInfo) { + await webln.getInfo(); + } return true; } catch (error) { console.error('WebLN test failed:', error); diff --git a/src/hooks/useZaps.ts b/src/hooks/useZaps.ts index 4ed7162..1bfeb2b 100644 --- a/src/hooks/useZaps.ts +++ b/src/hooks/useZaps.ts @@ -7,7 +7,7 @@ import { useNWC } from '@/hooks/useNWCContext'; import type { NWCConnection } from '@/hooks/useNWC'; import { nip57 } from 'nostr-tools'; import type { Event } from 'nostr-tools'; -import type { WebLNProvider } from 'webln'; +import type { WebLNProvider } from '@webbtc/webln-types'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useNostr } from '@nostrify/react'; import type { NostrEvent } from '@nostrify/nostrify'; @@ -263,10 +263,22 @@ export function useZaps( }); } } - + if (webln) { // Try WebLN next try { - await webln.sendPayment(newInvoice); + // For native WebLN, we may need to enable it first + let webLnProvider = webln; + if (webln.enable && typeof webln.enable === 'function') { + const enabledProvider = await webln.enable(); + // Some implementations return the provider, others return void + // Cast to WebLNProvider to handle both cases + const provider = enabledProvider as WebLNProvider | undefined; + if (provider) { + webLnProvider = provider; + } + } + + await webLnProvider.sendPayment(newInvoice); // Clear states immediately on success setIsZapping(false); @@ -283,7 +295,7 @@ export function useZaps( // Close dialog last to ensure clean state onZapSuccess?.(); } catch (weblnError) { - console.error('webln payment failed, falling back:', weblnError); + console.error('WebLN payment failed, falling back:', weblnError); // Show specific WebLN error to user for debugging const errorMessage = weblnError instanceof Error ? weblnError.message : 'Unknown WebLN error'; From 45cdcecf9ecfca44150361051f10ff66a31bbac6 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sun, 5 Oct 2025 19:49:46 +0000 Subject: [PATCH 2/6] Review fixes --- src/components/WalletModal.tsx | 7 ++-- src/components/ZapDialog.tsx | 10 +----- src/hooks/useWallet.ts | 64 +++------------------------------- 3 files changed, 8 insertions(+), 73 deletions(-) diff --git a/src/components/WalletModal.tsx b/src/components/WalletModal.tsx index 7643ea5..c8f26dc 100644 --- a/src/components/WalletModal.tsx +++ b/src/components/WalletModal.tsx @@ -69,7 +69,6 @@ AddWalletContent.displayName = 'AddWalletContent'; // Extracted WalletContent to prevent re-renders const WalletContent = forwardRef; @@ -79,7 +78,6 @@ const WalletContent = forwardRef void; }>(({ hasWebLN, - isDetecting, hasNWC, connections, connectionInfo, @@ -105,7 +103,7 @@ const WalletContent = forwardRef {hasWebLN && } - {isDetecting ? "..." : hasWebLN ? "Ready" : "Not Found"} + {hasWebLN ? "Ready" : "Not Found"} @@ -222,7 +220,7 @@ export function WalletModal({ children, className }: WalletModalProps) { setActiveConnection } = useNWC(); - const { hasWebLN, isDetecting } = useWallet(); + const { hasWebLN } = useWallet(); const hasNWC = connections.length > 0 && connections.some(c => c.isConnected); const { toast } = useToast(); @@ -264,7 +262,6 @@ export function WalletModal({ children, className }: WalletModalProps) { const walletContentProps = { hasWebLN, - isDetecting, hasNWC, connections, connectionInfo, diff --git a/src/components/ZapDialog.tsx b/src/components/ZapDialog.tsx index 39e4207..a7b2481 100644 --- a/src/components/ZapDialog.tsx +++ b/src/components/ZapDialog.tsx @@ -234,13 +234,12 @@ const ZapContent = forwardRef(({ )); ZapContent.displayName = 'ZapContent'; - export function ZapDialog({ target, children, className }: ZapDialogProps) { const [open, setOpen] = useState(false); const { user } = useCurrentUser(); const { data: author } = useAuthor(target.pubkey); const { toast } = useToast(); - const { webln, activeNWC, hasWebLN, detectWebLN } = useWallet(); + const { webln, activeNWC, hasWebLN } = useWallet(); const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, activeNWC, () => setOpen(false)); const [amount, setAmount] = useState(100); const [comment, setComment] = useState(''); @@ -255,13 +254,6 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) { } }, [target]); - // Detect WebLN when dialog opens - useEffect(() => { - if (open && !hasWebLN) { - detectWebLN(); - } - }, [open, hasWebLN, detectWebLN]); - // Generate QR code useEffect(() => { let isCancelled = false; diff --git a/src/hooks/useWallet.ts b/src/hooks/useWallet.ts index b1514da..25448e5 100644 --- a/src/hooks/useWallet.ts +++ b/src/hooks/useWallet.ts @@ -7,69 +7,21 @@ export interface WalletStatus { hasNWC: boolean; webln: WebLNProvider | null; activeNWC: ReturnType['getActiveConnection'] extends () => infer T ? T : null; - isDetecting: boolean; preferredMethod: 'nwc' | 'webln' | 'manual'; } export function useWallet() { const [webln, setWebln] = useState(null); - const [isDetecting, setIsDetecting] = useState(false); - const [hasAttemptedDetection, setHasAttemptedDetection] = useState(false); const { connections, getActiveConnection } = useNWC(); // Get the active connection directly - no memoization to avoid stale state const activeNWC = getActiveConnection(); - // Detect WebLN - const detectWebLN = useCallback(async () => { - if (webln || isDetecting) return webln; - - setIsDetecting(true); - try { - // Check if native WebLN is available without enabling it yet - const webLn = (window as Window & { webln?: WebLNProvider }).webln; - if (webLn) { - setWebln(webLn); - setHasAttemptedDetection(true); - return webLn; - } else { - throw new Error('no WebLN provider'); - } - } catch (error) { - // Only log the error if it's not the common "no provider" error - if (error instanceof Error && !error.message.includes('no WebLN provider')) { - console.warn('WebLN detection error:', error); - } - setWebln(null); - setHasAttemptedDetection(true); - return null; - } finally { - setIsDetecting(false); - } - }, [webln, isDetecting]); - - // Only auto-detect once on mount + // Detect WebLN synchronously on mount useEffect(() => { - if (!hasAttemptedDetection) { - detectWebLN(); - } - }, [detectWebLN, hasAttemptedDetection]); - - // Test WebLN connection - const testWebLN = useCallback(async (): Promise => { - if (!webln) return false; - - try { - // Test the connection with a getInfo call if available, otherwise just check if provider exists - if (webln.getInfo) { - await webln.getInfo(); - } - return true; - } catch (error) { - console.error('WebLN test failed:', error); - return false; - } - }, [webln]); + const webLn = (globalThis as { webln?: WebLNProvider }).webln; + setWebln(webLn || null); + }, []); // Calculate status values reactively const hasNWC = useMemo(() => { @@ -88,14 +40,8 @@ export function useWallet() { hasNWC, webln, activeNWC, - isDetecting, preferredMethod, }; - return { - ...status, - hasAttemptedDetection, - detectWebLN, - testWebLN, - }; + return status; } \ No newline at end of file From b77885093c04d300d2a11fa1ef7ffd9ca67be1ec Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sun, 5 Oct 2025 20:48:58 +0000 Subject: [PATCH 3/6] Remove webln from state, simplify webln detection. --- src/components/WalletModal.tsx | 16 ++++++++-------- src/components/ZapDialog.tsx | 10 +++++----- src/hooks/useShakespeare.ts | 32 ++++++++++++++++---------------- src/hooks/useWallet.ts | 12 +++--------- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/components/WalletModal.tsx b/src/components/WalletModal.tsx index c8f26dc..ef77855 100644 --- a/src/components/WalletModal.tsx +++ b/src/components/WalletModal.tsx @@ -68,7 +68,7 @@ AddWalletContent.displayName = 'AddWalletContent'; // Extracted WalletContent to prevent re-renders const WalletContent = forwardRef; @@ -77,7 +77,7 @@ const WalletContent = forwardRef void; setAddDialogOpen: (open: boolean) => void; }>(({ - hasWebLN, + webln, hasNWC, connections, connectionInfo, @@ -101,9 +101,9 @@ const WalletContent = forwardRef
- {hasWebLN && } - - {hasWebLN ? "Ready" : "Not Found"} + {webln && } + + {webln ? "Ready" : "Not Found"}
@@ -189,7 +189,7 @@ const WalletContent = forwardRef {/* Help */} - {!hasWebLN && connections.length === 0 && ( + {!webln && connections.length === 0 && ( <>
@@ -220,7 +220,7 @@ export function WalletModal({ children, className }: WalletModalProps) { setActiveConnection } = useNWC(); - const { hasWebLN } = useWallet(); + const { webln } = useWallet(); const hasNWC = connections.length > 0 && connections.some(c => c.isConnected); const { toast } = useToast(); @@ -261,7 +261,7 @@ export function WalletModal({ children, className }: WalletModalProps) { }; const walletContentProps = { - hasWebLN, + webln, hasNWC, connections, connectionInfo, diff --git a/src/components/ZapDialog.tsx b/src/components/ZapDialog.tsx index a7b2481..934dfa6 100644 --- a/src/components/ZapDialog.tsx +++ b/src/components/ZapDialog.tsx @@ -55,7 +55,7 @@ interface ZapContentProps { isZapping: boolean; qrCodeUrl: string; copied: boolean; - hasWebLN: boolean; + webln: import('@webbtc/webln-types').WebLNProvider | null; handleZap: () => void; handleCopy: () => void; openInWallet: () => void; @@ -73,7 +73,7 @@ const ZapContent = forwardRef(({ isZapping, qrCodeUrl, copied, - hasWebLN, + webln, handleZap, handleCopy, openInWallet, @@ -138,7 +138,7 @@ const ZapContent = forwardRef(({ {/* Payment buttons */}
- {hasWebLN && ( + {webln && (