fixed and add wallet migration
This commit is contained in:
parent
6f1113636f
commit
4aa9608212
|
@ -22,6 +22,7 @@ import dynamic from 'next/dynamic'
|
||||||
import { HasNewNotesProvider } from '@/components/use-has-new-notes'
|
import { HasNewNotesProvider } from '@/components/use-has-new-notes'
|
||||||
import { WebLnProvider } from '@/wallets/webln/client'
|
import { WebLnProvider } from '@/wallets/webln/client'
|
||||||
import { AccountProvider } from '@/components/account'
|
import { AccountProvider } from '@/components/account'
|
||||||
|
import { WalletsMigrator } from '@/wallets/index'
|
||||||
|
|
||||||
const PWAPrompt = dynamic(() => import('react-ios-pwa-prompt'), { ssr: false })
|
const PWAPrompt = dynamic(() => import('react-ios-pwa-prompt'), { ssr: false })
|
||||||
|
|
||||||
|
@ -110,20 +111,22 @@ export default function MyApp ({ Component, pageProps: { ...props } }) {
|
||||||
<ServiceWorkerProvider>
|
<ServiceWorkerProvider>
|
||||||
<AccountProvider>
|
<AccountProvider>
|
||||||
<PriceProvider price={price}>
|
<PriceProvider price={price}>
|
||||||
<LightningProvider>
|
<WalletsMigrator>
|
||||||
<ToastProvider>
|
<LightningProvider>
|
||||||
<ShowModalProvider>
|
<ToastProvider>
|
||||||
<BlockHeightProvider blockHeight={blockHeight}>
|
<ShowModalProvider>
|
||||||
<ChainFeeProvider chainFee={chainFee}>
|
<BlockHeightProvider blockHeight={blockHeight}>
|
||||||
<ErrorBoundary>
|
<ChainFeeProvider chainFee={chainFee}>
|
||||||
<Component ssrData={ssrData} {...otherProps} />
|
<ErrorBoundary>
|
||||||
{!router?.query?.disablePrompt && <PWAPrompt copyBody='This website has app functionality. Add it to your home screen to use it in fullscreen and receive notifications. In Safari:' promptOnVisit={2} />}
|
<Component ssrData={ssrData} {...otherProps} />
|
||||||
</ErrorBoundary>
|
{!router?.query?.disablePrompt && <PWAPrompt copyBody='This website has app functionality. Add it to your home screen to use it in fullscreen and receive notifications. In Safari:' promptOnVisit={2} />}
|
||||||
</ChainFeeProvider>
|
</ErrorBoundary>
|
||||||
</BlockHeightProvider>
|
</ChainFeeProvider>
|
||||||
</ShowModalProvider>
|
</BlockHeightProvider>
|
||||||
</ToastProvider>
|
</ShowModalProvider>
|
||||||
</LightningProvider>
|
</ToastProvider>
|
||||||
|
</LightningProvider>
|
||||||
|
</WalletsMigrator>
|
||||||
</PriceProvider>
|
</PriceProvider>
|
||||||
</AccountProvider>
|
</AccountProvider>
|
||||||
</ServiceWorkerProvider>
|
</ServiceWorkerProvider>
|
||||||
|
|
|
@ -12,7 +12,7 @@ import { useShowModal } from '@/components/modal'
|
||||||
import { useToast } from '../components/toast'
|
import { useToast } from '../components/toast'
|
||||||
import { generateResolverName, isConfigured, isClientField, isServerField } from '@/lib/wallet'
|
import { generateResolverName, isConfigured, isClientField, isServerField } from '@/lib/wallet'
|
||||||
import { walletValidate } from '@/lib/validate'
|
import { walletValidate } from '@/lib/validate'
|
||||||
|
import { SSR } from '@/lib/constants'
|
||||||
export const Status = {
|
export const Status = {
|
||||||
Initialized: 'Initialized',
|
Initialized: 'Initialized',
|
||||||
Enabled: 'Enabled',
|
Enabled: 'Enabled',
|
||||||
|
@ -88,6 +88,10 @@ export function useWallet (name) {
|
||||||
|
|
||||||
const save = useCallback(async (newConfig) => {
|
const save = useCallback(async (newConfig) => {
|
||||||
await saveConfig(newConfig, { logger })
|
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])
|
}, [saveConfig, me])
|
||||||
|
|
||||||
// delete is a reserved keyword
|
// delete is a reserved keyword
|
||||||
|
@ -130,14 +134,12 @@ export function useWallet (name) {
|
||||||
wallet.logger = logger
|
wallet.logger = logger
|
||||||
wallet.sendPayment = sendPayment
|
wallet.sendPayment = sendPayment
|
||||||
wallet.def = walletDef
|
wallet.def = walletDef
|
||||||
logger.ok(walletDef.isConfigured ? 'payment details updated' : 'wallet attached for payments')
|
|
||||||
return wallet
|
return wallet
|
||||||
}, [walletDef, config, status, enabled, priority, logger, enablePayments, disablePayments, save, delete_, deleteLogs_, setPriority, hasConfig])
|
}, [walletDef, config, status, enabled, priority, logger, enablePayments, disablePayments, save, delete_, deleteLogs_, setPriority, hasConfig])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (wallet.enabled && wallet.canSend) {
|
if (wallet.enabled && wallet.canSend) {
|
||||||
disableFreebies().catch(console.error)
|
disableFreebies().catch(console.error)
|
||||||
logger.ok('payments enabled')
|
|
||||||
}
|
}
|
||||||
}, [wallet])
|
}, [wallet])
|
||||||
|
|
||||||
|
@ -312,8 +314,8 @@ function useConfig (walletDef) {
|
||||||
...newServerConfig,
|
...newServerConfig,
|
||||||
id: currentWallet?.id,
|
id: currentWallet?.id,
|
||||||
settings: {
|
settings: {
|
||||||
autoWithdrawThreshold: Number(autoWithdrawThreshold),
|
autoWithdrawThreshold: Number(autoWithdrawThreshold == null ? autowithdrawSettings.autoWithdrawThreshold : autoWithdrawThreshold),
|
||||||
autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent),
|
autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent == null ? autowithdrawSettings.autoWithdrawMaxFeePercent : autoWithdrawMaxFeePercent),
|
||||||
priority,
|
priority,
|
||||||
enabled: enabled && (isReadyToSend || isReadyToReceive)
|
enabled: enabled && (isReadyToSend || isReadyToReceive)
|
||||||
},
|
},
|
||||||
|
@ -459,3 +461,37 @@ export function useWallets () {
|
||||||
|
|
||||||
return { wallets: walletsReady, resetClient }
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue