2015-12-25 22:42:14 +01:00
|
|
|
/*
|
2019-12-02 00:42:40 +01:00
|
|
|
This file is part of GNU Taler
|
2019-11-30 00:36:20 +01:00
|
|
|
(C) 2015-2019 GNUnet e.V.
|
2015-12-25 22:42:14 +01:00
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
2015-12-25 22:42:14 +01:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
2015-12-25 22:42:14 +01:00
|
|
|
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
|
2019-12-02 00:42:40 +01:00
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2015-12-25 22:42:14 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-05 15:42:46 +01:00
|
|
|
/**
|
|
|
|
* High-level wallet operations that should be indepentent from the underlying
|
|
|
|
* browser extension interface.
|
|
|
|
*/
|
|
|
|
|
2017-05-24 16:52:00 +02:00
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2019-12-09 13:29:11 +01:00
|
|
|
import { CryptoWorkerFactory } from "./crypto/workers/cryptoApi";
|
2019-12-02 00:42:40 +01:00
|
|
|
import { HttpRequestLibrary } from "./util/http";
|
2019-12-16 12:53:22 +01:00
|
|
|
import { Database } from "./util/query";
|
2019-12-02 00:42:40 +01:00
|
|
|
|
|
|
|
import { AmountJson } from "./util/amounts";
|
|
|
|
import * as Amounts from "./util/amounts";
|
2018-01-03 14:42:06 +01:00
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
import {
|
|
|
|
getWithdrawDetailsForUri,
|
2019-12-09 19:59:08 +01:00
|
|
|
getExchangeWithdrawalInfo,
|
2019-12-12 20:53:15 +01:00
|
|
|
} from "./operations/withdraw";
|
2018-01-03 14:42:06 +01:00
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
import {
|
|
|
|
abortFailedPayment,
|
|
|
|
preparePay,
|
|
|
|
confirmPay,
|
2019-12-03 00:52:15 +01:00
|
|
|
processDownloadProposal,
|
2019-12-06 00:24:34 +01:00
|
|
|
processPurchasePay,
|
2019-12-12 20:53:15 +01:00
|
|
|
} from "./operations/pay";
|
2018-01-04 11:35:04 +01:00
|
|
|
|
2016-05-24 01:53:56 +02:00
|
|
|
import {
|
2017-05-28 01:10:54 +02:00
|
|
|
CoinRecord,
|
|
|
|
CurrencyRecord,
|
|
|
|
DenominationRecord,
|
2016-11-15 15:07:17 +01:00
|
|
|
ExchangeRecord,
|
2019-11-30 00:36:20 +01:00
|
|
|
ProposalRecord,
|
2017-08-30 17:08:54 +02:00
|
|
|
PurchaseRecord,
|
2018-01-03 14:42:06 +01:00
|
|
|
ReserveRecord,
|
2018-01-04 11:35:04 +01:00
|
|
|
Stores,
|
2019-11-21 23:09:43 +01:00
|
|
|
ReserveRecordStatus,
|
2019-12-12 20:53:15 +01:00
|
|
|
} from "./types/dbTypes";
|
|
|
|
import { MerchantRefundPermission } from "./types/talerTypes";
|
2018-01-03 14:42:06 +01:00
|
|
|
import {
|
2018-09-20 02:56:13 +02:00
|
|
|
BenchmarkResult,
|
2018-01-03 14:42:06 +01:00
|
|
|
ConfirmPayResult,
|
|
|
|
ConfirmReserveRequest,
|
|
|
|
CreateReserveRequest,
|
|
|
|
CreateReserveResponse,
|
2017-08-14 04:16:12 +02:00
|
|
|
ReturnCoinsRequest,
|
|
|
|
SenderWireInfos,
|
2017-11-30 04:07:36 +01:00
|
|
|
TipStatus,
|
2016-11-13 08:16:12 +01:00
|
|
|
WalletBalance,
|
2019-08-20 17:58:01 +02:00
|
|
|
PreparePayResult,
|
2019-08-29 23:12:55 +02:00
|
|
|
WithdrawDetails,
|
|
|
|
AcceptWithdrawalResponse,
|
2019-08-31 13:27:12 +02:00
|
|
|
PurchaseDetails,
|
2019-12-09 19:59:08 +01:00
|
|
|
ExchangeWithdrawDetails,
|
2019-12-15 16:59:00 +01:00
|
|
|
RefreshReason,
|
2019-12-12 20:53:15 +01:00
|
|
|
} from "./types/walletTypes";
|
2019-12-02 00:42:40 +01:00
|
|
|
import { Logger } from "./util/logging";
|
2015-12-13 23:47:30 +01:00
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
import { assertUnreachable } from "./util/assertUnreachable";
|
|
|
|
|
|
|
|
import {
|
|
|
|
updateExchangeFromUrl,
|
|
|
|
getExchangeTrust,
|
|
|
|
getExchangePaytoUri,
|
2019-12-09 19:59:08 +01:00
|
|
|
acceptExchangeTermsOfService,
|
2019-12-12 20:53:15 +01:00
|
|
|
} from "./operations/exchanges";
|
2019-12-16 16:59:09 +01:00
|
|
|
import { processReserve, createTalerWithdrawReserve } from "./operations/reserves";
|
2019-12-12 20:53:15 +01:00
|
|
|
|
|
|
|
import { InternalWalletState } from "./operations/state";
|
|
|
|
import { createReserve, confirmReserve } from "./operations/reserves";
|
2019-12-15 16:59:00 +01:00
|
|
|
import { processRefreshGroup, createRefreshGroup } from "./operations/refresh";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { processWithdrawSession } from "./operations/withdraw";
|
|
|
|
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";
|
2019-12-02 17:35:47 +01:00
|
|
|
import { TimerGroup } from "./util/timer";
|
|
|
|
import { AsyncCondition } from "./util/promiseUtils";
|
2019-12-05 19:38:19 +01:00
|
|
|
import { AsyncOpMemoSingle } from "./util/asyncMemo";
|
2019-12-16 12:53:22 +01:00
|
|
|
import {
|
|
|
|
PendingOperationInfo,
|
|
|
|
PendingOperationsResponse,
|
|
|
|
PendingOperationType,
|
|
|
|
} from "./types/pending";
|
2019-12-12 20:53:15 +01:00
|
|
|
import { WalletNotification, NotificationType } from "./types/notifications";
|
|
|
|
import { HistoryQuery, HistoryEvent } from "./types/history";
|
2019-12-16 12:53:22 +01:00
|
|
|
import {
|
|
|
|
processPurchaseQueryRefund,
|
|
|
|
processPurchaseApplyRefund,
|
|
|
|
getFullRefundFees,
|
|
|
|
applyRefund,
|
|
|
|
} from "./operations/refund";
|
2019-12-19 20:42:49 +01:00
|
|
|
import { durationMin, Duration } from "./util/time";
|
2017-12-12 21:54:14 +01:00
|
|
|
|
2019-08-31 22:07:16 +02:00
|
|
|
|
2017-03-24 16:59:23 +01:00
|
|
|
const builtinCurrencies: CurrencyRecord[] = [
|
|
|
|
{
|
|
|
|
auditors: [
|
|
|
|
{
|
2017-06-04 19:41:43 +02:00
|
|
|
auditorPub: "BW9DC48PHQY4NH011SHHX36DZZ3Q22Y6X7FZ1VD1CMZ2PTFZ6PN0",
|
2017-04-28 23:28:27 +02:00
|
|
|
baseUrl: "https://auditor.demo.taler.net/",
|
2019-06-26 15:30:32 +02:00
|
|
|
expirationStamp: new Date(2027, 1).getTime(),
|
2017-03-24 16:59:23 +01:00
|
|
|
},
|
2017-04-12 17:47:14 +02:00
|
|
|
],
|
|
|
|
exchanges: [],
|
2017-05-28 01:10:54 +02:00
|
|
|
fractionalDigits: 2,
|
|
|
|
name: "KUDOS",
|
2017-03-24 16:59:23 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2019-11-21 23:09:43 +01:00
|
|
|
const logger = new Logger("wallet.ts");
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* The platform-independent wallet implementation.
|
|
|
|
*/
|
2016-01-10 20:07:42 +01:00
|
|
|
export class Wallet {
|
2019-12-02 00:42:40 +01:00
|
|
|
private ws: InternalWalletState;
|
2019-12-02 17:35:47 +01:00
|
|
|
private timerGroup: TimerGroup = new TimerGroup();
|
|
|
|
private latch = new AsyncCondition();
|
|
|
|
private stopped: boolean = false;
|
2019-12-05 19:38:19 +01:00
|
|
|
private memoRunRetryLoop = new AsyncOpMemoSingle<void>();
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
get db(): Database {
|
2019-12-02 00:42:40 +01:00
|
|
|
return this.ws.db;
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:30:32 +02:00
|
|
|
constructor(
|
2019-12-12 22:39:45 +01:00
|
|
|
db: Database,
|
2019-06-26 15:30:32 +02:00
|
|
|
http: HttpRequestLibrary,
|
2019-08-15 19:10:23 +02:00
|
|
|
cryptoWorkerFactory: CryptoWorkerFactory,
|
2019-06-26 15:30:32 +02:00
|
|
|
) {
|
2019-12-05 19:38:19 +01:00
|
|
|
this.ws = new InternalWalletState(db, http, cryptoWorkerFactory);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getExchangePaytoUri(exchangeBaseUrl: string, supportedTargetTypes: string[]) {
|
|
|
|
return getExchangePaytoUri(this.ws, exchangeBaseUrl, supportedTargetTypes);
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:59:08 +01:00
|
|
|
getWithdrawDetailsForAmount(
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
amount: AmountJson,
|
|
|
|
): Promise<ExchangeWithdrawDetails> {
|
|
|
|
return getExchangeWithdrawalInfo(this.ws, exchangeBaseUrl, amount);
|
2019-11-20 19:48:43 +01:00
|
|
|
}
|
2017-06-05 02:00:03 +02:00
|
|
|
|
2019-12-05 19:38:19 +01:00
|
|
|
addNotificationListener(f: (n: WalletNotification) => void): void {
|
|
|
|
this.ws.addNotificationListener(f);
|
|
|
|
}
|
|
|
|
|
2019-11-21 23:09:43 +01:00
|
|
|
/**
|
2019-11-30 00:36:20 +01:00
|
|
|
* Execute one operation based on the pending operation info record.
|
2019-11-21 23:09:43 +01:00
|
|
|
*/
|
2019-11-30 00:36:20 +01:00
|
|
|
async processOnePendingOperation(
|
|
|
|
pending: PendingOperationInfo,
|
2019-12-03 14:40:05 +01:00
|
|
|
forceNow: boolean = false,
|
2019-11-30 00:36:20 +01:00
|
|
|
): Promise<void> {
|
2019-12-05 22:17:01 +01:00
|
|
|
console.log("running pending", pending);
|
2019-11-30 00:36:20 +01:00
|
|
|
switch (pending.type) {
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.Bug:
|
2019-12-05 19:38:19 +01:00
|
|
|
// Nothing to do, will just be displayed to the user
|
2019-11-30 00:36:20 +01:00
|
|
|
return;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.ExchangeUpdate:
|
2019-12-07 22:02:11 +01:00
|
|
|
await updateExchangeFromUrl(this.ws, pending.exchangeBaseUrl, forceNow);
|
2019-11-30 00:36:20 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.Refresh:
|
2019-12-16 12:53:22 +01:00
|
|
|
await processRefreshGroup(this.ws, pending.refreshGroupId, forceNow);
|
2019-11-30 00:36:20 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.Reserve:
|
2019-12-05 19:38:19 +01:00
|
|
|
await processReserve(this.ws, pending.reservePub, forceNow);
|
2019-11-30 00:36:20 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.Withdraw:
|
2019-12-09 19:59:08 +01:00
|
|
|
await processWithdrawSession(
|
|
|
|
this.ws,
|
|
|
|
pending.withdrawSessionId,
|
|
|
|
forceNow,
|
|
|
|
);
|
2019-11-30 00:36:20 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.ProposalChoice:
|
2019-11-30 00:36:20 +01:00
|
|
|
// Nothing to do, user needs to accept/reject
|
|
|
|
break;
|
2019-12-16 12:53:22 +01:00
|
|
|
case PendingOperationType.ProposalDownload:
|
2019-12-07 22:02:11 +01:00
|
|
|
await processDownloadProposal(this.ws, pending.proposalId, forceNow);
|
2019-12-03 00:52:15 +01:00
|
|
|
break;
|
2019-12-16 12:53:22 +01:00
|
|
|
case PendingOperationType.TipChoice:
|
|
|
|
// Nothing to do, user needs to accept/reject
|
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.TipPickup:
|
2019-12-07 22:02:11 +01:00
|
|
|
await processTip(this.ws, pending.tipId, forceNow);
|
2019-12-02 17:35:47 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.Pay:
|
2019-12-07 22:02:11 +01:00
|
|
|
await processPurchasePay(this.ws, pending.proposalId, forceNow);
|
2019-12-06 00:24:34 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.RefundQuery:
|
2019-12-07 22:02:11 +01:00
|
|
|
await processPurchaseQueryRefund(this.ws, pending.proposalId, forceNow);
|
2019-12-06 00:24:34 +01:00
|
|
|
break;
|
2019-12-15 16:59:00 +01:00
|
|
|
case PendingOperationType.RefundApply:
|
2019-12-07 22:02:11 +01:00
|
|
|
await processPurchaseApplyRefund(this.ws, pending.proposalId, forceNow);
|
2019-12-03 00:52:15 +01:00
|
|
|
break;
|
2019-11-30 00:36:20 +01:00
|
|
|
default:
|
|
|
|
assertUnreachable(pending);
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
2019-11-30 00:36:20 +01:00
|
|
|
}
|
2019-11-21 23:09:43 +01:00
|
|
|
|
2019-11-30 00:36:20 +01:00
|
|
|
/**
|
|
|
|
* Process pending operations.
|
|
|
|
*/
|
2019-12-03 14:40:05 +01:00
|
|
|
public async runPending(forceNow: boolean = false): Promise<void> {
|
2019-12-05 19:38:19 +01:00
|
|
|
const onlyDue = !forceNow;
|
|
|
|
const pendingOpsResponse = await this.getPendingOperations(onlyDue);
|
2019-11-30 00:36:20 +01:00
|
|
|
for (const p of pendingOpsResponse.pendingOperations) {
|
|
|
|
try {
|
2019-12-03 14:40:05 +01:00
|
|
|
await this.processOnePendingOperation(p, forceNow);
|
2019-11-30 00:36:20 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
2019-11-20 19:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-05 19:38:19 +01:00
|
|
|
* Run the wallet until there are no more pending operations that give
|
|
|
|
* liveness left. The wallet will be in a stopped state when this function
|
|
|
|
* returns without resolving to an exception.
|
2019-11-20 19:48:43 +01:00
|
|
|
*/
|
2019-12-05 19:38:19 +01:00
|
|
|
public async runUntilDone(): Promise<void> {
|
|
|
|
const p = new Promise((resolve, reject) => {
|
|
|
|
// Run this asynchronously
|
|
|
|
this.addNotificationListener(n => {
|
|
|
|
if (
|
|
|
|
n.type === NotificationType.WaitingForRetry &&
|
|
|
|
n.numGivingLiveness == 0
|
|
|
|
) {
|
|
|
|
logger.trace("no liveness-giving operations left, stopping");
|
|
|
|
this.stop();
|
2019-12-02 17:35:47 +01:00
|
|
|
}
|
2019-12-05 19:38:19 +01:00
|
|
|
});
|
|
|
|
this.runRetryLoop().catch(e => {
|
|
|
|
console.log("exception in wallet retry loop");
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await p;
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-05 19:38:19 +01:00
|
|
|
* Process pending operations and wait for scheduled operations in
|
|
|
|
* a loop until the wallet is stopped explicitly.
|
2019-11-21 23:09:43 +01:00
|
|
|
*/
|
2019-12-05 19:38:19 +01:00
|
|
|
public async runRetryLoop(): Promise<void> {
|
|
|
|
// Make sure we only run one main loop at a time.
|
|
|
|
return this.memoRunRetryLoop.memo(async () => {
|
|
|
|
try {
|
|
|
|
await this.runRetryLoopImpl();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("error during retry loop execution", e);
|
|
|
|
throw e;
|
2019-11-30 00:36:20 +01:00
|
|
|
}
|
2019-12-05 19:38:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async runRetryLoopImpl(): Promise<void> {
|
|
|
|
while (!this.stopped) {
|
|
|
|
console.log("running wallet retry loop iteration");
|
|
|
|
let pending = await this.getPendingOperations(true);
|
|
|
|
if (pending.pendingOperations.length === 0) {
|
|
|
|
const allPending = await this.getPendingOperations(false);
|
|
|
|
let numPending = 0;
|
|
|
|
let numGivingLiveness = 0;
|
|
|
|
for (const p of allPending.pendingOperations) {
|
|
|
|
numPending++;
|
|
|
|
if (p.givesLifeness) {
|
|
|
|
numGivingLiveness++;
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 20:42:49 +01:00
|
|
|
let dt: Duration;
|
2019-12-05 19:38:19 +01:00
|
|
|
if (
|
|
|
|
allPending.pendingOperations.length === 0 ||
|
|
|
|
allPending.nextRetryDelay.d_ms === Number.MAX_SAFE_INTEGER
|
|
|
|
) {
|
2019-12-06 11:01:39 +01:00
|
|
|
// Wait for 5 seconds
|
2019-12-19 20:42:49 +01:00
|
|
|
dt = { d_ms: 5000 };
|
2019-12-05 19:38:19 +01:00
|
|
|
} else {
|
2019-12-19 20:42:49 +01:00
|
|
|
dt = durationMin({ d_ms: 5000}, allPending.nextRetryDelay);
|
2019-12-05 19:38:19 +01:00
|
|
|
}
|
2019-12-06 11:01:39 +01:00
|
|
|
const timeout = this.timerGroup.resolveAfter(dt);
|
2019-12-05 19:38:19 +01:00
|
|
|
this.ws.notify({
|
|
|
|
type: NotificationType.WaitingForRetry,
|
|
|
|
numGivingLiveness,
|
|
|
|
numPending,
|
|
|
|
});
|
|
|
|
await Promise.race([timeout, this.latch.wait()]);
|
|
|
|
console.log("timeout done");
|
|
|
|
} else {
|
|
|
|
logger.trace("running pending operations that are due");
|
|
|
|
// FIXME: maybe be a bit smarter about executing these
|
2019-12-06 11:01:39 +01:00
|
|
|
// operations in parallel?
|
2019-12-05 19:38:19 +01:00
|
|
|
for (const p of pending.pendingOperations) {
|
|
|
|
try {
|
|
|
|
console.log("running", p);
|
|
|
|
await this.processOnePendingOperation(p);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2019-12-06 11:01:39 +01:00
|
|
|
this.ws.notify({ type: NotificationType.Wildcard });
|
2019-12-05 19:38:19 +01:00
|
|
|
}
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 19:38:19 +01:00
|
|
|
logger.trace("exiting wallet retry loop");
|
2016-05-24 01:18:23 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 19:48:43 +01:00
|
|
|
/**
|
|
|
|
* Insert the hard-coded defaults for exchanges, coins and
|
|
|
|
* auditors into the database, unless these defaults have
|
|
|
|
* already been applied.
|
|
|
|
*/
|
|
|
|
async fillDefaults() {
|
2019-12-12 22:39:45 +01:00
|
|
|
await this.db.runWithWriteTransaction(
|
2019-11-20 20:02:48 +01:00
|
|
|
[Stores.config, Stores.currencies],
|
|
|
|
async tx => {
|
2019-11-20 19:48:43 +01:00
|
|
|
let applied = false;
|
2019-11-20 20:02:48 +01:00
|
|
|
await tx.iter(Stores.config).forEach(x => {
|
2019-11-20 19:48:43 +01:00
|
|
|
if (x.key == "currencyDefaultsApplied" && x.value == true) {
|
|
|
|
applied = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!applied) {
|
|
|
|
for (let c of builtinCurrencies) {
|
|
|
|
await tx.put(Stores.currencies, c);
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 20:02:48 +01:00
|
|
|
},
|
|
|
|
);
|
2017-03-24 16:59:23 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 09:48:00 +02:00
|
|
|
/**
|
|
|
|
* Check if a payment for the given taler://pay/ URI is possible.
|
2019-09-06 11:06:28 +02:00
|
|
|
*
|
2019-09-06 09:48:00 +02:00
|
|
|
* If the payment is possible, the signature are already generated but not
|
|
|
|
* yet send to the merchant.
|
|
|
|
*/
|
|
|
|
async preparePay(talerPayUri: string): Promise<PreparePayResult> {
|
2019-12-02 00:42:40 +01:00
|
|
|
return preparePay(this.ws, talerPayUri);
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 02:03:31 +01:00
|
|
|
/**
|
2018-01-29 16:41:17 +01:00
|
|
|
* Add a contract to the wallet and sign coins, and send them.
|
2016-02-10 02:03:31 +01:00
|
|
|
*/
|
2019-06-26 15:30:32 +02:00
|
|
|
async confirmPay(
|
2019-11-30 00:36:20 +01:00
|
|
|
proposalId: string,
|
2019-08-24 18:42:00 +02:00
|
|
|
sessionIdOverride: string | undefined,
|
2019-06-26 15:30:32 +02:00
|
|
|
): Promise<ConfirmPayResult> {
|
2019-12-02 17:35:47 +01:00
|
|
|
try {
|
|
|
|
return await confirmPay(this.ws, proposalId, sessionIdOverride);
|
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2019-11-30 00:36:20 +01:00
|
|
|
|
2016-02-09 21:56:06 +01:00
|
|
|
/**
|
|
|
|
* First fetch information requred to withdraw from the reserve,
|
|
|
|
* then deplete the reserve, withdrawing coins until it is empty.
|
2019-11-21 23:09:43 +01:00
|
|
|
*
|
|
|
|
* The returned promise resolves once the reserve is set to the
|
|
|
|
* state DORMANT.
|
2016-02-09 21:56:06 +01:00
|
|
|
*/
|
2019-08-01 23:21:15 +02:00
|
|
|
async processReserve(reservePub: string): Promise<void> {
|
2019-12-02 17:35:47 +01:00
|
|
|
try {
|
|
|
|
return await processReserve(this.ws, reservePub);
|
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2016-01-24 19:57:09 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 21:56:06 +01:00
|
|
|
/**
|
|
|
|
* Create a reserve, but do not flag it as confirmed yet.
|
2017-04-28 23:28:27 +02:00
|
|
|
*
|
|
|
|
* Adds the corresponding exchange as a trusted exchange if it is neither
|
|
|
|
* audited nor trusted already.
|
2016-02-09 21:56:06 +01:00
|
|
|
*/
|
2019-06-26 15:30:32 +02:00
|
|
|
async createReserve(
|
|
|
|
req: CreateReserveRequest,
|
|
|
|
): Promise<CreateReserveResponse> {
|
2019-12-02 17:35:47 +01:00
|
|
|
try {
|
|
|
|
return createReserve(this.ws, req);
|
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2016-02-09 21:56:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark an existing reserve as confirmed. The wallet will start trying
|
|
|
|
* to withdraw from that reserve. This may not immediately succeed,
|
2016-03-01 19:39:17 +01:00
|
|
|
* since the exchange might not know about the reserve yet, even though the
|
2016-02-09 21:56:06 +01:00
|
|
|
* bank confirmed its creation.
|
|
|
|
*
|
|
|
|
* A confirmed reserve should be shown to the user in the UI, while
|
|
|
|
* an unconfirmed reserve should be hidden.
|
|
|
|
*/
|
2016-09-28 18:54:48 +02:00
|
|
|
async confirmReserve(req: ConfirmReserveRequest): Promise<void> {
|
2019-12-02 17:35:47 +01:00
|
|
|
try {
|
|
|
|
return confirmReserve(this.ws, req);
|
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2016-09-28 18:54:48 +02:00
|
|
|
|
2016-01-06 15:39:22 +01:00
|
|
|
/**
|
2019-12-02 00:42:40 +01:00
|
|
|
* Check if and how an exchange is trusted and/or audited.
|
2016-01-06 15:39:22 +01:00
|
|
|
*/
|
2019-12-02 00:42:40 +01:00
|
|
|
async getExchangeTrust(
|
|
|
|
exchangeInfo: ExchangeRecord,
|
|
|
|
): Promise<{ isTrusted: boolean; isAudited: boolean }> {
|
|
|
|
return getExchangeTrust(this.ws, exchangeInfo);
|
2019-11-30 00:36:20 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
async getWithdrawDetailsForUri(
|
|
|
|
talerWithdrawUri: string,
|
|
|
|
maybeSelectedExchange?: string,
|
|
|
|
): Promise<WithdrawDetails> {
|
|
|
|
return getWithdrawDetailsForUri(
|
|
|
|
this.ws,
|
|
|
|
talerWithdrawUri,
|
|
|
|
maybeSelectedExchange,
|
2019-11-30 00:36:20 +01:00
|
|
|
);
|
2016-01-06 15:39:22 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 18:17:02 +01:00
|
|
|
/**
|
2019-12-02 00:42:40 +01:00
|
|
|
* Update or add exchange DB entry by fetching the /keys and /wire information.
|
|
|
|
* Optionally link the reserve entry to the new or existing
|
|
|
|
* exchange entry in then DB.
|
2016-02-11 18:17:02 +01:00
|
|
|
*/
|
2019-12-02 00:42:40 +01:00
|
|
|
async updateExchangeFromUrl(
|
|
|
|
baseUrl: string,
|
|
|
|
force: boolean = false,
|
|
|
|
): Promise<ExchangeRecord> {
|
2019-12-06 11:01:39 +01:00
|
|
|
try {
|
|
|
|
return updateExchangeFromUrl(this.ws, baseUrl, force);
|
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-15 18:30:02 +02:00
|
|
|
/**
|
2019-12-02 00:42:40 +01:00
|
|
|
* Get detailed balance information, sliced by exchange and by currency.
|
2017-10-15 18:30:02 +02:00
|
|
|
*/
|
2019-12-02 00:42:40 +01:00
|
|
|
async getBalances(): Promise<WalletBalance> {
|
2019-12-05 19:38:19 +01:00
|
|
|
return this.ws.memoGetBalance.memo(() => getBalances(this.ws));
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2017-10-15 18:30:02 +02:00
|
|
|
|
2019-12-15 16:59:00 +01:00
|
|
|
async refresh(oldCoinPub: string): Promise<void> {
|
2019-12-06 11:01:39 +01:00
|
|
|
try {
|
2019-12-16 12:53:22 +01:00
|
|
|
const refreshGroupId = await this.db.runWithWriteTransaction(
|
|
|
|
[Stores.refreshGroups],
|
|
|
|
async tx => {
|
|
|
|
return await createRefreshGroup(
|
|
|
|
tx,
|
|
|
|
[{ coinPub: oldCoinPub }],
|
|
|
|
RefreshReason.Manual,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2019-12-15 16:59:00 +01:00
|
|
|
await processRefreshGroup(this.ws, refreshGroupId.refreshGroupId);
|
2019-12-06 11:01:39 +01:00
|
|
|
} catch (e) {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2017-10-15 18:30:02 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:02:48 +01:00
|
|
|
async findExchange(
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
): Promise<ExchangeRecord | undefined> {
|
2019-12-12 22:39:45 +01:00
|
|
|
return await this.db.get(Stores.exchanges, exchangeBaseUrl);
|
2019-11-20 19:48:43 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 18:17:02 +01:00
|
|
|
/**
|
|
|
|
* Retrive the full event history for this wallet.
|
|
|
|
*/
|
2019-11-20 19:48:43 +01:00
|
|
|
async getHistory(
|
|
|
|
historyQuery?: HistoryQuery,
|
2019-11-21 23:09:43 +01:00
|
|
|
): Promise<{ history: HistoryEvent[] }> {
|
2019-12-02 00:42:40 +01:00
|
|
|
return getHistory(this.ws, historyQuery);
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:38:19 +01:00
|
|
|
async getPendingOperations(
|
|
|
|
onlyDue: boolean = false,
|
|
|
|
): Promise<PendingOperationsResponse> {
|
|
|
|
return this.ws.memoGetPending.memo(() =>
|
|
|
|
getPendingOperations(this.ws, onlyDue),
|
|
|
|
);
|
2019-11-19 16:16:12 +01:00
|
|
|
}
|
|
|
|
|
2019-12-09 19:59:08 +01:00
|
|
|
async acceptExchangeTermsOfService(
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
etag: string | undefined,
|
|
|
|
) {
|
|
|
|
return acceptExchangeTermsOfService(this.ws, exchangeBaseUrl, etag);
|
|
|
|
}
|
|
|
|
|
2016-11-16 01:59:39 +01:00
|
|
|
async getDenoms(exchangeUrl: string): Promise<DenominationRecord[]> {
|
2019-12-16 12:53:22 +01:00
|
|
|
const denoms = await this.db
|
|
|
|
.iterIndex(Stores.denominations.exchangeBaseUrlIndex, exchangeUrl)
|
|
|
|
.toArray();
|
2016-11-16 01:59:39 +01:00
|
|
|
return denoms;
|
|
|
|
}
|
2016-11-13 10:17:39 +01:00
|
|
|
|
2016-11-15 15:07:17 +01:00
|
|
|
async getExchanges(): Promise<ExchangeRecord[]> {
|
2019-12-12 22:39:45 +01:00
|
|
|
return await this.db.iter(Stores.exchanges).toArray();
|
2016-01-24 02:29:13 +01:00
|
|
|
}
|
2016-02-23 14:07:53 +01:00
|
|
|
|
2017-03-24 17:54:22 +01:00
|
|
|
async getCurrencies(): Promise<CurrencyRecord[]> {
|
2019-12-12 22:39:45 +01:00
|
|
|
return await this.db.iter(Stores.currencies).toArray();
|
2017-03-24 17:54:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateCurrency(currencyRecord: CurrencyRecord): Promise<void> {
|
2019-12-02 00:42:40 +01:00
|
|
|
logger.trace("updating currency to", currencyRecord);
|
2019-12-12 22:39:45 +01:00
|
|
|
await this.db.put(Stores.currencies, currencyRecord);
|
2017-03-24 17:54:22 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 02:23:24 +02:00
|
|
|
async getReserves(exchangeBaseUrl: string): Promise<ReserveRecord[]> {
|
2019-12-16 12:53:22 +01:00
|
|
|
return await this.db
|
|
|
|
.iter(Stores.reserves)
|
|
|
|
.filter(r => r.exchangeBaseUrl === exchangeBaseUrl);
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 23:09:43 +01:00
|
|
|
async getCoinsForExchange(exchangeBaseUrl: string): Promise<CoinRecord[]> {
|
2019-12-16 12:53:22 +01:00
|
|
|
return await this.db
|
|
|
|
.iter(Stores.coins)
|
|
|
|
.filter(c => c.exchangeBaseUrl === exchangeBaseUrl);
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 23:09:43 +01:00
|
|
|
async getCoins(): Promise<CoinRecord[]> {
|
2019-12-12 22:39:45 +01:00
|
|
|
return await this.db.iter(Stores.coins).toArray();
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 04:05:16 +02:00
|
|
|
async payback(coinPub: string): Promise<void> {
|
2019-12-02 00:42:40 +01:00
|
|
|
return payback(this.ws, coinPub);
|
2017-05-01 04:05:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getPaybackReserves(): Promise<ReserveRecord[]> {
|
2019-12-16 12:53:22 +01:00
|
|
|
return await this.db.iter(Stores.reserves).filter(r => r.hasPayback);
|
2017-05-01 04:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 02:00:03 +02:00
|
|
|
/**
|
|
|
|
* Stop ongoing processing.
|
|
|
|
*/
|
|
|
|
stop() {
|
2019-12-02 17:35:47 +01:00
|
|
|
this.stopped = true;
|
|
|
|
this.timerGroup.stopCurrentAndFutureTimers();
|
2019-12-05 19:38:19 +01:00
|
|
|
this.ws.cryptoApi.stop();
|
2017-06-05 02:00:03 +02:00
|
|
|
}
|
2017-08-14 04:16:12 +02:00
|
|
|
|
|
|
|
async getSenderWireInfos(): Promise<SenderWireInfos> {
|
|
|
|
const m: { [url: string]: Set<string> } = {};
|
2019-11-20 19:48:43 +01:00
|
|
|
|
2019-12-12 22:39:45 +01:00
|
|
|
await this.db.iter(Stores.exchanges).forEach(x => {
|
2019-11-20 19:48:43 +01:00
|
|
|
const wi = x.wireInfo;
|
|
|
|
if (!wi) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const s = (m[x.baseUrl] = m[x.baseUrl] || new Set());
|
|
|
|
Object.keys(wi.feesForType).map(k => s.add(k));
|
|
|
|
});
|
|
|
|
|
2017-08-14 04:16:12 +02:00
|
|
|
const exchangeWireTypes: { [url: string]: string[] } = {};
|
2019-06-26 15:30:32 +02:00
|
|
|
Object.keys(m).map(e => {
|
|
|
|
exchangeWireTypes[e] = Array.from(m[e]);
|
|
|
|
});
|
2017-08-14 04:16:12 +02:00
|
|
|
|
2019-07-21 23:50:10 +02:00
|
|
|
const senderWiresSet: Set<string> = new Set();
|
2019-12-12 22:39:45 +01:00
|
|
|
await this.db.iter(Stores.senderWires).forEach(x => {
|
2019-11-20 19:48:43 +01:00
|
|
|
senderWiresSet.add(x.paytoUri);
|
|
|
|
});
|
|
|
|
|
2019-07-21 23:50:10 +02:00
|
|
|
const senderWires: string[] = Array.from(senderWiresSet);
|
2017-08-14 04:16:12 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
exchangeWireTypes,
|
|
|
|
senderWires,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger paying coins back into the user's account.
|
|
|
|
*/
|
|
|
|
async returnCoins(req: ReturnCoinsRequest): Promise<void> {
|
2019-12-19 20:42:49 +01:00
|
|
|
throw Error("not implemented");
|
2018-01-29 16:41:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept a refund, return the contract hash for the contract
|
|
|
|
* that was involved in the refund.
|
|
|
|
*/
|
2019-08-31 11:49:36 +02:00
|
|
|
async applyRefund(talerRefundUri: string): Promise<string> {
|
2019-12-02 00:42:40 +01:00
|
|
|
return applyRefund(this.ws, talerRefundUri);
|
2017-08-27 03:56:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 15:30:32 +02:00
|
|
|
async getPurchase(
|
|
|
|
contractTermsHash: string,
|
|
|
|
): Promise<PurchaseRecord | undefined> {
|
2019-12-12 22:39:45 +01:00
|
|
|
return this.db.get(Stores.purchases, contractTermsHash);
|
2017-08-27 03:56:19 +02:00
|
|
|
}
|
2017-08-30 17:08:54 +02:00
|
|
|
|
2019-06-26 15:30:32 +02:00
|
|
|
async getFullRefundFees(
|
|
|
|
refundPermissions: MerchantRefundPermission[],
|
|
|
|
): Promise<AmountJson> {
|
2019-12-02 00:42:40 +01:00
|
|
|
return getFullRefundFees(this.ws, refundPermissions);
|
2017-08-30 17:08:54 +02:00
|
|
|
}
|
2017-11-30 04:07:36 +01:00
|
|
|
|
2019-11-30 00:36:20 +01:00
|
|
|
async acceptTip(talerTipUri: string): Promise<void> {
|
2019-12-06 11:01:39 +01:00
|
|
|
try {
|
|
|
|
return acceptTip(this.ws, talerTipUri);
|
|
|
|
} catch (e) {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2017-11-30 04:07:36 +01:00
|
|
|
}
|
|
|
|
|
2019-08-30 17:27:59 +02:00
|
|
|
async getTipStatus(talerTipUri: string): Promise<TipStatus> {
|
2019-12-02 00:42:40 +01:00
|
|
|
return getTipStatus(this.ws, talerTipUri);
|
2017-11-30 04:07:36 +01:00
|
|
|
}
|
2017-12-10 21:34:56 +01:00
|
|
|
|
2018-01-29 16:41:17 +01:00
|
|
|
async abortFailedPayment(contractTermsHash: string): Promise<void> {
|
2019-12-06 11:01:39 +01:00
|
|
|
try {
|
|
|
|
return abortFailedPayment(this.ws, contractTermsHash);
|
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2018-01-29 16:41:17 +01:00
|
|
|
}
|
|
|
|
|
2019-12-09 19:59:08 +01:00
|
|
|
/**
|
|
|
|
* Inform the wallet that the status of a reserve has changed (e.g. due to a
|
|
|
|
* confirmation from the bank.).
|
|
|
|
*/
|
2019-11-30 00:36:20 +01:00
|
|
|
public async handleNotifyReserve() {
|
2019-12-12 22:39:45 +01:00
|
|
|
const reserves = await this.db.iter(Stores.reserves).toArray();
|
2019-11-30 00:36:20 +01:00
|
|
|
for (const r of reserves) {
|
|
|
|
if (r.reserveStatus === ReserveRecordStatus.WAIT_CONFIRM_BANK) {
|
|
|
|
try {
|
2019-12-02 00:42:40 +01:00
|
|
|
this.processReserve(r.reservePub);
|
2019-11-30 00:36:20 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 21:34:56 +01:00
|
|
|
/**
|
|
|
|
* Remove unreferenced / expired data from the wallet's database
|
|
|
|
* based on the current system time.
|
|
|
|
*/
|
|
|
|
async collectGarbage() {
|
2019-08-20 17:58:01 +02:00
|
|
|
// FIXME(#5845)
|
|
|
|
// We currently do not garbage-collect the wallet database. This might change
|
|
|
|
// after the feature has been properly re-designed, and we have come up with a
|
|
|
|
// strategy to test it.
|
2017-12-10 21:34:56 +01:00
|
|
|
}
|
2017-12-12 16:39:55 +01:00
|
|
|
|
2019-08-29 23:12:55 +02:00
|
|
|
async acceptWithdrawal(
|
2019-08-28 02:49:27 +02:00
|
|
|
talerWithdrawUri: string,
|
|
|
|
selectedExchange: string,
|
2019-08-29 23:12:55 +02:00
|
|
|
): Promise<AcceptWithdrawalResponse> {
|
2019-12-06 11:01:39 +01:00
|
|
|
try {
|
2019-12-16 16:59:09 +01:00
|
|
|
return createTalerWithdrawReserve(this.ws, talerWithdrawUri, selectedExchange);
|
2019-12-06 11:01:39 +01:00
|
|
|
} finally {
|
|
|
|
this.latch.trigger();
|
|
|
|
}
|
2019-08-28 02:49:27 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 13:27:12 +02:00
|
|
|
async getPurchaseDetails(hc: string): Promise<PurchaseDetails> {
|
2019-12-12 22:39:45 +01:00
|
|
|
const purchase = await this.db.get(Stores.purchases, hc);
|
2019-08-31 13:27:12 +02:00
|
|
|
if (!purchase) {
|
|
|
|
throw Error("unknown purchase");
|
|
|
|
}
|
2019-12-16 12:53:22 +01:00
|
|
|
const refundsDoneAmounts = Object.values(
|
|
|
|
purchase.refundState.refundsDone,
|
|
|
|
).map(x => Amounts.parseOrThrow(x.perm.refund_amount));
|
2019-11-20 20:02:48 +01:00
|
|
|
const refundsPendingAmounts = Object.values(
|
2019-12-15 19:04:14 +01:00
|
|
|
purchase.refundState.refundsPending,
|
|
|
|
).map(x => Amounts.parseOrThrow(x.perm.refund_amount));
|
2019-08-31 13:27:12 +02:00
|
|
|
const totalRefundAmount = Amounts.sum([
|
|
|
|
...refundsDoneAmounts,
|
|
|
|
...refundsPendingAmounts,
|
|
|
|
]).amount;
|
2019-12-16 12:53:22 +01:00
|
|
|
const refundsDoneFees = Object.values(
|
|
|
|
purchase.refundState.refundsDone,
|
|
|
|
).map(x => Amounts.parseOrThrow(x.perm.refund_amount));
|
|
|
|
const refundsPendingFees = Object.values(
|
|
|
|
purchase.refundState.refundsPending,
|
|
|
|
).map(x => Amounts.parseOrThrow(x.perm.refund_amount));
|
2019-08-31 13:27:12 +02:00
|
|
|
const totalRefundFees = Amounts.sum([
|
|
|
|
...refundsDoneFees,
|
|
|
|
...refundsPendingFees,
|
|
|
|
]).amount;
|
|
|
|
const totalFees = totalRefundFees;
|
|
|
|
return {
|
2019-12-19 20:42:49 +01:00
|
|
|
contractTerms: purchase.contractTermsRaw,
|
2019-12-16 16:20:45 +01:00
|
|
|
hasRefund: purchase.timestampLastRefundStatus !== undefined,
|
2019-08-31 13:27:12 +02:00
|
|
|
totalRefundAmount: totalRefundAmount,
|
|
|
|
totalRefundAndRefreshFees: totalFees,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
benchmarkCrypto(repetitions: number): Promise<BenchmarkResult> {
|
2019-12-05 19:38:19 +01:00
|
|
|
return this.ws.cryptoApi.benchmark(repetitions);
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2016-10-18 01:16:31 +02:00
|
|
|
}
|