From 6e2881fabf74a3c1da8e94dcbe3e68fce6080d9e Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 10 Mar 2020 14:47:46 +0530 Subject: cleanup --- src/operations/payback.ts | 89 ----------------------------------------------- src/operations/recoup.ts | 89 +++++++++++++++++++++++++++++++++++++++++++++++ src/wallet.ts | 6 +--- src/webex/messages.ts | 4 --- src/webex/wxApi.ts | 7 ---- src/webex/wxBackend.ts | 28 +++++++-------- 6 files changed, 104 insertions(+), 119 deletions(-) delete mode 100644 src/operations/payback.ts create mode 100644 src/operations/recoup.ts (limited to 'src') diff --git a/src/operations/payback.ts b/src/operations/payback.ts deleted file mode 100644 index 181527693..000000000 --- a/src/operations/payback.ts +++ /dev/null @@ -1,89 +0,0 @@ -/* - 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 - */ - -/** - * Imports. - */ -import { - Database -} from "../util/query"; -import { InternalWalletState } from "./state"; -import { Stores, TipRecord, CoinStatus } from "../types/dbTypes"; - -import { Logger } from "../util/logging"; -import { RecoupConfirmation, codecForRecoupConfirmation } from "../types/talerTypes"; -import { updateExchangeFromUrl } from "./exchanges"; -import { NotificationType } from "../types/notifications"; - -const logger = new Logger("payback.ts"); - -export async function payback( - ws: InternalWalletState, - coinPub: string, -): Promise { - let coin = await ws.db.get(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 ws.db.get(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 ws.db.runWithWriteTransaction( - [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 = codecForRecoupConfirmation().decode(await resp.json()); - if (paybackConfirmation.reserve_pub !== coin.reservePub) { - throw Error(`Coin's reserve doesn't match reserve on payback`); - } - coin = await ws.db.get(Stores.coins, coinPub); - if (!coin) { - throw Error(`Coin ${coinPub} not found, can't confirm payback`); - } - coin.status = CoinStatus.Dormant; - await ws.db.put(Stores.coins, coin); - ws.notify({ - type: NotificationType.PaybackFinished, - }); - await updateExchangeFromUrl(ws, coin.exchangeBaseUrl, true); -} diff --git a/src/operations/recoup.ts b/src/operations/recoup.ts new file mode 100644 index 000000000..2b646a4d8 --- /dev/null +++ b/src/operations/recoup.ts @@ -0,0 +1,89 @@ +/* + 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 + */ + +/** + * Imports. + */ +import { + Database +} from "../util/query"; +import { InternalWalletState } from "./state"; +import { Stores, TipRecord, CoinStatus } from "../types/dbTypes"; + +import { Logger } from "../util/logging"; +import { RecoupConfirmation, codecForRecoupConfirmation } from "../types/talerTypes"; +import { updateExchangeFromUrl } from "./exchanges"; +import { NotificationType } from "../types/notifications"; + +const logger = new Logger("payback.ts"); + +export async function recoup( + ws: InternalWalletState, + coinPub: string, +): Promise { + let coin = await ws.db.get(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 ws.db.get(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 ws.db.runWithWriteTransaction( + [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 = codecForRecoupConfirmation().decode(await resp.json()); + if (paybackConfirmation.reserve_pub !== coin.reservePub) { + throw Error(`Coin's reserve doesn't match reserve on payback`); + } + coin = await ws.db.get(Stores.coins, coinPub); + if (!coin) { + throw Error(`Coin ${coinPub} not found, can't confirm payback`); + } + coin.status = CoinStatus.Dormant; + await ws.db.put(Stores.coins, coin); + ws.notify({ + type: NotificationType.PaybackFinished, + }); + await updateExchangeFromUrl(ws, coin.exchangeBaseUrl, true); +} diff --git a/src/wallet.ts b/src/wallet.ts index 12bc2ccbb..23ac8490b 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -95,7 +95,7 @@ import { getHistory } from "./operations/history"; import { getPendingOperations } from "./operations/pending"; import { getBalances } from "./operations/balance"; import { acceptTip, getTipStatus, processTip } from "./operations/tip"; -import { payback } from "./operations/payback"; +import { recoup } from "./operations/recoup"; import { TimerGroup } from "./util/timer"; import { AsyncCondition } from "./util/promiseUtils"; import { AsyncOpMemoSingle } from "./util/asyncMemo"; @@ -577,10 +577,6 @@ export class Wallet { return await this.db.iter(Stores.coins).toArray(); } - async payback(coinPub: string): Promise { - return payback(this.ws, coinPub); - } - async getPaybackReserves(): Promise { return await this.db.iter(Stores.reserves).filter(r => r.hasPayback); } diff --git a/src/webex/messages.ts b/src/webex/messages.ts index 579dd4347..132c8c58d 100644 --- a/src/webex/messages.ts +++ b/src/webex/messages.ts @@ -118,10 +118,6 @@ export interface MessageMap { request: { exchangeBaseUrl: string }; response: dbTypes.DenominationRecord[]; }; - "payback-coin": { - request: { coinPub: string }; - response: void; - }; "check-upgrade": { request: {}; response: UpgradeResponse; diff --git a/src/webex/wxApi.ts b/src/webex/wxApi.ts index bb5222a23..7464b1f74 100644 --- a/src/webex/wxApi.ts +++ b/src/webex/wxApi.ts @@ -193,13 +193,6 @@ export function refresh(coinPub: string): Promise { } -/** - * Request payback for a coin. Only works for non-refreshed coins. - */ -export function payback(coinPub: string): Promise { - return callBackend("payback-coin", { coinPub }); -} - /** * Pay for a proposal. */ diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts index 053b82964..faf917f86 100644 --- a/src/webex/wxBackend.ts +++ b/src/webex/wxBackend.ts @@ -24,9 +24,18 @@ * Imports. */ import { BrowserCryptoWorkerFactory } from "../crypto/workers/cryptoApi"; -import { deleteTalerDatabase, openTalerDatabase, WALLET_DB_VERSION } from "../db"; -import { ConfirmReserveRequest, CreateReserveRequest, ReturnCoinsRequest, WalletDiagnostics, codecForCreateReserveRequest, codecForConfirmReserveRequest } from "../types/walletTypes"; -import { AmountJson, codecForAmountJson } from "../util/amounts"; +import { + deleteTalerDatabase, + openTalerDatabase, + WALLET_DB_VERSION, +} from "../db"; +import { + ReturnCoinsRequest, + WalletDiagnostics, + codecForCreateReserveRequest, + codecForConfirmReserveRequest, +} from "../types/walletTypes"; +import { codecForAmountJson } from "../util/amounts"; import { BrowserHttpLib } from "../util/http"; import { OpenedPromise, openPromise } from "../util/promiseUtils"; import { classifyTalerUri, TalerUriType } from "../util/taleruri"; @@ -67,7 +76,7 @@ async function handleMessage( } case "dump-db": { const db = needsWallet().db; - return db.exportDatabase() + return db.exportDatabase(); } case "import-db": { const db = needsWallet().db; @@ -166,12 +175,6 @@ async function handleMessage( } return needsWallet().refresh(detail.coinPub); } - case "payback-coin": { - if (typeof detail.coinPub !== "string") { - return Promise.reject(Error("coinPub missing")); - } - return needsWallet().payback(detail.coinPub); - } case "get-sender-wire-infos": { return needsWallet().getSenderWireInfos(); } @@ -399,10 +402,7 @@ async function reinitWallet() { setBadgeText({ text: "" }); const badge = new ChromeBadge(); try { - currentDatabase = await openTalerDatabase( - indexedDB, - reinitWallet, - ); + currentDatabase = await openTalerDatabase(indexedDB, reinitWallet); } catch (e) { console.error("could not open database", e); walletInit.reject(e); -- cgit v1.2.3