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/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
|
|
|
import {
|
2019-12-12 22:39:45 +01:00
|
|
|
Database
|
2019-12-02 00:42:40 +01:00
|
|
|
} from "../util/query";
|
|
|
|
import { InternalWalletState } from "./state";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { Stores, TipRecord, CoinStatus } from "../types/dbTypes";
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
import { Logger } from "../util/logging";
|
2019-12-19 20:42:49 +01:00
|
|
|
import { RecoupConfirmation, codecForRecoupConfirmation } from "../types/talerTypes";
|
2019-12-02 00:42:40 +01:00
|
|
|
import { updateExchangeFromUrl } from "./exchanges";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { NotificationType } from "../types/notifications";
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
const logger = new Logger("payback.ts");
|
|
|
|
|
2020-03-10 10:17:46 +01:00
|
|
|
export async function recoup(
|
2019-12-02 00:42:40 +01:00
|
|
|
ws: InternalWalletState,
|
|
|
|
coinPub: string,
|
|
|
|
): Promise<void> {
|
2019-12-12 22:39:45 +01:00
|
|
|
let coin = await ws.db.get(Stores.coins, coinPub);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!coin) {
|
|
|
|
throw Error(`Coin ${coinPub} not found, can't request payback`);
|
|
|
|
}
|
|
|
|
const reservePub = coin.reservePub;
|
|
|
|
if (!reservePub) {
|
|
|
|
throw Error(`Can't request payback for a refreshed coin`);
|
|
|
|
}
|
2019-12-12 22:39:45 +01:00
|
|
|
const reserve = await ws.db.get(Stores.reserves, reservePub);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!reserve) {
|
|
|
|
throw Error(`Reserve of coin ${coinPub} not found`);
|
|
|
|
}
|
|
|
|
switch (coin.status) {
|
|
|
|
case CoinStatus.Dormant:
|
|
|
|
throw Error(`Can't do payback for coin ${coinPub} since it's dormant`);
|
|
|
|
}
|
|
|
|
coin.status = CoinStatus.Dormant;
|
|
|
|
// Even if we didn't get the payback yet, we suspend withdrawal, since
|
|
|
|
// technically we might update reserve status before we get the response
|
|
|
|
// from the reserve for the payback request.
|
|
|
|
reserve.hasPayback = true;
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.runWithWriteTransaction(
|
2019-12-02 00:42:40 +01:00
|
|
|
[Stores.coins, Stores.reserves],
|
|
|
|
async tx => {
|
|
|
|
await tx.put(Stores.coins, coin!!);
|
|
|
|
await tx.put(Stores.reserves, reserve);
|
|
|
|
},
|
|
|
|
);
|
2019-12-05 19:38:19 +01:00
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.PaybackStarted,
|
|
|
|
});
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
const paybackRequest = await ws.cryptoApi.createPaybackRequest(coin);
|
|
|
|
const reqUrl = new URL("payback", coin.exchangeBaseUrl);
|
|
|
|
const resp = await ws.http.postJson(reqUrl.href, paybackRequest);
|
|
|
|
if (resp.status !== 200) {
|
|
|
|
throw Error();
|
|
|
|
}
|
2019-12-19 20:42:49 +01:00
|
|
|
const paybackConfirmation = codecForRecoupConfirmation().decode(await resp.json());
|
2019-12-02 00:42:40 +01:00
|
|
|
if (paybackConfirmation.reserve_pub !== coin.reservePub) {
|
|
|
|
throw Error(`Coin's reserve doesn't match reserve on payback`);
|
|
|
|
}
|
2019-12-12 22:39:45 +01:00
|
|
|
coin = await ws.db.get(Stores.coins, coinPub);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!coin) {
|
|
|
|
throw Error(`Coin ${coinPub} not found, can't confirm payback`);
|
|
|
|
}
|
|
|
|
coin.status = CoinStatus.Dormant;
|
2019-12-12 22:39:45 +01:00
|
|
|
await ws.db.put(Stores.coins, coin);
|
2019-12-05 19:38:19 +01:00
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.PaybackFinished,
|
|
|
|
});
|
2019-12-02 00:42:40 +01:00
|
|
|
await updateExchangeFromUrl(ws, coin.exchangeBaseUrl, true);
|
|
|
|
}
|