From ca16ba170a9e1b51b2b55428e9a97f934c7d1e88 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 10 Sep 2024 16:28:52 +0200 Subject: [PATCH 1/4] Enable WebLN wallet on 'webln:enabled' --- jsconfig.json | 3 +++ pages/_app.js | 39 +++++++++++++++++++++------------------ wallets/index.js | 6 ++---- wallets/webln/index.js | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/jsconfig.json b/jsconfig.json index bb96307cd..9b5c18ac9 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -17,6 +17,9 @@ "@/components/*": [ "components/*" ], + "@/wallets/*": [ + "wallets/*" + ], "@/styles/*": [ "styles/*" ], diff --git a/pages/_app.js b/pages/_app.js index 9161017e6..c018094be 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -21,6 +21,7 @@ import { WalletLoggerProvider } from '@/components/wallet-logger' import { ChainFeeProvider } from '@/components/chain-fee.js' import dynamic from 'next/dynamic' import { HasNewNotesProvider } from '@/components/use-has-new-notes' +import WebLnProvider from '@/wallets/webln' const PWAPrompt = dynamic(() => import('react-ios-pwa-prompt'), { ssr: false }) @@ -106,24 +107,26 @@ export default function MyApp ({ Component, pageProps: { ...props } }) { - - - - - - - - - - {!router?.query?.disablePrompt && } - - - - - - - - + + + + + + + + + + + {!router?.query?.disablePrompt && } + + + + + + + + + diff --git a/wallets/index.js b/wallets/index.js index 5a910f443..ea13a7849 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -420,16 +420,14 @@ function getStorageKey (name, me) { function enableWallet (name, me) { const key = getStorageKey(name, me) - const config = JSON.parse(window.localStorage.getItem(key)) - if (!config) return + const config = JSON.parse(window.localStorage.getItem(key)) || {} config.enabled = true window.localStorage.setItem(key, JSON.stringify(config)) } function disableWallet (name, me) { const key = getStorageKey(name, me) - const config = JSON.parse(window.localStorage.getItem(key)) - if (!config) return + const config = JSON.parse(window.localStorage.getItem(key)) || {} config.enabled = false window.localStorage.setItem(key, JSON.stringify(config)) } diff --git a/wallets/webln/index.js b/wallets/webln/index.js index f1d970af3..ff67d7a57 100644 --- a/wallets/webln/index.js +++ b/wallets/webln/index.js @@ -1,3 +1,6 @@ +import { useEffect } from 'react' +import { useWallet } from 'wallets' + export const name = 'webln' export const fields = [] @@ -19,3 +22,27 @@ export const card = { subtitle: 'use a [WebLN provider](https://www.webln.guide/ressources/webln-providers) for payments', badges: ['send only'] } + +export default function WebLnProvider ({ children }) { + const webLnWallet = useWallet(name) + + useEffect(() => { + const onEnable = () => { + webLnWallet.enablePayments() + } + + const onDisable = () => { + webLnWallet.disablePayments() + } + + window.addEventListener('webln:enabled', onEnable) + // event is not fired by Alby browser extension but added here for sake of completeness + window.addEventListener('webln:disabled', onDisable) + return () => { + window.removeEventListener('webln:enabled', onEnable) + window.removeEventListener('webln:disabled', onDisable) + } + }, []) + + return children +} From 54da0fdb2cd3f57ae611c556ed228625407692a3 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 10 Sep 2024 17:14:50 +0200 Subject: [PATCH 2/4] Optimistically use WebLN for login with lightning --- components/lightning-auth.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/components/lightning-auth.js b/components/lightning-auth.js index 5efddc4cc..4e6c1dbef 100644 --- a/components/lightning-auth.js +++ b/components/lightning-auth.js @@ -27,6 +27,18 @@ function QrAuth ({ k1, encodedUrl, callbackUrl }) { } }, [data?.lnAuth]) + useEffect(() => { + if (typeof window.webln === 'undefined') return + + // optimistically use WebLN for authentication + async function effect () { + // this will also enable our WebLN wallet + await window.webln.enable() + await window.webln.lnurl(encodedUrl) + } + effect() + }, [encodedUrl]) + // output pubkey and k1 return ( From 4c1991f020e30827fb6c09fe72bdbdab3474b3f3 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 10 Sep 2024 17:30:15 +0200 Subject: [PATCH 3/4] Don't scope WebLN config to user --- wallets/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wallets/index.js b/wallets/index.js index ea13a7849..4d1cbf8fc 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -412,9 +412,13 @@ export function useWallets () { function getStorageKey (name, me) { let storageKey = `wallet:${name}` - if (me) { + + // WebLN has no credentials we need to scope to users + // so we can use the same storage key for all users + if (me && name !== 'webln') { storageKey = `${storageKey}:${me.id}` } + return storageKey } From a6bd5db47dfe2bd6fc1764cd931c3640f5855f63 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 10 Sep 2024 17:51:40 +0200 Subject: [PATCH 4/4] Rename var to wallet --- wallets/webln/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wallets/webln/index.js b/wallets/webln/index.js index ff67d7a57..6bfb26d5d 100644 --- a/wallets/webln/index.js +++ b/wallets/webln/index.js @@ -24,15 +24,15 @@ export const card = { } export default function WebLnProvider ({ children }) { - const webLnWallet = useWallet(name) + const wallet = useWallet(name) useEffect(() => { const onEnable = () => { - webLnWallet.enablePayments() + wallet.enablePayments() } const onDisable = () => { - webLnWallet.disablePayments() + wallet.disablePayments() } window.addEventListener('webln:enabled', onEnable)