diff options
Diffstat (limited to 'packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts | 117 |
1 files changed, 55 insertions, 62 deletions
diff --git a/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts b/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts index f357c41d5..5bcfa3418 100644 --- a/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts +++ b/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts @@ -19,6 +19,7 @@ import { Amounts, CoinRefreshRequest, ConfirmPeerPullDebitRequest, + ContractTermsUtil, ExchangePurseDeposits, HttpStatusCode, Logger, @@ -103,9 +104,7 @@ async function handlePurseCreationConflict( throw new TalerProtocolViolationError(); } - const instructedAmount = Amounts.parseOrThrow( - peerPullInc.contractTerms.amount, - ); + const instructedAmount = Amounts.parseOrThrow(peerPullInc.amount); const sel = peerPullInc.coinSel; if (!sel) { @@ -142,9 +141,7 @@ async function handlePurseCreationConflict( await ws.db .mktx((x) => [x.peerPullDebit]) .runReadWrite(async (tx) => { - const myPpi = await tx.peerPullDebit.get( - peerPullInc.peerPullDebitId, - ); + const myPpi = await tx.peerPullDebit.get(peerPullInc.peerPullDebitId); if (!myPpi) { return; } @@ -220,9 +217,7 @@ async function processPeerPullDebitPendingDeposit( const transitionInfo = await ws.db .mktx((x) => [x.peerPullDebit]) .runReadWrite(async (tx) => { - const pi = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pi = await tx.peerPullDebit.get(peerPullDebitId); if (!pi) { throw Error("peer pull payment not found anymore"); } @@ -248,9 +243,7 @@ async function processPeerPullDebitPendingDeposit( x.coins, ]) .runReadWrite(async (tx) => { - const pi = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pi = await tx.peerPullDebit.get(peerPullDebitId); if (!pi) { throw Error("peer pull payment not found anymore"); } @@ -335,9 +328,7 @@ async function processPeerPullDebitAbortingRefresh( } } if (newOpState) { - const newDg = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const newDg = await tx.peerPullDebit.get(peerPullDebitId); if (!newDg) { return; } @@ -391,9 +382,7 @@ export async function confirmPeerPullDebit( } else if (req.peerPullDebitId) { peerPullDebitId = req.peerPullDebitId; } else { - throw Error( - "invalid request, transactionId or peerPullDebitId required", - ); + throw Error("invalid request, transactionId or peerPullDebitId required"); } const peerPullInc = await ws.db @@ -408,9 +397,7 @@ export async function confirmPeerPullDebit( ); } - const instructedAmount = Amounts.parseOrThrow( - peerPullInc.contractTerms.amount, - ); + const instructedAmount = Amounts.parseOrThrow(peerPullInc.amount); const coinSelRes = await selectPeerCoins(ws, { instructedAmount }); logger.info(`selected p2p coins (pull): ${j2s(coinSelRes)}`); @@ -454,9 +441,7 @@ export async function confirmPeerPullDebit( refreshReason: RefreshReason.PayPeerPull, }); - const pi = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pi = await tx.peerPullDebit.get(peerPullDebitId); if (!pi) { throw Error(); } @@ -498,27 +483,36 @@ export async function preparePeerPullDebit( throw Error("got invalid taler://pay-pull URI"); } - const existingPullIncomingRecord = await ws.db - .mktx((x) => [x.peerPullDebit]) + const existing = await ws.db + .mktx((x) => [x.peerPullDebit, x.contractTerms]) .runReadOnly(async (tx) => { - return tx.peerPullDebit.indexes.byExchangeAndContractPriv.get([ - uri.exchangeBaseUrl, - uri.contractPriv, - ]); + const peerPullDebitRecord = + await tx.peerPullDebit.indexes.byExchangeAndContractPriv.get([ + uri.exchangeBaseUrl, + uri.contractPriv, + ]); + if (!peerPullDebitRecord) { + return; + } + const contractTerms = await tx.contractTerms.get( + peerPullDebitRecord.contractTermsHash, + ); + if (!contractTerms) { + return; + } + return { peerPullDebitRecord, contractTerms }; }); - if (existingPullIncomingRecord) { + if (existing) { return { - amount: existingPullIncomingRecord.contractTerms.amount, - amountRaw: existingPullIncomingRecord.contractTerms.amount, - amountEffective: existingPullIncomingRecord.totalCostEstimated, - contractTerms: existingPullIncomingRecord.contractTerms, - peerPullDebitId: - existingPullIncomingRecord.peerPullDebitId, + amount: existing.peerPullDebitRecord.amount, + amountRaw: existing.peerPullDebitRecord.amount, + amountEffective: existing.peerPullDebitRecord.totalCostEstimated, + contractTerms: existing.contractTerms.contractTermsRaw, + peerPullDebitId: existing.peerPullDebitRecord.peerPullDebitId, transactionId: constructTransactionIdentifier({ tag: TransactionType.PeerPullDebit, - peerPullDebitId: - existingPullIncomingRecord.peerPullDebitId, + peerPullDebitId: existing.peerPullDebitRecord.peerPullDebitId, }), }; } @@ -566,6 +560,8 @@ export async function preparePeerPullDebit( throw Error("pull payments without contract terms not supported yet"); } + const contractTermsHash = ContractTermsUtil.hashContractTerms(contractTerms); + // FIXME: Why don't we compute the totalCost here?! const instructedAmount = Amounts.parseOrThrow(contractTerms.amount); @@ -588,18 +584,23 @@ export async function preparePeerPullDebit( ); await ws.db - .mktx((x) => [x.peerPullDebit]) + .mktx((x) => [x.peerPullDebit, x.contractTerms]) .runReadWrite(async (tx) => { - await tx.peerPullDebit.add({ - peerPullDebitId, - contractPriv: contractPriv, - exchangeBaseUrl: exchangeBaseUrl, - pursePub: pursePub, - timestampCreated: TalerPreciseTimestamp.now(), - contractTerms, - status: PeerPullDebitRecordStatus.DialogProposed, - totalCostEstimated: Amounts.stringify(totalAmount), - }); + await tx.contractTerms.put({ + h: contractTermsHash, + contractTermsRaw: contractTerms, + }), + await tx.peerPullDebit.add({ + peerPullDebitId, + contractPriv: contractPriv, + exchangeBaseUrl: exchangeBaseUrl, + pursePub: pursePub, + timestampCreated: TalerPreciseTimestamp.now(), + contractTermsHash, + amount: contractTerms.amount, + status: PeerPullDebitRecordStatus.DialogProposed, + totalCostEstimated: Amounts.stringify(totalAmount), + }); }); return { @@ -631,9 +632,7 @@ export async function suspendPeerPullDebitTransaction( const transitionInfo = await ws.db .mktx((x) => [x.peerPullDebit]) .runReadWrite(async (tx) => { - const pullDebitRec = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pullDebitRec = await tx.peerPullDebit.get(peerPullDebitId); if (!pullDebitRec) { logger.warn(`peer pull debit ${peerPullDebitId} not found`); return; @@ -692,9 +691,7 @@ export async function abortPeerPullDebitTransaction( const transitionInfo = await ws.db .mktx((x) => [x.peerPullDebit]) .runReadWrite(async (tx) => { - const pullDebitRec = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pullDebitRec = await tx.peerPullDebit.get(peerPullDebitId); if (!pullDebitRec) { logger.warn(`peer pull debit ${peerPullDebitId} not found`); return; @@ -753,9 +750,7 @@ export async function failPeerPullDebitTransaction( const transitionInfo = await ws.db .mktx((x) => [x.peerPullDebit]) .runReadWrite(async (tx) => { - const pullDebitRec = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pullDebitRec = await tx.peerPullDebit.get(peerPullDebitId); if (!pullDebitRec) { logger.warn(`peer pull debit ${peerPullDebitId} not found`); return; @@ -814,9 +809,7 @@ export async function resumePeerPullDebitTransaction( const transitionInfo = await ws.db .mktx((x) => [x.peerPullDebit]) .runReadWrite(async (tx) => { - const pullDebitRec = await tx.peerPullDebit.get( - peerPullDebitId, - ); + const pullDebitRec = await tx.peerPullDebit.get(peerPullDebitId); if (!pullDebitRec) { logger.warn(`peer pull debit ${peerPullDebitId} not found`); return; |