aboutsummaryrefslogtreecommitdiff
path: root/src/wallet-impl/payback.ts
blob: 8cdfbf7ed51498a9371354e17998c4e18acbe9e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 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 {
  oneShotIter,
  runWithWriteTransaction,
  oneShotGet,
  oneShotPut,
} from "../util/query";
import { InternalWalletState } from "./state";
import { Stores, TipRecord, CoinStatus } from "../dbTypes";

import { Logger } from "../util/logging";
import { PaybackConfirmation } from "../talerTypes";
import { updateExchangeFromUrl } from "./exchanges";
import { NotificationType } from "../walletTypes";

const logger = new Logger("payback.ts");

export async function payback(
  ws: InternalWalletState,
  coinPub: string,
): Promise<void> {
  let coin = await oneShotGet(ws.db, Stores.coins, coinPub);
  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`);
  }
  const reserve = await oneShotGet(ws.db, Stores.reserves, reservePub);
  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;
  await runWithWriteTransaction(
    ws.db,
    [Stores.coins, Stores.reserves],
    async tx => {
      await tx.put(Stores.coins, coin!!);
      await tx.put(Stores.reserves, reserve);
    },
  );
  ws.notify({
    type: NotificationType.PaybackStarted,
  });

  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();
  }
  const paybackConfirmation = PaybackConfirmation.checked(await resp.json());
  if (paybackConfirmation.reserve_pub !== coin.reservePub) {
    throw Error(`Coin's reserve doesn't match reserve on payback`);
  }
  coin = await oneShotGet(ws.db, Stores.coins, coinPub);
  if (!coin) {
    throw Error(`Coin ${coinPub} not found, can't confirm payback`);
  }
  coin.status = CoinStatus.Dormant;
  await oneShotPut(ws.db, Stores.coins, coin);
  ws.notify({
    type: NotificationType.PaybackFinished,
  });
  await updateExchangeFromUrl(ws, coin.exchangeBaseUrl, true);
}