2019-12-02 00:42:40 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
2019-12-16 16:59:09 +01:00
|
|
|
(C) 2019 Taler Systems S.A.
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
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/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { InternalWalletState } from "./state";
|
|
|
|
import { parseTipUri } from "../util/taleruri";
|
2020-09-08 14:10:47 +02:00
|
|
|
import { PrepareTipResult, TalerErrorDetails } from "../types/walletTypes";
|
2019-12-16 16:59:09 +01:00
|
|
|
import {
|
|
|
|
TipPlanchetDetail,
|
2019-12-19 20:42:49 +01:00
|
|
|
codecForTipPickupGetResponse,
|
|
|
|
codecForTipResponse,
|
2019-12-16 16:59:09 +01:00
|
|
|
} from "../types/talerTypes";
|
2019-12-02 00:42:40 +01:00
|
|
|
import * as Amounts from "../util/amounts";
|
2019-12-16 16:59:09 +01:00
|
|
|
import {
|
|
|
|
Stores,
|
2020-05-11 14:33:25 +02:00
|
|
|
TipPlanchet,
|
2020-09-08 15:57:08 +02:00
|
|
|
CoinRecord,
|
|
|
|
CoinSourceType,
|
|
|
|
CoinStatus,
|
2019-12-16 16:59:09 +01:00
|
|
|
} from "../types/dbTypes";
|
|
|
|
import {
|
|
|
|
getExchangeWithdrawalInfo,
|
2020-05-15 12:33:52 +02:00
|
|
|
selectWithdrawalDenoms,
|
|
|
|
denomSelectionInfoToState,
|
2019-12-16 16:59:09 +01:00
|
|
|
} from "./withdraw";
|
2019-12-02 00:42:40 +01:00
|
|
|
import { updateExchangeFromUrl } from "./exchanges";
|
|
|
|
import { getRandomBytes, encodeCrock } from "../crypto/talerCrypto";
|
2020-09-08 15:57:08 +02:00
|
|
|
import { guardOperationException, makeErrorDetails } from "./errors";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { NotificationType } from "../types/notifications";
|
2019-12-19 20:42:49 +01:00
|
|
|
import { getTimestampNow } from "../util/time";
|
2020-11-26 12:27:31 +01:00
|
|
|
import { getHttpResponseErrorDetails, readSuccessResponseJsonOrThrow } from "../util/http";
|
2020-08-03 09:30:48 +02:00
|
|
|
import { URL } from "../util/url";
|
2020-08-14 12:23:50 +02:00
|
|
|
import { Logger } from "../util/logging";
|
2020-09-08 14:10:47 +02:00
|
|
|
import { checkDbInvariant } from "../util/invariants";
|
2020-09-08 15:57:08 +02:00
|
|
|
import { TalerErrorCode } from "../TalerErrorCode";
|
2020-09-08 16:59:47 +02:00
|
|
|
import { initRetryInfo, updateRetryInfoTimeout } from "../util/retries";
|
2020-11-16 14:48:33 +01:00
|
|
|
import { j2s } from "../util/helpers";
|
2020-08-14 12:23:50 +02:00
|
|
|
|
|
|
|
const logger = new Logger("operations/tip.ts");
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-09-08 14:10:47 +02:00
|
|
|
export async function prepareTip(
|
2019-12-02 00:42:40 +01:00
|
|
|
ws: InternalWalletState,
|
2019-12-16 16:59:09 +01:00
|
|
|
talerTipUri: string,
|
2020-09-08 14:10:47 +02:00
|
|
|
): Promise<PrepareTipResult> {
|
2019-12-02 00:42:40 +01:00
|
|
|
const res = parseTipUri(talerTipUri);
|
|
|
|
if (!res) {
|
|
|
|
throw Error("invalid taler://tip URI");
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:48:33 +01:00
|
|
|
let tipRecord = await ws.db.getIndexed(
|
|
|
|
Stores.tips.byMerchantTipIdAndBaseUrl,
|
|
|
|
[res.merchantTipId, res.merchantBaseUrl],
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
if (!tipRecord) {
|
2020-11-16 16:17:26 +01:00
|
|
|
const tipStatusUrl = new URL(
|
|
|
|
`tips/${res.merchantTipId}`,
|
|
|
|
res.merchantBaseUrl,
|
|
|
|
);
|
|
|
|
logger.trace("checking tip status from", tipStatusUrl.href);
|
|
|
|
const merchantResp = await ws.http.get(tipStatusUrl.href);
|
|
|
|
const tipPickupStatus = await readSuccessResponseJsonOrThrow(
|
|
|
|
merchantResp,
|
|
|
|
codecForTipPickupGetResponse(),
|
|
|
|
);
|
|
|
|
logger.trace(`status ${j2s(tipPickupStatus)}`);
|
|
|
|
|
|
|
|
const amount = Amounts.parseOrThrow(tipPickupStatus.tip_amount);
|
|
|
|
|
2020-11-16 14:12:37 +01:00
|
|
|
logger.trace("new tip, creating tip record");
|
2020-05-11 14:33:25 +02:00
|
|
|
await updateExchangeFromUrl(ws, tipPickupStatus.exchange_url);
|
2019-12-09 19:59:08 +01:00
|
|
|
const withdrawDetails = await getExchangeWithdrawalInfo(
|
2019-12-02 00:42:40 +01:00
|
|
|
ws,
|
|
|
|
tipPickupStatus.exchange_url,
|
|
|
|
amount,
|
|
|
|
);
|
|
|
|
|
2020-09-08 14:10:47 +02:00
|
|
|
const walletTipId = encodeCrock(getRandomBytes(32));
|
2020-05-15 12:33:52 +02:00
|
|
|
const selectedDenoms = await selectWithdrawalDenoms(
|
2020-05-11 14:33:25 +02:00
|
|
|
ws,
|
|
|
|
tipPickupStatus.exchange_url,
|
|
|
|
amount,
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
tipRecord = {
|
2020-09-08 14:10:47 +02:00
|
|
|
walletTipId: walletTipId,
|
2019-12-16 12:53:22 +01:00
|
|
|
acceptedTimestamp: undefined,
|
2020-09-08 15:57:08 +02:00
|
|
|
tipAmountRaw: amount,
|
2020-09-08 16:24:23 +02:00
|
|
|
tipExpiration: tipPickupStatus.expiration,
|
|
|
|
exchangeBaseUrl: tipPickupStatus.exchange_url,
|
2019-12-02 00:42:40 +01:00
|
|
|
merchantBaseUrl: res.merchantBaseUrl,
|
|
|
|
planchets: undefined,
|
2019-12-05 19:38:19 +01:00
|
|
|
createdTimestamp: getTimestampNow(),
|
2019-12-02 00:42:40 +01:00
|
|
|
merchantTipId: res.merchantTipId,
|
2020-09-08 17:33:10 +02:00
|
|
|
tipAmountEffective: Amounts.sub(
|
|
|
|
amount,
|
|
|
|
Amounts.add(withdrawDetails.overhead, withdrawDetails.withdrawFee)
|
|
|
|
.amount,
|
|
|
|
).amount,
|
2019-12-05 19:38:19 +01:00
|
|
|
retryInfo: initRetryInfo(),
|
|
|
|
lastError: undefined,
|
2020-05-15 12:33:52 +02:00
|
|
|
denomsSel: denomSelectionInfoToState(selectedDenoms),
|
2020-09-08 16:24:23 +02:00
|
|
|
pickedUpTimestamp: undefined,
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.put(Stores.tips, tipRecord);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:10:47 +02:00
|
|
|
const tipStatus: PrepareTipResult = {
|
2019-12-16 12:53:22 +01:00
|
|
|
accepted: !!tipRecord && !!tipRecord.acceptedTimestamp,
|
2020-11-16 16:17:26 +01:00
|
|
|
tipAmountRaw: Amounts.stringify(tipRecord.tipAmountRaw),
|
|
|
|
exchangeBaseUrl: tipRecord.exchangeBaseUrl,
|
2020-11-18 12:44:06 +01:00
|
|
|
merchantBaseUrl: tipRecord.merchantBaseUrl,
|
2020-11-16 16:17:26 +01:00
|
|
|
expirationTimestamp: tipRecord.tipExpiration,
|
2020-09-08 15:57:08 +02:00
|
|
|
tipAmountEffective: Amounts.stringify(tipRecord.tipAmountEffective),
|
2020-09-08 14:10:47 +02:00
|
|
|
walletTipId: tipRecord.walletTipId,
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return tipStatus;
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:38:19 +01:00
|
|
|
async function incrementTipRetry(
|
|
|
|
ws: InternalWalletState,
|
2020-11-26 12:27:31 +01:00
|
|
|
walletTipId: string,
|
2020-09-01 14:57:22 +02:00
|
|
|
err: TalerErrorDetails | undefined,
|
2019-12-05 19:38:19 +01:00
|
|
|
): Promise<void> {
|
2020-03-30 12:39:32 +02:00
|
|
|
await ws.db.runWithWriteTransaction([Stores.tips], async (tx) => {
|
2020-11-26 12:27:31 +01:00
|
|
|
const t = await tx.get(Stores.tips, walletTipId);
|
2019-12-05 19:38:19 +01:00
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!t.retryInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
t.retryInfo.retryCounter++;
|
|
|
|
updateRetryInfoTimeout(t.retryInfo);
|
|
|
|
t.lastError = err;
|
|
|
|
await tx.put(Stores.tips, t);
|
|
|
|
});
|
2020-09-08 14:10:47 +02:00
|
|
|
if (err) {
|
|
|
|
ws.notify({ type: NotificationType.TipOperationError, error: err });
|
|
|
|
}
|
2019-12-05 19:38:19 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
export async function processTip(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
tipId: string,
|
2020-04-06 17:45:41 +02:00
|
|
|
forceNow = false,
|
2019-12-05 19:38:19 +01:00
|
|
|
): Promise<void> {
|
2020-09-01 14:57:22 +02:00
|
|
|
const onOpErr = (e: TalerErrorDetails): Promise<void> =>
|
2020-04-07 10:07:32 +02:00
|
|
|
incrementTipRetry(ws, tipId, e);
|
2019-12-16 16:59:09 +01:00
|
|
|
await guardOperationException(
|
|
|
|
() => processTipImpl(ws, tipId, forceNow),
|
|
|
|
onOpErr,
|
|
|
|
);
|
2019-12-07 22:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function resetTipRetry(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
tipId: string,
|
|
|
|
): Promise<void> {
|
2020-03-30 12:39:32 +02:00
|
|
|
await ws.db.mutate(Stores.tips, tipId, (x) => {
|
2019-12-07 22:02:11 +01:00
|
|
|
if (x.retryInfo.active) {
|
|
|
|
x.retryInfo = initRetryInfo();
|
|
|
|
}
|
|
|
|
return x;
|
2019-12-16 16:59:09 +01:00
|
|
|
});
|
2019-12-05 19:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function processTipImpl(
|
|
|
|
ws: InternalWalletState,
|
2020-09-08 15:57:08 +02:00
|
|
|
walletTipId: string,
|
2019-12-07 22:02:11 +01:00
|
|
|
forceNow: boolean,
|
2020-04-07 10:07:32 +02:00
|
|
|
): Promise<void> {
|
2019-12-07 22:02:11 +01:00
|
|
|
if (forceNow) {
|
2020-09-08 15:57:08 +02:00
|
|
|
await resetTipRetry(ws, walletTipId);
|
2019-12-07 22:02:11 +01:00
|
|
|
}
|
2020-09-08 15:57:08 +02:00
|
|
|
let tipRecord = await ws.db.get(Stores.tips, walletTipId);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!tipRecord) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-08 16:24:23 +02:00
|
|
|
if (tipRecord.pickedUpTimestamp) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.warn("tip already picked up");
|
2019-12-02 00:42:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-11 14:33:25 +02:00
|
|
|
const denomsForWithdraw = tipRecord.denomsSel;
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-05-11 14:33:25 +02:00
|
|
|
if (!tipRecord.planchets) {
|
|
|
|
const planchets: TipPlanchet[] = [];
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-05-11 14:33:25 +02:00
|
|
|
for (const sd of denomsForWithdraw.selectedDenoms) {
|
2020-09-08 17:33:10 +02:00
|
|
|
const denom = await ws.db.get(Stores.denominations, [
|
|
|
|
tipRecord.exchangeBaseUrl,
|
2020-06-03 13:16:25 +02:00
|
|
|
sd.denomPubHash,
|
2020-09-08 17:33:10 +02:00
|
|
|
]);
|
2020-05-11 14:33:25 +02:00
|
|
|
if (!denom) {
|
|
|
|
throw Error("denom does not exist anymore");
|
|
|
|
}
|
2020-05-11 17:13:19 +02:00
|
|
|
for (let i = 0; i < sd.count; i++) {
|
2020-05-11 14:33:25 +02:00
|
|
|
const r = await ws.cryptoApi.createTipPlanchet(denom);
|
|
|
|
planchets.push(r);
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 15:57:08 +02:00
|
|
|
await ws.db.mutate(Stores.tips, walletTipId, (r) => {
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!r.planchets) {
|
|
|
|
r.planchets = planchets;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:57:08 +02:00
|
|
|
tipRecord = await ws.db.get(Stores.tips, walletTipId);
|
2020-09-08 14:10:47 +02:00
|
|
|
checkDbInvariant(!!tipRecord, "tip record should be in database");
|
|
|
|
checkDbInvariant(!!tipRecord.planchets, "tip record should have planchets");
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
// Planchets in the form that the merchant expects
|
2020-03-30 12:39:32 +02:00
|
|
|
const planchetsDetail: TipPlanchetDetail[] = tipRecord.planchets.map((p) => ({
|
2019-12-02 00:42:40 +01:00
|
|
|
coin_ev: p.coinEv,
|
|
|
|
denom_pub_hash: p.denomPubHash,
|
|
|
|
}));
|
|
|
|
|
2020-09-08 14:10:47 +02:00
|
|
|
const tipStatusUrl = new URL(
|
2020-11-16 14:12:37 +01:00
|
|
|
`tips/${tipRecord.merchantTipId}/pickup`,
|
2020-09-08 14:10:47 +02:00
|
|
|
tipRecord.merchantBaseUrl,
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-09-08 14:10:47 +02:00
|
|
|
const req = { planchets: planchetsDetail };
|
|
|
|
const merchantResp = await ws.http.postJson(tipStatusUrl.href, req);
|
2020-11-26 12:27:31 +01:00
|
|
|
|
|
|
|
// Hide transient errors.
|
|
|
|
if (
|
|
|
|
tipRecord.retryInfo.retryCounter < 5 &&
|
|
|
|
merchantResp.status >= 500 &&
|
|
|
|
merchantResp.status <= 599
|
|
|
|
) {
|
|
|
|
const err = makeErrorDetails(
|
|
|
|
TalerErrorCode.WALLET_UNEXPECTED_REQUEST_ERROR,
|
|
|
|
"tip pickup failed (transient)",
|
|
|
|
getHttpResponseErrorDetails(merchantResp),
|
|
|
|
);
|
|
|
|
await incrementTipRetry(ws, tipRecord.walletTipId, err);
|
|
|
|
// FIXME: Maybe we want to signal to the caller that the transient error happened?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:10:47 +02:00
|
|
|
const response = await readSuccessResponseJsonOrThrow(
|
|
|
|
merchantResp,
|
|
|
|
codecForTipResponse(),
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-09-08 15:57:08 +02:00
|
|
|
if (response.blind_sigs.length !== tipRecord.planchets.length) {
|
2019-12-02 00:42:40 +01:00
|
|
|
throw Error("number of tip responses does not match requested planchets");
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:57:08 +02:00
|
|
|
const newCoinRecords: CoinRecord[] = [];
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-09-08 15:57:08 +02:00
|
|
|
for (let i = 0; i < response.blind_sigs.length; i++) {
|
|
|
|
const blindedSig = response.blind_sigs[i].blind_sig;
|
|
|
|
|
|
|
|
const planchet = tipRecord.planchets[i];
|
|
|
|
|
|
|
|
const denomSig = await ws.cryptoApi.rsaUnblind(
|
|
|
|
blindedSig,
|
|
|
|
planchet.blindingKey,
|
|
|
|
planchet.denomPub,
|
|
|
|
);
|
|
|
|
|
|
|
|
const isValid = await ws.cryptoApi.rsaVerify(
|
|
|
|
planchet.coinPub,
|
|
|
|
denomSig,
|
|
|
|
planchet.denomPub,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isValid) {
|
2020-11-26 22:14:46 +01:00
|
|
|
await ws.db.runWithWriteTransaction([Stores.tips], async (tx) => {
|
2020-09-08 15:57:08 +02:00
|
|
|
const tipRecord = await tx.get(Stores.tips, walletTipId);
|
|
|
|
if (!tipRecord) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tipRecord.lastError = makeErrorDetails(
|
|
|
|
TalerErrorCode.WALLET_TIPPING_COIN_SIGNATURE_INVALID,
|
|
|
|
"invalid signature from the exchange (via merchant tip) after unblinding",
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
await tx.put(Stores.tips, tipRecord);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newCoinRecords.push({
|
|
|
|
blindingKey: planchet.blindingKey,
|
|
|
|
coinPriv: planchet.coinPriv,
|
|
|
|
coinPub: planchet.coinPub,
|
|
|
|
coinSource: {
|
|
|
|
type: CoinSourceType.Tip,
|
|
|
|
coinIndex: i,
|
|
|
|
walletTipId: walletTipId,
|
|
|
|
},
|
|
|
|
currentAmount: planchet.coinValue,
|
|
|
|
denomPub: planchet.denomPub,
|
|
|
|
denomPubHash: planchet.denomPubHash,
|
|
|
|
denomSig: denomSig,
|
2020-09-08 16:24:23 +02:00
|
|
|
exchangeBaseUrl: tipRecord.exchangeBaseUrl,
|
2020-09-08 15:57:08 +02:00
|
|
|
status: CoinStatus.Fresh,
|
|
|
|
suspended: false,
|
|
|
|
});
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2019-12-16 16:59:09 +01:00
|
|
|
await ws.db.runWithWriteTransaction(
|
2020-09-08 15:57:08 +02:00
|
|
|
[Stores.coins, Stores.tips, Stores.withdrawalGroups],
|
2020-03-30 12:39:32 +02:00
|
|
|
async (tx) => {
|
2020-09-08 15:57:08 +02:00
|
|
|
const tr = await tx.get(Stores.tips, walletTipId);
|
2019-12-16 16:59:09 +01:00
|
|
|
if (!tr) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-08 16:24:23 +02:00
|
|
|
if (tr.pickedUpTimestamp) {
|
2019-12-16 16:59:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-09-08 16:24:23 +02:00
|
|
|
tr.pickedUpTimestamp = getTimestampNow();
|
|
|
|
tr.lastError = undefined;
|
2019-12-16 16:59:09 +01:00
|
|
|
tr.retryInfo = initRetryInfo(false);
|
|
|
|
await tx.put(Stores.tips, tr);
|
2020-09-08 15:57:08 +02:00
|
|
|
for (const cr of newCoinRecords) {
|
|
|
|
await tx.put(Stores.coins, cr);
|
2020-05-11 14:33:25 +02:00
|
|
|
}
|
2019-12-16 16:59:09 +01:00
|
|
|
},
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function acceptTip(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
tipId: string,
|
|
|
|
): Promise<void> {
|
2019-12-12 22:39:45 +01:00
|
|
|
const tipRecord = await ws.db.get(Stores.tips, tipId);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!tipRecord) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.error("tip not found");
|
2019-12-02 00:42:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:53:22 +01:00
|
|
|
tipRecord.acceptedTimestamp = getTimestampNow();
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.put(Stores.tips, tipRecord);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
await processTip(ws, tipId);
|
|
|
|
return;
|
|
|
|
}
|