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.
|
|
|
|
*/
|
2021-05-12 16:18:32 +02:00
|
|
|
import {
|
2021-06-17 21:06:45 +02:00
|
|
|
BalancesResponse,
|
2021-05-12 16:18:32 +02:00
|
|
|
codecForAny,
|
2021-05-20 18:27:35 +02:00
|
|
|
codecForDeleteTransactionRequest,
|
2021-06-14 19:37:35 +02:00
|
|
|
codecForRetryTransactionRequest,
|
2021-06-15 18:52:43 +02:00
|
|
|
codecForSetWalletDeviceIdRequest,
|
2021-09-08 20:30:32 +02:00
|
|
|
codecForGetExchangeWithdrawalInfo,
|
2021-06-10 16:32:37 +02:00
|
|
|
durationFromSpec,
|
|
|
|
durationMin,
|
|
|
|
getDurationRemaining,
|
|
|
|
isTimestampExpired,
|
|
|
|
j2s,
|
2021-05-12 16:18:32 +02:00
|
|
|
TalerErrorCode,
|
2021-06-10 16:32:37 +02:00
|
|
|
Timestamp,
|
|
|
|
timestampMin,
|
2021-06-17 21:06:45 +02:00
|
|
|
WalletNotification,
|
2021-10-14 11:36:43 +02:00
|
|
|
codecForWithdrawFakebankRequest,
|
|
|
|
URL,
|
|
|
|
parsePaytoUri,
|
2021-05-12 16:18:32 +02:00
|
|
|
} from "@gnu-taler/taler-util";
|
2019-12-02 00:42:40 +01:00
|
|
|
import {
|
2021-02-08 15:38:34 +01:00
|
|
|
addBackupProvider,
|
|
|
|
codecForAddBackupProviderRequest,
|
2021-07-15 20:03:45 +02:00
|
|
|
codecForRemoveBackupProvider,
|
2021-07-07 18:28:02 +02:00
|
|
|
codecForRunBackupCycle,
|
2021-02-08 15:38:34 +01:00
|
|
|
getBackupInfo,
|
|
|
|
getBackupRecovery,
|
|
|
|
loadBackupRecovery,
|
2021-06-25 13:27:06 +02:00
|
|
|
processBackupForProvider,
|
2021-07-15 20:03:45 +02:00
|
|
|
removeBackupProvider,
|
2021-02-08 15:38:34 +01:00
|
|
|
runBackupCycle,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/backup/index.js";
|
|
|
|
import { exportBackup } from "./operations/backup/export.js";
|
|
|
|
import { getBalances } from "./operations/balance.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
createDepositGroup,
|
|
|
|
processDepositGroup,
|
|
|
|
trackDepositGroup,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/deposits.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
makeErrorDetails,
|
|
|
|
OperationFailedAndReportedError,
|
|
|
|
OperationFailedError,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./errors.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
acceptExchangeTermsOfService,
|
2021-06-09 15:14:17 +02:00
|
|
|
getExchangeDetails,
|
2021-06-17 21:06:45 +02:00
|
|
|
getExchangeTrust,
|
2021-02-08 15:38:34 +01:00
|
|
|
updateExchangeFromUrl,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/exchanges.js";
|
2019-12-02 00:42:40 +01:00
|
|
|
import {
|
|
|
|
confirmPay,
|
2021-02-08 15:38:34 +01:00
|
|
|
preparePayForUri,
|
2019-12-03 00:52:15 +01:00
|
|
|
processDownloadProposal,
|
2019-12-06 00:24:34 +01:00
|
|
|
processPurchasePay,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/pay.js";
|
|
|
|
import { getPendingOperations } from "./operations/pending.js";
|
2021-06-22 12:18:12 +02:00
|
|
|
import { createRecoupGroup, processRecoupGroup } from "./operations/recoup.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
autoRefresh,
|
|
|
|
createRefreshGroup,
|
|
|
|
processRefreshGroup,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/refresh.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
abortFailedPayWithRefund,
|
|
|
|
applyRefund,
|
|
|
|
processPurchaseQueryRefund,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/refund.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
createReserve,
|
|
|
|
createTalerWithdrawReserve,
|
|
|
|
getFundingPaytoUris,
|
|
|
|
processReserve,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/reserves.js";
|
2021-06-17 21:06:45 +02:00
|
|
|
import {
|
|
|
|
ExchangeOperations,
|
|
|
|
InternalWalletState,
|
2021-11-23 23:51:12 +01:00
|
|
|
MerchantInfo,
|
|
|
|
MerchantOperations,
|
2021-06-17 21:06:45 +02:00
|
|
|
NotificationListener,
|
2021-06-22 12:18:12 +02:00
|
|
|
RecoupOperations,
|
2021-06-17 21:06:45 +02:00
|
|
|
} from "./common.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
runIntegrationTest,
|
|
|
|
testPay,
|
|
|
|
withdrawTestBalance,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/testing.js";
|
|
|
|
import { acceptTip, prepareTip, processTip } from "./operations/tip.js";
|
2021-06-15 18:52:43 +02:00
|
|
|
import {
|
|
|
|
deleteTransaction,
|
|
|
|
getTransactions,
|
|
|
|
retryTransaction,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/transactions.js";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
|
|
|
getExchangeWithdrawalInfo,
|
|
|
|
getWithdrawalDetailsForUri,
|
|
|
|
processWithdrawGroup,
|
2021-06-17 17:40:42 +02:00
|
|
|
} from "./operations/withdraw.js";
|
2016-05-24 01:53:56 +02:00
|
|
|
import {
|
2021-05-20 13:14:47 +02:00
|
|
|
AuditorTrustRecord,
|
2021-02-08 15:38:34 +01:00
|
|
|
CoinSourceType,
|
2021-12-01 18:16:40 +01:00
|
|
|
exportDb,
|
2019-11-21 23:09:43 +01:00
|
|
|
ReserveRecordStatus,
|
2021-06-17 21:06:45 +02:00
|
|
|
WalletStoresV1,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "./db.js";
|
2021-06-15 18:52:43 +02:00
|
|
|
import { NotificationType } from "@gnu-taler/taler-util";
|
2021-02-08 15:38:34 +01:00
|
|
|
import {
|
2021-06-25 13:27:06 +02:00
|
|
|
PendingTaskInfo,
|
2021-06-17 21:06:45 +02:00
|
|
|
PendingOperationsResponse,
|
2021-06-25 13:27:06 +02:00
|
|
|
PendingTaskType,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "./pending-types.js";
|
|
|
|
import { CoinDumpJson } from "@gnu-taler/taler-util";
|
2021-06-17 21:06:45 +02:00
|
|
|
import { codecForTransactionsRequest } from "@gnu-taler/taler-util";
|
2018-01-03 14:42:06 +01:00
|
|
|
import {
|
2020-07-16 19:22:56 +02:00
|
|
|
AcceptManualWithdrawalResult,
|
2021-02-08 15:38:34 +01:00
|
|
|
AcceptWithdrawalResponse,
|
|
|
|
codecForAbortPayWithRefundRequest,
|
|
|
|
codecForAcceptBankIntegratedWithdrawalRequest,
|
2020-08-14 09:36:42 +02:00
|
|
|
codecForAcceptExchangeTosRequest,
|
2021-02-08 15:38:34 +01:00
|
|
|
codecForAcceptManualWithdrawalRequet,
|
|
|
|
codecForAcceptTipRequest,
|
|
|
|
codecForAddExchangeRequest,
|
2020-08-14 09:36:42 +02:00
|
|
|
codecForApplyRefundRequest,
|
|
|
|
codecForConfirmPayRequest,
|
2021-02-08 15:38:34 +01:00
|
|
|
codecForCreateDepositGroupRequest,
|
2020-09-03 22:50:20 +02:00
|
|
|
codecForForceRefreshRequest,
|
2021-02-08 15:38:34 +01:00
|
|
|
codecForGetExchangeTosRequest,
|
|
|
|
codecForGetWithdrawalDetailsForAmountRequest,
|
|
|
|
codecForGetWithdrawalDetailsForUri,
|
|
|
|
codecForIntegrationTestArgs,
|
|
|
|
codecForPreparePayRequest,
|
2020-09-08 14:10:47 +02:00
|
|
|
codecForPrepareTipRequest,
|
2021-02-08 15:38:34 +01:00
|
|
|
codecForSetCoinSuspendedRequest,
|
|
|
|
codecForTestPayArgs,
|
|
|
|
codecForTrackDepositGroupRequest,
|
|
|
|
codecForWithdrawTestBalance,
|
|
|
|
CoreApiResponse,
|
|
|
|
ExchangeListItem,
|
|
|
|
ExchangesListRespose,
|
|
|
|
GetExchangeTosResult,
|
|
|
|
ManualWithdrawalDetails,
|
|
|
|
RefreshReason,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "@gnu-taler/taler-util";
|
|
|
|
import { AmountJson, Amounts } from "@gnu-taler/taler-util";
|
2021-06-17 17:40:42 +02:00
|
|
|
import { assertUnreachable } from "./util/assertUnreachable.js";
|
2021-06-08 20:58:13 +02:00
|
|
|
import { Logger } from "@gnu-taler/taler-util";
|
2021-06-14 11:21:29 +02:00
|
|
|
import { setWalletDeviceId } from "./operations/backup/state.js";
|
2021-06-15 18:58:11 +02:00
|
|
|
import { WalletCoreApiClient } from "./wallet-api-types.js";
|
2021-06-17 21:06:45 +02:00
|
|
|
import { AsyncOpMemoMap, AsyncOpMemoSingle } from "./util/asyncMemo.js";
|
|
|
|
import { CryptoApi, CryptoWorkerFactory } from "./crypto/workers/cryptoApi.js";
|
|
|
|
import { TimerGroup } from "./util/timer.js";
|
|
|
|
import {
|
|
|
|
AsyncCondition,
|
|
|
|
OpenedPromise,
|
|
|
|
openPromise,
|
|
|
|
} from "./util/promiseUtils.js";
|
|
|
|
import { DbAccess } from "./util/query.js";
|
2021-10-14 11:36:43 +02:00
|
|
|
import {
|
|
|
|
HttpRequestLibrary,
|
|
|
|
readSuccessResponseJsonOrThrow,
|
|
|
|
} from "./util/http.js";
|
2021-11-23 23:51:12 +01:00
|
|
|
import { getMerchantInfo } from "./operations/merchants.js";
|
2021-12-01 18:16:40 +01:00
|
|
|
import { Event, IDBDatabase } from "@gnu-taler/idb-bridge";
|
2017-12-12 21:54:14 +01:00
|
|
|
|
2021-05-20 13:14:47 +02:00
|
|
|
const builtinAuditors: AuditorTrustRecord[] = [
|
2017-03-24 16:59:23 +01:00
|
|
|
{
|
2021-05-20 13:14:47 +02:00
|
|
|
currency: "KUDOS",
|
|
|
|
auditorPub: "BW9DC48PHQY4NH011SHHX36DZZ3Q22Y6X7FZ1VD1CMZ2PTFZ6PN0",
|
|
|
|
auditorBaseUrl: "https://auditor.demo.taler.net/",
|
|
|
|
uids: ["5P25XF8TVQP9AW6VYGY2KV47WT5Y3ZXFSJAA570GJPX5SVJXKBVG"],
|
2017-03-24 16:59:23 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2019-11-21 23:09:43 +01:00
|
|
|
const logger = new Logger("wallet.ts");
|
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
async function getWithdrawalDetailsForAmount(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
amount: AmountJson,
|
|
|
|
): Promise<ManualWithdrawalDetails> {
|
|
|
|
const wi = await getExchangeWithdrawalInfo(ws, exchangeBaseUrl, amount);
|
|
|
|
const paytoUris = wi.exchangeDetails.wireInfo.accounts.map(
|
|
|
|
(x) => x.payto_uri,
|
|
|
|
);
|
|
|
|
if (!paytoUris) {
|
|
|
|
throw Error("exchange is in invalid state");
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
return {
|
|
|
|
amountRaw: Amounts.stringify(amount),
|
|
|
|
amountEffective: Amounts.stringify(wi.selectedDenoms.totalCoinValue),
|
|
|
|
paytoUris,
|
|
|
|
tosAccepted: wi.termsOfServiceAccepted,
|
|
|
|
};
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Execute one operation based on the pending operation info record.
|
|
|
|
*/
|
|
|
|
async function processOnePendingOperation(
|
|
|
|
ws: InternalWalletState,
|
2021-06-25 13:27:06 +02:00
|
|
|
pending: PendingTaskInfo,
|
2021-06-15 18:52:43 +02:00
|
|
|
forceNow = false,
|
|
|
|
): Promise<void> {
|
|
|
|
logger.trace(`running pending ${JSON.stringify(pending, undefined, 2)}`);
|
|
|
|
switch (pending.type) {
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.ExchangeUpdate:
|
2021-10-14 11:36:43 +02:00
|
|
|
await updateExchangeFromUrl(
|
|
|
|
ws,
|
|
|
|
pending.exchangeBaseUrl,
|
|
|
|
undefined,
|
|
|
|
forceNow,
|
|
|
|
);
|
2021-06-15 18:52:43 +02:00
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Refresh:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processRefreshGroup(ws, pending.refreshGroupId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Reserve:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processReserve(ws, pending.reservePub, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Withdraw:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processWithdrawGroup(ws, pending.withdrawalGroupId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.ProposalDownload:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processDownloadProposal(ws, pending.proposalId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.TipPickup:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processTip(ws, pending.tipId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Pay:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processPurchasePay(ws, pending.proposalId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.RefundQuery:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processPurchaseQueryRefund(ws, pending.proposalId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Recoup:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processRecoupGroup(ws, pending.recoupGroupId, forceNow);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.ExchangeCheckRefresh:
|
2021-06-15 18:52:43 +02:00
|
|
|
await autoRefresh(ws, pending.exchangeBaseUrl);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Deposit:
|
2021-06-15 18:52:43 +02:00
|
|
|
await processDepositGroup(ws, pending.depositGroupId);
|
|
|
|
break;
|
2021-06-25 13:27:06 +02:00
|
|
|
case PendingTaskType.Backup:
|
|
|
|
await processBackupForProvider(ws, pending.backupProviderBaseUrl);
|
|
|
|
break;
|
2021-06-15 18:52:43 +02:00
|
|
|
default:
|
|
|
|
assertUnreachable(pending);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Process pending operations.
|
|
|
|
*/
|
|
|
|
export async function runPending(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
forceNow = false,
|
|
|
|
): Promise<void> {
|
|
|
|
const pendingOpsResponse = await getPendingOperations(ws);
|
|
|
|
for (const p of pendingOpsResponse.pendingOperations) {
|
|
|
|
if (!forceNow && !isTimestampExpired(p.timestampDue)) {
|
|
|
|
continue;
|
2020-07-11 09:56:07 +02:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
try {
|
|
|
|
await processOnePendingOperation(ws, p, forceNow);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof OperationFailedAndReportedError) {
|
|
|
|
console.error(
|
|
|
|
"Operation failed:",
|
|
|
|
JSON.stringify(e.operationError, undefined, 2),
|
2019-12-09 19:59:08 +01:00
|
|
|
);
|
2021-06-15 18:52:43 +02:00
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
2019-11-30 00:36:20 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2019-11-21 23:09:43 +01:00
|
|
|
|
2021-06-22 13:52:28 +02:00
|
|
|
export interface RetryLoopOpts {
|
|
|
|
/**
|
|
|
|
* Stop when the number of retries is exceeded for any pending
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
maxRetries?: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the retry loop when all lifeness-giving pending operations
|
|
|
|
* are done.
|
|
|
|
*
|
|
|
|
* Defaults to false.
|
|
|
|
*/
|
|
|
|
stopWhenDone?: boolean;
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2019-12-05 19:38:19 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
2021-06-22 13:52:28 +02:00
|
|
|
* Main retry loop of the wallet.
|
|
|
|
*
|
|
|
|
* Looks up pending operations from the wallet, runs them, repeat.
|
2021-06-15 18:52:43 +02:00
|
|
|
*/
|
2021-06-22 13:52:28 +02:00
|
|
|
async function runTaskLoop(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
opts: RetryLoopOpts = {},
|
|
|
|
): Promise<void> {
|
2021-06-15 18:52:43 +02:00
|
|
|
for (let iteration = 0; !ws.stopped; iteration++) {
|
|
|
|
const pending = await getPendingOperations(ws);
|
|
|
|
logger.trace(`pending operations: ${j2s(pending)}`);
|
|
|
|
let numGivingLiveness = 0;
|
|
|
|
let numDue = 0;
|
|
|
|
let minDue: Timestamp = { t_ms: "never" };
|
|
|
|
for (const p of pending.pendingOperations) {
|
|
|
|
minDue = timestampMin(minDue, p.timestampDue);
|
|
|
|
if (isTimestampExpired(p.timestampDue)) {
|
|
|
|
numDue++;
|
|
|
|
}
|
|
|
|
if (p.givesLifeness) {
|
|
|
|
numGivingLiveness++;
|
|
|
|
}
|
2021-06-22 13:52:28 +02:00
|
|
|
|
|
|
|
const maxRetries = opts.maxRetries;
|
|
|
|
|
|
|
|
if (maxRetries && p.retryInfo && p.retryInfo.retryCounter > maxRetries) {
|
|
|
|
logger.warn(
|
|
|
|
`stopping, as ${maxRetries} retries are exceeded in an operation of type ${p.type}`,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2021-06-22 13:52:28 +02:00
|
|
|
|
2021-08-19 15:12:33 +02:00
|
|
|
if (opts.stopWhenDone && numGivingLiveness === 0 && iteration !== 0) {
|
2021-06-22 13:52:28 +02:00
|
|
|
logger.warn(`stopping, as no pending operations have lifeness`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
// Make sure that we run tasks that don't give lifeness at least
|
|
|
|
// one time.
|
|
|
|
if (iteration !== 0 && numDue === 0) {
|
|
|
|
// We've executed pending, due operations at least one.
|
|
|
|
// Now we don't have any more operations available,
|
|
|
|
// and need to wait.
|
|
|
|
|
|
|
|
// Wait for at most 5 seconds to the next check.
|
|
|
|
const dt = durationMin(
|
|
|
|
durationFromSpec({
|
|
|
|
seconds: 5,
|
|
|
|
}),
|
|
|
|
getDurationRemaining(minDue),
|
|
|
|
);
|
|
|
|
logger.trace(`waiting for at most ${dt.d_ms} ms`);
|
|
|
|
const timeout = ws.timerGroup.resolveAfter(dt);
|
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.WaitingForRetry,
|
|
|
|
numGivingLiveness,
|
|
|
|
numPending: pending.pendingOperations.length,
|
|
|
|
});
|
|
|
|
// Wait until either the timeout, or we are notified (via the latch)
|
|
|
|
// that more work might be available.
|
|
|
|
await Promise.race([timeout, ws.latch.wait()]);
|
|
|
|
} else {
|
|
|
|
logger.trace(
|
|
|
|
`running ${pending.pendingOperations.length} pending operations`,
|
|
|
|
);
|
2020-09-03 17:08:26 +02:00
|
|
|
for (const p of pending.pendingOperations) {
|
2021-06-15 18:52:43 +02:00
|
|
|
if (!isTimestampExpired(p.timestampDue)) {
|
|
|
|
continue;
|
2020-09-03 17:08:26 +02:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
try {
|
|
|
|
await processOnePendingOperation(ws, p);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof OperationFailedAndReportedError) {
|
|
|
|
logger.warn("operation processed resulted in reported error");
|
2021-11-27 20:56:58 +01:00
|
|
|
logger.warn(`reported error was: ${j2s(e.operationError)}`);
|
2021-06-15 18:52:43 +02:00
|
|
|
} else {
|
|
|
|
logger.error("Uncaught exception", e);
|
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.InternalError,
|
|
|
|
message: "uncaught exception",
|
|
|
|
exception: e,
|
|
|
|
});
|
2019-12-05 19:38:19 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.PendingOperationProcessed,
|
|
|
|
});
|
2019-11-21 23:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 01:18:23 +02:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
logger.trace("exiting wallet retry loop");
|
|
|
|
}
|
2016-05-24 01:18:23 +02:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Insert the hard-coded defaults for exchanges, coins and
|
|
|
|
* auditors into the database, unless these defaults have
|
|
|
|
* already been applied.
|
|
|
|
*/
|
|
|
|
async function fillDefaults(ws: InternalWalletState): Promise<void> {
|
|
|
|
await ws.db
|
|
|
|
.mktx((x) => ({ config: x.config, auditorTrustStore: x.auditorTrust }))
|
|
|
|
.runReadWrite(async (tx) => {
|
|
|
|
let applied = false;
|
|
|
|
await tx.config.iter().forEach((x) => {
|
|
|
|
if (x.key == "currencyDefaultsApplied" && x.value == true) {
|
|
|
|
applied = true;
|
2019-11-20 19:48:43 +01:00
|
|
|
}
|
2021-06-09 15:14:17 +02:00
|
|
|
});
|
2021-06-15 18:52:43 +02:00
|
|
|
if (!applied) {
|
|
|
|
for (const c of builtinAuditors) {
|
|
|
|
await tx.auditorTrustStore.put(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-24 16:59:23 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
2021-10-14 11:36:43 +02:00
|
|
|
* Create a reserve for a manual withdrawal.
|
2021-06-15 18:52:43 +02:00
|
|
|
*
|
|
|
|
* Adds the corresponding exchange as a trusted exchange if it is neither
|
|
|
|
* audited nor trusted already.
|
|
|
|
*/
|
|
|
|
async function acceptManualWithdrawal(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
amount: AmountJson,
|
|
|
|
): Promise<AcceptManualWithdrawalResult> {
|
|
|
|
try {
|
|
|
|
const resp = await createReserve(ws, {
|
|
|
|
amount,
|
|
|
|
exchange: exchangeBaseUrl,
|
|
|
|
});
|
|
|
|
const exchangePaytoUris = await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
exchanges: x.exchanges,
|
|
|
|
exchangeDetails: x.exchangeDetails,
|
|
|
|
reserves: x.reserves,
|
|
|
|
}))
|
|
|
|
.runReadWrite((tx) => getFundingPaytoUris(tx, resp.reservePub));
|
|
|
|
return {
|
|
|
|
reservePub: resp.reservePub,
|
|
|
|
exchangePaytoUris,
|
|
|
|
};
|
|
|
|
} finally {
|
|
|
|
ws.latch.trigger();
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2018-01-17 03:49:54 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
async function getExchangeTos(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
2021-10-13 19:26:18 +02:00
|
|
|
acceptedFormat?: string[],
|
2021-06-15 18:52:43 +02:00
|
|
|
): Promise<GetExchangeTosResult> {
|
2021-10-13 19:26:18 +02:00
|
|
|
const { exchangeDetails } = await updateExchangeFromUrl(
|
2021-06-15 18:52:43 +02:00
|
|
|
ws,
|
|
|
|
exchangeBaseUrl,
|
2021-10-13 19:26:18 +02:00
|
|
|
acceptedFormat,
|
2021-06-15 18:52:43 +02:00
|
|
|
);
|
2021-10-13 19:26:18 +02:00
|
|
|
const content = exchangeDetails.termsOfServiceText;
|
2021-06-15 18:52:43 +02:00
|
|
|
const currentEtag = exchangeDetails.termsOfServiceLastEtag;
|
2021-10-13 19:26:18 +02:00
|
|
|
const contentType = exchangeDetails.termsOfServiceContentType;
|
2021-10-14 11:36:43 +02:00
|
|
|
if (
|
|
|
|
content === undefined ||
|
|
|
|
currentEtag === undefined ||
|
|
|
|
contentType === undefined
|
|
|
|
) {
|
2021-06-15 18:52:43 +02:00
|
|
|
throw Error("exchange is in invalid state");
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
return {
|
|
|
|
acceptedEtag: exchangeDetails.termsOfServiceAcceptedEtag,
|
|
|
|
currentEtag,
|
2021-10-13 19:26:18 +02:00
|
|
|
content,
|
|
|
|
contentType,
|
2021-06-15 18:52:43 +02:00
|
|
|
};
|
|
|
|
}
|
2019-11-30 00:36:20 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
async function getExchanges(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
): Promise<ExchangesListRespose> {
|
|
|
|
const exchanges: ExchangeListItem[] = [];
|
|
|
|
await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
exchanges: x.exchanges,
|
|
|
|
exchangeDetails: x.exchangeDetails,
|
|
|
|
}))
|
|
|
|
.runReadOnly(async (tx) => {
|
|
|
|
const exchangeRecords = await tx.exchanges.iter().toArray();
|
|
|
|
for (const r of exchangeRecords) {
|
|
|
|
const dp = r.detailsPointer;
|
|
|
|
if (!dp) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-13 19:26:18 +02:00
|
|
|
const { currency } = dp;
|
2021-06-15 18:52:43 +02:00
|
|
|
const exchangeDetails = await getExchangeDetails(tx, r.baseUrl);
|
|
|
|
if (!exchangeDetails) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-24 12:55:37 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
exchanges.push({
|
|
|
|
exchangeBaseUrl: r.baseUrl,
|
|
|
|
currency,
|
2021-11-24 12:55:37 +01:00
|
|
|
tos: {
|
|
|
|
acceptedVersion: exchangeDetails.termsOfServiceAcceptedEtag,
|
|
|
|
currentVersion: exchangeDetails.termsOfServiceLastEtag,
|
|
|
|
contentType: exchangeDetails.termsOfServiceContentType,
|
|
|
|
content: exchangeDetails.termsOfServiceText,
|
|
|
|
},
|
2021-06-15 18:52:43 +02:00
|
|
|
paytoUris: exchangeDetails.wireInfo.accounts.map((x) => x.payto_uri),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return { exchanges };
|
|
|
|
}
|
|
|
|
|
|
|
|
async function acceptWithdrawal(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
talerWithdrawUri: string,
|
|
|
|
selectedExchange: string,
|
|
|
|
): Promise<AcceptWithdrawalResponse> {
|
|
|
|
try {
|
|
|
|
return createTalerWithdrawReserve(ws, talerWithdrawUri, selectedExchange);
|
|
|
|
} finally {
|
|
|
|
ws.latch.trigger();
|
2016-01-24 19:57:09 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2016-01-24 19:57:09 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Inform the wallet that the status of a reserve has changed (e.g. due to a
|
|
|
|
* confirmation from the bank.).
|
|
|
|
*/
|
|
|
|
export async function handleNotifyReserve(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
): Promise<void> {
|
|
|
|
const reserves = await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
reserves: x.reserves,
|
|
|
|
}))
|
|
|
|
.runReadOnly(async (tx) => {
|
|
|
|
return tx.reserves.iter().toArray();
|
|
|
|
});
|
|
|
|
for (const r of reserves) {
|
|
|
|
if (r.reserveStatus === ReserveRecordStatus.WAIT_CONFIRM_BANK) {
|
|
|
|
try {
|
|
|
|
processReserve(ws, r.reservePub);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2019-12-02 17:35:47 +01:00
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
2016-09-28 18:54:48 +02:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
async function setCoinSuspended(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
coinPub: string,
|
|
|
|
suspended: boolean,
|
|
|
|
): Promise<void> {
|
|
|
|
await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
coins: x.coins,
|
|
|
|
}))
|
|
|
|
.runReadWrite(async (tx) => {
|
|
|
|
const c = await tx.coins.get(coinPub);
|
|
|
|
if (!c) {
|
|
|
|
logger.warn(`coin ${coinPub} not found, won't suspend`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c.suspended = suspended;
|
|
|
|
await tx.coins.put(c);
|
|
|
|
});
|
|
|
|
}
|
2019-11-30 00:36:20 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Dump the public information of coins we have in an easy-to-process format.
|
|
|
|
*/
|
|
|
|
async function dumpCoins(ws: InternalWalletState): Promise<CoinDumpJson> {
|
|
|
|
const coinsJson: CoinDumpJson = { coins: [] };
|
|
|
|
await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
coins: x.coins,
|
|
|
|
denominations: x.denominations,
|
|
|
|
withdrawalGroups: x.withdrawalGroups,
|
|
|
|
}))
|
|
|
|
.runReadOnly(async (tx) => {
|
|
|
|
const coins = await tx.coins.iter().toArray();
|
|
|
|
for (const c of coins) {
|
|
|
|
const denom = await tx.denominations.get([
|
|
|
|
c.exchangeBaseUrl,
|
|
|
|
c.denomPubHash,
|
|
|
|
]);
|
|
|
|
if (!denom) {
|
|
|
|
console.error("no denom session found for coin");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const cs = c.coinSource;
|
|
|
|
let refreshParentCoinPub: string | undefined;
|
|
|
|
if (cs.type == CoinSourceType.Refresh) {
|
|
|
|
refreshParentCoinPub = cs.oldCoinPub;
|
|
|
|
}
|
|
|
|
let withdrawalReservePub: string | undefined;
|
|
|
|
if (cs.type == CoinSourceType.Withdraw) {
|
|
|
|
const ws = await tx.withdrawalGroups.get(cs.withdrawalGroupId);
|
|
|
|
if (!ws) {
|
|
|
|
console.error("no withdrawal session found for coin");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
withdrawalReservePub = ws.reservePub;
|
|
|
|
}
|
|
|
|
coinsJson.coins.push({
|
|
|
|
coin_pub: c.coinPub,
|
|
|
|
denom_pub: c.denomPub,
|
|
|
|
denom_pub_hash: c.denomPubHash,
|
|
|
|
denom_value: Amounts.stringify(denom.value),
|
|
|
|
exchange_base_url: c.exchangeBaseUrl,
|
|
|
|
refresh_parent_coin_pub: refreshParentCoinPub,
|
|
|
|
remaining_value: Amounts.stringify(c.currentAmount),
|
|
|
|
withdrawal_reserve_pub: withdrawalReservePub,
|
|
|
|
coin_suspended: c.suspended,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return coinsJson;
|
|
|
|
}
|
2016-01-06 15:39:22 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Get an API client from an internal wallet state object.
|
|
|
|
*/
|
|
|
|
export async function getClientFromWalletState(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
): Promise<WalletCoreApiClient> {
|
|
|
|
let id = 0;
|
|
|
|
const client: WalletCoreApiClient = {
|
|
|
|
async call(op, payload): Promise<any> {
|
|
|
|
const res = await handleCoreApiRequest(ws, op, `${id++}`, payload);
|
|
|
|
switch (res.type) {
|
|
|
|
case "error":
|
|
|
|
throw new OperationFailedError(res.error);
|
|
|
|
case "response":
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return client;
|
|
|
|
}
|
2016-11-16 01:59:39 +01:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Implementation of the "wallet-core" API.
|
|
|
|
*/
|
|
|
|
async function dispatchRequestInternal(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
operation: string,
|
|
|
|
payload: unknown,
|
|
|
|
): Promise<Record<string, any>> {
|
2021-07-30 23:35:38 +02:00
|
|
|
if (!ws.initCalled && operation !== "initWallet") {
|
2021-06-15 18:52:43 +02:00
|
|
|
throw Error(
|
|
|
|
`wallet must be initialized before running operation ${operation}`,
|
2021-06-02 13:23:51 +02:00
|
|
|
);
|
2020-07-11 10:32:17 +02:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
switch (operation) {
|
|
|
|
case "initWallet": {
|
|
|
|
ws.initCalled = true;
|
2021-06-22 12:18:12 +02:00
|
|
|
await fillDefaults(ws);
|
2021-06-15 18:52:43 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "withdrawTestkudos": {
|
|
|
|
await withdrawTestBalance(
|
|
|
|
ws,
|
|
|
|
"TESTKUDOS:10",
|
|
|
|
"https://bank.test.taler.net/",
|
|
|
|
"https://exchange.test.taler.net/",
|
|
|
|
);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "withdrawTestBalance": {
|
|
|
|
const req = codecForWithdrawTestBalance().decode(payload);
|
|
|
|
await withdrawTestBalance(
|
|
|
|
ws,
|
|
|
|
req.amount,
|
|
|
|
req.bankBaseUrl,
|
|
|
|
req.exchangeBaseUrl,
|
|
|
|
);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "runIntegrationTest": {
|
|
|
|
const req = codecForIntegrationTestArgs().decode(payload);
|
|
|
|
await runIntegrationTest(ws, req);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "testPay": {
|
|
|
|
const req = codecForTestPayArgs().decode(payload);
|
|
|
|
await testPay(ws, req);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "getTransactions": {
|
|
|
|
const req = codecForTransactionsRequest().decode(payload);
|
|
|
|
return await getTransactions(ws, req);
|
|
|
|
}
|
|
|
|
case "addExchange": {
|
|
|
|
const req = codecForAddExchangeRequest().decode(payload);
|
2021-10-14 11:36:43 +02:00
|
|
|
await updateExchangeFromUrl(
|
|
|
|
ws,
|
|
|
|
req.exchangeBaseUrl,
|
|
|
|
undefined,
|
|
|
|
req.forceUpdate,
|
|
|
|
);
|
2021-06-15 18:52:43 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "listExchanges": {
|
|
|
|
return await getExchanges(ws);
|
|
|
|
}
|
|
|
|
case "getWithdrawalDetailsForUri": {
|
|
|
|
const req = codecForGetWithdrawalDetailsForUri().decode(payload);
|
|
|
|
return await getWithdrawalDetailsForUri(ws, req.talerWithdrawUri);
|
|
|
|
}
|
2021-09-08 20:30:32 +02:00
|
|
|
case "getExchangeWithdrawalInfo": {
|
|
|
|
const req = codecForGetExchangeWithdrawalInfo().decode(payload);
|
2021-10-14 11:36:43 +02:00
|
|
|
return await getExchangeWithdrawalInfo(
|
|
|
|
ws,
|
|
|
|
req.exchangeBaseUrl,
|
|
|
|
req.amount,
|
|
|
|
);
|
2021-09-08 20:30:32 +02:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
case "acceptManualWithdrawal": {
|
|
|
|
const req = codecForAcceptManualWithdrawalRequet().decode(payload);
|
|
|
|
const res = await acceptManualWithdrawal(
|
|
|
|
ws,
|
|
|
|
req.exchangeBaseUrl,
|
|
|
|
Amounts.parseOrThrow(req.amount),
|
|
|
|
);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
case "getWithdrawalDetailsForAmount": {
|
|
|
|
const req = codecForGetWithdrawalDetailsForAmountRequest().decode(
|
|
|
|
payload,
|
|
|
|
);
|
|
|
|
return await getWithdrawalDetailsForAmount(
|
|
|
|
ws,
|
|
|
|
req.exchangeBaseUrl,
|
|
|
|
Amounts.parseOrThrow(req.amount),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "getBalances": {
|
|
|
|
return await getBalances(ws);
|
|
|
|
}
|
|
|
|
case "getPendingOperations": {
|
|
|
|
return await getPendingOperations(ws);
|
|
|
|
}
|
|
|
|
case "setExchangeTosAccepted": {
|
|
|
|
const req = codecForAcceptExchangeTosRequest().decode(payload);
|
|
|
|
await acceptExchangeTermsOfService(ws, req.exchangeBaseUrl, req.etag);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "applyRefund": {
|
|
|
|
const req = codecForApplyRefundRequest().decode(payload);
|
|
|
|
return await applyRefund(ws, req.talerRefundUri);
|
|
|
|
}
|
|
|
|
case "acceptBankIntegratedWithdrawal": {
|
|
|
|
const req = codecForAcceptBankIntegratedWithdrawalRequest().decode(
|
|
|
|
payload,
|
|
|
|
);
|
|
|
|
return await acceptWithdrawal(
|
|
|
|
ws,
|
|
|
|
req.talerWithdrawUri,
|
|
|
|
req.exchangeBaseUrl,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "getExchangeTos": {
|
|
|
|
const req = codecForGetExchangeTosRequest().decode(payload);
|
2021-10-14 11:36:43 +02:00
|
|
|
return getExchangeTos(ws, req.exchangeBaseUrl, req.acceptedFormat);
|
2021-06-15 18:52:43 +02:00
|
|
|
}
|
|
|
|
case "retryPendingNow": {
|
|
|
|
await runPending(ws, true);
|
|
|
|
return {};
|
|
|
|
}
|
2021-06-17 13:34:59 +02:00
|
|
|
// FIXME: Deprecate one of the aliases!
|
|
|
|
case "preparePayForUri":
|
2021-06-15 18:52:43 +02:00
|
|
|
case "preparePay": {
|
|
|
|
const req = codecForPreparePayRequest().decode(payload);
|
|
|
|
return await preparePayForUri(ws, req.talerPayUri);
|
|
|
|
}
|
|
|
|
case "confirmPay": {
|
|
|
|
const req = codecForConfirmPayRequest().decode(payload);
|
|
|
|
return await confirmPay(ws, req.proposalId, req.sessionId);
|
|
|
|
}
|
|
|
|
case "abortFailedPayWithRefund": {
|
|
|
|
const req = codecForAbortPayWithRefundRequest().decode(payload);
|
|
|
|
await abortFailedPayWithRefund(ws, req.proposalId);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "dumpCoins": {
|
|
|
|
return await dumpCoins(ws);
|
|
|
|
}
|
|
|
|
case "setCoinSuspended": {
|
|
|
|
const req = codecForSetCoinSuspendedRequest().decode(payload);
|
|
|
|
await setCoinSuspended(ws, req.coinPub, req.suspended);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "forceRefresh": {
|
|
|
|
const req = codecForForceRefreshRequest().decode(payload);
|
|
|
|
const coinPubs = req.coinPubList.map((x) => ({ coinPub: x }));
|
|
|
|
const refreshGroupId = await ws.db
|
2021-06-09 15:14:17 +02:00
|
|
|
.mktx((x) => ({
|
|
|
|
refreshGroups: x.refreshGroups,
|
|
|
|
denominations: x.denominations,
|
|
|
|
coins: x.coins,
|
|
|
|
}))
|
|
|
|
.runReadWrite(async (tx) => {
|
2019-12-16 12:53:22 +01:00
|
|
|
return await createRefreshGroup(
|
2021-06-15 18:52:43 +02:00
|
|
|
ws,
|
2019-12-16 12:53:22 +01:00
|
|
|
tx,
|
2021-06-15 18:52:43 +02:00
|
|
|
coinPubs,
|
2019-12-16 12:53:22 +01:00
|
|
|
RefreshReason.Manual,
|
|
|
|
);
|
2021-06-09 15:14:17 +02:00
|
|
|
});
|
2021-06-15 18:52:43 +02:00
|
|
|
processRefreshGroup(ws, refreshGroupId.refreshGroupId, true).catch(
|
|
|
|
(x) => {
|
|
|
|
logger.error(x);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
refreshGroupId,
|
|
|
|
};
|
2019-12-06 11:01:39 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
case "prepareTip": {
|
|
|
|
const req = codecForPrepareTipRequest().decode(payload);
|
|
|
|
return await prepareTip(ws, req.talerTipUri);
|
2019-12-06 11:01:39 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
case "acceptTip": {
|
|
|
|
const req = codecForAcceptTipRequest().decode(payload);
|
|
|
|
await acceptTip(ws, req.walletTipId);
|
|
|
|
return {};
|
2019-11-30 00:36:20 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
case "exportBackupPlain": {
|
|
|
|
return exportBackup(ws);
|
2019-12-06 11:01:39 +01:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
case "addBackupProvider": {
|
|
|
|
const req = codecForAddBackupProviderRequest().decode(payload);
|
|
|
|
await addBackupProvider(ws, req);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "runBackupCycle": {
|
2021-07-07 18:28:02 +02:00
|
|
|
const req = codecForRunBackupCycle().decode(payload);
|
|
|
|
await runBackupCycle(ws, req);
|
2021-06-15 18:52:43 +02:00
|
|
|
return {};
|
|
|
|
}
|
2021-07-15 20:03:45 +02:00
|
|
|
case "removeBackupProvider": {
|
|
|
|
const req = codecForRemoveBackupProvider().decode(payload);
|
|
|
|
await removeBackupProvider(ws, req);
|
|
|
|
return {};
|
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
case "exportBackupRecovery": {
|
|
|
|
const resp = await getBackupRecovery(ws);
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
case "importBackupRecovery": {
|
|
|
|
const req = codecForAny().decode(payload);
|
|
|
|
await loadBackupRecovery(ws, req);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "getBackupInfo": {
|
|
|
|
const resp = await getBackupInfo(ws);
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
case "createDepositGroup": {
|
|
|
|
const req = codecForCreateDepositGroupRequest().decode(payload);
|
|
|
|
return await createDepositGroup(ws, req);
|
|
|
|
}
|
|
|
|
case "trackDepositGroup": {
|
|
|
|
const req = codecForTrackDepositGroupRequest().decode(payload);
|
|
|
|
return trackDepositGroup(ws, req);
|
|
|
|
}
|
|
|
|
case "deleteTransaction": {
|
|
|
|
const req = codecForDeleteTransactionRequest().decode(payload);
|
|
|
|
await deleteTransaction(ws, req.transactionId);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "retryTransaction": {
|
|
|
|
const req = codecForRetryTransactionRequest().decode(payload);
|
|
|
|
await retryTransaction(ws, req.transactionId);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "setWalletDeviceId": {
|
|
|
|
const req = codecForSetWalletDeviceIdRequest().decode(payload);
|
|
|
|
await setWalletDeviceId(ws, req.walletDeviceId);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case "listCurrencies": {
|
|
|
|
return await ws.db
|
|
|
|
.mktx((x) => ({
|
|
|
|
auditorTrust: x.auditorTrust,
|
|
|
|
exchangeTrust: x.exchangeTrust,
|
|
|
|
}))
|
|
|
|
.runReadOnly(async (tx) => {
|
|
|
|
const trustedAuditors = await tx.auditorTrust.iter().toArray();
|
|
|
|
const trustedExchanges = await tx.exchangeTrust.iter().toArray();
|
|
|
|
return {
|
|
|
|
trustedAuditors: trustedAuditors.map((x) => ({
|
|
|
|
currency: x.currency,
|
|
|
|
auditorBaseUrl: x.auditorBaseUrl,
|
|
|
|
auditorPub: x.auditorPub,
|
|
|
|
})),
|
|
|
|
trustedExchanges: trustedExchanges.map((x) => ({
|
|
|
|
currency: x.currency,
|
|
|
|
exchangeBaseUrl: x.exchangeBaseUrl,
|
|
|
|
exchangeMasterPub: x.exchangeMasterPub,
|
|
|
|
})),
|
|
|
|
};
|
2020-08-14 12:23:50 +02:00
|
|
|
});
|
2020-08-14 09:36:42 +02:00
|
|
|
}
|
2021-10-14 11:36:43 +02:00
|
|
|
case "withdrawFakebank": {
|
|
|
|
const req = codecForWithdrawFakebankRequest().decode(payload);
|
|
|
|
const amount = Amounts.parseOrThrow(req.amount);
|
|
|
|
const details = await getWithdrawalDetailsForAmount(
|
|
|
|
ws,
|
|
|
|
req.exchange,
|
|
|
|
amount,
|
|
|
|
);
|
|
|
|
const wres = await acceptManualWithdrawal(ws, req.exchange, amount);
|
|
|
|
const paytoUri = details.paytoUris[0];
|
|
|
|
const pt = parsePaytoUri(paytoUri);
|
|
|
|
if (!pt) {
|
|
|
|
throw Error("failed to parse payto URI");
|
|
|
|
}
|
|
|
|
const components = pt.targetPath.split("/");
|
|
|
|
const creditorAcct = components[components.length - 1];
|
2021-11-17 10:23:22 +01:00
|
|
|
logger.info(`making testbank transfer to '${creditorAcct}''`);
|
2021-10-14 11:36:43 +02:00
|
|
|
const fbReq = await ws.http.postJson(
|
|
|
|
new URL(`${creditorAcct}/admin/add-incoming`, req.bank).href,
|
|
|
|
{
|
|
|
|
amount: Amounts.stringify(amount),
|
|
|
|
reserve_pub: wres.reservePub,
|
|
|
|
debit_account: "payto://x-taler-bank/localhost/testdebtor",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const fbResp = await readSuccessResponseJsonOrThrow(fbReq, codecForAny());
|
|
|
|
logger.info(`started fakebank withdrawal: ${j2s(fbResp)}`);
|
|
|
|
return {};
|
|
|
|
}
|
2021-12-01 18:16:40 +01:00
|
|
|
case "exportDb": {
|
|
|
|
const dbDump = await exportDb(ws.db.idbHandle());
|
|
|
|
return dbDump;
|
|
|
|
}
|
2020-08-14 09:36:42 +02:00
|
|
|
}
|
2021-06-15 18:52:43 +02:00
|
|
|
throw OperationFailedError.fromCode(
|
|
|
|
TalerErrorCode.WALLET_CORE_API_OPERATION_UNKNOWN,
|
|
|
|
"unknown operation",
|
|
|
|
{
|
|
|
|
operation,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-08-14 09:36:42 +02:00
|
|
|
|
2021-06-15 18:52:43 +02:00
|
|
|
/**
|
|
|
|
* Handle a request to the wallet-core API.
|
|
|
|
*/
|
|
|
|
export async function handleCoreApiRequest(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
operation: string,
|
|
|
|
id: string,
|
|
|
|
payload: unknown,
|
|
|
|
): Promise<CoreApiResponse> {
|
|
|
|
try {
|
|
|
|
const result = await dispatchRequestInternal(ws, operation, payload);
|
|
|
|
return {
|
|
|
|
type: "response",
|
|
|
|
operation,
|
|
|
|
id,
|
|
|
|
result,
|
|
|
|
};
|
2021-10-14 11:36:43 +02:00
|
|
|
} catch (e: any) {
|
2021-06-15 18:52:43 +02:00
|
|
|
if (
|
|
|
|
e instanceof OperationFailedError ||
|
|
|
|
e instanceof OperationFailedAndReportedError
|
|
|
|
) {
|
2021-11-27 20:56:58 +01:00
|
|
|
logger.error("Caught operation failed error");
|
|
|
|
logger.trace((e as any).stack);
|
2020-08-14 09:36:42 +02:00
|
|
|
return {
|
2021-06-15 18:52:43 +02:00
|
|
|
type: "error",
|
2020-08-14 09:36:42 +02:00
|
|
|
operation,
|
|
|
|
id,
|
2021-06-15 18:52:43 +02:00
|
|
|
error: e.operationError,
|
|
|
|
};
|
|
|
|
} else {
|
2021-10-14 11:36:43 +02:00
|
|
|
try {
|
|
|
|
logger.error("Caught unexpected exception:");
|
|
|
|
logger.error(e.stack);
|
2021-12-01 18:16:40 +01:00
|
|
|
} catch (e) {}
|
2021-06-15 18:52:43 +02:00
|
|
|
return {
|
|
|
|
type: "error",
|
|
|
|
operation,
|
|
|
|
id,
|
|
|
|
error: makeErrorDetails(
|
|
|
|
TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
|
|
|
|
`unexpected exception: ${e}`,
|
|
|
|
{},
|
|
|
|
),
|
2020-08-14 09:36:42 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2016-10-18 01:16:31 +02:00
|
|
|
}
|
2021-06-17 21:06:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public handle to a running wallet.
|
|
|
|
*/
|
|
|
|
export class Wallet {
|
|
|
|
private ws: InternalWalletState;
|
|
|
|
private _client: WalletCoreApiClient;
|
|
|
|
|
|
|
|
private constructor(
|
|
|
|
db: DbAccess<typeof WalletStoresV1>,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
cryptoWorkerFactory: CryptoWorkerFactory,
|
|
|
|
) {
|
|
|
|
this.ws = new InternalWalletStateImpl(db, http, cryptoWorkerFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
get client() {
|
|
|
|
return this._client;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async create(
|
|
|
|
db: DbAccess<typeof WalletStoresV1>,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
cryptoWorkerFactory: CryptoWorkerFactory,
|
|
|
|
): Promise<Wallet> {
|
|
|
|
const w = new Wallet(db, http, cryptoWorkerFactory);
|
|
|
|
w._client = await getClientFromWalletState(w.ws);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
addNotificationListener(f: (n: WalletNotification) => void): void {
|
|
|
|
return this.ws.addNotificationListener(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
stop(): void {
|
|
|
|
this.ws.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
runPending(forceNow: boolean = false) {
|
|
|
|
return runPending(this.ws, forceNow);
|
|
|
|
}
|
|
|
|
|
2021-08-19 15:12:33 +02:00
|
|
|
runTaskLoop(opts?: RetryLoopOpts) {
|
2021-06-22 13:52:28 +02:00
|
|
|
return runTaskLoop(this.ws, opts);
|
2021-06-17 21:06:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCoreApiRequest(
|
|
|
|
operation: string,
|
|
|
|
id: string,
|
|
|
|
payload: unknown,
|
|
|
|
): Promise<CoreApiResponse> {
|
|
|
|
return handleCoreApiRequest(this.ws, operation, id, payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal state of the wallet.
|
|
|
|
*
|
|
|
|
* This ties together all the operation implementations.
|
|
|
|
*/
|
|
|
|
class InternalWalletStateImpl implements InternalWalletState {
|
|
|
|
memoProcessReserve: AsyncOpMemoMap<void> = new AsyncOpMemoMap();
|
|
|
|
memoMakePlanchet: AsyncOpMemoMap<void> = new AsyncOpMemoMap();
|
|
|
|
memoGetPending: AsyncOpMemoSingle<PendingOperationsResponse> = new AsyncOpMemoSingle();
|
|
|
|
memoGetBalance: AsyncOpMemoSingle<BalancesResponse> = new AsyncOpMemoSingle();
|
|
|
|
memoProcessRefresh: AsyncOpMemoMap<void> = new AsyncOpMemoMap();
|
|
|
|
memoProcessRecoup: AsyncOpMemoMap<void> = new AsyncOpMemoMap();
|
|
|
|
memoProcessDeposit: AsyncOpMemoMap<void> = new AsyncOpMemoMap();
|
|
|
|
cryptoApi: CryptoApi;
|
|
|
|
|
2021-11-23 23:51:12 +01:00
|
|
|
merchantInfoCache: Record<string, MerchantInfo> = {};
|
|
|
|
|
2021-06-17 21:06:45 +02:00
|
|
|
timerGroup: TimerGroup = new TimerGroup();
|
|
|
|
latch = new AsyncCondition();
|
|
|
|
stopped = false;
|
|
|
|
|
|
|
|
listeners: NotificationListener[] = [];
|
|
|
|
|
|
|
|
initCalled: boolean = false;
|
|
|
|
|
|
|
|
exchangeOps: ExchangeOperations = {
|
|
|
|
getExchangeDetails,
|
|
|
|
getExchangeTrust,
|
|
|
|
updateExchangeFromUrl,
|
|
|
|
};
|
|
|
|
|
2021-06-22 12:18:12 +02:00
|
|
|
recoupOps: RecoupOperations = {
|
|
|
|
createRecoupGroup: createRecoupGroup,
|
|
|
|
processRecoupGroup: processRecoupGroup,
|
|
|
|
};
|
|
|
|
|
2021-11-23 23:51:12 +01:00
|
|
|
merchantOps: MerchantOperations = {
|
|
|
|
getMerchantInfo: getMerchantInfo,
|
|
|
|
};
|
|
|
|
|
2021-06-17 21:06:45 +02:00
|
|
|
/**
|
|
|
|
* Promises that are waiting for a particular resource.
|
|
|
|
*/
|
|
|
|
private resourceWaiters: Record<string, OpenedPromise<void>[]> = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resources that are currently locked.
|
|
|
|
*/
|
|
|
|
private resourceLocks: Set<string> = new Set();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
// FIXME: Make this a getter and make
|
|
|
|
// the actual value nullable.
|
|
|
|
// Check if we are in a DB migration / garbage collection
|
|
|
|
// and throw an error in that case.
|
|
|
|
public db: DbAccess<typeof WalletStoresV1>,
|
|
|
|
public http: HttpRequestLibrary,
|
|
|
|
cryptoWorkerFactory: CryptoWorkerFactory,
|
|
|
|
) {
|
|
|
|
this.cryptoApi = new CryptoApi(cryptoWorkerFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
notify(n: WalletNotification): void {
|
|
|
|
logger.trace("Notification", n);
|
|
|
|
for (const l of this.listeners) {
|
|
|
|
const nc = JSON.parse(JSON.stringify(n));
|
|
|
|
setTimeout(() => {
|
|
|
|
l(nc);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addNotificationListener(f: (n: WalletNotification) => void): void {
|
|
|
|
this.listeners.push(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop ongoing processing.
|
|
|
|
*/
|
|
|
|
stop(): void {
|
|
|
|
this.stopped = true;
|
|
|
|
this.timerGroup.stopCurrentAndFutureTimers();
|
|
|
|
this.cryptoApi.stop();
|
|
|
|
}
|
|
|
|
|
2021-06-22 12:18:12 +02:00
|
|
|
async runUntilDone(
|
|
|
|
req: {
|
|
|
|
maxRetries?: number;
|
|
|
|
} = {},
|
|
|
|
): Promise<void> {
|
2021-06-22 13:52:28 +02:00
|
|
|
await runTaskLoop(this, { ...req, stopWhenDone: true });
|
2021-06-22 12:18:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:06:45 +02:00
|
|
|
/**
|
|
|
|
* Run an async function after acquiring a list of locks, identified
|
|
|
|
* by string tokens.
|
|
|
|
*/
|
|
|
|
async runSequentialized<T>(tokens: string[], f: () => Promise<T>) {
|
|
|
|
// Make sure locks are always acquired in the same order
|
|
|
|
tokens = [...tokens].sort();
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
|
|
|
if (this.resourceLocks.has(token)) {
|
|
|
|
const p = openPromise<void>();
|
|
|
|
let waitList = this.resourceWaiters[token];
|
|
|
|
if (!waitList) {
|
|
|
|
waitList = this.resourceWaiters[token] = [];
|
|
|
|
}
|
|
|
|
waitList.push(p);
|
|
|
|
await p.promise;
|
|
|
|
}
|
|
|
|
this.resourceLocks.add(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
logger.trace(`begin exclusive execution on ${JSON.stringify(tokens)}`);
|
|
|
|
const result = await f();
|
|
|
|
logger.trace(`end exclusive execution on ${JSON.stringify(tokens)}`);
|
|
|
|
return result;
|
|
|
|
} finally {
|
|
|
|
for (const token of tokens) {
|
|
|
|
this.resourceLocks.delete(token);
|
|
|
|
let waiter = (this.resourceWaiters[token] ?? []).shift();
|
|
|
|
if (waiter) {
|
|
|
|
waiter.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|