From 4aa96082126e2964c9f041e7d3342e1a25bf817f Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Tue, 15 Oct 2024 00:17:32 +0200 Subject: [PATCH] fixed and add wallet migration --- pages/_app.js | 31 +++++++++++++++++-------------- wallets/index.js | 46 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/pages/_app.js b/pages/_app.js index 6bb9d10d..7980945a 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -22,6 +22,7 @@ import dynamic from 'next/dynamic' import { HasNewNotesProvider } from '@/components/use-has-new-notes' import { WebLnProvider } from '@/wallets/webln/client' import { AccountProvider } from '@/components/account' +import { WalletsMigrator } from '@/wallets/index' const PWAPrompt = dynamic(() => import('react-ios-pwa-prompt'), { ssr: false }) @@ -110,20 +111,22 @@ export default function MyApp ({ Component, pageProps: { ...props } }) { - - - - - - - - {!router?.query?.disablePrompt && } - - - - - - + + + + + + + + + {!router?.query?.disablePrompt && } + + + + + + + diff --git a/wallets/index.js b/wallets/index.js index 72515d39..5406bd4e 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -12,7 +12,7 @@ import { useShowModal } from '@/components/modal' import { useToast } from '../components/toast' import { generateResolverName, isConfigured, isClientField, isServerField } from '@/lib/wallet' import { walletValidate } from '@/lib/validate' - +import { SSR } from '@/lib/constants' export const Status = { Initialized: 'Initialized', Enabled: 'Enabled', @@ -88,6 +88,10 @@ export function useWallet (name) { const save = useCallback(async (newConfig) => { await saveConfig(newConfig, { logger }) + const available = (!walletDef.isAvailable || walletDef.isAvailable()) + logger.ok(_isConfigured() ? 'payment details updated' : 'wallet attached for payments') + if (newConfig.enabled && available) logger.ok('payments enabled') + else logger.ok('payments disabled') }, [saveConfig, me]) // delete is a reserved keyword @@ -130,14 +134,12 @@ export function useWallet (name) { wallet.logger = logger wallet.sendPayment = sendPayment wallet.def = walletDef - logger.ok(walletDef.isConfigured ? 'payment details updated' : 'wallet attached for payments') return wallet }, [walletDef, config, status, enabled, priority, logger, enablePayments, disablePayments, save, delete_, deleteLogs_, setPriority, hasConfig]) useEffect(() => { if (wallet.enabled && wallet.canSend) { disableFreebies().catch(console.error) - logger.ok('payments enabled') } }, [wallet]) @@ -312,8 +314,8 @@ function useConfig (walletDef) { ...newServerConfig, id: currentWallet?.id, settings: { - autoWithdrawThreshold: Number(autoWithdrawThreshold), - autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent), + autoWithdrawThreshold: Number(autoWithdrawThreshold == null ? autowithdrawSettings.autoWithdrawThreshold : autoWithdrawThreshold), + autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent == null ? autowithdrawSettings.autoWithdrawMaxFeePercent : autoWithdrawMaxFeePercent), priority, enabled: enabled && (isReadyToSend || isReadyToReceive) }, @@ -459,3 +461,37 @@ export function useWallets () { return { wallets: walletsReady, resetClient } } + +export function WalletsMigrator ({ children }) { + const { me } = useMe() + const { wallets } = useWallets() + const keys = !SSR ? Object.keys(window.localStorage).filter(k => k.startsWith('wallet:')) : [] + const ran = useRef(false) + useEffect(() => { + if (SSR || !me?.id || !wallets.length) return + if (ran.current) return + ran.current = true + if (!keys?.length) { + console.log('wallet migrator: nothing to migrate', keys) + return + } + const userId = me.id + // List all local storage keys related to wallet settings + const userKeys = keys.filter(k => k.endsWith(`:${userId}`)) + ;(async () => { + for (const key of userKeys) { + const walletType = key.substring('wallet:'.length, key.length - userId.length - 1) + const walletConfig = JSON.parse(window.localStorage.getItem(key)) + const wallet = wallets.find(w => w.def.name === walletType) + if (wallet) { + console.log('Migrating', walletType, walletConfig) + await wallet.save(walletConfig) + window.localStorage.removeItem(key) + } else { + console.warn('No wallet found for', walletType, wallets) + } + } + })() + }, [me, wallets]) + return children +}