From de0eb8a52c15283e9c3f6c9b749cc8b6ee8654ed Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 16 Oct 2024 12:48:05 +0200 Subject: [PATCH] ensure that wallets are configured to send and/or receive --- api/resolvers/wallet.js | 1 + wallets/index.js | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js index 11914f76..35c09057 100644 --- a/api/resolvers/wallet.js +++ b/api/resolvers/wallet.js @@ -48,6 +48,7 @@ function injectResolvers (resolvers) { } } + if (!canReceive && !canSend) throw new GqlInputError('wallet must be able to send or receive') return await upsertWallet({ wallet: { field: diff --git a/wallets/index.js b/wallets/index.js index 1e1e822f..4cfdefc5 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -313,6 +313,8 @@ function useConfig (walletDef) { } // set server config (will create wallet if it doesn't exist) (it is also testing receive config) + if (!isReadyToSend && !isReadyToReceive) throw new Error('wallet should be configured to send or receive payments') + const mutation = generateMutation(walletDef) const variables = { ...newServerConfig, @@ -321,7 +323,7 @@ function useConfig (walletDef) { autoWithdrawThreshold: Number(autoWithdrawThreshold == null ? autowithdrawSettings.autoWithdrawThreshold : autoWithdrawThreshold), autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent == null ? autowithdrawSettings.autoWithdrawMaxFeePercent : autoWithdrawMaxFeePercent), priority, - enabled: enabled && (isReadyToSend || isReadyToReceive) + enabled }, canSend: isReadyToSend, canReceive: isReadyToReceive, @@ -503,15 +505,20 @@ export function WalletProvider ({ children }) { const userKeys = migratableKeys.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) + try { + 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) + } + } catch (e) { window.localStorage.removeItem(key) - } else { - console.warn('No wallet found for', walletType, wallets) + console.error('Failed to migrate wallet', key, e) } } })()