taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 7d2fb66afe0dbde8edc6d2fe576f84ab1134a9c4
parent cdc80af8b37da3043b773f8f1871d08e093766a9
Author: Florian Dold <dold@taler.net>
Date:   Tue, 17 Nov 2026 20:37:03 +0100

wallet database: preserve contacts across backend migrations

Map IndexedDB contact fields to the names SQLite expects. Preserve the
self-contact flag and optional expiration when copying in either
direction.

Diffstat:
Mpackages/taler-wallet-core/src/db/indexeddb/schema.ts | 5++++-
Mpackages/taler-wallet-core/src/db/indexeddb/transaction.ts | 39++++++++++++++++++++++++++++-----------
Mpackages/taler-wallet-core/src/db/migration/converter.test.ts | 43+++++++++++++++++++++++++++++++++++++++----
Mpackages/taler-wallet-core/src/db/sqlite/transaction.ts | 16+++++++++-------
4 files changed, 80 insertions(+), 23 deletions(-)

diff --git a/packages/taler-wallet-core/src/db/indexeddb/schema.ts b/packages/taler-wallet-core/src/db/indexeddb/schema.ts @@ -511,8 +511,11 @@ export interface ContactRecord { self?: boolean; /** - * Version of the contact. + * Soft expiration of the contact. */ + expiration?: DbProtocolTimestamp; + + /** Obsolete directory version, present in older contact records. */ version?: number; // Reserved legacy fields, migrated by the IndexedDB fixup: diff --git a/packages/taler-wallet-core/src/db/indexeddb/transaction.ts b/packages/taler-wallet-core/src/db/indexeddb/transaction.ts @@ -87,8 +87,10 @@ import { WalletGlobalCurrencyAuditor, WalletBankAccount, WalletExchangeDetails, + timestampProtocolFromDb, + timestampProtocolToDb, } from "../records.js"; -import { WalletIndexedDbTransaction } from "./schema.js"; +import { ContactRecord, WalletIndexedDbTransaction } from "./schema.js"; import type { WalletCurrencyInfoEntry, WalletDbRecordCounts, @@ -113,6 +115,22 @@ function denominationRefMapKey(ref: WalletDenomRef): string { return `${ref.exchangeMasterPub}:${ref.denomPubHash}`; } +/** Use the same contact representation for wallet reads and migration pages. */ +function contactEntryFromRecord(record: ContactRecord): ContactEntry { + const { mailboxBaseUri, self, expiration, version, ...fields } = record; + // Keep unrecognized fields in the migration digest so a lossy copy fails + // verification. Only the obsolete directory version can be discarded. + void version; + return { + ...fields, + mailboxBaseUrl: mailboxBaseUri, + ...(self === undefined ? {} : { isSelf: self }), + ...(expiration === undefined + ? {} + : { expiration: timestampProtocolFromDb(expiration) }), + }; +} + export class IdbWalletTransaction implements WalletDbTransaction { tx: WalletIndexedDbTransaction; constructor(tx: WalletIndexedDbTransaction) { @@ -186,7 +204,10 @@ export class IdbWalletTransaction implements WalletDbTransaction { const accessor = this.migrationStore(store); const page = await accessor.scan(cursor, limit); return { - records: page.records as T[], + records: + store === "contacts" + ? page.records.map(contactEntryFromRecord) + : (page.records as T[]), ...(page.records.length > 0 ? { nextCursor: page.lastKey } : {}), }; } @@ -277,6 +298,10 @@ export class IdbWalletTransaction implements WalletDbTransaction { source: contact.source, petname: contact.petname, self: contact.isSelf, + expiration: + contact.expiration === undefined + ? undefined + : timestampProtocolToDb(contact.expiration), }); } @@ -288,15 +313,7 @@ export class IdbWalletTransaction implements WalletDbTransaction { async listContacts(): Promise<ContactEntry[]> { const tx = this.tx; const records = await tx.contacts.getAll(); - return records.map((r) => ({ - alias: r.alias, - aliasType: r.aliasType, - mailboxBaseUrl: r.mailboxBaseUri, - mailboxAddress: r.mailboxAddress, - source: r.source, - petname: r.petname, - self: r.self, - })); + return records.map(contactEntryFromRecord); } async upsertMailboxMessage(message: MailboxMessageRecord): Promise<void> { diff --git a/packages/taler-wallet-core/src/db/migration/converter.test.ts b/packages/taler-wallet-core/src/db/migration/converter.test.ts @@ -30,6 +30,7 @@ import { test } from "node:test"; import { CancellationToken, CoinStatus, + ContactEntry, DatabaseMaintenanceProgressNotification, DenomKeyType, encodeCrock, @@ -82,8 +83,9 @@ const quietAsserts: ConformanceAsserts = { }, }; -test("IndexedDB fixup migrates legacy contacts before conversion", async () => { +test("IndexedDB fixup migrates legacy contacts before conversion", async (t) => { const src = await makeIdbRunner(); + t.after(() => src.close()); const raw = await (src as IdbWalletDbHandle).rawAccess(); await raw.runAllStoresReadWriteTx({}, async (tx) => { await tx.contacts.put({ @@ -104,6 +106,8 @@ test("IndexedDB fixup migrates legacy contacts before conversion", async () => { mailboxBaseUri: "https://mailbox.example.com/mb/", mailboxAddress: "ALICEPKEY", source: "intermediate", + self: true, + version: 3, } as any); await tx.fixups.delete("fixup20260831LegacyRecordShapes"); }); @@ -119,6 +123,8 @@ test("IndexedDB fixup migrates legacy contacts before conversion", async () => { mailboxBaseUri: "https://mailbox.example.com/mb/", mailboxAddress: "ALICEPKEY", source: "intermediate", + self: true, + version: 3, petname: "alice@example.com", }, { @@ -142,14 +148,43 @@ test("IndexedDB fixup migrates legacy contacts before conversion", async () => { fixedContacts, ); + const expected = fixedContacts.map( + ({ mailboxBaseUri, self, version, ...contact }): ContactEntry => ({ + ...contact, + mailboxBaseUrl: mailboxBaseUri, + ...(self === undefined ? {} : { isSelf: self }), + }), + ); + // Current contacts can also carry an explicit self flag and expiration. + for (const [alias, expiration] of [ + ["carol", 0], + ["dave", 1800000000], + ] as const) { + const entry: ContactEntry = { + ...expected[0], + alias, + isSelf: false, + expiration: { t_s: expiration }, + }; + await src.runReadWriteTx((tx) => tx.addContact(entry)); + expected.push(entry); + } + const dst = await makeSqliteRunner(); + t.after(() => dst.close()); await convertWalletDb(src, dst); assert.deepStrictEqual( await dst.runReadWriteTx((tx) => tx.listContacts()), - fixedContacts, + expected, + ); + + const restored = await makeIdbRunner(); + t.after(() => restored.close()); + await convertWalletDb(dst, restored); + assert.deepStrictEqual( + await restored.runReadWriteTx((tx) => tx.listContacts()), + expected, ); - await src.close(); - await dst.close(); }); test("IndexedDB fixup migrates legacy withdrawal and peer KYC fields", async () => { diff --git a/packages/taler-wallet-core/src/db/sqlite/transaction.ts b/packages/taler-wallet-core/src/db/sqlite/transaction.ts @@ -45,7 +45,6 @@ import { MailboxConfiguration, MailboxMessageRecord, ScopeInfo, - TalerProtocolTimestamp, } from "@gnu-taler/taler-util"; import { checkExchangeInScopeGeneric, @@ -4750,10 +4749,11 @@ export class SqliteWalletTransaction implements WalletDbTransaction { addr: contact.mailboxAddress, src: contact.source, pet: contact.petname, - self: boolToDb(contact.isSelf ?? false), - exp: timestampProtocolToDb( - contact.expiration ?? TalerProtocolTimestamp.zero(), - ), + self: boolToDb(contact.isSelf), + exp: + contact.expiration === undefined + ? null + : timestampProtocolToDb(contact.expiration), }, ); } @@ -4774,8 +4774,10 @@ export class SqliteWalletTransaction implements WalletDbTransaction { mailboxAddress: str(row.mailbox_address), source: str(row.source), petname: str(row.petname), - self: dbToOptBool(row.self), - expiration: timestampProtocolFromDb(dbTimestamp(row.expiration)), + ...(row.self == null ? {} : { isSelf: dbToBool(row.self) }), + ...(row.expiration == null + ? {} + : { expiration: timestampProtocolFromDb(dbTimestamp(row.expiration)) }), })); }