2019-12-02 00:42:40 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2019 GNUnet e.V.
|
|
|
|
|
|
|
|
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";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { TipStatus, getTimestampNow, OperationError } from "../types/walletTypes";
|
|
|
|
import { TipPickupGetResponse, TipPlanchetDetail, TipResponse } from "../types/talerTypes";
|
2019-12-02 00:42:40 +01:00
|
|
|
import * as Amounts from "../util/amounts";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { Stores, PlanchetRecord, WithdrawalSessionRecord, initRetryInfo, updateRetryInfoTimeout } from "../types/dbTypes";
|
2019-12-09 19:59:08 +01:00
|
|
|
import { getExchangeWithdrawalInfo, getVerifiedWithdrawDenomList, processWithdrawSession } from "./withdraw";
|
2019-12-07 18:42:18 +01:00
|
|
|
import { getTalerStampSec, extractTalerStampOrThrow } from "../util/helpers";
|
2019-12-02 00:42:40 +01:00
|
|
|
import { updateExchangeFromUrl } from "./exchanges";
|
|
|
|
import { getRandomBytes, encodeCrock } from "../crypto/talerCrypto";
|
2019-12-05 19:38:19 +01:00
|
|
|
import { guardOperationException } from "./errors";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { NotificationType } from "../types/notifications";
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
export async function getTipStatus(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
talerTipUri: string): Promise<TipStatus> {
|
|
|
|
const res = parseTipUri(talerTipUri);
|
|
|
|
if (!res) {
|
|
|
|
throw Error("invalid taler://tip URI");
|
|
|
|
}
|
|
|
|
|
|
|
|
const tipStatusUrl = new URL("tip-pickup", res.merchantBaseUrl);
|
|
|
|
tipStatusUrl.searchParams.set("tip_id", res.merchantTipId);
|
|
|
|
console.log("checking tip status from", tipStatusUrl.href);
|
|
|
|
const merchantResp = await ws.http.get(tipStatusUrl.href);
|
2019-12-09 13:29:11 +01:00
|
|
|
if (merchantResp.status !== 200) {
|
|
|
|
throw Error(`unexpected status ${merchantResp.status} for tip-pickup`);
|
|
|
|
}
|
|
|
|
const respJson = await merchantResp.json();
|
|
|
|
console.log("resp:", respJson);
|
|
|
|
const tipPickupStatus = TipPickupGetResponse.checked(respJson);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
console.log("status", tipPickupStatus);
|
|
|
|
|
|
|
|
let amount = Amounts.parseOrThrow(tipPickupStatus.amount);
|
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
let tipRecord = await ws.db.get(Stores.tips, [
|
2019-12-02 00:42:40 +01:00
|
|
|
res.merchantTipId,
|
|
|
|
res.merchantOrigin,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!tipRecord) {
|
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,
|
|
|
|
);
|
|
|
|
|
|
|
|
const tipId = encodeCrock(getRandomBytes(32));
|
|
|
|
|
|
|
|
tipRecord = {
|
|
|
|
tipId,
|
2019-12-16 12:53:22 +01:00
|
|
|
acceptedTimestamp: undefined,
|
|
|
|
rejectedTimestamp: undefined,
|
2019-12-02 00:42:40 +01:00
|
|
|
amount,
|
2019-12-07 18:42:18 +01:00
|
|
|
deadline: extractTalerStampOrThrow(tipPickupStatus.stamp_expire),
|
2019-12-02 00:42:40 +01:00
|
|
|
exchangeUrl: tipPickupStatus.exchange_url,
|
|
|
|
merchantBaseUrl: res.merchantBaseUrl,
|
|
|
|
nextUrl: undefined,
|
|
|
|
pickedUp: false,
|
|
|
|
planchets: undefined,
|
|
|
|
response: undefined,
|
2019-12-05 19:38:19 +01:00
|
|
|
createdTimestamp: getTimestampNow(),
|
2019-12-02 00:42:40 +01:00
|
|
|
merchantTipId: res.merchantTipId,
|
|
|
|
totalFees: Amounts.add(
|
|
|
|
withdrawDetails.overhead,
|
|
|
|
withdrawDetails.withdrawFee,
|
|
|
|
).amount,
|
2019-12-05 19:38:19 +01:00
|
|
|
retryInfo: initRetryInfo(),
|
|
|
|
lastError: 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
|
|
|
}
|
|
|
|
|
|
|
|
const tipStatus: TipStatus = {
|
2019-12-16 12:53:22 +01:00
|
|
|
accepted: !!tipRecord && !!tipRecord.acceptedTimestamp,
|
2019-12-02 00:42:40 +01:00
|
|
|
amount: Amounts.parseOrThrow(tipPickupStatus.amount),
|
|
|
|
amountLeft: Amounts.parseOrThrow(tipPickupStatus.amount_left),
|
|
|
|
exchangeUrl: tipPickupStatus.exchange_url,
|
|
|
|
nextUrl: tipPickupStatus.extra.next_url,
|
|
|
|
merchantOrigin: res.merchantOrigin,
|
|
|
|
merchantTipId: res.merchantTipId,
|
|
|
|
expirationTimestamp: getTalerStampSec(tipPickupStatus.stamp_expire)!,
|
|
|
|
timestamp: getTalerStampSec(tipPickupStatus.stamp_created)!,
|
|
|
|
totalFees: tipRecord.totalFees,
|
|
|
|
tipId: tipRecord.tipId,
|
|
|
|
};
|
|
|
|
|
|
|
|
return tipStatus;
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:38:19 +01:00
|
|
|
async function incrementTipRetry(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
refreshSessionId: string,
|
|
|
|
err: OperationError | undefined,
|
|
|
|
): Promise<void> {
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.runWithWriteTransaction([Stores.tips], async tx => {
|
2019-12-05 19:38:19 +01:00
|
|
|
const t = await tx.get(Stores.tips, refreshSessionId);
|
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!t.retryInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
t.retryInfo.retryCounter++;
|
|
|
|
updateRetryInfoTimeout(t.retryInfo);
|
|
|
|
t.lastError = err;
|
|
|
|
await tx.put(Stores.tips, t);
|
|
|
|
});
|
2019-12-06 02:52:16 +01:00
|
|
|
ws.notify({ type: NotificationType.TipOperationError });
|
2019-12-05 19:38:19 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
export async function processTip(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
tipId: string,
|
2019-12-07 22:02:11 +01:00
|
|
|
forceNow: boolean = false,
|
2019-12-05 19:38:19 +01:00
|
|
|
): Promise<void> {
|
|
|
|
const onOpErr = (e: OperationError) => incrementTipRetry(ws, tipId, e);
|
2019-12-07 22:02:11 +01:00
|
|
|
await guardOperationException(() => processTipImpl(ws, tipId, forceNow), onOpErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resetTipRetry(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
tipId: string,
|
|
|
|
): Promise<void> {
|
2019-12-12 22:39:45 +01: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-05 19:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function processTipImpl(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
tipId: string,
|
2019-12-07 22:02:11 +01:00
|
|
|
forceNow: boolean,
|
2019-12-02 00:42:40 +01:00
|
|
|
) {
|
2019-12-07 22:02:11 +01:00
|
|
|
if (forceNow) {
|
|
|
|
await resetTipRetry(ws, tipId);
|
|
|
|
}
|
2019-12-12 22:39:45 +01:00
|
|
|
let tipRecord = await ws.db.get(Stores.tips, tipId);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!tipRecord) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tipRecord.pickedUp) {
|
|
|
|
console.log("tip already picked up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tipRecord.planchets) {
|
|
|
|
await updateExchangeFromUrl(ws, tipRecord.exchangeUrl);
|
|
|
|
const denomsForWithdraw = await getVerifiedWithdrawDenomList(
|
|
|
|
ws,
|
|
|
|
tipRecord.exchangeUrl,
|
|
|
|
tipRecord.amount,
|
|
|
|
);
|
|
|
|
|
|
|
|
const planchets = await Promise.all(
|
|
|
|
denomsForWithdraw.map(d => ws.cryptoApi.createTipPlanchet(d)),
|
|
|
|
);
|
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.mutate(Stores.tips, tipId, r => {
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!r.planchets) {
|
|
|
|
r.planchets = planchets;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
tipRecord = await ws.db.get(Stores.tips, tipId);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!tipRecord) {
|
|
|
|
throw Error("tip not in database");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tipRecord.planchets) {
|
|
|
|
throw Error("invariant violated");
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("got planchets for tip!");
|
|
|
|
|
|
|
|
// Planchets in the form that the merchant expects
|
|
|
|
const planchetsDetail: TipPlanchetDetail[] = tipRecord.planchets.map(p => ({
|
|
|
|
coin_ev: p.coinEv,
|
|
|
|
denom_pub_hash: p.denomPubHash,
|
|
|
|
}));
|
|
|
|
|
|
|
|
let merchantResp;
|
|
|
|
|
|
|
|
const tipStatusUrl = new URL("tip-pickup", tipRecord.merchantBaseUrl);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const req = { planchets: planchetsDetail, tip_id: tipRecord.merchantTipId };
|
|
|
|
merchantResp = await ws.http.postJson(tipStatusUrl.href, req);
|
2019-12-09 13:29:11 +01:00
|
|
|
if (merchantResp.status !== 200) {
|
|
|
|
throw Error(`unexpected status ${merchantResp.status} for tip-pickup`);
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
console.log("got merchant resp:", merchantResp);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("tipping failed", e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2019-12-09 13:29:11 +01:00
|
|
|
const response = TipResponse.checked(await merchantResp.json());
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
if (response.reserve_sigs.length !== tipRecord.planchets.length) {
|
|
|
|
throw Error("number of tip responses does not match requested planchets");
|
|
|
|
}
|
|
|
|
|
|
|
|
const planchets: PlanchetRecord[] = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < tipRecord.planchets.length; i++) {
|
|
|
|
const tipPlanchet = tipRecord.planchets[i];
|
|
|
|
const planchet: PlanchetRecord = {
|
|
|
|
blindingKey: tipPlanchet.blindingKey,
|
|
|
|
coinEv: tipPlanchet.coinEv,
|
|
|
|
coinPriv: tipPlanchet.coinPriv,
|
|
|
|
coinPub: tipPlanchet.coinPub,
|
|
|
|
coinValue: tipPlanchet.coinValue,
|
|
|
|
denomPub: tipPlanchet.denomPub,
|
|
|
|
denomPubHash: tipPlanchet.denomPubHash,
|
|
|
|
reservePub: response.reserve_pub,
|
|
|
|
withdrawSig: response.reserve_sigs[i].reserve_sig,
|
|
|
|
isFromTip: true,
|
|
|
|
};
|
|
|
|
planchets.push(planchet);
|
|
|
|
}
|
|
|
|
|
|
|
|
const withdrawalSessionId = encodeCrock(getRandomBytes(32));
|
|
|
|
|
|
|
|
const withdrawalSession: WithdrawalSessionRecord = {
|
|
|
|
denoms: planchets.map((x) => x.denomPub),
|
|
|
|
exchangeBaseUrl: tipRecord.exchangeUrl,
|
|
|
|
planchets: planchets,
|
|
|
|
source: {
|
|
|
|
type: "tip",
|
|
|
|
tipId: tipRecord.tipId,
|
|
|
|
},
|
|
|
|
startTimestamp: getTimestampNow(),
|
|
|
|
withdrawSessionId: withdrawalSessionId,
|
2019-12-03 01:33:25 +01:00
|
|
|
rawWithdrawalAmount: tipRecord.amount,
|
2019-12-02 00:42:40 +01:00
|
|
|
withdrawn: planchets.map((x) => false),
|
2019-12-03 01:33:25 +01:00
|
|
|
totalCoinValue: Amounts.sum(planchets.map((p) => p.coinValue)).amount,
|
2019-12-16 12:53:22 +01:00
|
|
|
lastErrorPerCoin: {},
|
2019-12-05 19:38:19 +01:00
|
|
|
retryInfo: initRetryInfo(),
|
|
|
|
finishTimestamp: undefined,
|
|
|
|
lastError: undefined,
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.runWithWriteTransaction([Stores.tips, Stores.withdrawalSession], async (tx) => {
|
2019-12-02 00:42:40 +01:00
|
|
|
const tr = await tx.get(Stores.tips, tipId);
|
|
|
|
if (!tr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tr.pickedUp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tr.pickedUp = true;
|
2019-12-05 19:38:19 +01:00
|
|
|
tr.retryInfo = initRetryInfo(false);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
await tx.put(Stores.tips, tr);
|
|
|
|
await tx.put(Stores.withdrawalSession, withdrawalSession);
|
|
|
|
});
|
|
|
|
|
|
|
|
await processWithdrawSession(ws, withdrawalSessionId);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
console.log("tip not found");
|
|
|
|
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;
|
|
|
|
}
|