From 3a2b96f3c2dcdf106c989713253a8b3679ae078e Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 14 Oct 2022 22:56:29 +0200 Subject: [PATCH] wallet-core: address DB FIXME --- packages/taler-wallet-core/src/db.ts | 9 +----- .../src/operations/pay-peer.ts | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts index 72dd0dc9c..002db9805 100644 --- a/packages/taler-wallet-core/src/db.ts +++ b/packages/taler-wallet-core/src/db.ts @@ -500,11 +500,6 @@ export interface ExchangeDetailsPointer { updateClock: TalerProtocolTimestamp; } -export interface MergeReserveInfo { - reservePub: string; - reservePriv: string; -} - /** * Exchange record as stored in the wallet's database. */ @@ -555,10 +550,8 @@ export interface ExchangeRecord { /** * Public key of the reserve that we're currently using for * receiving P2P payments. - * - * FIXME: Make this a rowId of reserves! */ - currentMergeReserveInfo?: MergeReserveInfo; + currentMergeReserveRowId?: number; } export enum PlanchetStatus { diff --git a/packages/taler-wallet-core/src/operations/pay-peer.ts b/packages/taler-wallet-core/src/operations/pay-peer.ts index 8bce887c0..ffc49c24c 100644 --- a/packages/taler-wallet-core/src/operations/pay-peer.ts +++ b/packages/taler-wallet-core/src/operations/pay-peer.ts @@ -64,10 +64,10 @@ import { } from "@gnu-taler/taler-util"; import { CoinStatus, - MergeReserveInfo, WithdrawalGroupStatus, WalletStoresV1, WithdrawalRecordType, + ReserveRecord, } from "../db.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { readSuccessResponseJsonOrThrow } from "../util/http.js"; @@ -340,7 +340,10 @@ export async function initiatePeerToPeerPush( exchangeBaseUrl: coinSelRes.exchangeBaseUrl, contractPriv: econtractResp.contractPriv, }), - transactionId: makeTransactionId(TransactionType.PeerPushDebit, pursePair.pub), + transactionId: makeTransactionId( + TransactionType.PeerPushDebit, + pursePair.pub, + ), }; } @@ -448,28 +451,34 @@ async function getMergeReserveInfo( req: { exchangeBaseUrl: string; }, -): Promise { +): Promise { // We have to eagerly create the key pair outside of the transaction, // due to the async crypto API. const newReservePair = await ws.cryptoApi.createEddsaKeypair({}); - const mergeReserveInfo: MergeReserveInfo = await ws.db - .mktx((x) => [x.exchanges, x.withdrawalGroups]) + const mergeReserveRecord: ReserveRecord = await ws.db + .mktx((x) => [x.exchanges, x.reserves, x.withdrawalGroups]) .runReadWrite(async (tx) => { const ex = await tx.exchanges.get(req.exchangeBaseUrl); checkDbInvariant(!!ex); - if (ex.currentMergeReserveInfo) { - return ex.currentMergeReserveInfo; + if (ex.currentMergeReserveRowId != null) { + const reserve = await tx.reserves.get(ex.currentMergeReserveRowId); + checkDbInvariant(!!reserve); + return reserve; } - await tx.exchanges.put(ex); - ex.currentMergeReserveInfo = { + const reserve: ReserveRecord = { reservePriv: newReservePair.priv, reservePub: newReservePair.pub, }; - return ex.currentMergeReserveInfo; + const insertResp = await tx.reserves.put(reserve); + checkDbInvariant(typeof insertResp.key === "number"); + reserve.rowId = insertResp.key; + ex.currentMergeReserveRowId = reserve.rowId; + await tx.exchanges.put(ex); + return reserve; }); - return mergeReserveInfo; + return mergeReserveRecord; } export async function acceptPeerPushPayment(