wallet-core/packages/taler-wallet-core/src/operations/pay.ts

1894 lines
54 KiB
TypeScript
Raw Normal View History

/*
This file is part of GNU Taler
(C) 2019-2022 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Implementation of the payment operation, including downloading and
* claiming of proposals.
*
* @author Florian Dold
*/
/**
* Imports.
*/
2022-09-16 16:24:47 +02:00
import { GlobalIDB } from "@gnu-taler/idb-bridge";
2021-03-27 19:35:44 +01:00
import {
AbsoluteTime,
AgeRestriction,
2021-03-27 19:35:44 +01:00
AmountJson,
Amounts,
codecForContractTerms,
codecForMerchantPayResponse,
codecForProposal,
CoinDepositPermission,
ConfirmPayResult,
ConfirmPayResultType,
ContractTerms,
ContractTermsUtil,
DenominationInfo,
Duration,
encodeCrock,
ForcedCoinSel,
getRandomBytes,
HttpStatusCode,
j2s,
Logger,
NotificationType,
parsePaytoUri,
parsePayUri,
PayCoinSelection,
PreparePayResult,
PreparePayResultType,
RefreshReason,
strcmp,
TalerErrorCode,
TalerErrorDetail,
2022-03-18 15:32:41 +01:00
TalerProtocolTimestamp,
TransactionType,
URL,
2021-03-27 19:35:44 +01:00
} from "@gnu-taler/taler-util";
2022-09-05 18:12:30 +02:00
import { EddsaKeypair } from "../crypto/cryptoImplementation.js";
2021-06-14 16:08:58 +02:00
import {
AbortStatus,
AllowedAuditorInfo,
AllowedExchangeInfo,
BackupProviderStateTag,
2021-06-14 16:08:58 +02:00
CoinRecord,
CoinStatus,
DenominationRecord,
ProposalRecord,
ProposalStatus,
PurchaseRecord,
WalletContractData,
} from "../db.js";
import {
makeErrorDetail,
makePendingOperationFailedError,
TalerError,
TalerProtocolViolationError,
} from "../errors.js";
2022-09-05 18:12:30 +02:00
import {
EXCHANGE_COINS_LOCK,
InternalWalletState,
} from "../internal-wallet-state.js";
2022-09-19 12:13:31 +02:00
import { PendingTaskType } from "../pending-types.js";
2022-09-05 18:12:30 +02:00
import { assertUnreachable } from "../util/assertUnreachable.js";
import {
CoinSelectionTally,
PreviousPayCoins,
tallyFees,
} from "../util/coinSelection.js";
2021-06-14 16:08:58 +02:00
import {
getHttpResponseErrorDetails,
readSuccessResponseJsonOrErrorCode,
readSuccessResponseJsonOrThrow,
readTalerErrorResponse,
2021-08-24 15:08:34 +02:00
readUnexpectedResponseDetails,
throwUnexpectedRequestError,
2021-06-14 16:08:58 +02:00
} from "../util/http.js";
import { checkDbInvariant, checkLogicInvariant } from "../util/invariants.js";
2022-09-16 19:27:24 +02:00
import {
OperationAttemptResult,
OperationAttemptResultType,
RetryInfo,
RetryTags,
scheduleRetry,
} from "../util/retries.js";
2022-09-19 12:13:31 +02:00
import {
spendCoins,
storeOperationError,
storeOperationPending,
} from "../wallet.js";
import { getExchangeDetails } from "./exchanges.js";
2022-09-16 16:24:47 +02:00
import { getTotalRefreshCost } from "./refresh.js";
import { makeEventId } from "./transactions.js";
2019-12-25 21:47:57 +01:00
/**
* Logger.
*/
const logger = new Logger("pay.ts");
/**
2019-12-25 19:11:20 +01:00
* Compute the total cost of a payment to the customer.
2020-03-30 12:39:32 +02:00
*
2019-12-25 21:47:57 +01:00
* This includes the amount taken by the merchant, fees (wire/deposit) contributed
* by the customer, refreshing fees, fees for withdraw-after-refresh and "trimmings"
* of coins that are too small to spend.
2019-12-25 19:11:20 +01:00
*/
export async function getTotalPaymentCost(
ws: InternalWalletState,
pcs: PayCoinSelection,
2020-09-08 17:15:33 +02:00
): Promise<AmountJson> {
2021-06-09 15:14:17 +02:00
return ws.db
.mktx((x) => [x.coins, x.denominations])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
const costs: AmountJson[] = [];
2021-06-09 15:14:17 +02:00
for (let i = 0; i < pcs.coinPubs.length; i++) {
const coin = await tx.coins.get(pcs.coinPubs[i]);
if (!coin) {
throw Error("can't calculate payment cost, coin not found");
}
const denom = await tx.denominations.get([
coin.exchangeBaseUrl,
coin.denomPubHash,
]);
if (!denom) {
throw Error(
"can't calculate payment cost, denomination for coin not found",
);
}
const allDenoms = await tx.denominations.indexes.byExchangeBaseUrl
.iter(coin.exchangeBaseUrl)
.filter((x) =>
Amounts.isSameCurrency(
DenominationRecord.getValue(x),
pcs.coinContributions[i],
),
);
2022-01-13 22:01:14 +01:00
const amountLeft = Amounts.sub(
DenominationRecord.getValue(denom),
2022-01-13 22:01:14 +01:00
pcs.coinContributions[i],
).amount;
const refreshCost = getTotalRefreshCost(
allDenoms,
DenominationRecord.toDenomInfo(denom),
amountLeft,
);
2021-06-09 15:14:17 +02:00
costs.push(pcs.coinContributions[i]);
costs.push(refreshCost);
}
2021-07-12 15:55:31 +02:00
const zero = Amounts.getZero(pcs.paymentAmount.currency);
return Amounts.sum([zero, ...costs]).amount;
2021-06-09 15:14:17 +02:00
});
2019-12-25 19:11:20 +01:00
}
2021-01-18 23:35:41 +01:00
export interface CoinSelectionRequest {
amount: AmountJson;
2021-01-18 23:35:41 +01:00
allowedAuditors: AllowedAuditorInfo[];
allowedExchanges: AllowedExchangeInfo[];
/**
* Timestamp of the contract.
*/
2022-03-18 15:32:41 +01:00
timestamp: TalerProtocolTimestamp;
2021-01-18 23:35:41 +01:00
wireMethod: string;
wireFeeAmortization: number;
maxWireFee: AmountJson;
maxDepositFee: AmountJson;
/**
* Minimum age requirement for the coin selection.
*
* When present, only select coins with either no age restriction
* or coins with an age commitment that matches the minimum age.
*/
minimumAge?: number;
2021-01-18 23:35:41 +01:00
}
/**
* Record all information that is necessary to
* pay for a proposal in the wallet's database.
*/
async function recordConfirmPay(
ws: InternalWalletState,
proposal: ProposalRecord,
2019-12-25 19:11:20 +01:00
coinSelection: PayCoinSelection,
coinDepositPermissions: CoinDepositPermission[],
sessionIdOverride: string | undefined,
): Promise<PurchaseRecord> {
2019-12-03 00:52:15 +01:00
const d = proposal.download;
if (!d) {
throw Error("proposal is in invalid state");
}
let sessionId;
if (sessionIdOverride) {
sessionId = sessionIdOverride;
} else {
sessionId = proposal.downloadSessionId;
}
2020-12-14 16:45:15 +01:00
logger.trace(
`recording payment on ${proposal.orderId} with session ID ${sessionId}`,
);
2020-05-12 12:34:28 +02:00
const payCostInfo = await getTotalPaymentCost(ws, coinSelection);
const t: PurchaseRecord = {
abortStatus: AbortStatus.None,
2020-12-21 13:23:07 +01:00
download: d,
lastSessionId: sessionId,
payCoinSelection: coinSelection,
payCoinSelectionUid: encodeCrock(getRandomBytes(32)),
2020-09-08 17:15:33 +02:00
totalPayCost: payCostInfo,
2020-07-21 08:53:48 +02:00
coinDepositPermissions,
2022-03-18 15:32:41 +01:00
timestampAccept: AbsoluteTime.toTimestamp(AbsoluteTime.now()),
2019-12-16 16:20:45 +01:00
timestampLastRefundStatus: undefined,
2019-12-03 00:52:15 +01:00
proposalId: proposal.proposalId,
refundQueryRequested: false,
2019-12-16 16:20:45 +01:00
timestampFirstSuccessfulPay: undefined,
2019-12-07 18:42:18 +01:00
autoRefundDeadline: undefined,
2022-05-14 23:09:33 +02:00
refundAwaiting: undefined,
paymentSubmitPending: true,
2020-07-23 14:05:17 +02:00
refunds: {},
2020-08-19 17:25:38 +02:00
merchantPaySig: undefined,
noncePriv: proposal.noncePriv,
noncePub: proposal.noncePub,
};
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [
x.proposals,
x.purchases,
x.coins,
x.refreshGroups,
x.denominations,
x.coinAvailability,
])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const p = await tx.proposals.get(proposal.proposalId);
2019-12-03 00:52:15 +01:00
if (p) {
p.proposalStatus = ProposalStatus.Accepted;
2021-06-09 15:14:17 +02:00
await tx.proposals.put(p);
2019-12-03 00:52:15 +01:00
}
2021-06-09 15:14:17 +02:00
await tx.purchases.put(t);
await spendCoins(ws, tx, {
allocationId: `proposal:${t.proposalId}`,
coinPubs: coinSelection.coinPubs,
contributions: coinSelection.coinContributions,
refreshReason: RefreshReason.PayMerchant,
});
2021-06-09 15:14:17 +02:00
});
2019-12-05 19:38:19 +01:00
ws.notify({
type: NotificationType.ProposalAccepted,
proposalId: proposal.proposalId,
});
return t;
}
async function failProposalPermanently(
ws: InternalWalletState,
proposalId: string,
err: TalerErrorDetail,
): Promise<void> {
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const p = await tx.proposals.get(proposalId);
if (!p) {
return;
}
p.proposalStatus = ProposalStatus.PermanentlyFailed;
2021-06-09 15:14:17 +02:00
await tx.proposals.put(p);
});
}
2022-09-05 18:12:30 +02:00
function getProposalRequestTimeout(retryInfo?: RetryInfo): Duration {
return Duration.clamp({
2022-05-19 11:01:07 +02:00
lower: Duration.fromSpec({ seconds: 1 }),
upper: Duration.fromSpec({ seconds: 60 }),
2022-09-05 18:12:30 +02:00
value: retryInfo ? RetryInfo.getDuration(retryInfo) : Duration.fromSpec({}),
});
}
2020-09-07 12:24:22 +02:00
function getPayRequestTimeout(purchase: PurchaseRecord): Duration {
2022-09-05 18:12:30 +02:00
return Duration.multiply(
2021-02-05 12:10:56 +01:00
{ d_ms: 15000 },
1 + purchase.payCoinSelection.coinPubs.length / 5,
);
}
2021-01-18 23:35:41 +01:00
export function extractContractData(
parsedContractTerms: ContractTerms,
contractTermsHash: string,
merchantSig: string,
): WalletContractData {
const amount = Amounts.parseOrThrow(parsedContractTerms.amount);
let maxWireFee: AmountJson;
if (parsedContractTerms.max_wire_fee) {
maxWireFee = Amounts.parseOrThrow(parsedContractTerms.max_wire_fee);
} else {
maxWireFee = Amounts.getZero(amount.currency);
}
return {
amount,
contractTermsHash: contractTermsHash,
fulfillmentUrl: parsedContractTerms.fulfillment_url ?? "",
merchantBaseUrl: parsedContractTerms.merchant_base_url,
merchantPub: parsedContractTerms.merchant_pub,
merchantSig,
orderId: parsedContractTerms.order_id,
summary: parsedContractTerms.summary,
autoRefund: parsedContractTerms.auto_refund,
maxWireFee,
payDeadline: parsedContractTerms.pay_deadline,
refundDeadline: parsedContractTerms.refund_deadline,
wireFeeAmortization: parsedContractTerms.wire_fee_amortization || 1,
allowedAuditors: parsedContractTerms.auditors.map((x) => ({
auditorBaseUrl: x.url,
auditorPub: x.auditor_pub,
})),
allowedExchanges: parsedContractTerms.exchanges.map((x) => ({
exchangeBaseUrl: x.url,
exchangePub: x.master_pub,
})),
timestamp: parsedContractTerms.timestamp,
wireMethod: parsedContractTerms.wire_method,
wireInfoHash: parsedContractTerms.h_wire,
maxDepositFee: Amounts.parseOrThrow(parsedContractTerms.max_fee),
merchant: parsedContractTerms.merchant,
products: parsedContractTerms.products,
summaryI18n: parsedContractTerms.summary_i18n,
minimumAge: parsedContractTerms.minimum_age,
deliveryDate: parsedContractTerms.delivery_date,
deliveryLocation: parsedContractTerms.delivery_location,
2021-01-18 23:35:41 +01:00
};
}
2022-09-05 18:12:30 +02:00
export async function processDownloadProposal(
2019-12-05 19:38:19 +01:00
ws: InternalWalletState,
proposalId: string,
options: object = {},
2022-09-05 18:12:30 +02:00
): Promise<OperationAttemptResult> {
2021-06-09 15:14:17 +02:00
const proposal = await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return await tx.proposals.get(proposalId);
2021-06-09 15:14:17 +02:00
});
2019-12-03 00:52:15 +01:00
if (!proposal) {
2022-09-05 18:12:30 +02:00
return {
type: OperationAttemptResultType.Finished,
result: undefined,
};
2019-12-03 00:52:15 +01:00
}
if (proposal.proposalStatus != ProposalStatus.Downloading) {
2022-09-05 18:12:30 +02:00
return {
type: OperationAttemptResultType.Finished,
result: undefined,
};
2019-12-03 00:52:15 +01:00
}
const orderClaimUrl = new URL(
`orders/${proposal.orderId}/claim`,
proposal.merchantBaseUrl,
).href;
logger.trace("downloading contract from '" + orderClaimUrl + "'");
2020-07-30 13:58:09 +02:00
const requestBody: {
nonce: string;
2020-07-30 13:58:09 +02:00
token?: string;
} = {
nonce: proposal.noncePub,
};
2020-07-30 13:58:09 +02:00
if (proposal.claimToken) {
requestBody.token = proposal.claimToken;
}
2022-09-05 18:12:30 +02:00
const opId = RetryTags.forProposalClaim(proposal);
const retryRecord = await ws.db
.mktx((x) => [x.operationRetries])
2022-09-05 18:12:30 +02:00
.runReadOnly(async (tx) => {
return tx.operationRetries.get(opId);
});
// FIXME: Do this in the background using the new return value
const httpResponse = await ws.http.postJson(orderClaimUrl, requestBody, {
2022-09-05 18:12:30 +02:00
timeout: getProposalRequestTimeout(retryRecord?.retryInfo),
});
const r = await readSuccessResponseJsonOrErrorCode(
httpResponse,
codecForProposal(),
);
if (r.isError) {
switch (r.talerErrorResponse.code) {
2020-11-08 01:20:50 +01:00
case TalerErrorCode.MERCHANT_POST_ORDERS_ID_CLAIM_ALREADY_CLAIMED:
throw TalerError.fromDetail(
TalerErrorCode.WALLET_ORDER_ALREADY_CLAIMED,
{
orderId: proposal.orderId,
claimUrl: orderClaimUrl,
},
"order already claimed (likely by other wallet)",
);
default:
throwUnexpectedRequestError(httpResponse, r.talerErrorResponse);
}
}
const proposalResp = r.response;
2019-12-09 13:29:11 +01:00
// The proposalResp contains the contract terms as raw JSON,
// as the coded to parse them doesn't necessarily round-trip.
// We need this raw JSON to compute the contract terms hash.
2019-12-03 00:52:15 +01:00
2021-04-14 14:36:46 +02:00
// FIXME: Do better error handling, check if the
// contract terms have all their forgettable information still
// present. The wallet should never accept contract terms
// with missing information from the merchant.
const isWellFormed = ContractTermsUtil.validateForgettable(
proposalResp.contract_terms,
2019-12-03 00:52:15 +01:00
);
2021-04-14 14:36:46 +02:00
if (!isWellFormed) {
logger.trace(
`malformed contract terms: ${j2s(proposalResp.contract_terms)}`,
);
const err = makeErrorDetail(
2021-04-14 14:36:46 +02:00
TalerErrorCode.WALLET_CONTRACT_TERMS_MALFORMED,
{},
"validation for well-formedness failed",
2021-04-14 14:36:46 +02:00
);
await failProposalPermanently(ws, proposalId, err);
throw makePendingOperationFailedError(
err,
TransactionType.Payment,
proposalId,
);
2021-04-14 14:36:46 +02:00
}
const contractTermsHash = ContractTermsUtil.hashContractTerms(
proposalResp.contract_terms,
);
logger.info(`Contract terms hash: ${contractTermsHash}`);
2021-04-14 14:36:46 +02:00
let parsedContractTerms: ContractTerms;
try {
parsedContractTerms = codecForContractTerms().decode(
proposalResp.contract_terms,
);
} catch (e) {
const err = makeErrorDetail(
2021-04-14 14:36:46 +02:00
TalerErrorCode.WALLET_CONTRACT_TERMS_MALFORMED,
{},
`schema validation failed: ${e}`,
2021-04-14 14:36:46 +02:00
);
await failProposalPermanently(ws, proposalId, err);
throw makePendingOperationFailedError(
err,
TransactionType.Payment,
proposalId,
);
2021-04-14 14:36:46 +02:00
}
2022-03-23 21:24:23 +01:00
const sigValid = await ws.cryptoApi.isValidContractTermsSignature({
contractTermsHash,
2022-03-23 21:24:23 +01:00
merchantPub: parsedContractTerms.merchant_pub,
sig: proposalResp.sig,
});
if (!sigValid) {
const err = makeErrorDetail(
TalerErrorCode.WALLET_CONTRACT_TERMS_SIGNATURE_INVALID,
{
merchantPub: parsedContractTerms.merchant_pub,
orderId: parsedContractTerms.order_id,
},
"merchant's signature on contract terms is invalid",
);
await failProposalPermanently(ws, proposalId, err);
throw makePendingOperationFailedError(
err,
TransactionType.Payment,
proposalId,
);
}
const fulfillmentUrl = parsedContractTerms.fulfillment_url;
2019-12-03 00:52:15 +01:00
const baseUrlForDownload = proposal.merchantBaseUrl;
const baseUrlFromContractTerms = parsedContractTerms.merchant_base_url;
if (baseUrlForDownload !== baseUrlFromContractTerms) {
const err = makeErrorDetail(
TalerErrorCode.WALLET_CONTRACT_TERMS_BASE_URL_MISMATCH,
{
baseUrlForDownload,
baseUrlFromContractTerms,
},
"merchant base URL mismatch",
);
await failProposalPermanently(ws, proposalId, err);
throw makePendingOperationFailedError(
err,
TransactionType.Payment,
proposalId,
);
}
2021-01-18 23:35:41 +01:00
const contractData = extractContractData(
parsedContractTerms,
contractTermsHash,
proposalResp.sig,
);
logger.trace(`extracted contract data: ${j2s(contractData)}`);
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.purchases, x.proposals])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const p = await tx.proposals.get(proposalId);
2019-12-03 00:52:15 +01:00
if (!p) {
return;
}
if (p.proposalStatus !== ProposalStatus.Downloading) {
2019-12-03 00:52:15 +01:00
return;
}
2019-12-16 22:42:10 +01:00
p.download = {
2021-01-18 23:35:41 +01:00
contractData,
2020-12-21 13:23:07 +01:00
contractTermsRaw: proposalResp.contract_terms,
2019-12-16 22:42:10 +01:00
};
2019-12-03 00:52:15 +01:00
if (
fulfillmentUrl &&
(fulfillmentUrl.startsWith("http://") ||
fulfillmentUrl.startsWith("https://"))
2019-12-03 00:52:15 +01:00
) {
2022-01-13 22:01:14 +01:00
const differentPurchase =
await tx.purchases.indexes.byFulfillmentUrl.get(fulfillmentUrl);
2019-12-03 00:52:15 +01:00
if (differentPurchase) {
logger.warn("repurchase detected");
p.proposalStatus = ProposalStatus.Repurchase;
2019-12-03 00:52:15 +01:00
p.repurchaseProposalId = differentPurchase.proposalId;
2021-06-09 15:14:17 +02:00
await tx.proposals.put(p);
2019-12-03 00:52:15 +01:00
return;
}
}
p.proposalStatus = ProposalStatus.Proposed;
2021-06-09 15:14:17 +02:00
await tx.proposals.put(p);
});
2019-12-03 00:52:15 +01:00
2019-12-05 19:38:19 +01:00
ws.notify({
type: NotificationType.ProposalDownloaded,
proposalId: proposal.proposalId,
});
2022-09-05 18:12:30 +02:00
return {
type: OperationAttemptResultType.Finished,
result: undefined,
};
2019-12-03 00:52:15 +01:00
}
/**
* Download a proposal and store it in the database.
* Returns an id for it to retrieve it later.
*
* @param sessionId Current session ID, if the proposal is being
* downloaded in the context of a session ID.
*/
2019-12-03 00:52:15 +01:00
async function startDownloadProposal(
ws: InternalWalletState,
2019-12-06 12:47:28 +01:00
merchantBaseUrl: string,
orderId: string,
sessionId: string | undefined,
2020-07-30 13:58:09 +02:00
claimToken: string | undefined,
2021-09-17 20:48:33 +02:00
noncePriv: string | undefined,
): Promise<string> {
2021-06-09 15:14:17 +02:00
const oldProposal = await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.proposals.indexes.byUrlAndOrderId.get([
merchantBaseUrl,
orderId,
]);
});
2021-11-27 20:56:58 +01:00
/* If we have already claimed this proposal with the same sessionId
* nonce and claim token, reuse it. */
2021-11-27 20:56:58 +01:00
if (
oldProposal &&
oldProposal.downloadSessionId === sessionId &&
(!noncePriv || oldProposal.noncePriv === noncePriv) &&
oldProposal.claimToken === claimToken
) {
2019-12-03 00:52:15 +01:00
await processDownloadProposal(ws, oldProposal.proposalId);
return oldProposal.proposalId;
}
2022-03-23 21:24:23 +01:00
let noncePair: EddsaKeypair;
if (noncePriv) {
noncePair = {
priv: noncePriv,
pub: (await ws.cryptoApi.eddsaGetPublic({ priv: noncePriv })).pub,
};
2022-03-23 21:24:23 +01:00
} else {
noncePair = await ws.cryptoApi.createEddsaKeypair({});
}
const { priv, pub } = noncePair;
const proposalId = encodeCrock(getRandomBytes(32));
const proposalRecord: ProposalRecord = {
2019-12-03 00:52:15 +01:00
download: undefined,
noncePriv: priv,
2019-12-03 00:52:15 +01:00
noncePub: pub,
2020-07-30 13:58:09 +02:00
claimToken,
2022-03-18 15:32:41 +01:00
timestamp: AbsoluteTime.toTimestamp(AbsoluteTime.now()),
2019-12-06 12:47:28 +01:00
merchantBaseUrl,
orderId,
proposalId: proposalId,
proposalStatus: ProposalStatus.Downloading,
2019-12-03 00:52:15 +01:00
repurchaseProposalId: undefined,
downloadSessionId: sessionId,
};
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
2021-06-09 16:47:45 +02:00
const existingRecord = await tx.proposals.indexes.byUrlAndOrderId.get([
2021-06-09 15:14:17 +02:00
merchantBaseUrl,
orderId,
]);
if (existingRecord) {
// Created concurrently
return;
}
await tx.proposals.put(proposalRecord);
});
2019-12-03 00:52:15 +01:00
await processDownloadProposal(ws, proposalId);
return proposalId;
}
2020-08-19 17:25:38 +02:00
async function storeFirstPaySuccess(
ws: InternalWalletState,
proposalId: string,
sessionId: string | undefined,
paySig: string,
): Promise<void> {
2022-03-18 15:32:41 +01:00
const now = AbsoluteTime.toTimestamp(AbsoluteTime.now());
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const purchase = await tx.purchases.get(proposalId);
2020-08-19 17:25:38 +02:00
2021-06-09 15:14:17 +02:00
if (!purchase) {
logger.warn("purchase does not exist anymore");
return;
2020-08-19 17:25:38 +02:00
}
2021-06-09 15:14:17 +02:00
const isFirst = purchase.timestampFirstSuccessfulPay === undefined;
if (!isFirst) {
logger.warn("payment success already stored");
return;
}
purchase.timestampFirstSuccessfulPay = now;
purchase.paymentSubmitPending = false;
purchase.lastSessionId = sessionId;
purchase.merchantPaySig = paySig;
2022-05-14 23:09:33 +02:00
const protoAr = purchase.download.contractData.autoRefund;
if (protoAr) {
const ar = Duration.fromTalerProtocolDuration(protoAr);
logger.info("auto_refund present");
purchase.refundQueryRequested = true;
purchase.autoRefundDeadline = AbsoluteTime.toTimestamp(
AbsoluteTime.addDuration(AbsoluteTime.now(), ar),
);
2021-06-09 15:14:17 +02:00
}
await tx.purchases.put(purchase);
});
2020-08-19 17:25:38 +02:00
}
async function storePayReplaySuccess(
ws: InternalWalletState,
proposalId: string,
sessionId: string | undefined,
): Promise<void> {
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const purchase = await tx.purchases.get(proposalId);
2020-08-19 17:25:38 +02:00
2021-06-09 15:14:17 +02:00
if (!purchase) {
logger.warn("purchase does not exist anymore");
return;
}
const isFirst = purchase.timestampFirstSuccessfulPay === undefined;
if (isFirst) {
throw Error("invalid payment state");
}
purchase.paymentSubmitPending = false;
purchase.lastSessionId = sessionId;
await tx.purchases.put(purchase);
});
2020-08-19 17:25:38 +02:00
}
/**
* Handle a 409 Conflict response from the merchant.
*
* We do this by going through the coin history provided by the exchange and
* (1) verifying the signatures from the exchange
* (2) adjusting the remaining coin value and refreshing it
* (3) re-do coin selection with the bad coin removed
*/
async function handleInsufficientFunds(
ws: InternalWalletState,
proposalId: string,
err: TalerErrorDetail,
): Promise<void> {
logger.trace("handling insufficient funds, trying to re-select coins");
2021-06-09 15:14:17 +02:00
const proposal = await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.purchases.get(proposalId);
2021-06-09 15:14:17 +02:00
});
if (!proposal) {
return;
}
logger.trace(`got error details: ${j2s(err)}`);
const exchangeReply = (err as any).exchange_reply;
if (
exchangeReply.code !== TalerErrorCode.EXCHANGE_GENERIC_INSUFFICIENT_FUNDS
) {
// FIXME: set as failed
if (logger.shouldLogTrace()) {
logger.trace("got exchange error reply (see below)");
logger.trace(j2s(exchangeReply));
}
throw Error(`unable to handle /pay error response (${exchangeReply.code})`);
}
const brokenCoinPub = (exchangeReply as any).coin_pub;
logger.trace(`excluded broken coin pub=${brokenCoinPub}`);
if (!brokenCoinPub) {
throw new TalerProtocolViolationError();
}
const { contractData } = proposal.download;
const prevPayCoins: PreviousPayCoins = [];
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.coins, x.denominations])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
for (let i = 0; i < proposal.payCoinSelection.coinPubs.length; i++) {
const coinPub = proposal.payCoinSelection.coinPubs[i];
if (coinPub === brokenCoinPub) {
continue;
}
const contrib = proposal.payCoinSelection.coinContributions[i];
const coin = await tx.coins.get(coinPub);
if (!coin) {
continue;
}
const denom = await tx.denominations.get([
coin.exchangeBaseUrl,
coin.denomPubHash,
]);
if (!denom) {
continue;
}
prevPayCoins.push({
coinPub,
contribution: contrib,
exchangeBaseUrl: coin.exchangeBaseUrl,
feeDeposit: denom.fees.feeDeposit,
2021-06-09 15:14:17 +02:00
});
}
});
const res = await selectPayCoinsNew(ws, {
auditors: contractData.allowedAuditors,
exchanges: contractData.allowedExchanges,
wireMethod: contractData.wireMethod,
contractTermsAmount: contractData.amount,
depositFeeLimit: contractData.maxDepositFee,
wireFeeAmortization: contractData.wireFeeAmortization ?? 1,
wireFeeLimit: contractData.maxWireFee,
prevPayCoins,
requiredMinimumAge: contractData.minimumAge,
});
if (!res) {
logger.trace("insufficient funds for coin re-selection");
return;
}
logger.trace("re-selected coins");
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [
x.purchases,
x.coins,
x.coinAvailability,
x.denominations,
x.refreshGroups,
])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const p = await tx.purchases.get(proposalId);
if (!p) {
return;
}
p.payCoinSelection = res;
p.payCoinSelectionUid = encodeCrock(getRandomBytes(32));
p.coinDepositPermissions = undefined;
2021-06-09 15:14:17 +02:00
await tx.purchases.put(p);
await spendCoins(ws, tx, {
allocationId: `proposal:${p.proposalId}`,
coinPubs: p.payCoinSelection.coinPubs,
contributions: p.payCoinSelection.coinContributions,
refreshReason: RefreshReason.PayMerchant,
});
2021-06-09 15:14:17 +02:00
});
}
async function unblockBackup(
ws: InternalWalletState,
proposalId: string,
): Promise<void> {
await ws.db
.mktx((x) => [x.backupProviders])
.runReadWrite(async (tx) => {
await tx.backupProviders.indexes.byPaymentProposalId
.iter(proposalId)
.forEachAsync(async (bp) => {
if (bp.state.tag === BackupProviderStateTag.Retrying) {
bp.state = {
tag: BackupProviderStateTag.Ready,
2022-03-18 15:32:41 +01:00
nextBackupTimestamp: TalerProtocolTimestamp.now(),
};
}
});
});
}
export interface SelectPayCoinRequestNg {
exchanges: AllowedExchangeInfo[];
auditors: AllowedAuditorInfo[];
wireMethod: string;
contractTermsAmount: AmountJson;
depositFeeLimit: AmountJson;
wireFeeLimit: AmountJson;
wireFeeAmortization: number;
prevPayCoins?: PreviousPayCoins;
requiredMinimumAge?: number;
forcedSelection?: ForcedCoinSel;
}
export type AvailableDenom = DenominationInfo & {
maxAge: number;
numAvailable: number;
};
2022-09-16 16:24:47 +02:00
export async function selectCandidates(
ws: InternalWalletState,
req: SelectPayCoinRequestNg,
): Promise<[AvailableDenom[], Record<string, AmountJson>]> {
return await ws.db
.mktx((x) => [
x.exchanges,
x.exchangeDetails,
x.denominations,
x.coinAvailability,
])
.runReadOnly(async (tx) => {
const denoms: AvailableDenom[] = [];
const exchanges = await tx.exchanges.iter().toArray();
const wfPerExchange: Record<string, AmountJson> = {};
for (const exchange of exchanges) {
const exchangeDetails = await getExchangeDetails(tx, exchange.baseUrl);
if (exchangeDetails?.currency !== req.contractTermsAmount.currency) {
continue;
}
let wireMethodSupported = false;
for (const acc of exchangeDetails.wireInfo.accounts) {
const pp = parsePaytoUri(acc.payto_uri);
checkLogicInvariant(!!pp);
if (pp.targetType === req.wireMethod) {
wireMethodSupported = true;
break;
}
}
if (!wireMethodSupported) {
break;
}
exchangeDetails.wireInfo.accounts;
let accepted = false;
for (const allowedExchange of req.exchanges) {
if (allowedExchange.exchangePub === exchangeDetails.masterPublicKey) {
accepted = true;
break;
}
}
for (const allowedAuditor of req.auditors) {
for (const providedAuditor of exchangeDetails.auditors) {
if (allowedAuditor.auditorPub === providedAuditor.auditor_pub) {
accepted = true;
break;
}
}
}
if (!accepted) {
continue;
}
let ageLower = 0;
2022-09-16 16:24:47 +02:00
let ageUpper = AgeRestriction.AGE_UNRESTRICTED;
if (req.requiredMinimumAge) {
ageLower = req.requiredMinimumAge;
}
const myExchangeDenoms =
await tx.coinAvailability.indexes.byExchangeAgeAvailability.getAll(
GlobalIDB.KeyRange.bound(
[exchangeDetails.exchangeBaseUrl, ageLower, 1],
[
exchangeDetails.exchangeBaseUrl,
ageUpper,
Number.MAX_SAFE_INTEGER,
],
),
);
// FIXME: Check that the individual denomination is audited!
// FIXME: Should we exclude denominations that are
// not spendable anymore?
for (const denomAvail of myExchangeDenoms) {
const denom = await tx.denominations.get([
denomAvail.exchangeBaseUrl,
denomAvail.denomPubHash,
]);
checkDbInvariant(!!denom);
2022-09-19 12:21:00 +02:00
if (denom.isRevoked || !denom.isOffered) {
continue;
}
denoms.push({
...DenominationRecord.toDenomInfo(denom),
numAvailable: denomAvail.freshCoinCount ?? 0,
maxAge: denomAvail.maxAge,
});
}
}
// Sort by available amount (descending), deposit fee (ascending) and
// denomPub (ascending) if deposit fee is the same
// (to guarantee deterministic results)
denoms.sort(
(o1, o2) =>
-Amounts.cmp(o1.value, o2.value) ||
Amounts.cmp(o1.feeDeposit, o2.feeDeposit) ||
strcmp(o1.denomPubHash, o2.denomPubHash),
);
return [denoms, wfPerExchange];
});
}
function makeAvailabilityKey(
exchangeBaseUrl: string,
denomPubHash: string,
maxAge: number,
): string {
return `${denomPubHash};${maxAge};${exchangeBaseUrl}`;
}
/**
* Selection result.
*/
interface SelResult {
/**
* Map from an availability key
* to an array of contributions.
*/
[avKey: string]: {
exchangeBaseUrl: string;
denomPubHash: string;
maxAge: number;
contributions: AmountJson[];
};
}
export function selectGreedy(
req: SelectPayCoinRequestNg,
candidateDenoms: AvailableDenom[],
wireFeesPerExchange: Record<string, AmountJson>,
tally: CoinSelectionTally,
): SelResult | undefined {
const { wireFeeAmortization } = req;
const selectedDenom: SelResult = {};
for (const aci of candidateDenoms) {
const contributions: AmountJson[] = [];
for (let i = 0; i < aci.numAvailable; i++) {
// Don't use this coin if depositing it is more expensive than
// the amount it would give the merchant.
if (Amounts.cmp(aci.feeDeposit, aci.value) > 0) {
continue;
}
if (Amounts.isZero(tally.amountPayRemaining)) {
// We have spent enough!
break;
}
tally = tallyFees(
tally,
wireFeesPerExchange,
wireFeeAmortization,
aci.exchangeBaseUrl,
aci.feeDeposit,
);
let coinSpend = Amounts.max(
Amounts.min(tally.amountPayRemaining, aci.value),
aci.feeDeposit,
);
tally.amountPayRemaining = Amounts.sub(
tally.amountPayRemaining,
coinSpend,
).amount;
contributions.push(coinSpend);
}
if (contributions.length) {
const avKey = makeAvailabilityKey(
aci.exchangeBaseUrl,
aci.denomPubHash,
aci.maxAge,
);
let sd = selectedDenom[avKey];
if (!sd) {
sd = {
contributions: [],
denomPubHash: aci.denomPubHash,
exchangeBaseUrl: aci.exchangeBaseUrl,
maxAge: aci.maxAge,
};
}
sd.contributions.push(...contributions);
selectedDenom[avKey] = sd;
}
if (Amounts.isZero(tally.amountPayRemaining)) {
return selectedDenom;
}
}
return undefined;
}
export function selectForced(
req: SelectPayCoinRequestNg,
candidateDenoms: AvailableDenom[],
): SelResult | undefined {
const selectedDenom: SelResult = {};
const forcedSelection = req.forcedSelection;
checkLogicInvariant(!!forcedSelection);
for (const forcedCoin of forcedSelection.coins) {
let found = false;
for (const aci of candidateDenoms) {
if (aci.numAvailable <= 0) {
continue;
}
if (Amounts.cmp(aci.value, forcedCoin.value) === 0) {
aci.numAvailable--;
const avKey = makeAvailabilityKey(
aci.exchangeBaseUrl,
aci.denomPubHash,
aci.maxAge,
);
let sd = selectedDenom[avKey];
if (!sd) {
sd = {
contributions: [],
denomPubHash: aci.denomPubHash,
exchangeBaseUrl: aci.exchangeBaseUrl,
maxAge: aci.maxAge,
};
}
sd.contributions.push(Amounts.parseOrThrow(forcedCoin.value));
selectedDenom[avKey] = sd;
found = true;
break;
}
}
if (!found) {
throw Error("can't find coin for forced coin selection");
}
}
return selectedDenom;
}
/**
* Given a list of candidate coins, select coins to spend under the merchant's
* constraints.
*
* The prevPayCoins can be specified to "repair" a coin selection
* by adding additional coins, after a broken (e.g. double-spent) coin
* has been removed from the selection.
*
* This function is only exported for the sake of unit tests.
*/
export async function selectPayCoinsNew(
ws: InternalWalletState,
req: SelectPayCoinRequestNg,
): Promise<PayCoinSelection | undefined> {
const {
contractTermsAmount,
depositFeeLimit,
wireFeeLimit,
wireFeeAmortization,
} = req;
const [candidateDenoms, wireFeesPerExchange] = await selectCandidates(
ws,
req,
);
// logger.trace(`candidate denoms: ${j2s(candidateDenoms)}`);
const coinPubs: string[] = [];
const coinContributions: AmountJson[] = [];
const currency = contractTermsAmount.currency;
let tally: CoinSelectionTally = {
amountPayRemaining: contractTermsAmount,
amountWireFeeLimitRemaining: wireFeeLimit,
amountDepositFeeLimitRemaining: depositFeeLimit,
customerDepositFees: Amounts.getZero(currency),
customerWireFees: Amounts.getZero(currency),
wireFeeCoveredForExchange: new Set(),
};
const prevPayCoins = req.prevPayCoins ?? [];
// Look at existing pay coin selection and tally up
for (const prev of prevPayCoins) {
tally = tallyFees(
tally,
wireFeesPerExchange,
wireFeeAmortization,
prev.exchangeBaseUrl,
prev.feeDeposit,
);
tally.amountPayRemaining = Amounts.sub(
tally.amountPayRemaining,
prev.contribution,
).amount;
coinPubs.push(prev.coinPub);
coinContributions.push(prev.contribution);
}
let selectedDenom: SelResult | undefined;
if (req.forcedSelection) {
selectedDenom = selectForced(req, candidateDenoms);
} else {
// FIXME: Here, we should select coins in a smarter way.
// Instead of always spending the next-largest coin,
// we should try to find the smallest coin that covers the
// amount.
selectedDenom = selectGreedy(
req,
candidateDenoms,
wireFeesPerExchange,
tally,
);
}
if (!selectedDenom) {
return undefined;
}
const finalSel = selectedDenom;
logger.trace(`coin selection request ${j2s(req)}`);
logger.trace(`selected coins (via denoms) for payment: ${j2s(finalSel)}`);
await ws.db
.mktx((x) => [x.coins, x.denominations])
.runReadOnly(async (tx) => {
for (const dph of Object.keys(finalSel)) {
const selInfo = finalSel[dph];
const numRequested = selInfo.contributions.length;
const query = [
selInfo.exchangeBaseUrl,
selInfo.denomPubHash,
selInfo.maxAge,
CoinStatus.Fresh,
];
logger.info(`query: ${j2s(query)}`);
const coins =
await tx.coins.indexes.byExchangeDenomPubHashAndAgeAndStatus.getAll(
query,
numRequested,
);
if (coins.length != numRequested) {
throw Error(
`coin selection failed (not available anymore, got only ${coins.length}/${numRequested})`,
);
}
coinPubs.push(...coins.map((x) => x.coinPub));
coinContributions.push(...selInfo.contributions);
}
});
return {
paymentAmount: contractTermsAmount,
coinContributions,
coinPubs,
customerDepositFees: tally.customerDepositFees,
customerWireFees: tally.customerWireFees,
};
}
export async function checkPaymentByProposalId(
ws: InternalWalletState,
proposalId: string,
sessionId?: string,
): Promise<PreparePayResult> {
2021-06-09 15:14:17 +02:00
let proposal = await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.proposals.get(proposalId);
});
2019-12-03 00:52:15 +01:00
if (!proposal) {
throw Error(`could not get proposal ${proposalId}`);
}
if (proposal.proposalStatus === ProposalStatus.Repurchase) {
2019-12-03 00:52:15 +01:00
const existingProposalId = proposal.repurchaseProposalId;
if (!existingProposalId) {
throw Error("invalid proposal state");
}
logger.trace("using existing purchase for same product");
2021-06-09 15:14:17 +02:00
proposal = await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.proposals.get(existingProposalId);
});
2019-12-03 00:52:15 +01:00
if (!proposal) {
throw Error("existing proposal is in wrong state");
}
}
const d = proposal.download;
if (!d) {
logger.error("bad proposal", proposal);
2019-12-03 00:52:15 +01:00
throw Error("proposal is in invalid state");
}
const contractData = d.contractData;
const merchantSig = d.contractData.merchantSig;
if (!merchantSig) {
2019-12-03 00:52:15 +01:00
throw Error("BUG: proposal is in invalid state");
}
2019-12-06 00:56:31 +01:00
proposalId = proposal.proposalId;
2019-12-03 00:52:15 +01:00
2021-04-27 23:42:25 +02:00
// First check if we already paid for it.
2021-06-09 15:14:17 +02:00
const purchase = await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.purchases.get(proposalId);
});
if (!purchase) {
// If not already paid, check if we could pay for it.
const res = await selectPayCoinsNew(ws, {
auditors: contractData.allowedAuditors,
exchanges: contractData.allowedExchanges,
contractTermsAmount: contractData.amount,
depositFeeLimit: contractData.maxDepositFee,
wireFeeAmortization: contractData.wireFeeAmortization ?? 1,
wireFeeLimit: contractData.maxWireFee,
prevPayCoins: [],
requiredMinimumAge: contractData.minimumAge,
wireMethod: contractData.wireMethod,
});
if (!res) {
logger.info("not allowing payment, insufficient coins");
return {
status: PreparePayResultType.InsufficientBalance,
2020-12-21 13:23:07 +01:00
contractTerms: d.contractTermsRaw,
proposalId: proposal.proposalId,
2021-09-17 20:48:33 +02:00
noncePriv: proposal.noncePriv,
2020-08-10 16:35:41 +02:00
amountRaw: Amounts.stringify(d.contractData.amount),
};
}
2020-09-08 17:15:33 +02:00
const totalCost = await getTotalPaymentCost(ws, res);
logger.trace("costInfo", totalCost);
2020-07-29 19:40:41 +02:00
logger.trace("coinsForPayment", res);
2019-12-25 19:11:20 +01:00
return {
status: PreparePayResultType.PaymentPossible,
2020-12-21 13:23:07 +01:00
contractTerms: d.contractTermsRaw,
proposalId: proposal.proposalId,
2021-09-17 20:48:33 +02:00
noncePriv: proposal.noncePriv,
2020-09-08 17:15:33 +02:00
amountEffective: Amounts.stringify(totalCost),
2020-07-29 19:40:41 +02:00
amountRaw: Amounts.stringify(res.paymentAmount),
2021-08-06 11:45:08 +02:00
contractTermsHash: d.contractData.contractTermsHash,
};
}
if (purchase.lastSessionId !== sessionId) {
2020-07-29 19:40:41 +02:00
logger.trace(
"automatically re-submitting payment with different session ID",
);
2021-06-09 15:14:17 +02:00
await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const p = await tx.purchases.get(proposalId);
if (!p) {
return;
}
p.lastSessionId = sessionId;
p.paymentSubmitPending = true;
2021-06-09 15:14:17 +02:00
await tx.purchases.put(p);
});
const r = await processPurchasePay(ws, proposalId, { forceNow: true });
2022-09-05 18:12:30 +02:00
if (r.type !== OperationAttemptResultType.Finished) {
// FIXME: This does not surface the original error
2020-08-11 14:02:11 +02:00
throw Error("submitting pay failed");
}
return {
status: PreparePayResultType.AlreadyConfirmed,
2020-12-21 13:23:07 +01:00
contractTerms: purchase.download.contractTermsRaw,
contractTermsHash: purchase.download.contractData.contractTermsHash,
paid: true,
2020-12-21 13:23:07 +01:00
amountRaw: Amounts.stringify(purchase.download.contractData.amount),
2020-09-08 17:15:33 +02:00
amountEffective: Amounts.stringify(purchase.totalPayCost),
proposalId,
};
} else if (!purchase.timestampFirstSuccessfulPay) {
return {
status: PreparePayResultType.AlreadyConfirmed,
2020-12-21 13:23:07 +01:00
contractTerms: purchase.download.contractTermsRaw,
contractTermsHash: purchase.download.contractData.contractTermsHash,
paid: false,
2020-12-21 13:23:07 +01:00
amountRaw: Amounts.stringify(purchase.download.contractData.amount),
2020-09-08 17:15:33 +02:00
amountEffective: Amounts.stringify(purchase.totalPayCost),
proposalId,
2020-07-30 13:58:09 +02:00
};
} else {
const paid = !purchase.paymentSubmitPending;
return {
status: PreparePayResultType.AlreadyConfirmed,
2020-12-21 13:23:07 +01:00
contractTerms: purchase.download.contractTermsRaw,
contractTermsHash: purchase.download.contractData.contractTermsHash,
paid,
2020-12-21 13:23:07 +01:00
amountRaw: Amounts.stringify(purchase.download.contractData.amount),
2020-09-08 17:15:33 +02:00
amountEffective: Amounts.stringify(purchase.totalPayCost),
2020-12-21 13:23:07 +01:00
...(paid ? { nextUrl: purchase.download.contractData.orderId } : {}),
proposalId,
};
}
}
export async function getContractTermsDetails(
ws: InternalWalletState,
proposalId: string,
): Promise<WalletContractData> {
const proposal = await ws.db
.mktx((x) => [x.proposals])
.runReadOnly(async (tx) => {
return tx.proposals.get(proposalId);
});
if (!proposal) {
throw Error(`proposal with id ${proposalId} not found`);
}
if (!proposal.download || !proposal.download.contractData) {
throw Error("proposal is in invalid state");
}
return proposal.download.contractData;
}
/**
* Check if a payment for the given taler://pay/ URI is possible.
*
* If the payment is possible, the signature are already generated but not
* yet send to the merchant.
*/
export async function preparePayForUri(
ws: InternalWalletState,
talerPayUri: string,
): Promise<PreparePayResult> {
const uriResult = parsePayUri(talerPayUri);
if (!uriResult) {
throw TalerError.fromDetail(
TalerErrorCode.WALLET_INVALID_TALER_PAY_URI,
{
talerPayUri,
},
`invalid taler://pay URI (${talerPayUri})`,
);
}
let proposalId = await startDownloadProposal(
ws,
uriResult.merchantBaseUrl,
uriResult.orderId,
uriResult.sessionId,
uriResult.claimToken,
2021-09-17 20:48:33 +02:00
uriResult.noncePriv,
);
return checkPaymentByProposalId(ws, proposalId, uriResult.sessionId);
}
2021-01-04 13:30:38 +01:00
/**
* Generate deposit permissions for a purchase.
*
* Accesses the database and the crypto worker.
*/
2021-01-18 23:35:41 +01:00
export async function generateDepositPermissions(
2021-01-04 13:30:38 +01:00
ws: InternalWalletState,
payCoinSel: PayCoinSelection,
contractData: WalletContractData,
): Promise<CoinDepositPermission[]> {
const depositPermissions: CoinDepositPermission[] = [];
2021-06-09 15:14:17 +02:00
const coinWithDenom: Array<{
coin: CoinRecord;
denom: DenominationRecord;
}> = [];
await ws.db
.mktx((x) => [x.coins, x.denominations])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
for (let i = 0; i < payCoinSel.coinPubs.length; i++) {
const coin = await tx.coins.get(payCoinSel.coinPubs[i]);
if (!coin) {
throw Error("can't pay, allocated coin not found anymore");
}
const denom = await tx.denominations.get([
coin.exchangeBaseUrl,
coin.denomPubHash,
]);
if (!denom) {
throw Error(
"can't pay, denomination of allocated coin not found anymore",
);
}
coinWithDenom.push({ coin, denom });
}
});
2021-01-04 13:30:38 +01:00
for (let i = 0; i < payCoinSel.coinPubs.length; i++) {
2021-06-09 15:14:17 +02:00
const { coin, denom } = coinWithDenom[i];
2021-11-27 20:56:58 +01:00
let wireInfoHash: string;
wireInfoHash = contractData.wireInfoHash;
logger.trace(
`signing deposit permission for coin with ageRestriction=${j2s(
coin.ageCommitmentProof,
)}`,
);
2021-01-04 13:30:38 +01:00
const dp = await ws.cryptoApi.signDepositPermission({
coinPriv: coin.coinPriv,
coinPub: coin.coinPub,
contractTermsHash: contractData.contractTermsHash,
denomPubHash: coin.denomPubHash,
denomKeyType: denom.denomPub.cipher,
2021-01-04 13:30:38 +01:00
denomSig: coin.denomSig,
exchangeBaseUrl: coin.exchangeBaseUrl,
feeDeposit: denom.fees.feeDeposit,
2021-01-04 13:30:38 +01:00
merchantPub: contractData.merchantPub,
refundDeadline: contractData.refundDeadline,
spendAmount: payCoinSel.coinContributions[i],
timestamp: contractData.timestamp,
2021-11-27 20:56:58 +01:00
wireInfoHash,
ageCommitmentProof: coin.ageCommitmentProof,
requiredMinimumAge: contractData.minimumAge,
2021-01-04 13:30:38 +01:00
});
depositPermissions.push(dp);
}
return depositPermissions;
}
2022-09-05 18:12:30 +02:00
/**
* Run the operation handler for a payment
* and return the result as a {@link ConfirmPayResult}.
*/
export async function runPayForConfirmPay(
ws: InternalWalletState,
proposalId: string,
): Promise<ConfirmPayResult> {
const res = await processPurchasePay(ws, proposalId, { forceNow: true });
switch (res.type) {
case OperationAttemptResultType.Finished: {
const purchase = await ws.db
.mktx((x) => [x.purchases])
2022-09-05 18:12:30 +02:00
.runReadOnly(async (tx) => {
return tx.purchases.get(proposalId);
});
if (!purchase?.download) {
throw Error("purchase record not available anymore");
}
return {
type: ConfirmPayResultType.Done,
contractTerms: purchase.download.contractTermsRaw,
2022-09-16 16:24:47 +02:00
transactionId: makeEventId(TransactionType.Payment, proposalId),
2022-09-05 18:12:30 +02:00
};
}
2022-09-19 12:13:31 +02:00
case OperationAttemptResultType.Error: {
// We hide transient errors from the caller.
const opRetry = await ws.db
.mktx((x) => [x.operationRetries])
.runReadOnly(async (tx) =>
tx.operationRetries.get(RetryTags.byPaymentProposalId(proposalId)),
);
const maxRetry = 3;
const numRetry = opRetry?.retryInfo.retryCounter ?? 0;
if (
res.errorDetail.code ===
TalerErrorCode.WALLET_PAY_MERCHANT_SERVER_ERROR &&
numRetry < maxRetry
) {
// Pretend the operation is pending instead of reporting
// an error, but only up to maxRetry attempts.
await storeOperationPending(
ws,
RetryTags.byPaymentProposalId(proposalId),
);
return {
type: ConfirmPayResultType.Pending,
lastError: opRetry?.lastError,
transactionId: makeEventId(TransactionType.Payment, proposalId),
};
} else {
// FIXME: allocate error code!
await storeOperationError(
ws,
RetryTags.byPaymentProposalId(proposalId),
res.errorDetail,
);
throw Error("payment failed");
}
}
2022-09-05 18:12:30 +02:00
case OperationAttemptResultType.Pending:
2022-09-19 12:13:31 +02:00
await storeOperationPending(ws, `${PendingTaskType.Pay}:${proposalId}`);
2022-09-05 18:12:30 +02:00
return {
type: ConfirmPayResultType.Pending,
transactionId: makeEventId(TransactionType.Payment, proposalId),
2022-09-05 18:12:30 +02:00
lastError: undefined,
};
case OperationAttemptResultType.Longpoll:
throw Error("unexpected processPurchasePay result (longpoll)");
default:
assertUnreachable(res);
}
}
/**
2022-09-19 12:13:31 +02:00
* Confirm payment for a proposal previously claimed by the wallet.
*/
export async function confirmPay(
ws: InternalWalletState,
proposalId: string,
2021-01-07 19:50:53 +01:00
sessionIdOverride?: string,
forcedCoinSel?: ForcedCoinSel,
): Promise<ConfirmPayResult> {
logger.trace(
`executing confirmPay with proposalId ${proposalId} and sessionIdOverride ${sessionIdOverride}`,
);
2021-06-09 15:14:17 +02:00
const proposal = await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.proposals.get(proposalId);
});
if (!proposal) {
throw Error(`proposal with id ${proposalId} not found`);
}
2019-12-03 00:52:15 +01:00
const d = proposal.download;
if (!d) {
throw Error("proposal is in invalid state");
}
2021-06-09 15:14:17 +02:00
const existingPurchase = await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
const purchase = await tx.purchases.get(proposalId);
if (
purchase &&
sessionIdOverride !== undefined &&
sessionIdOverride != purchase.lastSessionId
) {
logger.trace(`changing session ID to ${sessionIdOverride}`);
purchase.lastSessionId = sessionIdOverride;
purchase.paymentSubmitPending = true;
await tx.purchases.put(purchase);
}
return purchase;
});
2021-06-09 15:14:17 +02:00
if (existingPurchase) {
logger.trace("confirmPay: submitting payment for existing purchase");
2022-09-05 18:12:30 +02:00
return runPayForConfirmPay(ws, proposalId);
}
logger.trace("confirmPay: purchase record does not exist yet");
const contractData = d.contractData;
let res: PayCoinSelection | undefined = undefined;
res = await selectPayCoinsNew(ws, {
auditors: contractData.allowedAuditors,
exchanges: contractData.allowedExchanges,
wireMethod: contractData.wireMethod,
contractTermsAmount: contractData.amount,
depositFeeLimit: contractData.maxDepositFee,
wireFeeAmortization: contractData.wireFeeAmortization ?? 1,
wireFeeLimit: contractData.maxWireFee,
prevPayCoins: [],
requiredMinimumAge: contractData.minimumAge,
forcedSelection: forcedCoinSel,
});
logger.trace("coin selection result", res);
if (!res) {
// Should not happen, since checkPay should be called first
// FIXME: Actually, this should be handled gracefully,
// and the status should be stored in the DB.
2020-07-30 13:58:09 +02:00
logger.warn("not confirming payment, insufficient coins");
throw Error("insufficient balance");
}
2021-01-04 13:30:38 +01:00
const depositPermissions = await generateDepositPermissions(
ws,
res,
d.contractData,
);
2022-09-05 18:12:30 +02:00
2021-06-09 15:14:17 +02:00
await recordConfirmPay(
2019-12-15 21:40:06 +01:00
ws,
proposal,
2019-12-25 19:11:20 +01:00
res,
depositPermissions,
sessionIdOverride,
2019-12-15 21:40:06 +01:00
);
2022-09-05 18:12:30 +02:00
return runPayForConfirmPay(ws, proposalId);
}
2019-12-03 14:40:05 +01:00
export async function processPurchasePay(
2019-12-05 19:38:19 +01:00
ws: InternalWalletState,
proposalId: string,
options: {
forceNow?: boolean;
} = {},
2022-09-05 18:12:30 +02:00
): Promise<OperationAttemptResult> {
2021-06-09 15:14:17 +02:00
const purchase = await ws.db
.mktx((x) => [x.purchases])
2021-06-09 15:14:17 +02:00
.runReadOnly(async (tx) => {
return tx.purchases.get(proposalId);
});
2019-12-05 19:38:19 +01:00
if (!purchase) {
return {
2022-09-05 18:12:30 +02:00
type: OperationAttemptResultType.Error,
errorDetail: {
// FIXME: allocate more specific error code
code: TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
hint: `trying to pay for purchase that is not in the database`,
proposalId: proposalId,
},
};
2019-12-05 19:38:19 +01:00
}
if (!purchase.paymentSubmitPending) {
2022-09-05 18:12:30 +02:00
OperationAttemptResult.finishedEmpty();
}
logger.trace(`processing purchase pay ${proposalId}`);
const sessionId = purchase.lastSessionId;
logger.trace("paying with session ID", sessionId);
if (!purchase.merchantPaySig) {
const payUrl = new URL(
`orders/${purchase.download.contractData.orderId}/pay`,
purchase.download.contractData.merchantBaseUrl,
).href;
let depositPermissions: CoinDepositPermission[];
if (purchase.coinDepositPermissions) {
depositPermissions = purchase.coinDepositPermissions;
} else {
// FIXME: also cache!
depositPermissions = await generateDepositPermissions(
ws,
purchase.payCoinSelection,
purchase.download.contractData,
);
}
const reqBody = {
coins: depositPermissions,
session_id: purchase.lastSessionId,
};
logger.trace(
"making pay request ... ",
JSON.stringify(reqBody, undefined, 2),
);
const resp = await ws.runSequentialized([EXCHANGE_COINS_LOCK], () =>
ws.http.postJson(payUrl, reqBody, {
timeout: getPayRequestTimeout(purchase),
}),
);
logger.trace(`got resp ${JSON.stringify(resp)}`);
2022-09-19 12:13:31 +02:00
if (resp.status >= 500 && resp.status <= 599) {
const errDetails = await readUnexpectedResponseDetails(resp);
return {
type: OperationAttemptResultType.Error,
errorDetail: makeErrorDetail(
TalerErrorCode.WALLET_PAY_MERCHANT_SERVER_ERROR,
{
requestError: errDetails,
},
),
};
}
if (resp.status === HttpStatusCode.BadRequest) {
const errDetails = await readUnexpectedResponseDetails(resp);
logger.warn("unexpected 400 response for /pay");
logger.warn(j2s(errDetails));
await ws.db
.mktx((x) => [x.purchases])
.runReadWrite(async (tx) => {
const purch = await tx.purchases.get(proposalId);
if (!purch) {
return;
}
purch.payFrozen = true;
await tx.purchases.put(purch);
});
throw makePendingOperationFailedError(
errDetails,
TransactionType.Payment,
proposalId,
);
}
if (resp.status === HttpStatusCode.Conflict) {
const err = await readTalerErrorResponse(resp);
if (
err.code ===
TalerErrorCode.MERCHANT_POST_ORDERS_ID_PAY_INSUFFICIENT_FUNDS
) {
// Do this in the background, as it might take some time
handleInsufficientFunds(ws, proposalId, err).catch(async (e) => {
2022-09-05 18:12:30 +02:00
console.log("handling insufficient funds failed");
await scheduleRetry(ws, RetryTags.forPay(purchase), {
code: TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
message: "unexpected exception",
hint: "unexpected exception",
details: {
exception: e.toString(),
},
});
});
return {
2022-09-05 18:12:30 +02:00
type: OperationAttemptResultType.Pending,
result: undefined,
};
}
}
const merchantResp = await readSuccessResponseJsonOrThrow(
resp,
codecForMerchantPayResponse(),
);
logger.trace("got success from pay URL", merchantResp);
const merchantPub = purchase.download.contractData.merchantPub;
2022-03-23 21:24:23 +01:00
const { valid } = await ws.cryptoApi.isValidPaymentSignature({
contractHash: purchase.download.contractData.contractTermsHash,
merchantPub,
2022-03-23 21:24:23 +01:00
sig: merchantResp.sig,
});
if (!valid) {
logger.error("merchant payment signature invalid");
// FIXME: properly display error
throw Error("merchant payment signature invalid");
}
await storeFirstPaySuccess(ws, proposalId, sessionId, merchantResp.sig);
await unblockBackup(ws, proposalId);
} else {
const payAgainUrl = new URL(
`orders/${purchase.download.contractData.orderId}/paid`,
purchase.download.contractData.merchantBaseUrl,
).href;
const reqBody = {
sig: purchase.merchantPaySig,
h_contract: purchase.download.contractData.contractTermsHash,
session_id: sessionId ?? "",
};
const resp = await ws.runSequentialized([EXCHANGE_COINS_LOCK], () =>
ws.http.postJson(payAgainUrl, reqBody),
);
if (resp.status !== 204) {
throw TalerError.fromDetail(
TalerErrorCode.WALLET_UNEXPECTED_REQUEST_ERROR,
getHttpResponseErrorDetails(resp),
"/paid failed",
);
}
await storePayReplaySuccess(ws, proposalId, sessionId);
await unblockBackup(ws, proposalId);
}
ws.notify({
type: NotificationType.PayOperationSuccess,
proposalId: purchase.proposalId,
});
2022-09-05 18:12:30 +02:00
return OperationAttemptResult.finishedEmpty();
}
2019-12-20 01:25:22 +01:00
2019-12-25 19:11:20 +01:00
export async function refuseProposal(
ws: InternalWalletState,
proposalId: string,
2020-04-06 20:02:01 +02:00
): Promise<void> {
2021-06-09 15:26:18 +02:00
const success = await ws.db
.mktx((x) => [x.proposals])
2021-06-09 15:26:18 +02:00
.runReadWrite(async (tx) => {
2021-06-09 15:14:17 +02:00
const proposal = await tx.proposals.get(proposalId);
2019-12-25 19:11:20 +01:00
if (!proposal) {
logger.trace(`proposal ${proposalId} not found, won't refuse proposal`);
return false;
}
if (proposal.proposalStatus !== ProposalStatus.Proposed) {
2019-12-25 19:11:20 +01:00
return false;
}
proposal.proposalStatus = ProposalStatus.Refused;
2021-06-09 15:14:17 +02:00
await tx.proposals.put(proposal);
2019-12-25 19:11:20 +01:00
return true;
2021-06-09 15:26:18 +02:00
});
2019-12-20 01:25:22 +01:00
if (success) {
ws.notify({
2020-07-20 12:50:32 +02:00
type: NotificationType.ProposalRefused,
2019-12-20 01:25:22 +01:00
});
}
2019-12-25 19:11:20 +01:00
}