wallet-core/packages/taler-wallet-core/src/wallet.ts

1850 lines
54 KiB
TypeScript
Raw Normal View History

2015-12-25 22:42:14 +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
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.
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
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 {
AbsoluteTime,
Amounts,
2023-04-19 17:42:47 +02:00
CoinDumpJson,
CoinRefreshRequest,
CoinStatus,
CoreApiResponse,
DenomOperationMap,
DenominationInfo,
Duration,
ExchangeDetailedResponse,
ExchangeListItem,
ExchangeTosStatusDetails,
ExchangesListResponse,
FeeDescription,
GetExchangeTosResult,
InitResponse,
KnownBankAccounts,
KnownBankAccountsInfo,
Logger,
ManualWithdrawalDetails,
MerchantUsingTemplateDetails,
NotificationType,
RefreshReason,
TalerError,
TalerErrorCode,
2023-05-05 19:03:44 +02:00
TransactionType,
2023-04-19 17:42:47 +02:00
URL,
ValidateIbanResponse,
WalletCoreVersion,
WalletNotification,
codecForAbortTransaction,
codecForAcceptBankIntegratedWithdrawalRequest,
codecForAcceptExchangeTosRequest,
codecForAcceptManualWithdrawalRequet,
codecForAcceptPeerPullPaymentRequest,
codecForAcceptTipRequest,
codecForAddExchangeRequest,
codecForAddKnownBankAccounts,
codecForAny,
2022-10-17 01:03:36 +02:00
codecForApplyDevExperiment,
codecForApplyRefundFromPurchaseIdRequest,
codecForApplyRefundRequest,
codecForCheckPeerPullPaymentRequest,
2023-04-19 17:42:47 +02:00
codecForCheckPeerPushDebitRequest,
codecForConfirmPayRequest,
2023-04-19 17:42:47 +02:00
codecForConfirmPeerPushPaymentRequest,
codecForCreateDepositGroupRequest,
codecForDeleteTransactionRequest,
codecForForceRefreshRequest,
codecForForgetKnownBankAccounts,
codecForGetBalanceDetailRequest,
codecForGetContractTermsDetails,
codecForGetExchangeTosRequest,
codecForGetWithdrawalDetailsForAmountRequest,
codecForGetWithdrawalDetailsForUri,
codecForImportDbRequest,
codecForInitiatePeerPullPaymentRequest,
codecForInitiatePeerPushPaymentRequest,
codecForIntegrationTestArgs,
2023-04-19 17:42:47 +02:00
codecForIntegrationTestV2Args,
codecForListKnownBankAccounts,
codecForMerchantPostOrderResponse,
2022-05-03 05:16:03 +02:00
codecForPrepareDepositRequest,
codecForPreparePayRequest,
codecForPreparePayTemplateRequest,
2022-11-08 17:00:34 +01:00
codecForPreparePeerPullPaymentRequest,
2023-04-19 17:42:47 +02:00
codecForPreparePeerPushCreditRequest,
codecForPrepareRefundRequest,
codecForPrepareTipRequest,
2023-04-19 17:42:47 +02:00
codecForResumeTransaction,
codecForRetryTransactionRequest,
codecForSetCoinSuspendedRequest,
codecForSetWalletDeviceIdRequest,
2023-05-05 19:03:44 +02:00
codecForStartRefundQueryRequest,
2023-04-19 17:42:47 +02:00
codecForSuspendTransaction,
codecForTestPayArgs,
codecForTransactionByIdRequest,
codecForTransactionsRequest,
2023-01-11 14:19:24 +01:00
codecForUserAttentionByIdRequest,
codecForUserAttentionsRequest,
2023-04-19 17:42:47 +02:00
codecForValidateIbanRequest,
codecForWithdrawFakebankRequest,
codecForWithdrawTestBalance,
constructPayUri,
durationFromSpec,
durationMin,
2023-04-19 17:42:47 +02:00
getErrorDetailFromException,
j2s,
parsePayTemplateUri,
parsePaytoUri,
2023-03-29 18:49:06 +02:00
validateIban,
2021-05-12 16:18:32 +02:00
} from "@gnu-taler/taler-util";
2023-04-19 17:42:47 +02:00
import {
HttpRequestLibrary,
readSuccessResponseJsonOrThrow,
} from "@gnu-taler/taler-util/http";
import { TalerCryptoInterface } from "./crypto/cryptoImplementation.js";
2022-03-23 21:24:23 +01:00
import {
CryptoDispatcher,
CryptoWorkerFactory,
} from "./crypto/workers/crypto-dispatcher.js";
import {
CoinSourceType,
ConfigRecordKey,
DenominationRecord,
ExchangeDetailsRecord,
2023-04-19 17:42:47 +02:00
WalletStoresV1,
clearDatabase,
exportDb,
importDb,
} from "./db.js";
2023-04-19 17:42:47 +02:00
import { DevExperimentHttpLib, applyDevExperiment } from "./dev-experiments.js";
import {
ActiveLongpollInfo,
ExchangeOperations,
InternalWalletState,
MerchantInfo,
MerchantOperations,
NotificationListener,
RecoupOperations,
RefreshOperations,
} from "./internal-wallet-state.js";
2023-01-11 14:19:24 +01:00
import {
getUserAttentions,
getUserAttentionsUnreadCount,
markAttentionRequestAsRead,
} from "./operations/attention.js";
import { exportBackup } from "./operations/backup/export.js";
import {
2021-02-08 15:38:34 +01:00
addBackupProvider,
codecForAddBackupProviderRequest,
codecForRemoveBackupProvider,
2021-07-07 18:28:02 +02:00
codecForRunBackupCycle,
2021-02-08 15:38:34 +01:00
getBackupInfo,
getBackupRecovery,
importBackupPlain,
2021-02-08 15:38:34 +01:00
loadBackupRecovery,
processBackupForProvider,
removeBackupProvider,
runBackupCycle,
} from "./operations/backup/index.js";
import { setWalletDeviceId } from "./operations/backup/state.js";
import { getBalanceDetail, getBalances } from "./operations/balance.js";
import {
getExchangeTosStatus,
makeExchangeListItem,
runOperationWithErrorReporting,
} from "./operations/common.js";
2021-02-08 15:38:34 +01:00
import {
createDepositGroup,
2023-04-13 11:48:06 +02:00
generateDepositGroupTxId,
2022-05-03 05:16:03 +02:00
prepareDepositGroup,
2021-02-08 15:38:34 +01:00
processDepositGroup,
} from "./operations/deposits.js";
2021-02-08 15:38:34 +01:00
import {
acceptExchangeTermsOfService,
downloadTosFromAcceptedFormat,
2021-06-09 15:14:17 +02:00
getExchangeDetails,
getExchangeRequestTimeout,
getExchangeTrust,
provideExchangeRecordInTx,
updateExchangeFromUrl,
2022-09-05 18:12:30 +02:00
updateExchangeFromUrlHandler,
updateExchangeTermsOfService,
} from "./operations/exchanges.js";
import { getMerchantInfo } from "./operations/merchants.js";
import {
confirmPay,
getContractTermsDetails,
2021-02-08 15:38:34 +01:00
preparePayForUri,
processPurchase,
2023-05-05 19:03:44 +02:00
startRefundQueryForUri,
} from "./operations/pay-merchant.js";
import {
2023-04-19 17:42:47 +02:00
checkPeerPullPaymentInitiation,
checkPeerPushDebit,
confirmPeerPullDebit,
confirmPeerPushCredit,
2022-11-02 17:02:42 +01:00
initiatePeerPullPayment,
initiatePeerPushPayment,
2023-04-19 17:42:47 +02:00
preparePeerPullDebit,
preparePeerPushCredit,
processPeerPullCredit,
2023-02-19 23:13:44 +01:00
processPeerPullDebit,
processPeerPushCredit,
2023-05-05 10:56:42 +02:00
processPeerPushDebit,
} from "./operations/pay-peer.js";
import { getPendingOperations } from "./operations/pending.js";
2022-09-05 18:12:30 +02:00
import {
createRecoupGroup,
processRecoupGroup,
processRecoupGroupHandler,
} from "./operations/recoup.js";
2021-02-08 15:38:34 +01:00
import {
autoRefresh,
createRefreshGroup,
processRefreshGroup,
} from "./operations/refresh.js";
2021-02-08 15:38:34 +01:00
import {
runIntegrationTest,
runIntegrationTest2,
2021-02-08 15:38:34 +01:00
testPay,
withdrawTestBalance,
} from "./operations/testing.js";
import { acceptTip, prepareTip, processTip } from "./operations/tip.js";
import {
abortTransaction,
deleteTransaction,
getTransactionById,
getTransactions,
2023-05-05 19:03:44 +02:00
parseTransactionIdentifier,
resumeTransaction,
retryTransaction,
suspendTransaction,
} from "./operations/transactions.js";
2021-02-08 15:38:34 +01:00
import {
2022-08-09 15:00:45 +02:00
acceptWithdrawalFromUri,
createManualWithdrawal,
2021-02-08 15:38:34 +01:00
getExchangeWithdrawalInfo,
getWithdrawalDetailsForUri,
processWithdrawalGroup,
} from "./operations/withdraw.js";
import { PendingTaskInfo, PendingTaskType } from "./pending-types.js";
import { assertUnreachable } from "./util/assertUnreachable.js";
import {
createTimeline,
selectBestForOverlappingDenominations,
selectMinimumFee,
} from "./util/denominations.js";
import { checkDbInvariant } from "./util/invariants.js";
import {
AsyncCondition,
OpenedPromise,
openPromise,
} from "./util/promiseUtils.js";
import {
DbAccess,
GetReadOnlyAccess,
GetReadWriteAccess,
} from "./util/query.js";
import { OperationAttemptResult, TaskIdentifiers } from "./util/retries.js";
import { TimerAPI, TimerGroup } from "./util/timer.js";
import {
WALLET_BANK_INTEGRATION_PROTOCOL_VERSION,
WALLET_EXCHANGE_PROTOCOL_VERSION,
WALLET_MERCHANT_PROTOCOL_VERSION,
} from "./versions.js";
2022-10-17 01:03:36 +02:00
import {
WalletApiOperation,
2023-04-19 17:42:47 +02:00
WalletConfig,
WalletConfigParameter,
2022-10-17 01:03:36 +02:00
WalletCoreApiClient,
WalletCoreResponseType,
} from "./wallet-api-types.js";
2023-05-05 19:03:44 +02:00
import { startQueryRefund } from "./operations/pay-merchant.js";
2019-11-21 23:09:43 +01:00
const logger = new Logger("wallet.ts");
/**
2022-09-05 18:12:30 +02:00
* Call the right handler for a pending operation without doing
* any special error handling.
*/
2022-09-05 18:12:30 +02:00
async function callOperationHandler(
ws: InternalWalletState,
pending: PendingTaskInfo,
forceNow = false,
2022-09-16 17:21:54 +02:00
): Promise<OperationAttemptResult> {
switch (pending.type) {
case PendingTaskType.ExchangeUpdate:
2022-09-05 18:12:30 +02:00
return await updateExchangeFromUrlHandler(ws, pending.exchangeBaseUrl, {
2021-10-14 11:36:43 +02:00
forceNow,
});
case PendingTaskType.Refresh:
return await processRefreshGroup(ws, pending.refreshGroupId);
case PendingTaskType.Withdraw:
2022-09-13 15:28:34 +02:00
return await processWithdrawalGroup(ws, pending.withdrawalGroupId, {
forceNow,
});
case PendingTaskType.TipPickup:
return await processTip(ws, pending.tipId);
case PendingTaskType.Purchase:
return await processPurchase(ws, pending.proposalId);
case PendingTaskType.Recoup:
2022-09-05 18:12:30 +02:00
return await processRecoupGroupHandler(ws, pending.recoupGroupId, {
forceNow,
});
case PendingTaskType.ExchangeCheckRefresh:
2022-09-05 18:12:30 +02:00
return await autoRefresh(ws, pending.exchangeBaseUrl);
2022-03-28 23:59:16 +02:00
case PendingTaskType.Deposit: {
return await processDepositGroup(ws, pending.depositGroupId);
2022-03-28 23:59:16 +02:00
}
case PendingTaskType.Backup:
2022-09-05 18:12:30 +02:00
return await processBackupForProvider(ws, pending.backupProviderBaseUrl);
2023-05-05 10:56:42 +02:00
case PendingTaskType.PeerPushDebit:
return await processPeerPushDebit(ws, pending.pursePub);
case PendingTaskType.PeerPullCredit:
return await processPeerPullCredit(ws, pending.pursePub);
2023-02-19 23:13:44 +01:00
case PendingTaskType.PeerPullDebit:
return await processPeerPullDebit(ws, pending.peerPullPaymentIncomingId);
case PendingTaskType.PeerPushCredit:
return await processPeerPushCredit(ws, pending.peerPushPaymentIncomingId);
default:
2022-09-05 18:12:30 +02:00
return assertUnreachable(pending);
}
2022-09-13 15:28:34 +02:00
throw Error(`not reached ${pending.type}`);
2022-09-05 18:12:30 +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) {
2022-03-18 15:32:41 +01:00
if (!forceNow && !AbsoluteTime.isExpired(p.timestampDue)) {
continue;
2020-07-11 09:56:07 +02:00
}
await runOperationWithErrorReporting(ws, p.id, async () => {
logger.trace(`running pending ${JSON.stringify(p, undefined, 2)}`);
return await callOperationHandler(ws, p, forceNow);
});
2019-11-30 00:36:20 +01:00
}
}
2019-11-21 23:09:43 +01: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;
}
2019-12-05 19:38:19 +01:00
export interface TaskLoopResult {
/**
* Was the maximum number of retries exceeded in a task?
*/
retriesExceeded: boolean;
}
/**
* Main retry loop of the wallet.
*
* Looks up pending operations from the wallet, runs them, repeat.
*/
async function runTaskLoop(
ws: InternalWalletState,
opts: RetryLoopOpts = {},
): Promise<TaskLoopResult> {
logger.info(`running task loop opts=${j2s(opts)}`);
let retriesExceeded = false;
for (let iteration = 0; !ws.stopped; iteration++) {
const pending = await getPendingOperations(ws);
logger.trace(`pending operations: ${j2s(pending)}`);
let numGivingLiveness = 0;
let numDue = 0;
2022-03-18 15:32:41 +01:00
let minDue: AbsoluteTime = AbsoluteTime.never();
for (const p of pending.pendingOperations) {
const maxRetries = opts.maxRetries;
if (maxRetries && p.retryInfo && p.retryInfo.retryCounter > maxRetries) {
retriesExceeded = true;
logger.warn(
`skipping, as ${maxRetries} retries are exceeded in an operation of type ${p.type}`,
);
continue;
}
if (p.givesLifeness) {
numGivingLiveness++;
}
if (!p.isDue) {
continue;
}
minDue = AbsoluteTime.min(minDue, p.timestampDue);
numDue++;
}
if (opts.stopWhenDone && numGivingLiveness === 0 && iteration !== 0) {
logger.warn(`stopping, as no pending operations have lifeness`);
return {
retriesExceeded,
};
}
// 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,
}),
2022-03-18 15:32:41 +01:00
Duration.getRemaining(minDue),
);
logger.trace(`waiting for at most ${dt.d_ms} ms`);
const timeout = ws.timerGroup.resolveAfter(dt);
ws.notify({
type: NotificationType.WaitingForRetry,
numGivingLiveness,
numDue,
numPending: pending.pendingOperations.length,
});
// Wait until either the timeout, or we are notified (via the latch)
// that more work might be available.
2023-05-05 10:56:42 +02:00
await Promise.race([timeout, ws.workAvailable.wait()]);
} else {
logger.trace(
`running ${pending.pendingOperations.length} pending operations`,
);
2020-09-03 17:08:26 +02:00
for (const p of pending.pendingOperations) {
2022-03-18 15:32:41 +01:00
if (!AbsoluteTime.isExpired(p.timestampDue)) {
continue;
2020-09-03 17:08:26 +02:00
}
await runOperationWithErrorReporting(ws, p.id, async () => {
logger.trace(`running pending ${JSON.stringify(p, undefined, 2)}`);
return await callOperationHandler(ws, p);
});
ws.notify({
type: NotificationType.PendingOperationProcessed,
id: p.id,
});
2019-11-21 23:09:43 +01:00
}
}
}
logger.trace("exiting wallet retry loop");
return {
retriesExceeded,
};
}
/**
* 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) => [x.config, x.auditorTrust, x.exchanges, x.exchangeDetails])
.runReadWrite(async (tx) => {
const appliedRec = await tx.config.get("currencyDefaultsApplied");
let alreadyApplied = appliedRec ? !!appliedRec.value : false;
if (alreadyApplied) {
2022-10-07 12:43:17 +02:00
logger.trace("defaults already applied");
return;
}
2022-10-07 12:43:17 +02:00
logger.info("importing default exchanges and auditors");
2023-04-19 17:42:47 +02:00
for (const c of ws.config.builtin.auditors) {
await tx.auditorTrust.put(c);
}
2023-04-19 17:42:47 +02:00
for (const baseUrl of ws.config.builtin.exchanges) {
const now = AbsoluteTime.now();
provideExchangeRecordInTx(ws, tx, baseUrl, now);
}
await tx.config.put({
key: ConfigRecordKey.CurrencyDefaultsApplied,
value: true,
});
});
}
/**
* Get the exchange ToS in the requested format.
* Try to download in the accepted format not cached.
*/
async function getExchangeTos(
ws: InternalWalletState,
exchangeBaseUrl: string,
2021-10-13 19:26:18 +02:00
acceptedFormat?: string[],
): Promise<GetExchangeTosResult> {
// FIXME: download ToS in acceptable format if passed!
const { exchangeDetails } = await updateExchangeFromUrl(ws, exchangeBaseUrl);
const tosDetails = await ws.db
.mktx((x) => [x.exchangeTos])
.runReadOnly(async (tx) => {
return await getExchangeTosStatusDetails(tx, exchangeDetails);
});
const content = tosDetails.content;
const currentEtag = tosDetails.currentVersion;
const contentType = tosDetails.contentType;
2021-10-14 11:36:43 +02:00
if (
content === undefined ||
currentEtag === undefined ||
contentType === undefined
) {
throw Error("exchange is in invalid state");
}
if (
acceptedFormat &&
acceptedFormat.findIndex((f) => f === contentType) !== -1
) {
return {
acceptedEtag: exchangeDetails.tosAccepted?.etag,
currentEtag,
content,
contentType,
tosStatus: getExchangeTosStatus(exchangeDetails),
};
}
const tosDownload = await downloadTosFromAcceptedFormat(
ws,
exchangeBaseUrl,
getExchangeRequestTimeout(),
acceptedFormat,
);
if (tosDownload.tosContentType === contentType) {
return {
acceptedEtag: exchangeDetails.tosAccepted?.etag,
currentEtag,
content,
contentType,
tosStatus: getExchangeTosStatus(exchangeDetails),
};
}
await updateExchangeTermsOfService(ws, exchangeBaseUrl, tosDownload);
return {
acceptedEtag: exchangeDetails.tosAccepted?.etag,
currentEtag: tosDownload.tosEtag,
content: tosDownload.tosText,
contentType: tosDownload.tosContentType,
tosStatus: getExchangeTosStatus(exchangeDetails),
};
}
2019-11-30 00:36:20 +01:00
2022-08-09 15:00:45 +02:00
/**
* List bank accounts known to the wallet from
* previous withdrawals.
*/
2021-12-23 19:17:36 +01:00
async function listKnownBankAccounts(
ws: InternalWalletState,
currency?: string,
): Promise<KnownBankAccounts> {
2022-09-23 20:17:29 +02:00
const accounts: KnownBankAccountsInfo[] = [];
2021-12-23 19:17:36 +01:00
await ws.db
2022-09-23 20:17:29 +02:00
.mktx((x) => [x.bankAccounts])
2021-12-23 19:17:36 +01:00
.runReadOnly(async (tx) => {
2022-09-23 20:17:29 +02:00
const knownAccounts = await tx.bankAccounts.iter().toArray();
for (const r of knownAccounts) {
if (currency && currency !== r.currency) {
2022-01-13 12:08:31 +01:00
continue;
2021-12-23 19:17:36 +01:00
}
2022-09-23 20:17:29 +02:00
const payto = parsePaytoUri(r.uri);
if (payto) {
accounts.push({
uri: payto,
alias: r.alias,
kyc_completed: r.kycCompleted,
2022-09-23 20:17:29 +02:00
currency: r.currency,
});
2021-12-23 19:17:36 +01:00
}
}
2022-01-13 12:08:31 +01:00
});
return { accounts };
2021-12-23 19:17:36 +01:00
}
2022-09-23 20:17:29 +02:00
/**
*/
async function addKnownBankAccounts(
ws: InternalWalletState,
payto: string,
alias: string,
currency: string,
): Promise<void> {
await ws.db
.mktx((x) => [x.bankAccounts])
.runReadWrite(async (tx) => {
tx.bankAccounts.put({
uri: payto,
alias: alias,
currency: currency,
kycCompleted: false,
2022-09-23 20:17:29 +02:00
});
});
return;
}
/**
*/
async function forgetKnownBankAccounts(
ws: InternalWalletState,
payto: string,
): Promise<void> {
await ws.db
.mktx((x) => [x.bankAccounts])
.runReadWrite(async (tx) => {
const account = await tx.bankAccounts.get(payto);
if (!account) {
throw Error(`account not found: ${payto}`);
}
tx.bankAccounts.delete(account.uri);
});
return;
}
async function getExchangeTosStatusDetails(
tx: GetReadOnlyAccess<{ exchangeTos: typeof WalletStoresV1.exchangeTos }>,
exchangeDetails: ExchangeDetailsRecord,
): Promise<ExchangeTosStatusDetails> {
let exchangeTos = await tx.exchangeTos.get([
exchangeDetails.exchangeBaseUrl,
exchangeDetails.tosCurrentEtag,
]);
if (!exchangeTos) {
exchangeTos = {
etag: "not-available",
termsOfServiceContentType: "text/plain",
termsOfServiceText: "terms of service unavailable",
exchangeBaseUrl: exchangeDetails.exchangeBaseUrl,
};
}
return {
acceptedVersion: exchangeDetails.tosAccepted?.etag,
content: exchangeTos.termsOfServiceText,
contentType: exchangeTos.termsOfServiceContentType,
currentVersion: exchangeTos.etag,
};
}
async function getExchanges(
ws: InternalWalletState,
): Promise<ExchangesListResponse> {
const exchanges: ExchangeListItem[] = [];
await ws.db
.mktx((x) => [
x.exchanges,
x.exchangeDetails,
x.exchangeTos,
x.denominations,
x.operationRetries,
])
.runReadOnly(async (tx) => {
const exchangeRecords = await tx.exchanges.iter().toArray();
for (const r of exchangeRecords) {
const exchangeDetails = await getExchangeDetails(tx, r.baseUrl);
const opRetryRecord = await tx.operationRetries.get(
TaskIdentifiers.forExchangeUpdate(r),
);
2022-11-08 17:00:34 +01:00
exchanges.push(
makeExchangeListItem(r, exchangeDetails, opRetryRecord?.lastError),
);
}
});
return { exchanges };
}
async function getExchangeDetailedInfo(
ws: InternalWalletState,
exchangeBaseurl: string,
2022-10-25 17:23:08 +02:00
): Promise<ExchangeDetailedResponse> {
//TODO: should we use the forceUpdate parameter?
const exchange = await ws.db
.mktx((x) => [
x.exchanges,
x.exchangeTos,
x.exchangeDetails,
x.denominations,
])
.runReadOnly(async (tx) => {
2022-09-05 18:12:30 +02:00
const ex = await tx.exchanges.get(exchangeBaseurl);
const dp = ex?.detailsPointer;
if (!dp) {
return;
}
const { currency } = dp;
const exchangeDetails = await getExchangeDetails(tx, ex.baseUrl);
if (!exchangeDetails) {
return;
}
const denominationRecords =
await tx.denominations.indexes.byExchangeBaseUrl
.iter(ex.baseUrl)
.toArray();
if (!denominationRecords) {
return;
}
const tos = await getExchangeTosStatusDetails(tx, exchangeDetails);
const denominations: DenominationInfo[] = denominationRecords.map((x) =>
DenominationRecord.toDenomInfo(x),
);
return {
2022-09-12 15:57:13 +02:00
info: {
exchangeBaseUrl: ex.baseUrl,
currency,
tos,
2022-09-12 15:57:13 +02:00
paytoUris: exchangeDetails.wireInfo.accounts.map((x) => x.payto_uri),
auditors: exchangeDetails.auditors,
wireInfo: exchangeDetails.wireInfo,
globalFees: exchangeDetails.globalFees,
},
denominations,
2022-09-05 18:12:30 +02:00
};
});
2022-09-12 15:57:13 +02:00
if (!exchange) {
2022-09-05 18:12:30 +02:00
throw Error(`exchange with base url "${exchangeBaseurl}" not found`);
}
2022-09-12 15:57:13 +02:00
const denoms = exchange.denominations.map((d) => ({
...d,
group: Amounts.stringifyValue(d.value),
}));
const denomFees: DenomOperationMap<FeeDescription[]> = {
deposit: createTimeline(
denoms,
"denomPubHash",
"stampStart",
2022-09-12 15:57:13 +02:00
"stampExpireDeposit",
"feeDeposit",
"group",
selectBestForOverlappingDenominations,
2022-09-12 15:57:13 +02:00
),
refresh: createTimeline(
denoms,
"denomPubHash",
"stampStart",
2022-09-12 15:57:13 +02:00
"stampExpireWithdraw",
"feeRefresh",
"group",
selectBestForOverlappingDenominations,
2022-09-12 15:57:13 +02:00
),
refund: createTimeline(
denoms,
"denomPubHash",
"stampStart",
2022-09-12 15:57:13 +02:00
"stampExpireWithdraw",
"feeRefund",
"group",
selectBestForOverlappingDenominations,
2022-09-12 15:57:13 +02:00
),
withdraw: createTimeline(
denoms,
"denomPubHash",
"stampStart",
2022-09-12 15:57:13 +02:00
"stampExpireWithdraw",
"feeWithdraw",
"group",
selectBestForOverlappingDenominations,
2022-09-12 15:57:13 +02:00
),
};
const transferFees = Object.entries(
exchange.info.wireInfo.feesForType,
).reduce((prev, [wireType, infoForType]) => {
const feesByGroup = [
...infoForType.map((w) => ({
...w,
2022-11-02 17:42:14 +01:00
fee: Amounts.stringify(w.closingFee),
group: "closing",
})),
...infoForType.map((w) => ({ ...w, fee: w.wireFee, group: "wire" })),
];
prev[wireType] = createTimeline(
feesByGroup,
"sig",
"startStamp",
"endStamp",
"fee",
"group",
selectMinimumFee,
);
return prev;
}, {} as Record<string, FeeDescription[]>);
const globalFeesByGroup = [
...exchange.info.globalFees.map((w) => ({
...w,
fee: w.accountFee,
group: "account",
})),
...exchange.info.globalFees.map((w) => ({
...w,
fee: w.historyFee,
group: "history",
})),
...exchange.info.globalFees.map((w) => ({
...w,
fee: w.purseFee,
group: "purse",
})),
];
const globalFees = createTimeline(
globalFeesByGroup,
"signature",
"startDate",
"endDate",
"fee",
"group",
selectMinimumFee,
);
2022-09-12 15:57:13 +02:00
return {
2022-10-25 17:23:08 +02:00
exchange: {
...exchange.info,
denomFees,
transferFees,
globalFees,
},
2022-09-12 15:57:13 +02:00
};
}
async function setCoinSuspended(
ws: InternalWalletState,
coinPub: string,
suspended: boolean,
): Promise<void> {
await ws.db
.mktx((x) => [x.coins, x.coinAvailability])
.runReadWrite(async (tx) => {
const c = await tx.coins.get(coinPub);
if (!c) {
logger.warn(`coin ${coinPub} not found, won't suspend`);
return;
}
const coinAvailability = await tx.coinAvailability.get([
c.exchangeBaseUrl,
c.denomPubHash,
c.maxAge,
]);
checkDbInvariant(!!coinAvailability);
if (suspended) {
if (c.status !== CoinStatus.Fresh) {
return;
}
if (coinAvailability.freshCoinCount === 0) {
throw Error(
`invalid coin count ${coinAvailability.freshCoinCount} in DB`,
);
}
coinAvailability.freshCoinCount--;
c.status = CoinStatus.FreshSuspended;
} else {
if (c.status == CoinStatus.Dormant) {
return;
}
coinAvailability.freshCoinCount++;
c.status = CoinStatus.Fresh;
}
await tx.coins.put(c);
await tx.coinAvailability.put(coinAvailability);
});
}
2019-11-30 00:36:20 +01: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: [] };
2022-01-13 22:01:14 +01:00
logger.info("dumping coins");
await ws.db
.mktx((x) => [x.coins, x.denominations, 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) {
logger.warn("no denom 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) {
withdrawalReservePub = cs.reservePub;
}
const denomInfo = await ws.getDenomInfo(
ws,
tx,
c.exchangeBaseUrl,
c.denomPubHash,
);
if (!denomInfo) {
logger.warn("no denomination found for coin");
continue;
}
coinsJson.coins.push({
coin_pub: c.coinPub,
denom_pub: denomInfo.denomPub,
denom_pub_hash: c.denomPubHash,
denom_value: Amounts.stringify({
value: denom.amountVal,
currency: denom.currency,
fraction: denom.amountFrac,
}),
exchange_base_url: c.exchangeBaseUrl,
refresh_parent_coin_pub: refreshParentCoinPub,
withdrawal_reserve_pub: withdrawalReservePub,
coin_status: c.status,
ageCommitmentProof: c.ageCommitmentProof,
spend_allocation: c.spendAllocation
? {
amount: c.spendAllocation.amount,
id: c.spendAllocation.id,
}
: undefined,
});
}
});
return coinsJson;
}
/**
* 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 TalerError.fromUncheckedDetail(res.error);
case "response":
return res.result;
}
},
};
return client;
}
declare const __VERSION__: string;
declare const __GIT_HASH__: string;
const VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "dev";
const GIT_HASH = typeof __GIT_HASH__ !== "undefined" ? __GIT_HASH__ : undefined;
2022-09-05 18:12:30 +02:00
/**
* Implementation of the "wallet-core" API.
*/
2022-10-17 01:03:36 +02:00
async function dispatchRequestInternal<Op extends WalletApiOperation>(
ws: InternalWalletState,
2022-10-17 01:03:36 +02:00
operation: WalletApiOperation,
payload: unknown,
2022-10-17 01:03:36 +02:00
): Promise<WalletCoreResponseType<typeof operation>> {
if (!ws.initCalled && operation !== WalletApiOperation.InitWallet) {
throw Error(
`wallet must be initialized before running operation ${operation}`,
);
}
// FIXME: Can we make this more type-safe by using the request/response type
// definitions we already have?
switch (operation) {
2022-10-17 01:03:36 +02:00
case WalletApiOperation.InitWallet: {
2022-10-07 12:43:17 +02:00
logger.trace("initializing wallet");
ws.initCalled = true;
if (typeof payload === "object" && (payload as any).skipDefaults) {
2022-10-07 12:43:17 +02:00
logger.trace("skipping defaults");
} else {
2022-10-07 12:43:17 +02:00
logger.trace("filling defaults");
await fillDefaults(ws);
}
const resp: InitResponse = {
versionInfo: getVersion(ws),
};
return resp;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.WithdrawTestkudos: {
await withdrawTestBalance(ws, {
amount: "TESTKUDOS:10",
2023-04-05 17:38:34 +02:00
bankAccessApiBaseUrl:
"https://bank.test.taler.net/demobanks/default/access-api/",
exchangeBaseUrl: "https://exchange.test.taler.net/",
});
return {
versionInfo: getVersion(ws),
};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.WithdrawTestBalance: {
const req = codecForWithdrawTestBalance().decode(payload);
await withdrawTestBalance(ws, req);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.RunIntegrationTest: {
const req = codecForIntegrationTestArgs().decode(payload);
await runIntegrationTest(ws, req);
return {};
}
case WalletApiOperation.RunIntegrationTestV2: {
const req = codecForIntegrationTestV2Args().decode(payload);
await runIntegrationTest2(ws, req);
return {};
}
2023-03-29 18:49:06 +02:00
case WalletApiOperation.ValidateIban: {
const req = codecForValidateIbanRequest().decode(payload);
const valRes = validateIban(req.iban);
const resp: ValidateIbanResponse = {
valid: valRes.type === "valid",
};
return resp;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.TestPay: {
const req = codecForTestPayArgs().decode(payload);
return await testPay(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetTransactions: {
const req = codecForTransactionsRequest().decode(payload);
return await getTransactions(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetTransactionById: {
const req = codecForTransactionByIdRequest().decode(payload);
2022-09-16 17:21:54 +02:00
return await getTransactionById(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.AddExchange: {
const req = codecForAddExchangeRequest().decode(payload);
await updateExchangeFromUrl(ws, req.exchangeBaseUrl, {
forceNow: req.forceUpdate,
});
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ListExchanges: {
return await getExchanges(ws);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetExchangeDetailedInfo: {
const req = codecForAddExchangeRequest().decode(payload);
return await getExchangeDetailedInfo(ws, req.exchangeBaseUrl);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ListKnownBankAccounts: {
2021-12-23 19:17:36 +01:00
const req = codecForListKnownBankAccounts().decode(payload);
return await listKnownBankAccounts(ws, req.currency);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.AddKnownBankAccounts: {
2022-09-23 20:17:29 +02:00
const req = codecForAddKnownBankAccounts().decode(payload);
await addKnownBankAccounts(ws, req.payto, req.alias, req.currency);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ForgetKnownBankAccounts: {
2022-09-23 20:17:29 +02:00
const req = codecForForgetKnownBankAccounts().decode(payload);
await forgetKnownBankAccounts(ws, req.payto);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetWithdrawalDetailsForUri: {
const req = codecForGetWithdrawalDetailsForUri().decode(payload);
return await getWithdrawalDetailsForUri(ws, req.talerWithdrawUri);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.AcceptManualWithdrawal: {
const req = codecForAcceptManualWithdrawalRequet().decode(payload);
2022-08-09 15:00:45 +02:00
const res = await createManualWithdrawal(ws, {
amount: Amounts.parseOrThrow(req.amount),
exchangeBaseUrl: req.exchangeBaseUrl,
restrictAge: req.restrictAge,
});
return res;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetWithdrawalDetailsForAmount: {
2022-01-13 12:08:31 +01:00
const req =
codecForGetWithdrawalDetailsForAmountRequest().decode(payload);
const wi = await getExchangeWithdrawalInfo(
ws,
req.exchangeBaseUrl,
Amounts.parseOrThrow(req.amount),
2022-09-05 18:12:30 +02:00
req.restrictAge,
);
2022-11-25 16:12:14 +01:00
const resp: ManualWithdrawalDetails = {
amountRaw: req.amount,
amountEffective: Amounts.stringify(wi.selectedDenoms.totalCoinValue),
paytoUris: wi.exchangePaytoUris,
tosAccepted: wi.termsOfServiceAccepted,
2022-11-25 16:12:14 +01:00
ageRestrictionOptions: wi.ageRestrictionOptions,
};
2022-11-25 16:12:14 +01:00
return resp;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetBalances: {
return await getBalances(ws);
}
case WalletApiOperation.GetBalanceDetail: {
const req = codecForGetBalanceDetailRequest().decode(payload);
return await getBalanceDetail(ws, req);
}
2022-11-25 03:16:01 +01:00
case WalletApiOperation.GetUserAttentionRequests: {
const req = codecForUserAttentionsRequest().decode(payload);
return await getUserAttentions(ws, req);
}
case WalletApiOperation.MarkAttentionRequestAsRead: {
const req = codecForUserAttentionByIdRequest().decode(payload);
return await markAttentionRequestAsRead(ws, req);
}
case WalletApiOperation.GetUserAttentionUnreadCount: {
const req = codecForUserAttentionsRequest().decode(payload);
return await getUserAttentionsUnreadCount(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetPendingOperations: {
return await getPendingOperations(ws);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.SetExchangeTosAccepted: {
const req = codecForAcceptExchangeTosRequest().decode(payload);
await acceptExchangeTermsOfService(ws, req.exchangeBaseUrl, req.etag);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.AcceptBankIntegratedWithdrawal: {
2022-01-13 12:08:31 +01:00
const req =
codecForAcceptBankIntegratedWithdrawalRequest().decode(payload);
2022-08-09 15:00:45 +02:00
return await acceptWithdrawalFromUri(ws, {
selectedExchange: req.exchangeBaseUrl,
talerWithdrawUri: req.talerWithdrawUri,
forcedDenomSel: req.forcedDenomSel,
restrictAge: req.restrictAge,
});
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetExchangeTos: {
const req = codecForGetExchangeTosRequest().decode(payload);
2021-10-14 11:36:43 +02:00
return getExchangeTos(ws, req.exchangeBaseUrl, req.acceptedFormat);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetContractTermsDetails: {
const req = codecForGetContractTermsDetails().decode(payload);
return getContractTermsDetails(ws, req.proposalId);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.RetryPendingNow: {
await runPending(ws, true);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.PreparePayForUri: {
const req = codecForPreparePayRequest().decode(payload);
return await preparePayForUri(ws, req.talerPayUri);
}
case WalletApiOperation.PreparePayForTemplate: {
const req = codecForPreparePayTemplateRequest().decode(payload);
const url = parsePayTemplateUri(req.talerPayTemplateUri);
const templateDetails: MerchantUsingTemplateDetails = {};
if (!url) {
throw Error("invalid taler-template URI");
}
if (
url.templateParams.amount !== undefined &&
typeof url.templateParams.amount === "string"
) {
templateDetails.amount =
req.templateParams.amount ?? url.templateParams.amount;
}
if (
url.templateParams.summary !== undefined &&
typeof url.templateParams.summary === "string"
) {
templateDetails.summary =
req.templateParams.summary ?? url.templateParams.summary;
}
const reqUrl = new URL(
`templates/${url.templateId}`,
url.merchantBaseUrl,
);
const httpReq = await ws.http.postJson(reqUrl.href, templateDetails);
const resp = await readSuccessResponseJsonOrThrow(
httpReq,
codecForMerchantPostOrderResponse(),
);
const payUri = constructPayUri(
url.merchantBaseUrl,
resp.order_id,
"",
resp.token,
);
return await preparePayForUri(ws, payUri);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ConfirmPay: {
const req = codecForConfirmPayRequest().decode(payload);
return await confirmPay(ws, req.proposalId, req.sessionId);
}
case WalletApiOperation.AbortTransaction: {
const req = codecForAbortTransaction().decode(payload);
2023-05-02 10:59:50 +02:00
await abortTransaction(ws, req.transactionId);
return {};
}
case WalletApiOperation.SuspendTransaction: {
const req = codecForSuspendTransaction().decode(payload);
await suspendTransaction(ws, req.transactionId);
return {};
}
case WalletApiOperation.ResumeTransaction: {
const req = codecForResumeTransaction().decode(payload);
await resumeTransaction(ws, req.transactionId);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.DumpCoins: {
return await dumpCoins(ws);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.SetCoinSuspended: {
const req = codecForSetCoinSuspendedRequest().decode(payload);
await setCoinSuspended(ws, req.coinPub, req.suspended);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ForceRefresh: {
const req = codecForForceRefreshRequest().decode(payload);
if (req.coinPubList.length == 0) {
throw Error("refusing to create empty refresh group");
}
const refreshGroupId = await ws.db
.mktx((x) => [
x.refreshGroups,
x.coinAvailability,
x.denominations,
x.coins,
])
2021-06-09 15:14:17 +02:00
.runReadWrite(async (tx) => {
let coinPubs: CoinRefreshRequest[] = [];
for (const c of req.coinPubList) {
const coin = await tx.coins.get(c);
if (!coin) {
throw Error(`coin (pubkey ${c}) not found`);
}
const denom = await ws.getDenomInfo(
ws,
tx,
coin.exchangeBaseUrl,
coin.denomPubHash,
);
checkDbInvariant(!!denom);
coinPubs.push({
coinPub: c,
amount: denom?.value,
});
}
2019-12-16 12:53:22 +01:00
return await createRefreshGroup(
ws,
2019-12-16 12:53:22 +01:00
tx,
Amounts.currencyOf(coinPubs[0].amount),
coinPubs,
2019-12-16 12:53:22 +01:00
RefreshReason.Manual,
);
2021-06-09 15:14:17 +02:00
});
processRefreshGroup(ws, refreshGroupId.refreshGroupId).catch((x) => {
logger.error(x);
});
return {
refreshGroupId,
};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.PrepareTip: {
const req = codecForPrepareTipRequest().decode(payload);
return await prepareTip(ws, req.talerTipUri);
}
2023-05-05 19:03:44 +02:00
case WalletApiOperation.StartRefundQueryForUri: {
const req = codecForPrepareRefundRequest().decode(payload);
2023-05-05 19:03:44 +02:00
await startRefundQueryForUri(ws, req.talerRefundUri);
return {};
}
case WalletApiOperation.StartRefundQuery: {
const req = codecForStartRefundQueryRequest().decode(payload);
const txIdParsed = parseTransactionIdentifier(req.transactionId);
if (!txIdParsed) {
throw Error("invalid transaction ID");
}
if (txIdParsed.tag !== TransactionType.Payment) {
throw Error("expected payment transaction ID");
}
await startQueryRefund(ws, txIdParsed.proposalId);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.AcceptTip: {
const req = codecForAcceptTipRequest().decode(payload);
return await acceptTip(ws, req.walletTipId);
2019-11-30 00:36:20 +01:00
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ExportBackupPlain: {
return exportBackup(ws);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.AddBackupProvider: {
const req = codecForAddBackupProviderRequest().decode(payload);
return await addBackupProvider(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.RunBackupCycle: {
2021-07-07 18:28:02 +02:00
const req = codecForRunBackupCycle().decode(payload);
await runBackupCycle(ws, req);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.RemoveBackupProvider: {
const req = codecForRemoveBackupProvider().decode(payload);
await removeBackupProvider(ws, req);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ExportBackupRecovery: {
const resp = await getBackupRecovery(ws);
return resp;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ImportBackupRecovery: {
const req = codecForAny().decode(payload);
await loadBackupRecovery(ws, req);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetBackupInfo: {
const resp = await getBackupInfo(ws);
return resp;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.PrepareDeposit: {
2022-05-03 05:16:03 +02:00
const req = codecForPrepareDepositRequest().decode(payload);
return await prepareDepositGroup(ws, req);
}
case WalletApiOperation.GenerateDepositGroupTxId:
return {
transactionId: generateDepositGroupTxId(),
};
2022-10-17 01:03:36 +02:00
case WalletApiOperation.CreateDepositGroup: {
const req = codecForCreateDepositGroupRequest().decode(payload);
return await createDepositGroup(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.DeleteTransaction: {
const req = codecForDeleteTransactionRequest().decode(payload);
await deleteTransaction(ws, req.transactionId);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.RetryTransaction: {
const req = codecForRetryTransactionRequest().decode(payload);
await retryTransaction(ws, req.transactionId);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.SetWalletDeviceId: {
const req = codecForSetWalletDeviceIdRequest().decode(payload);
await setWalletDeviceId(ws, req.walletDeviceId);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ListCurrencies: {
return await ws.db
.mktx((x) => [x.auditorTrust, 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,
})),
};
});
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.WithdrawFakebank: {
2021-10-14 11:36:43 +02:00
const req = codecForWithdrawFakebankRequest().decode(payload);
const amount = Amounts.parseOrThrow(req.amount);
const details = await getExchangeWithdrawalInfo(
2021-10-14 11:36:43 +02:00
ws,
req.exchange,
amount,
2022-09-05 18:12:30 +02:00
undefined,
2021-10-14 11:36:43 +02:00
);
2022-08-09 15:00:45 +02:00
const wres = await createManualWithdrawal(ws, {
amount: amount,
exchangeBaseUrl: req.exchange,
});
const paytoUri = details.exchangePaytoUris[0];
2021-10-14 11:36:43 +02:00
const pt = parsePaytoUri(paytoUri);
if (!pt) {
throw Error("failed to parse payto URI");
}
const components = pt.targetPath.split("/");
const creditorAcct = components[components.length - 1];
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?receiver-name=Foo",
2021-10-14 11:36:43 +02:00
},
);
const fbResp = await readSuccessResponseJsonOrThrow(fbReq, codecForAny());
logger.info(`started fakebank withdrawal: ${j2s(fbResp)}`);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.TestCrypto: {
return await ws.cryptoApi.hashString({ str: "hello world" });
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ClearDb:
await clearDatabase(ws.db.idbHandle());
return {};
2022-10-17 01:03:36 +02:00
case WalletApiOperation.Recycle: {
const backup = await exportBackup(ws);
await clearDatabase(ws.db.idbHandle());
await importBackupPlain(ws, backup);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ExportDb: {
const dbDump = await exportDb(ws.db.idbHandle());
return dbDump;
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ImportDb: {
const req = codecForImportDbRequest().decode(payload);
await importDb(ws.db.idbHandle(), req.dump);
return [];
}
case WalletApiOperation.CheckPeerPushDebit: {
const req = codecForCheckPeerPushDebitRequest().decode(payload);
return await checkPeerPushDebit(ws, req);
}
case WalletApiOperation.InitiatePeerPushDebit: {
const req = codecForInitiatePeerPushPaymentRequest().decode(payload);
return await initiatePeerPushPayment(ws, req);
}
case WalletApiOperation.PreparePeerPushCredit: {
const req = codecForPreparePeerPushCreditRequest().decode(payload);
return await preparePeerPushCredit(ws, req);
}
case WalletApiOperation.ConfirmPeerPushCredit: {
const req = codecForConfirmPeerPushPaymentRequest().decode(payload);
return await confirmPeerPushCredit(ws, req);
}
case WalletApiOperation.CheckPeerPullCredit: {
2022-11-08 17:00:34 +01:00
const req = codecForPreparePeerPullPaymentRequest().decode(payload);
2023-02-19 23:13:44 +01:00
return await checkPeerPullPaymentInitiation(ws, req);
2022-11-08 17:00:34 +01:00
}
case WalletApiOperation.InitiatePeerPullCredit: {
const req = codecForInitiatePeerPullPaymentRequest().decode(payload);
2022-11-02 17:02:42 +01:00
return await initiatePeerPullPayment(ws, req);
}
case WalletApiOperation.PreparePeerPullDebit: {
const req = codecForCheckPeerPullPaymentRequest().decode(payload);
return await preparePeerPullDebit(ws, req);
}
case WalletApiOperation.ConfirmPeerPullDebit: {
const req = codecForAcceptPeerPullPaymentRequest().decode(payload);
return await confirmPeerPullDebit(ws, req);
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.ApplyDevExperiment: {
const req = codecForApplyDevExperiment().decode(payload);
await applyDevExperiment(ws, req.devExperimentUri);
return {};
}
2022-10-17 01:03:36 +02:00
case WalletApiOperation.GetVersion: {
return getVersion(ws);
}
2023-04-19 17:42:47 +02:00
// default:
// assertUnreachable(operation);
}
throw TalerError.fromDetail(
TalerErrorCode.WALLET_CORE_API_OPERATION_UNKNOWN,
{
operation,
},
"unknown operation",
);
}
export function getVersion(ws: InternalWalletState): WalletCoreVersion {
const version: WalletCoreVersion = {
hash: GIT_HASH,
version: VERSION,
exchange: WALLET_EXCHANGE_PROTOCOL_VERSION,
merchant: WALLET_MERCHANT_PROTOCOL_VERSION,
bank: WALLET_BANK_INTEGRATION_PROTOCOL_VERSION,
2023-04-19 17:42:47 +02:00
devMode: false,
};
return version;
}
function translateLegacyOperationNames(operation: string): string {
switch (operation) {
case "initiatePeerPullPayment":
return WalletApiOperation.InitiatePeerPullCredit.toString();
case "initiatePeerPushPayment":
return WalletApiOperation.InitiatePeerPushDebit.toString();
case "checkPeerPullPayment":
return WalletApiOperation.PreparePeerPullDebit.toString();
case "acceptPeerPullPayment":
return WalletApiOperation.ConfirmPeerPullDebit.toString();
case "checkPeerPushPayment":
return WalletApiOperation.PreparePeerPushCredit.toString();
case "acceptPeerPushPayment":
return WalletApiOperation.ConfirmPeerPushCredit.toString();
}
return operation;
}
/**
* Handle a request to the wallet-core API.
*/
export async function handleCoreApiRequest(
ws: InternalWalletState,
operation: string,
id: string,
payload: unknown,
): Promise<CoreApiResponse> {
operation = translateLegacyOperationNames(operation);
try {
2022-10-17 01:03:36 +02:00
const result = await dispatchRequestInternal(ws, operation as any, payload);
return {
type: "response",
operation,
id,
result,
};
2021-10-14 11:36:43 +02:00
} catch (e: any) {
const err = getErrorDetailFromException(e);
logger.info(`finished wallet core request with error: ${j2s(err)}`);
return {
type: "error",
operation,
id,
error: err,
};
}
2016-10-18 01:16:31 +02:00
}
/**
* Public handle to a running wallet.
*/
export class Wallet {
private ws: InternalWalletState;
2022-09-16 17:41:13 +02:00
private _client: WalletCoreApiClient | undefined;
private constructor(
db: DbAccess<typeof WalletStoresV1>,
http: HttpRequestLibrary,
timer: TimerAPI,
cryptoWorkerFactory: CryptoWorkerFactory,
2023-04-19 17:42:47 +02:00
config?: WalletConfigParameter,
) {
2023-04-19 17:42:47 +02:00
this.ws = new InternalWalletStateImpl(
db,
http,
timer,
cryptoWorkerFactory,
2023-04-24 17:53:20 +02:00
Wallet.getEffectiveConfig(config),
2023-04-19 17:42:47 +02:00
);
}
get client(): WalletCoreApiClient {
2022-09-16 17:41:13 +02:00
if (!this._client) {
throw Error();
}
return this._client;
}
static async create(
db: DbAccess<typeof WalletStoresV1>,
http: HttpRequestLibrary,
timer: TimerAPI,
cryptoWorkerFactory: CryptoWorkerFactory,
2023-04-19 17:42:47 +02:00
config?: WalletConfigParameter,
): Promise<Wallet> {
2023-04-19 17:42:47 +02:00
const w = new Wallet(db, http, timer, cryptoWorkerFactory, config);
w._client = await getClientFromWalletState(w.ws);
return w;
}
2023-04-24 17:53:20 +02:00
public static defaultConfig: Readonly<WalletConfig> = {
builtin: {
exchanges: ["https://exchange.demo.taler.net/"],
auditors: [
{
currency: "KUDOS",
auditorPub: "BW9DC48PHQY4NH011SHHX36DZZ3Q22Y6X7FZ1VD1CMZ2PTFZ6PN0",
auditorBaseUrl: "https://auditor.demo.taler.net/",
uids: ["5P25XF8TVQP9AW6VYGY2KV47WT5Y3ZXFSJAA570GJPX5SVJXKBVG"],
},
],
},
features: {
batchWithdrawal: false,
allowHttp: false,
},
testing: {
preventThrottling: false,
2023-04-24 17:53:20 +02:00
devModeActive: false,
insecureTrustExchange: false,
denomselAllowLate: false,
},
};
static getEffectiveConfig(
param?: WalletConfigParameter,
): Readonly<WalletConfig> {
return deepMerge(Wallet.defaultConfig, param ?? {});
2023-04-19 17:42:47 +02:00
}
addNotificationListener(f: (n: WalletNotification) => void): void {
return this.ws.addNotificationListener(f);
}
stop(): void {
this.ws.stop();
}
runPending(forceNow = false): Promise<void> {
return runPending(this.ws, forceNow);
}
runTaskLoop(opts?: RetryLoopOpts): Promise<TaskLoopResult> {
return runTaskLoop(this.ws, opts);
}
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 {
/**
* @see {@link InternalWalletState.activeLongpoll}
*/
activeLongpoll: ActiveLongpollInfo = {};
2022-03-23 21:24:23 +01:00
cryptoApi: TalerCryptoInterface;
cryptoDispatcher: CryptoDispatcher;
merchantInfoCache: Record<string, MerchantInfo> = {};
readonly timerGroup: TimerGroup;
2023-05-05 10:56:42 +02:00
workAvailable = new AsyncCondition();
stopped = false;
listeners: NotificationListener[] = [];
initCalled = false;
exchangeOps: ExchangeOperations = {
getExchangeDetails,
getExchangeTrust,
updateExchangeFromUrl,
};
2021-06-22 12:18:12 +02:00
recoupOps: RecoupOperations = {
createRecoupGroup,
processRecoupGroup,
2021-06-22 12:18:12 +02:00
};
merchantOps: MerchantOperations = {
getMerchantInfo,
};
refreshOps: RefreshOperations = {
createRefreshGroup,
};
2022-01-13 12:08:31 +01:00
// FIXME: Use an LRU cache here.
private denomCache: Record<string, DenominationInfo> = {};
/**
* 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();
2023-04-19 17:42:47 +02:00
config: Readonly<WalletConfig>;
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,
public timer: TimerAPI,
cryptoWorkerFactory: CryptoWorkerFactory,
2023-04-24 17:53:20 +02:00
configParam: WalletConfig,
) {
2022-03-23 21:24:23 +01:00
this.cryptoDispatcher = new CryptoDispatcher(cryptoWorkerFactory);
this.cryptoApi = this.cryptoDispatcher.cryptoApi;
this.timerGroup = new TimerGroup(timer);
2023-04-24 17:53:20 +02:00
this.config = configParam;
2023-04-19 17:42:47 +02:00
if (this.config.testing.devModeActive) {
this.http = new DevExperimentHttpLib(this.http);
}
}
2022-01-13 12:08:31 +01:00
async getDenomInfo(
ws: InternalWalletState,
tx: GetReadWriteAccess<{
denominations: typeof WalletStoresV1.denominations;
}>,
exchangeBaseUrl: string,
denomPubHash: string,
): Promise<DenominationInfo | undefined> {
2022-01-13 12:08:31 +01:00
const key = `${exchangeBaseUrl}:${denomPubHash}`;
const cached = this.denomCache[key];
if (cached) {
return cached;
}
2022-01-13 22:01:14 +01:00
const d = await tx.denominations.get([exchangeBaseUrl, denomPubHash]);
if (d) {
return DenominationRecord.toDenomInfo(d);
2022-01-13 22:01:14 +01:00
}
return undefined;
2022-01-13 12:08:31 +01:00
}
notify(n: WalletNotification): void {
2023-01-03 10:51:05 +01:00
logger.trace("Notification", j2s(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 {
logger.trace("stopping (at internal wallet state)");
this.stopped = true;
this.timerGroup.stopCurrentAndFutureTimers();
2022-03-23 21:24:23 +01:00
this.cryptoDispatcher.stop();
for (const key of Object.keys(this.activeLongpoll)) {
logger.trace(`cancelling active longpoll ${key}`);
this.activeLongpoll[key].cancel();
delete this.activeLongpoll[key];
}
}
2021-06-22 12:18:12 +02:00
async runUntilDone(
req: {
maxRetries?: number;
} = {},
): Promise<void> {
await runTaskLoop(this, { ...req, stopWhenDone: true });
2021-06-22 12:18:12 +02:00
}
/**
* Run an async function after acquiring a list of locks, identified
* by string tokens.
*/
async runSequentialized<T>(
tokens: string[],
f: () => Promise<T>,
): 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();
}
}
}
}
}
2023-04-19 17:42:47 +02:00
/**
* Take the full object as template, create a new result with all the values.
* Use the override object to change the values in the result
* return result
* @param full
* @param override
* @returns
*/
2023-04-24 17:53:20 +02:00
function deepMerge<T extends object>(full: T, override: object): T {
2023-04-19 17:42:47 +02:00
const keys = Object.keys(full);
const result = { ...full };
for (const k of keys) {
// @ts-ignore
const newVal = override[k];
if (newVal === undefined) continue;
// @ts-ignore
result[k] =
// @ts-ignore
typeof newVal === "object" ? deepMerge(full[k], newVal) : newVal;
}
return result;
}