From e0f4f29bc99372c22b6c1bfa09dcba2d85f3b608 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 27 Mar 2024 19:45:16 +0100 Subject: wallet-core: purge other database tables when purging an exchange --- packages/taler-wallet-core/src/db.ts | 5 +- packages/taler-wallet-core/src/exchanges.ts | 102 ++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 13 deletions(-) (limited to 'packages') diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts index 92cf63ae1..a587363c5 100644 --- a/packages/taler-wallet-core/src/db.ts +++ b/packages/taler-wallet-core/src/db.ts @@ -149,7 +149,7 @@ export const CURRENT_DB_CONFIG_KEY = "currentMainDbName"; * backwards-compatible way or object stores and indices * are added. */ -export const WALLET_DB_MINOR_VERSION = 7; +export const WALLET_DB_MINOR_VERSION = 8; declare const symDbProtocolTimestamp: unique symbol; @@ -2420,6 +2420,9 @@ export const WalletStoresV1 = { "maxAge", "freshCoinCount", ]), + byExchangeBaseUrl: describeIndex("byExchangeBaseUrl", "exchangeBaseUrl", { + versionAdded: 8, + }), }, ), coins: describeStore( diff --git a/packages/taler-wallet-core/src/exchanges.ts b/packages/taler-wallet-core/src/exchanges.ts index 152bc76ce..91d436100 100644 --- a/packages/taler-wallet-core/src/exchanges.ts +++ b/packages/taler-wallet-core/src/exchanges.ts @@ -2093,6 +2093,84 @@ async function internalGetExchangeResources( }; } +async function purgeExchange( + tx: WalletDbReadWriteTransaction< + [ + "exchanges", + "exchangeDetails", + "transactions", + "coinAvailability", + "coins", + "denominations", + "exchangeSignKeys", + "withdrawalGroups", + "planchets", + ] + >, + exchangeBaseUrl: string, +): Promise { + const detRecs = await tx.exchangeDetails.indexes.byExchangeBaseUrl.getAll(); + for (const r of detRecs) { + if (r.rowId == null) { + // Should never happen, as rowId is the primary key. + continue; + } + await tx.exchangeDetails.delete(r.rowId); + const signkeyRecs = + await tx.exchangeSignKeys.indexes.byExchangeDetailsRowId.getAll(r.rowId); + for (const rec of signkeyRecs) { + await tx.exchangeSignKeys.delete([r.rowId, rec.signkeyPub]); + } + } + // FIXME: Also remove records related to transactions? + await tx.exchanges.delete(exchangeBaseUrl); + + { + const coinAvailabilityRecs = + await tx.coinAvailability.indexes.byExchangeBaseUrl.getAll( + exchangeBaseUrl, + ); + for (const rec of coinAvailabilityRecs) { + await tx.coinAvailability.delete([ + exchangeBaseUrl, + rec.denomPubHash, + rec.maxAge, + ]); + } + } + + { + const coinRecs = await tx.coins.indexes.byBaseUrl.getAll(exchangeBaseUrl); + for (const rec of coinRecs) { + await tx.coins.delete(rec.coinPub); + } + } + + { + const denomRecs = + await tx.denominations.indexes.byExchangeBaseUrl.getAll(exchangeBaseUrl); + for (const rec of denomRecs) { + await tx.denominations.delete(rec.denomPubHash); + } + } + + { + const withdrawalGroupRecs = + await tx.withdrawalGroups.indexes.byExchangeBaseUrl.getAll( + exchangeBaseUrl, + ); + for (const wg of withdrawalGroupRecs) { + await tx.withdrawalGroups.delete(wg.withdrawalGroupId); + const planchets = await tx.planchets.indexes.byGroup.getAll( + wg.withdrawalGroupId, + ); + for (const p of planchets) { + await tx.planchets.delete(p.coinPub); + } + } + } +} + export async function deleteExchange( wex: WalletExecutionContext, req: DeleteExchangeRequest, @@ -2100,7 +2178,17 @@ export async function deleteExchange( let inUse: boolean = false; const exchangeBaseUrl = canonicalizeBaseUrl(req.exchangeBaseUrl); await wex.db.runReadWriteTx( - ["exchanges", "coins", "withdrawalGroups", "exchangeDetails"], + [ + "exchanges", + "exchangeDetails", + "transactions", + "coinAvailability", + "coins", + "denominations", + "exchangeSignKeys", + "withdrawalGroups", + "planchets", + ], async (tx) => { const exchangeRec = await tx.exchanges.get(exchangeBaseUrl); if (!exchangeRec) { @@ -2113,17 +2201,7 @@ export async function deleteExchange( inUse = true; return; } - const detRecs = - await tx.exchangeDetails.indexes.byExchangeBaseUrl.getAll(); - for (const r of detRecs) { - if (r.rowId == null) { - // Should never happen, as rowId is the primary key. - continue; - } - await tx.exchangeDetails.delete(r.rowId); - } - // FIXME: Also remove records related to transactions? - await tx.exchanges.delete(exchangeBaseUrl); + await purgeExchange(tx, exchangeBaseUrl); }, ); -- cgit v1.2.3