Skip to content

Commit

Permalink
fix removing server config on unsynced client vault
Browse files Browse the repository at this point in the history
  • Loading branch information
huumn committed Nov 3, 2024
1 parent 25facad commit fb65ea3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
17 changes: 16 additions & 1 deletion api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@ function injectResolvers (resolvers) {
console.log(resolverName)
resolvers.Mutation[resolverName] = async (parent, { settings, validateLightning, vaultEntries, ...data }, { me, models }) => {
console.log('resolving', resolverName, { settings, validateLightning, vaultEntries, ...data })
const validData = await validateWallet(walletDef, { ...data, ...settings, vaultEntries }, { serverSide: true })

let existingVaultEntries
if (typeof vaultEntries === 'undefined' && data.id) {
// this mutation was sent from an unsynced client
// to pass validation, we need to add the existing vault entries for validation
// in case the client is removing the receiving config
existingVaultEntries = await models.vaultEntry.findMany({
where: {
walletId: Number(data.id)
}
})
}

const validData = await validateWallet(walletDef,
{ ...data, ...settings, vaultEntries: vaultEntries ?? existingVaultEntries },
{ serverSide: true })
if (validData) {
data && Object.keys(validData).filter(key => key in data).forEach(key => { data[key] = validData[key] })
settings && Object.keys(validData).filter(key => key in settings).forEach(key => { settings[key] = validData[key] })
Expand Down
10 changes: 8 additions & 2 deletions wallets/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ export function useWalletConfigurator (wallet) {
if (canReceive({ def: wallet.def, config: serverConfig })) {
await _saveToServer(serverConfig, clientConfig, validateLightning)
} else if (wallet.config.id) {
// if it previously had a server config, remove it
await _detachFromServer()
// we previously had a server config
if (wallet.vaultEntries.length > 0) {
// we previously had a server config with vault entries, save it
await _saveToServer(serverConfig, clientConfig, validateLightning)
} else {
// we previously had a server config without vault entries, remove it
await _detachFromServer()
}
}
}
}, [isActive, wallet.def, _saveToServer, _saveToLocal, _validate,
Expand Down
5 changes: 3 additions & 2 deletions wallets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function WalletsProvider ({ children }) {

// the specific wallet config on the server is stored in wallet.wallet
// on the client, it's stored unnested
wallets.push({ config: { ...config, ...w.wallet }, def })
wallets.push({ config: { ...config, ...w.wallet }, def, vaultEntries })
}

setServerWallets(wallets)
Expand All @@ -109,7 +109,8 @@ export function WalletsProvider ({ children }) {
value ?? merged[wallet.def.name]?.config?.[key]
])
)
}
},
vaultEntries: wallet.vaultEntries
}
}

Expand Down

0 comments on commit fb65ea3

Please sign in to comment.