2019-12-02 00:42:40 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2019 GNUnet e.V.
|
|
|
|
|
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2021-03-17 17:56:37 +01:00
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2020-03-09 09:49:22 +01:00
|
|
|
import {
|
2022-03-28 23:21:49 +02:00
|
|
|
AbsoluteTime,
|
2021-03-17 17:56:37 +01:00
|
|
|
Amounts,
|
2022-03-29 13:47:32 +02:00
|
|
|
CancellationToken,
|
2021-06-17 15:49:05 +02:00
|
|
|
canonicalizeBaseUrl,
|
2020-03-09 09:49:22 +01:00
|
|
|
codecForExchangeKeysJson,
|
|
|
|
codecForExchangeWireJson,
|
2022-03-28 23:21:49 +02:00
|
|
|
DenominationPubKey,
|
2021-03-17 17:56:37 +01:00
|
|
|
Duration,
|
|
|
|
durationFromSpec,
|
2022-03-28 23:21:49 +02:00
|
|
|
encodeCrock,
|
|
|
|
ExchangeAuditor,
|
|
|
|
ExchangeDenomination,
|
2022-10-12 20:58:10 +02:00
|
|
|
ExchangeGlobalFees,
|
2021-06-02 13:23:51 +02:00
|
|
|
ExchangeSignKeyJson,
|
|
|
|
ExchangeWireJson,
|
2022-10-12 19:36:30 +02:00
|
|
|
GlobalFees,
|
2022-03-28 23:21:49 +02:00
|
|
|
hashDenomPub,
|
2022-04-19 17:12:43 +02:00
|
|
|
j2s,
|
2022-03-28 23:21:49 +02:00
|
|
|
LibtoolVersion,
|
2021-06-08 20:58:13 +02:00
|
|
|
Logger,
|
2021-03-17 17:56:37 +01:00
|
|
|
NotificationType,
|
|
|
|
parsePaytoUri,
|
2021-06-02 13:23:51 +02:00
|
|
|
Recoup,
|
2021-03-17 17:56:37 +01:00
|
|
|
TalerErrorCode,
|
2022-03-18 15:32:41 +01:00
|
|
|
TalerProtocolDuration,
|
2022-03-28 23:21:49 +02:00
|
|
|
TalerProtocolTimestamp,
|
|
|
|
URL,
|
2022-08-18 21:01:47 +02:00
|
|
|
WireFee,
|
|
|
|
WireFeeMap,
|
|
|
|
WireInfo,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "@gnu-taler/taler-util";
|
2019-12-02 00:42:40 +01:00
|
|
|
import {
|
|
|
|
DenominationRecord,
|
2021-08-24 15:43:06 +02:00
|
|
|
DenominationVerificationStatus,
|
2021-06-17 15:49:05 +02:00
|
|
|
ExchangeDetailsRecord,
|
2021-03-17 17:56:37 +01:00
|
|
|
ExchangeRecord,
|
2021-06-17 15:49:05 +02:00
|
|
|
WalletStoresV1,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "../db.js";
|
2022-03-28 23:21:49 +02:00
|
|
|
import { TalerError } from "../errors.js";
|
|
|
|
import { InternalWalletState, TrustInfo } from "../internal-wallet-state.js";
|
2021-06-15 18:52:43 +02:00
|
|
|
import {
|
2022-03-18 15:32:41 +01:00
|
|
|
getExpiry,
|
2021-06-15 18:52:43 +02:00
|
|
|
HttpRequestLibrary,
|
|
|
|
readSuccessResponseJsonOrThrow,
|
|
|
|
readSuccessResponseTextOrThrow,
|
|
|
|
} from "../util/http.js";
|
2022-10-15 16:03:48 +02:00
|
|
|
import { checkDbInvariant } from "../util/invariants.js";
|
2022-10-05 18:31:56 +02:00
|
|
|
import {
|
|
|
|
DbAccess,
|
|
|
|
GetReadOnlyAccess,
|
|
|
|
GetReadWriteAccess,
|
|
|
|
} from "../util/query.js";
|
2022-09-16 19:27:24 +02:00
|
|
|
import {
|
|
|
|
OperationAttemptResult,
|
|
|
|
OperationAttemptResultType,
|
2022-11-02 14:23:26 +01:00
|
|
|
RetryTags,
|
|
|
|
unwrapOperationHandlerResultOrThrow,
|
2022-09-16 19:27:24 +02:00
|
|
|
} from "../util/retries.js";
|
2022-08-24 11:11:02 +02:00
|
|
|
import { WALLET_EXCHANGE_PROTOCOL_VERSION } from "../versions.js";
|
2022-11-02 14:23:26 +01:00
|
|
|
import { runOperationWithErrorReporting } from "./common.js";
|
2022-11-01 15:31:48 +01:00
|
|
|
import { isWithdrawableDenom } from "./withdraw.js";
|
2020-07-23 15:54:00 +02:00
|
|
|
|
|
|
|
const logger = new Logger("exchanges.ts");
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
function denominationRecordFromKeys(
|
2019-12-02 00:42:40 +01:00
|
|
|
exchangeBaseUrl: string,
|
2021-06-11 13:26:18 +02:00
|
|
|
exchangeMasterPub: string,
|
2022-03-18 15:32:41 +01:00
|
|
|
listIssueDate: TalerProtocolTimestamp,
|
2021-11-27 20:56:58 +01:00
|
|
|
denomIn: ExchangeDenomination,
|
2021-06-02 13:23:51 +02:00
|
|
|
): DenominationRecord {
|
2021-11-27 20:56:58 +01:00
|
|
|
let denomPub: DenominationPubKey;
|
2022-02-21 12:40:51 +01:00
|
|
|
denomPub = denomIn.denom_pub;
|
2021-11-27 20:56:58 +01:00
|
|
|
const denomPubHash = encodeCrock(hashDenomPub(denomPub));
|
2022-09-14 21:27:03 +02:00
|
|
|
const value = Amounts.parseOrThrow(denomIn.value);
|
2019-12-02 00:42:40 +01:00
|
|
|
const d: DenominationRecord = {
|
2021-11-27 20:56:58 +01:00
|
|
|
denomPub,
|
2019-12-02 00:42:40 +01:00
|
|
|
denomPubHash,
|
|
|
|
exchangeBaseUrl,
|
2021-06-11 13:26:18 +02:00
|
|
|
exchangeMasterPub,
|
2022-09-14 21:27:03 +02:00
|
|
|
fees: {
|
2022-11-02 17:42:14 +01:00
|
|
|
feeDeposit: Amounts.stringify(denomIn.fee_deposit),
|
|
|
|
feeRefresh: Amounts.stringify(denomIn.fee_refresh),
|
|
|
|
feeRefund: Amounts.stringify(denomIn.fee_refund),
|
|
|
|
feeWithdraw: Amounts.stringify(denomIn.fee_withdraw),
|
2022-09-14 21:27:03 +02:00
|
|
|
},
|
2019-12-02 00:42:40 +01:00
|
|
|
isOffered: true,
|
2020-03-11 20:14:28 +01:00
|
|
|
isRevoked: false,
|
2019-12-02 00:42:40 +01:00
|
|
|
masterSig: denomIn.master_sig,
|
2019-12-19 20:42:49 +01:00
|
|
|
stampExpireDeposit: denomIn.stamp_expire_deposit,
|
|
|
|
stampExpireLegal: denomIn.stamp_expire_legal,
|
|
|
|
stampExpireWithdraw: denomIn.stamp_expire_withdraw,
|
|
|
|
stampStart: denomIn.stamp_start,
|
2021-08-24 15:43:06 +02:00
|
|
|
verificationStatus: DenominationVerificationStatus.Unverified,
|
2022-09-14 21:27:03 +02:00
|
|
|
amountFrac: value.fraction,
|
|
|
|
amountVal: value.value,
|
|
|
|
currency: value.currency,
|
2021-08-19 13:48:36 +02:00
|
|
|
listIssueDate,
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:12:12 +01:00
|
|
|
export function getExchangeRequestTimeout(): Duration {
|
2022-03-18 15:32:41 +01:00
|
|
|
return Duration.fromSpec({
|
|
|
|
seconds: 5,
|
|
|
|
});
|
2020-08-20 12:57:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:32:06 +02:00
|
|
|
export interface ExchangeTosDownloadResult {
|
2021-06-02 13:23:51 +02:00
|
|
|
tosText: string;
|
|
|
|
tosEtag: string;
|
2021-09-13 20:32:06 +02:00
|
|
|
tosContentType: string;
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:32:06 +02:00
|
|
|
export async function downloadExchangeWithTermsOfService(
|
2021-06-02 13:23:51 +02:00
|
|
|
exchangeBaseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
timeout: Duration,
|
2021-09-13 20:32:06 +02:00
|
|
|
contentType: string,
|
2021-06-02 13:23:51 +02:00
|
|
|
): Promise<ExchangeTosDownloadResult> {
|
2022-10-16 20:15:55 +02:00
|
|
|
logger.info(`downloading exchange tos (type ${contentType})`);
|
2021-06-02 13:23:51 +02:00
|
|
|
const reqUrl = new URL("terms", exchangeBaseUrl);
|
|
|
|
const headers = {
|
2021-09-13 20:32:06 +02:00
|
|
|
Accept: contentType,
|
2021-06-02 13:23:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const resp = await http.get(reqUrl.href, {
|
|
|
|
headers,
|
|
|
|
timeout,
|
|
|
|
});
|
|
|
|
const tosText = await readSuccessResponseTextOrThrow(resp);
|
|
|
|
const tosEtag = resp.headers.get("etag") || "unknown";
|
2021-09-13 20:32:06 +02:00
|
|
|
const tosContentType = resp.headers.get("content-type") || "text/plain";
|
2021-06-02 13:23:51 +02:00
|
|
|
|
2021-09-13 20:32:06 +02:00
|
|
|
return { tosText, tosEtag, tosContentType };
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:28:36 +02:00
|
|
|
/**
|
|
|
|
* Get exchange details from the database.
|
|
|
|
*/
|
2021-06-02 13:23:51 +02:00
|
|
|
export async function getExchangeDetails(
|
2021-06-09 15:14:17 +02:00
|
|
|
tx: GetReadOnlyAccess<{
|
|
|
|
exchanges: typeof WalletStoresV1.exchanges;
|
|
|
|
exchangeDetails: typeof WalletStoresV1.exchangeDetails;
|
|
|
|
}>,
|
2021-06-02 13:23:51 +02:00
|
|
|
exchangeBaseUrl: string,
|
|
|
|
): Promise<ExchangeDetailsRecord | undefined> {
|
2021-06-09 15:14:17 +02:00
|
|
|
const r = await tx.exchanges.get(exchangeBaseUrl);
|
2021-06-02 13:23:51 +02:00
|
|
|
if (!r) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const dp = r.detailsPointer;
|
|
|
|
if (!dp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { currency, masterPublicKey } = dp;
|
2022-10-15 16:03:48 +02:00
|
|
|
return await tx.exchangeDetails.indexes.byPointer.get([
|
|
|
|
r.baseUrl,
|
|
|
|
currency,
|
|
|
|
masterPublicKey,
|
|
|
|
]);
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 15:14:17 +02:00
|
|
|
getExchangeDetails.makeContext = (db: DbAccess<typeof WalletStoresV1>) =>
|
2022-09-13 13:25:41 +02:00
|
|
|
db.mktx((x) => [x.exchanges, x.exchangeDetails]);
|
2021-06-09 15:14:17 +02:00
|
|
|
|
2022-10-14 22:10:03 +02:00
|
|
|
/**
|
|
|
|
* Update the database based on the download of the terms of service.
|
|
|
|
*/
|
2022-01-24 18:12:12 +01:00
|
|
|
export async function updateExchangeTermsOfService(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
tos: ExchangeTosDownloadResult,
|
|
|
|
): Promise<void> {
|
|
|
|
await ws.db
|
2022-10-14 22:10:03 +02:00
|
|
|
.mktx((x) => [x.exchanges, x.exchangeTos, x.exchangeDetails])
|
2022-01-24 18:12:12 +01:00
|
|
|
.runReadWrite(async (tx) => {
|
|
|
|
const d = await getExchangeDetails(tx, exchangeBaseUrl);
|
2022-10-14 22:10:03 +02:00
|
|
|
let tosRecord = await tx.exchangeTos.get([exchangeBaseUrl, tos.tosEtag]);
|
|
|
|
if (!tosRecord) {
|
|
|
|
tosRecord = {
|
|
|
|
etag: tos.tosEtag,
|
|
|
|
exchangeBaseUrl,
|
|
|
|
termsOfServiceContentType: tos.tosContentType,
|
|
|
|
termsOfServiceText: tos.tosText,
|
|
|
|
};
|
|
|
|
await tx.exchangeTos.put(tosRecord);
|
|
|
|
}
|
2022-01-24 18:12:12 +01:00
|
|
|
if (d) {
|
2022-10-14 22:10:03 +02:00
|
|
|
d.tosCurrentEtag = tos.tosEtag;
|
2022-01-24 18:12:12 +01:00
|
|
|
await tx.exchangeDetails.put(d);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-14 22:10:03 +02:00
|
|
|
/**
|
|
|
|
* Mark a ToS version as accepted by the user.
|
2022-10-15 16:03:48 +02:00
|
|
|
*
|
2022-10-14 22:10:03 +02:00
|
|
|
* @param etag version of the ToS to accept, or current ToS version of not given
|
|
|
|
*/
|
2021-06-02 13:23:51 +02:00
|
|
|
export async function acceptExchangeTermsOfService(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
etag: string | undefined,
|
|
|
|
): Promise<void> {
|
2021-06-09 15:14:17 +02:00
|
|
|
await ws.db
|
2022-09-13 13:25:41 +02:00
|
|
|
.mktx((x) => [x.exchanges, x.exchangeDetails])
|
2021-06-09 15:14:17 +02:00
|
|
|
.runReadWrite(async (tx) => {
|
2021-06-02 13:23:51 +02:00
|
|
|
const d = await getExchangeDetails(tx, exchangeBaseUrl);
|
|
|
|
if (d) {
|
2022-10-14 22:10:03 +02:00
|
|
|
d.tosAccepted = {
|
|
|
|
etag: etag || d.tosCurrentEtag,
|
|
|
|
timestamp: TalerProtocolTimestamp.now(),
|
|
|
|
};
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.exchangeDetails.put(d);
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
2021-06-09 15:14:17 +02:00
|
|
|
});
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function validateWireInfo(
|
2021-12-08 16:23:00 +01:00
|
|
|
ws: InternalWalletState,
|
2021-11-27 20:56:58 +01:00
|
|
|
versionCurrent: number,
|
2021-06-02 13:23:51 +02:00
|
|
|
wireInfo: ExchangeWireJson,
|
|
|
|
masterPublicKey: string,
|
|
|
|
): Promise<WireInfo> {
|
|
|
|
for (const a of wireInfo.accounts) {
|
|
|
|
logger.trace("validating exchange acct");
|
2021-12-08 16:23:00 +01:00
|
|
|
let isValid = false;
|
|
|
|
if (ws.insecureTrustExchange) {
|
|
|
|
isValid = true;
|
|
|
|
} else {
|
2022-03-23 21:24:23 +01:00
|
|
|
const { valid: v } = await ws.cryptoApi.isValidWireAccount({
|
|
|
|
masterPub: masterPublicKey,
|
|
|
|
paytoUri: a.payto_uri,
|
|
|
|
sig: a.master_sig,
|
2021-12-08 16:23:00 +01:00
|
|
|
versionCurrent,
|
2022-03-23 21:24:23 +01:00
|
|
|
});
|
|
|
|
isValid = v;
|
2021-12-08 16:23:00 +01:00
|
|
|
}
|
2021-06-02 13:23:51 +02:00
|
|
|
if (!isValid) {
|
|
|
|
throw Error("exchange acct signature invalid");
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 21:01:47 +02:00
|
|
|
const feesForType: WireFeeMap = {};
|
2021-06-02 13:23:51 +02:00
|
|
|
for (const wireMethod of Object.keys(wireInfo.fees)) {
|
|
|
|
const feeList: WireFee[] = [];
|
|
|
|
for (const x of wireInfo.fees[wireMethod]) {
|
|
|
|
const startStamp = x.start_date;
|
|
|
|
const endStamp = x.end_date;
|
|
|
|
const fee: WireFee = {
|
2022-11-02 17:42:14 +01:00
|
|
|
closingFee: Amounts.stringify(x.closing_fee),
|
2021-06-02 13:23:51 +02:00
|
|
|
endStamp,
|
|
|
|
sig: x.sig,
|
|
|
|
startStamp,
|
2022-11-02 17:42:14 +01:00
|
|
|
wireFee: Amounts.stringify(x.wire_fee),
|
2021-06-02 13:23:51 +02:00
|
|
|
};
|
2021-12-08 16:23:00 +01:00
|
|
|
let isValid = false;
|
|
|
|
if (ws.insecureTrustExchange) {
|
|
|
|
isValid = true;
|
|
|
|
} else {
|
2022-03-23 21:24:23 +01:00
|
|
|
const { valid: v } = await ws.cryptoApi.isValidWireFee({
|
|
|
|
masterPub: masterPublicKey,
|
|
|
|
type: wireMethod,
|
|
|
|
wf: fee,
|
|
|
|
});
|
|
|
|
isValid = v;
|
2021-12-08 16:23:00 +01:00
|
|
|
}
|
2021-06-02 13:23:51 +02:00
|
|
|
if (!isValid) {
|
|
|
|
throw Error("exchange wire fee signature invalid");
|
|
|
|
}
|
|
|
|
feeList.push(fee);
|
|
|
|
}
|
|
|
|
feesForType[wireMethod] = feeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
accounts: wireInfo.accounts,
|
|
|
|
feesForType,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:36:30 +02:00
|
|
|
async function validateGlobalFees(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
fees: GlobalFees[],
|
|
|
|
masterPub: string,
|
2022-10-12 20:58:10 +02:00
|
|
|
): Promise<ExchangeGlobalFees[]> {
|
|
|
|
const egf: ExchangeGlobalFees[] = [];
|
2022-10-12 19:36:30 +02:00
|
|
|
for (const gf of fees) {
|
|
|
|
logger.trace("validating exchange global fees");
|
|
|
|
let isValid = false;
|
|
|
|
if (ws.insecureTrustExchange) {
|
|
|
|
isValid = true;
|
|
|
|
} else {
|
|
|
|
const { valid: v } = await ws.cryptoApi.isValidGlobalFees({
|
|
|
|
masterPub,
|
|
|
|
gf,
|
|
|
|
});
|
|
|
|
isValid = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
throw Error("exchange global fees signature invalid: " + gf.master_sig);
|
|
|
|
}
|
2022-10-12 20:58:10 +02:00
|
|
|
egf.push({
|
2022-11-02 17:42:14 +01:00
|
|
|
accountFee: Amounts.stringify(gf.account_fee),
|
|
|
|
historyFee: Amounts.stringify(gf.history_fee),
|
|
|
|
purseFee: Amounts.stringify(gf.purse_fee),
|
2022-10-12 20:58:10 +02:00
|
|
|
startDate: gf.start_date,
|
|
|
|
endDate: gf.end_date,
|
|
|
|
signature: gf.master_sig,
|
|
|
|
historyTimeout: gf.history_expiration,
|
|
|
|
purseLimit: gf.purse_account_limit,
|
|
|
|
purseTimeout: gf.purse_timeout,
|
|
|
|
});
|
2022-10-12 19:36:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 20:58:10 +02:00
|
|
|
return egf;
|
2022-10-12 19:36:30 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 18:31:30 +01:00
|
|
|
export interface ExchangeInfo {
|
|
|
|
wire: ExchangeWireJson;
|
|
|
|
keys: ExchangeKeysDownloadResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function downloadExchangeInfo(
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
): Promise<ExchangeInfo> {
|
|
|
|
const wireInfo = await downloadExchangeWireInfo(
|
|
|
|
exchangeBaseUrl,
|
|
|
|
http,
|
|
|
|
Duration.getForever(),
|
|
|
|
);
|
|
|
|
const keysInfo = await downloadExchangeKeysInfo(
|
|
|
|
exchangeBaseUrl,
|
|
|
|
http,
|
|
|
|
Duration.getForever(),
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
keys: keysInfo,
|
|
|
|
wire: wireInfo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
/**
|
2021-06-02 13:23:51 +02:00
|
|
|
* Fetch wire information for an exchange.
|
2019-12-02 00:42:40 +01:00
|
|
|
*
|
2021-06-02 13:23:51 +02:00
|
|
|
* @param exchangeBaseUrl Exchange base URL, assumed to be already normalized.
|
2019-12-02 00:42:40 +01:00
|
|
|
*/
|
2022-03-14 18:31:30 +01:00
|
|
|
async function downloadExchangeWireInfo(
|
2021-06-02 13:23:51 +02:00
|
|
|
exchangeBaseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
timeout: Duration,
|
|
|
|
): Promise<ExchangeWireJson> {
|
|
|
|
const reqUrl = new URL("wire", exchangeBaseUrl);
|
|
|
|
|
|
|
|
const resp = await http.get(reqUrl.href, {
|
|
|
|
timeout,
|
|
|
|
});
|
|
|
|
const wireInfo = await readSuccessResponseJsonOrThrow(
|
|
|
|
resp,
|
|
|
|
codecForExchangeWireJson(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return wireInfo;
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:31:56 +02:00
|
|
|
export async function provideExchangeRecordInTx(
|
2021-06-02 13:23:51 +02:00
|
|
|
ws: InternalWalletState,
|
2022-10-05 18:31:56 +02:00
|
|
|
tx: GetReadWriteAccess<{
|
|
|
|
exchanges: typeof WalletStoresV1.exchanges;
|
|
|
|
exchangeDetails: typeof WalletStoresV1.exchangeDetails;
|
|
|
|
}>,
|
2021-06-02 13:23:51 +02:00
|
|
|
baseUrl: string,
|
2022-03-18 15:32:41 +01:00
|
|
|
now: AbsoluteTime,
|
2022-03-15 04:24:39 +01:00
|
|
|
): Promise<{
|
|
|
|
exchange: ExchangeRecord;
|
|
|
|
exchangeDetails: ExchangeDetailsRecord | undefined;
|
|
|
|
}> {
|
2022-10-05 18:31:56 +02:00
|
|
|
let exchange = await tx.exchanges.get(baseUrl);
|
|
|
|
if (!exchange) {
|
|
|
|
const r: ExchangeRecord = {
|
|
|
|
permanent: true,
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
detailsPointer: undefined,
|
|
|
|
lastUpdate: undefined,
|
|
|
|
nextUpdate: AbsoluteTime.toTimestamp(now),
|
|
|
|
nextRefreshCheck: AbsoluteTime.toTimestamp(now),
|
|
|
|
lastKeysEtag: undefined,
|
|
|
|
lastWireEtag: undefined,
|
|
|
|
};
|
|
|
|
await tx.exchanges.put(r);
|
|
|
|
exchange = r;
|
|
|
|
}
|
|
|
|
const exchangeDetails = await getExchangeDetails(tx, baseUrl);
|
|
|
|
return { exchange, exchangeDetails };
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
2020-07-22 10:52:03 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
interface ExchangeKeysDownloadResult {
|
|
|
|
masterPublicKey: string;
|
|
|
|
currency: string;
|
2021-11-27 20:56:58 +01:00
|
|
|
auditors: ExchangeAuditor[];
|
2021-06-02 13:23:51 +02:00
|
|
|
currentDenominations: DenominationRecord[];
|
|
|
|
protocolVersion: string;
|
|
|
|
signingKeys: ExchangeSignKeyJson[];
|
2022-03-18 15:32:41 +01:00
|
|
|
reserveClosingDelay: TalerProtocolDuration;
|
|
|
|
expiry: TalerProtocolTimestamp;
|
2021-06-02 13:23:51 +02:00
|
|
|
recoup: Recoup[];
|
2022-03-18 15:32:41 +01:00
|
|
|
listIssueDate: TalerProtocolTimestamp;
|
2022-10-12 19:36:30 +02:00
|
|
|
globalFees: GlobalFees[];
|
2021-06-02 13:23:51 +02:00
|
|
|
}
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
/**
|
|
|
|
* Download and validate an exchange's /keys data.
|
|
|
|
*/
|
2022-03-14 18:31:30 +01:00
|
|
|
async function downloadExchangeKeysInfo(
|
2021-06-02 13:23:51 +02:00
|
|
|
baseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
timeout: Duration,
|
|
|
|
): Promise<ExchangeKeysDownloadResult> {
|
2019-12-02 00:42:40 +01:00
|
|
|
const keysUrl = new URL("keys", baseUrl);
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const resp = await http.get(keysUrl.href, {
|
|
|
|
timeout,
|
2020-08-20 12:57:20 +02:00
|
|
|
});
|
2021-11-27 20:56:58 +01:00
|
|
|
const exchangeKeysJsonUnchecked = await readSuccessResponseJsonOrThrow(
|
2020-07-22 10:52:03 +02:00
|
|
|
resp,
|
|
|
|
codecForExchangeKeysJson(),
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-11-27 20:56:58 +01:00
|
|
|
if (exchangeKeysJsonUnchecked.denoms.length === 0) {
|
2022-03-22 21:16:38 +01:00
|
|
|
throw TalerError.fromDetail(
|
2020-07-22 10:52:03 +02:00
|
|
|
TalerErrorCode.WALLET_EXCHANGE_DENOMINATIONS_INSUFFICIENT,
|
|
|
|
{
|
|
|
|
exchangeBaseUrl: baseUrl,
|
|
|
|
},
|
2022-03-22 21:16:38 +01:00
|
|
|
"exchange doesn't offer any denominations",
|
2020-07-22 10:52:03 +02:00
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:56:58 +01:00
|
|
|
const protocolVersion = exchangeKeysJsonUnchecked.version;
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-11-23 23:51:12 +01:00
|
|
|
const versionRes = LibtoolVersion.compare(
|
|
|
|
WALLET_EXCHANGE_PROTOCOL_VERSION,
|
|
|
|
protocolVersion,
|
|
|
|
);
|
2020-03-09 09:59:13 +01:00
|
|
|
if (versionRes?.compatible != true) {
|
2022-03-22 21:16:38 +01:00
|
|
|
throw TalerError.fromDetail(
|
2020-07-22 10:52:03 +02:00
|
|
|
TalerErrorCode.WALLET_EXCHANGE_PROTOCOL_VERSION_INCOMPATIBLE,
|
|
|
|
{
|
2020-03-09 09:59:13 +01:00
|
|
|
exchangeProtocolVersion: protocolVersion,
|
|
|
|
walletProtocolVersion: WALLET_EXCHANGE_PROTOCOL_VERSION,
|
|
|
|
},
|
2022-03-22 21:16:38 +01:00
|
|
|
"exchange protocol version not compatible with wallet",
|
2020-07-22 10:52:03 +02:00
|
|
|
);
|
2020-03-09 09:59:13 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const currency = Amounts.parseOrThrow(
|
2021-11-27 20:56:58 +01:00
|
|
|
exchangeKeysJsonUnchecked.denoms[0].value,
|
2021-06-02 13:23:51 +02:00
|
|
|
).currency.toUpperCase();
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
return {
|
2021-11-27 20:56:58 +01:00
|
|
|
masterPublicKey: exchangeKeysJsonUnchecked.master_public_key,
|
2021-06-02 13:23:51 +02:00
|
|
|
currency,
|
2021-11-27 20:56:58 +01:00
|
|
|
auditors: exchangeKeysJsonUnchecked.auditors,
|
|
|
|
currentDenominations: exchangeKeysJsonUnchecked.denoms.map((d) =>
|
2021-06-11 13:26:18 +02:00
|
|
|
denominationRecordFromKeys(
|
|
|
|
baseUrl,
|
2021-11-27 20:56:58 +01:00
|
|
|
exchangeKeysJsonUnchecked.master_public_key,
|
|
|
|
exchangeKeysJsonUnchecked.list_issue_date,
|
2021-06-11 13:26:18 +02:00
|
|
|
d,
|
|
|
|
),
|
2019-12-02 00:42:40 +01:00
|
|
|
),
|
2021-11-27 20:56:58 +01:00
|
|
|
protocolVersion: exchangeKeysJsonUnchecked.version,
|
|
|
|
signingKeys: exchangeKeysJsonUnchecked.signkeys,
|
|
|
|
reserveClosingDelay: exchangeKeysJsonUnchecked.reserve_closing_delay,
|
2022-03-18 15:32:41 +01:00
|
|
|
expiry: AbsoluteTime.toTimestamp(
|
|
|
|
getExpiry(resp, {
|
|
|
|
minDuration: durationFromSpec({ hours: 1 }),
|
|
|
|
}),
|
|
|
|
),
|
2021-11-27 20:56:58 +01:00
|
|
|
recoup: exchangeKeysJsonUnchecked.recoup ?? [],
|
|
|
|
listIssueDate: exchangeKeysJsonUnchecked.list_issue_date,
|
2022-10-12 19:36:30 +02:00
|
|
|
globalFees: exchangeKeysJsonUnchecked.global_fees,
|
2021-06-02 13:23:51 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:12:12 +01:00
|
|
|
export async function downloadTosFromAcceptedFormat(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
baseUrl: string,
|
|
|
|
timeout: Duration,
|
2022-03-07 12:09:38 +01:00
|
|
|
acceptedFormat?: string[],
|
|
|
|
): Promise<ExchangeTosDownloadResult> {
|
2022-01-24 18:12:12 +01:00
|
|
|
let tosFound: ExchangeTosDownloadResult | undefined;
|
|
|
|
//Remove this when exchange supports multiple content-type in accept header
|
|
|
|
if (acceptedFormat)
|
|
|
|
for (const format of acceptedFormat) {
|
|
|
|
const resp = await downloadExchangeWithTermsOfService(
|
|
|
|
baseUrl,
|
|
|
|
ws.http,
|
|
|
|
timeout,
|
|
|
|
format,
|
|
|
|
);
|
|
|
|
if (resp.tosContentType === format) {
|
|
|
|
tosFound = resp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 20:15:55 +02:00
|
|
|
if (tosFound !== undefined) {
|
|
|
|
return tosFound;
|
|
|
|
}
|
2022-01-24 18:12:12 +01:00
|
|
|
// If none of the specified format was found try text/plain
|
|
|
|
return await downloadExchangeWithTermsOfService(
|
|
|
|
baseUrl,
|
|
|
|
ws.http,
|
|
|
|
timeout,
|
|
|
|
"text/plain",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-05 18:12:30 +02:00
|
|
|
export async function updateExchangeFromUrl(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
baseUrl: string,
|
|
|
|
options: {
|
|
|
|
forceNow?: boolean;
|
|
|
|
cancellationToken?: CancellationToken;
|
|
|
|
} = {},
|
|
|
|
): Promise<{
|
|
|
|
exchange: ExchangeRecord;
|
|
|
|
exchangeDetails: ExchangeDetailsRecord;
|
|
|
|
}> {
|
2022-11-02 14:23:26 +01:00
|
|
|
const canonUrl = canonicalizeBaseUrl(baseUrl);
|
|
|
|
return unwrapOperationHandlerResultOrThrow(
|
|
|
|
await runOperationWithErrorReporting(
|
|
|
|
ws,
|
|
|
|
RetryTags.forExchangeUpdateFromUrl(canonUrl),
|
|
|
|
() => updateExchangeFromUrlHandler(ws, canonUrl, options),
|
|
|
|
),
|
2022-09-05 18:12:30 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
/**
|
|
|
|
* Update or add exchange DB entry by fetching the /keys and /wire information.
|
|
|
|
* Optionally link the reserve entry to the new or existing
|
|
|
|
* exchange entry in then DB.
|
|
|
|
*/
|
2022-09-05 18:12:30 +02:00
|
|
|
export async function updateExchangeFromUrlHandler(
|
2021-06-02 13:23:51 +02:00
|
|
|
ws: InternalWalletState,
|
2022-10-16 20:15:55 +02:00
|
|
|
exchangeBaseUrl: string,
|
2022-03-29 13:47:32 +02:00
|
|
|
options: {
|
|
|
|
forceNow?: boolean;
|
|
|
|
cancellationToken?: CancellationToken;
|
|
|
|
} = {},
|
2022-09-05 18:12:30 +02:00
|
|
|
): Promise<
|
|
|
|
OperationAttemptResult<{
|
|
|
|
exchange: ExchangeRecord;
|
|
|
|
exchangeDetails: ExchangeDetailsRecord;
|
|
|
|
}>
|
|
|
|
> {
|
2022-03-29 13:47:32 +02:00
|
|
|
const forceNow = options.forceNow ?? false;
|
2022-10-16 20:15:55 +02:00
|
|
|
logger.info(
|
|
|
|
`updating exchange info for ${exchangeBaseUrl}, forced: ${forceNow}`,
|
|
|
|
);
|
2022-05-18 20:57:10 +02:00
|
|
|
|
2022-03-18 15:32:41 +01:00
|
|
|
const now = AbsoluteTime.now();
|
2022-10-16 20:15:55 +02:00
|
|
|
exchangeBaseUrl = canonicalizeBaseUrl(exchangeBaseUrl);
|
2022-10-15 16:03:48 +02:00
|
|
|
let isNewExchange = true;
|
2022-10-05 18:31:56 +02:00
|
|
|
const { exchange, exchangeDetails } = await ws.db
|
|
|
|
.mktx((x) => [x.exchanges, x.exchangeDetails])
|
|
|
|
.runReadWrite(async (tx) => {
|
2022-10-16 20:15:55 +02:00
|
|
|
let oldExch = await tx.exchanges.get(exchangeBaseUrl);
|
2022-10-15 16:03:48 +02:00
|
|
|
if (oldExch) {
|
|
|
|
isNewExchange = false;
|
|
|
|
}
|
2022-10-16 20:15:55 +02:00
|
|
|
return provideExchangeRecordInTx(ws, tx, exchangeBaseUrl, now);
|
2022-10-05 18:31:56 +02:00
|
|
|
});
|
2022-03-15 04:24:39 +01:00
|
|
|
|
2022-03-18 15:32:41 +01:00
|
|
|
if (
|
|
|
|
!forceNow &&
|
|
|
|
exchangeDetails !== undefined &&
|
|
|
|
!AbsoluteTime.isExpired(AbsoluteTime.fromTimestamp(exchange.nextUpdate))
|
|
|
|
) {
|
2022-03-15 04:24:39 +01:00
|
|
|
logger.info("using existing exchange info");
|
2022-09-05 18:12:30 +02:00
|
|
|
return {
|
|
|
|
type: OperationAttemptResultType.Finished,
|
|
|
|
result: { exchange, exchangeDetails },
|
|
|
|
};
|
2021-06-10 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
logger.info("updating exchange /keys info");
|
|
|
|
|
2022-01-24 18:12:12 +01:00
|
|
|
const timeout = getExchangeRequestTimeout();
|
2021-06-02 13:23:51 +02:00
|
|
|
|
2022-10-16 20:15:55 +02:00
|
|
|
const keysInfo = await downloadExchangeKeysInfo(
|
|
|
|
exchangeBaseUrl,
|
|
|
|
ws.http,
|
|
|
|
timeout,
|
|
|
|
);
|
2021-06-02 13:23:51 +02:00
|
|
|
|
2021-08-06 16:27:18 +02:00
|
|
|
logger.info("updating exchange /wire info");
|
2022-03-14 18:31:30 +01:00
|
|
|
const wireInfoDownload = await downloadExchangeWireInfo(
|
2022-10-16 20:15:55 +02:00
|
|
|
exchangeBaseUrl,
|
2021-06-02 13:23:51 +02:00
|
|
|
ws.http,
|
|
|
|
timeout,
|
2019-12-02 00:42:40 +01:00
|
|
|
);
|
|
|
|
|
2021-08-06 16:27:18 +02:00
|
|
|
logger.info("validating exchange /wire info");
|
|
|
|
|
2021-11-27 20:56:58 +01:00
|
|
|
const version = LibtoolVersion.parseVersion(keysInfo.protocolVersion);
|
|
|
|
if (!version) {
|
|
|
|
// Should have been validated earlier.
|
|
|
|
throw Error("unexpected invalid version");
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const wireInfo = await validateWireInfo(
|
2021-12-08 16:23:00 +01:00
|
|
|
ws,
|
2021-11-27 20:56:58 +01:00
|
|
|
version.current,
|
2021-06-02 13:23:51 +02:00
|
|
|
wireInfoDownload,
|
|
|
|
keysInfo.masterPublicKey,
|
|
|
|
);
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2022-10-12 19:36:30 +02:00
|
|
|
const globalFees = await validateGlobalFees(
|
|
|
|
ws,
|
|
|
|
keysInfo.globalFees,
|
|
|
|
keysInfo.masterPublicKey,
|
|
|
|
);
|
|
|
|
|
2021-08-06 16:27:18 +02:00
|
|
|
logger.info("finished validating exchange /wire info");
|
|
|
|
|
2022-10-16 20:15:55 +02:00
|
|
|
// We download the text/plain version here,
|
|
|
|
// because that one needs to exist, and we
|
|
|
|
// will get the current etag from the response.
|
2022-03-07 12:09:38 +01:00
|
|
|
const tosDownload = await downloadTosFromAcceptedFormat(
|
|
|
|
ws,
|
2022-10-16 20:15:55 +02:00
|
|
|
exchangeBaseUrl,
|
2022-03-07 12:09:38 +01:00
|
|
|
timeout,
|
2022-03-29 13:47:32 +02:00
|
|
|
["text/plain"],
|
2022-03-07 12:09:38 +01:00
|
|
|
);
|
2022-01-24 18:12:12 +01:00
|
|
|
|
|
|
|
let recoupGroupId: string | undefined;
|
2020-03-11 20:14:28 +01:00
|
|
|
|
2021-08-06 16:27:18 +02:00
|
|
|
logger.trace("updating exchange info in database");
|
|
|
|
|
2022-10-15 16:03:48 +02:00
|
|
|
let detailsPointerChanged = false;
|
|
|
|
|
2022-11-01 15:31:48 +01:00
|
|
|
let ageMask = 0;
|
|
|
|
for (const x of keysInfo.currentDenominations) {
|
|
|
|
if (isWithdrawableDenom(x) && x.denomPub.age_mask != 0) {
|
|
|
|
ageMask = x.denomPub.age_mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 15:14:17 +02:00
|
|
|
const updated = await ws.db
|
2022-09-13 13:25:41 +02:00
|
|
|
.mktx((x) => [
|
|
|
|
x.exchanges,
|
2022-10-16 20:15:55 +02:00
|
|
|
x.exchangeTos,
|
2022-09-13 13:25:41 +02:00
|
|
|
x.exchangeDetails,
|
2022-10-15 16:03:48 +02:00
|
|
|
x.exchangeSignkeys,
|
2022-09-13 13:25:41 +02:00
|
|
|
x.denominations,
|
|
|
|
x.coins,
|
|
|
|
x.refreshGroups,
|
|
|
|
x.recoupGroups,
|
|
|
|
])
|
2021-06-09 15:14:17 +02:00
|
|
|
.runReadWrite(async (tx) => {
|
2022-10-16 20:15:55 +02:00
|
|
|
const r = await tx.exchanges.get(exchangeBaseUrl);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!r) {
|
2022-10-16 20:15:55 +02:00
|
|
|
logger.warn(`exchange ${exchangeBaseUrl} no longer present`);
|
2019-12-02 00:42:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-10-17 16:44:23 +02:00
|
|
|
const existingDetails = await getExchangeDetails(tx, r.baseUrl);
|
2022-10-15 16:03:48 +02:00
|
|
|
if (!existingDetails) {
|
|
|
|
detailsPointerChanged = true;
|
|
|
|
}
|
|
|
|
if (existingDetails) {
|
|
|
|
if (existingDetails.masterPublicKey !== keysInfo.masterPublicKey) {
|
|
|
|
detailsPointerChanged = true;
|
|
|
|
}
|
|
|
|
if (existingDetails.currency !== keysInfo.currency) {
|
|
|
|
detailsPointerChanged = true;
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
// FIXME: We need to do some consistency checks!
|
|
|
|
}
|
2022-10-17 16:44:23 +02:00
|
|
|
const existingTosAccepted = existingDetails?.tosAccepted;
|
|
|
|
const newDetails: ExchangeDetailsRecord = {
|
2021-06-02 13:23:51 +02:00
|
|
|
auditors: keysInfo.auditors,
|
|
|
|
currency: keysInfo.currency,
|
|
|
|
masterPublicKey: keysInfo.masterPublicKey,
|
2022-10-14 21:00:13 +02:00
|
|
|
protocolVersionRange: keysInfo.protocolVersion,
|
2021-06-02 13:23:51 +02:00
|
|
|
reserveClosingDelay: keysInfo.reserveClosingDelay,
|
2022-10-12 19:36:30 +02:00
|
|
|
globalFees,
|
2021-06-02 13:23:51 +02:00
|
|
|
exchangeBaseUrl: r.baseUrl,
|
|
|
|
wireInfo,
|
2022-10-15 16:03:48 +02:00
|
|
|
tosCurrentEtag: tosDownload.tosEtag,
|
|
|
|
tosAccepted: existingTosAccepted,
|
2022-11-01 15:31:48 +01:00
|
|
|
ageMask,
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
2022-10-17 16:44:23 +02:00
|
|
|
if (existingDetails?.rowId) {
|
|
|
|
newDetails.rowId = existingDetails.rowId;
|
|
|
|
}
|
2022-03-18 15:32:41 +01:00
|
|
|
r.lastUpdate = TalerProtocolTimestamp.now();
|
2021-08-23 22:28:36 +02:00
|
|
|
r.nextUpdate = keysInfo.expiry;
|
|
|
|
// New denominations might be available.
|
2022-03-18 15:32:41 +01:00
|
|
|
r.nextRefreshCheck = TalerProtocolTimestamp.now();
|
2022-10-15 16:03:48 +02:00
|
|
|
if (detailsPointerChanged) {
|
|
|
|
r.detailsPointer = {
|
|
|
|
currency: newDetails.currency,
|
|
|
|
masterPublicKey: newDetails.masterPublicKey,
|
|
|
|
updateClock: TalerProtocolTimestamp.now(),
|
|
|
|
};
|
|
|
|
}
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.exchanges.put(r);
|
2022-10-15 16:03:48 +02:00
|
|
|
const drRowId = await tx.exchangeDetails.put(newDetails);
|
|
|
|
checkDbInvariant(typeof drRowId.key === "number");
|
|
|
|
|
2022-10-16 20:15:55 +02:00
|
|
|
let tosRecord = await tx.exchangeTos.get([
|
|
|
|
exchangeBaseUrl,
|
|
|
|
tosDownload.tosEtag,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!tosRecord || tosRecord.etag !== existingTosAccepted?.etag) {
|
|
|
|
tosRecord = {
|
|
|
|
etag: tosDownload.tosEtag,
|
|
|
|
exchangeBaseUrl,
|
|
|
|
termsOfServiceContentType: tosDownload.tosContentType,
|
|
|
|
termsOfServiceText: tosDownload.tosText,
|
|
|
|
};
|
|
|
|
await tx.exchangeTos.put(tosRecord);
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:03:48 +02:00
|
|
|
for (const sk of keysInfo.signingKeys) {
|
|
|
|
// FIXME: validate signing keys before inserting them
|
|
|
|
await tx.exchangeSignKeys.put({
|
|
|
|
exchangeDetailsRowId: drRowId.key,
|
|
|
|
masterSig: sk.master_sig,
|
|
|
|
signkeyPub: sk.key,
|
|
|
|
stampEnd: sk.stamp_end,
|
|
|
|
stampExpire: sk.stamp_expire,
|
|
|
|
stampStart: sk.stamp_start,
|
|
|
|
});
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2022-01-13 22:01:14 +01:00
|
|
|
logger.info("updating denominations in database");
|
2021-08-23 22:28:36 +02:00
|
|
|
const currentDenomSet = new Set<string>(
|
|
|
|
keysInfo.currentDenominations.map((x) => x.denomPubHash),
|
|
|
|
);
|
2021-06-02 13:23:51 +02:00
|
|
|
for (const currentDenom of keysInfo.currentDenominations) {
|
2021-06-09 15:14:17 +02:00
|
|
|
const oldDenom = await tx.denominations.get([
|
2022-10-16 20:15:55 +02:00
|
|
|
exchangeBaseUrl,
|
2021-06-02 13:23:51 +02:00
|
|
|
currentDenom.denomPubHash,
|
2019-12-02 00:42:40 +01:00
|
|
|
]);
|
|
|
|
if (oldDenom) {
|
2021-08-23 22:28:36 +02:00
|
|
|
// FIXME: Do consistency check, report to auditor if necessary.
|
2019-12-02 00:42:40 +01:00
|
|
|
} else {
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.denominations.put(currentDenom);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 22:28:36 +02:00
|
|
|
|
|
|
|
// Update list issue date for all denominations,
|
|
|
|
// and mark non-offered denominations as such.
|
|
|
|
await tx.denominations.indexes.byExchangeBaseUrl
|
|
|
|
.iter(r.baseUrl)
|
|
|
|
.forEachAsync(async (x) => {
|
|
|
|
if (!currentDenomSet.has(x.denomPubHash)) {
|
|
|
|
// FIXME: Here, an auditor report should be created, unless
|
|
|
|
// the denomination is really legally expired.
|
|
|
|
if (x.isOffered) {
|
|
|
|
x.isOffered = false;
|
|
|
|
logger.info(
|
|
|
|
`setting denomination ${x.denomPubHash} to offered=false`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x.listIssueDate = keysInfo.listIssueDate;
|
|
|
|
if (!x.isOffered) {
|
|
|
|
x.isOffered = true;
|
|
|
|
logger.info(
|
|
|
|
`setting denomination ${x.denomPubHash} to offered=true`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await tx.denominations.put(x);
|
|
|
|
});
|
|
|
|
|
2021-08-06 16:27:18 +02:00
|
|
|
logger.trace("done updating denominations in database");
|
2020-03-11 20:14:28 +01:00
|
|
|
|
|
|
|
// Handle recoup
|
2021-06-02 13:23:51 +02:00
|
|
|
const recoupDenomList = keysInfo.recoup;
|
2020-03-11 20:14:28 +01:00
|
|
|
const newlyRevokedCoinPubs: string[] = [];
|
2020-07-23 15:54:00 +02:00
|
|
|
logger.trace("recoup list from exchange", recoupDenomList);
|
2020-03-12 14:55:38 +01:00
|
|
|
for (const recoupInfo of recoupDenomList) {
|
2021-06-09 15:14:17 +02:00
|
|
|
const oldDenom = await tx.denominations.get([
|
2020-09-08 17:33:10 +02:00
|
|
|
r.baseUrl,
|
2020-03-12 14:55:38 +01:00
|
|
|
recoupInfo.h_denom_pub,
|
2020-09-08 17:33:10 +02:00
|
|
|
]);
|
2020-03-11 20:14:28 +01:00
|
|
|
if (!oldDenom) {
|
|
|
|
// We never even knew about the revoked denomination, all good.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (oldDenom.isRevoked) {
|
|
|
|
// We already marked the denomination as revoked,
|
|
|
|
// this implies we revoked all coins
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.trace("denom already revoked");
|
2020-03-11 20:14:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-11 12:48:32 +01:00
|
|
|
logger.info("revoking denom", recoupInfo.h_denom_pub);
|
2020-03-11 20:14:28 +01:00
|
|
|
oldDenom.isRevoked = true;
|
2021-06-09 15:14:17 +02:00
|
|
|
await tx.denominations.put(oldDenom);
|
|
|
|
const affectedCoins = await tx.coins.indexes.byDenomPubHash
|
|
|
|
.iter(recoupInfo.h_denom_pub)
|
2020-03-11 20:14:28 +01:00
|
|
|
.toArray();
|
|
|
|
for (const ac of affectedCoins) {
|
|
|
|
newlyRevokedCoinPubs.push(ac.coinPub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newlyRevokedCoinPubs.length != 0) {
|
2022-01-11 12:48:32 +01:00
|
|
|
logger.info("recouping coins", newlyRevokedCoinPubs);
|
2021-06-22 12:18:12 +02:00
|
|
|
recoupGroupId = await ws.recoupOps.createRecoupGroup(
|
|
|
|
ws,
|
|
|
|
tx,
|
2022-08-26 01:18:01 +02:00
|
|
|
exchange.baseUrl,
|
2021-06-22 12:18:12 +02:00
|
|
|
newlyRevokedCoinPubs,
|
|
|
|
);
|
2020-03-11 20:14:28 +01:00
|
|
|
}
|
2022-10-16 20:15:55 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
return {
|
|
|
|
exchange: r,
|
2022-10-15 16:03:48 +02:00
|
|
|
exchangeDetails: newDetails,
|
2021-06-02 13:23:51 +02:00
|
|
|
};
|
2021-06-09 15:14:17 +02:00
|
|
|
});
|
2020-03-11 20:14:28 +01:00
|
|
|
|
|
|
|
if (recoupGroupId) {
|
|
|
|
// Asynchronously start recoup. This doesn't need to finish
|
|
|
|
// for the exchange update to be considered finished.
|
2021-06-22 12:18:12 +02:00
|
|
|
ws.recoupOps.processRecoupGroup(ws, recoupGroupId).catch((e) => {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.error("error while recouping coins:", e);
|
2020-03-11 20:14:28 +01:00
|
|
|
});
|
|
|
|
}
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
if (!updated) {
|
|
|
|
throw Error("something went wrong with updating the exchange");
|
2019-12-16 12:53:22 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 16:27:18 +02:00
|
|
|
logger.trace("done updating exchange info in database");
|
|
|
|
|
2022-10-15 16:03:48 +02:00
|
|
|
if (isNewExchange) {
|
|
|
|
ws.notify({
|
|
|
|
type: NotificationType.ExchangeAdded,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
return {
|
2022-09-05 18:12:30 +02:00
|
|
|
type: OperationAttemptResultType.Finished,
|
|
|
|
result: {
|
|
|
|
exchange: updated.exchange,
|
|
|
|
exchangeDetails: updated.exchangeDetails,
|
|
|
|
},
|
2019-12-09 19:59:08 +01:00
|
|
|
};
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
2022-10-05 10:22:32 +02:00
|
|
|
/**
|
|
|
|
* Find a payto:// URI of the exchange that is of one
|
|
|
|
* of the given target types.
|
|
|
|
*
|
|
|
|
* Throws if no matching account was found.
|
|
|
|
*/
|
2019-12-02 00:42:40 +01:00
|
|
|
export async function getExchangePaytoUri(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
supportedTargetTypes: string[],
|
|
|
|
): Promise<string> {
|
|
|
|
// We do the update here, since the exchange might not even exist
|
|
|
|
// yet in our database.
|
2021-06-09 15:14:17 +02:00
|
|
|
const details = await getExchangeDetails
|
|
|
|
.makeContext(ws.db)
|
|
|
|
.runReadOnly(async (tx) => {
|
2021-06-02 13:23:51 +02:00
|
|
|
return getExchangeDetails(tx, exchangeBaseUrl);
|
2021-06-09 15:14:17 +02:00
|
|
|
});
|
2021-06-02 13:23:51 +02:00
|
|
|
const accounts = details?.wireInfo.accounts ?? [];
|
|
|
|
for (const account of accounts) {
|
2020-01-19 19:02:47 +01:00
|
|
|
const res = parsePaytoUri(account.payto_uri);
|
2019-12-02 00:42:40 +01:00
|
|
|
if (!res) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (supportedTargetTypes.includes(res.targetType)) {
|
2020-01-19 19:02:47 +01:00
|
|
|
return account.payto_uri;
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-05 10:22:32 +02:00
|
|
|
throw Error(
|
|
|
|
`no matching account found at exchange ${exchangeBaseUrl} for wire types ${j2s(
|
|
|
|
supportedTargetTypes,
|
|
|
|
)}`,
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2021-06-17 15:49:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if and how an exchange is trusted and/or audited.
|
|
|
|
*/
|
|
|
|
export async function getExchangeTrust(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeInfo: ExchangeRecord,
|
|
|
|
): Promise<TrustInfo> {
|
|
|
|
let isTrusted = false;
|
|
|
|
let isAudited = false;
|
|
|
|
|
|
|
|
return await ws.db
|
2022-09-13 13:25:41 +02:00
|
|
|
.mktx((x) => [
|
|
|
|
x.exchanges,
|
|
|
|
x.exchangeDetails,
|
|
|
|
x.exchangeTrust,
|
|
|
|
x.auditorTrust,
|
|
|
|
])
|
2021-06-17 15:49:05 +02:00
|
|
|
.runReadOnly(async (tx) => {
|
|
|
|
const exchangeDetails = await getExchangeDetails(
|
|
|
|
tx,
|
|
|
|
exchangeInfo.baseUrl,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!exchangeDetails) {
|
|
|
|
throw Error(`exchange ${exchangeInfo.baseUrl} details not available`);
|
|
|
|
}
|
2022-01-13 22:01:14 +01:00
|
|
|
const exchangeTrustRecord =
|
2022-09-13 13:25:41 +02:00
|
|
|
await tx.exchangeTrust.indexes.byExchangeMasterPub.get(
|
2022-01-13 22:01:14 +01:00
|
|
|
exchangeDetails.masterPublicKey,
|
|
|
|
);
|
2021-06-17 15:49:05 +02:00
|
|
|
if (
|
|
|
|
exchangeTrustRecord &&
|
|
|
|
exchangeTrustRecord.uids.length > 0 &&
|
|
|
|
exchangeTrustRecord.currency === exchangeDetails.currency
|
|
|
|
) {
|
|
|
|
isTrusted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auditor of exchangeDetails.auditors) {
|
2022-01-13 22:01:14 +01:00
|
|
|
const auditorTrustRecord =
|
|
|
|
await tx.auditorTrust.indexes.byAuditorPub.get(auditor.auditor_pub);
|
2021-06-17 15:49:05 +02:00
|
|
|
if (auditorTrustRecord && auditorTrustRecord.uids.length > 0) {
|
|
|
|
isAudited = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isTrusted, isAudited };
|
|
|
|
});
|
|
|
|
}
|