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 {
|
2021-03-17 17:56:37 +01:00
|
|
|
Amounts,
|
2021-06-02 13:23:51 +02:00
|
|
|
Auditor,
|
2020-03-09 09:49:22 +01:00
|
|
|
codecForExchangeKeysJson,
|
|
|
|
codecForExchangeWireJson,
|
2021-03-17 17:56:37 +01:00
|
|
|
compare,
|
|
|
|
Denomination,
|
|
|
|
Duration,
|
|
|
|
durationFromSpec,
|
2021-06-02 13:23:51 +02:00
|
|
|
ExchangeSignKeyJson,
|
|
|
|
ExchangeWireJson,
|
2021-03-17 17:56:37 +01:00
|
|
|
getTimestampNow,
|
|
|
|
isTimestampExpired,
|
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,
|
|
|
|
TalerErrorDetails,
|
2021-06-02 13:23:51 +02:00
|
|
|
Timestamp,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "@gnu-taler/taler-util";
|
2019-12-02 00:42:40 +01:00
|
|
|
import {
|
|
|
|
DenominationRecord,
|
|
|
|
DenominationStatus,
|
2021-03-17 17:56:37 +01:00
|
|
|
Stores,
|
|
|
|
ExchangeRecord,
|
|
|
|
ExchangeUpdateStatus,
|
2019-12-02 00:42:40 +01:00
|
|
|
WireFee,
|
2019-12-16 12:53:22 +01:00
|
|
|
ExchangeUpdateReason,
|
2021-06-02 13:23:51 +02:00
|
|
|
ExchangeDetailsRecord,
|
|
|
|
WireInfo,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "../db.js";
|
|
|
|
import {
|
|
|
|
URL,
|
|
|
|
readSuccessResponseJsonOrThrow,
|
|
|
|
getExpiryTimestamp,
|
|
|
|
readSuccessResponseTextOrThrow,
|
2021-06-02 13:23:51 +02:00
|
|
|
encodeCrock,
|
|
|
|
hash,
|
|
|
|
decodeCrock,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "../index.js";
|
2021-04-07 19:29:51 +02:00
|
|
|
import { j2s, canonicalizeBaseUrl } from "@gnu-taler/taler-util";
|
2021-03-17 17:56:37 +01:00
|
|
|
import { updateRetryInfoTimeout, initRetryInfo } from "../util/retries.js";
|
2019-12-06 00:24:34 +01:00
|
|
|
import {
|
2021-03-17 17:56:37 +01:00
|
|
|
makeErrorDetails,
|
2019-12-06 00:24:34 +01:00
|
|
|
guardOperationException,
|
2021-06-02 13:23:51 +02:00
|
|
|
OperationFailedError,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "./errors.js";
|
|
|
|
import { createRecoupGroup, processRecoupGroup } from "./recoup.js";
|
|
|
|
import { InternalWalletState } from "./state.js";
|
2020-03-09 09:59:13 +01:00
|
|
|
import {
|
|
|
|
WALLET_CACHE_BREAKER_CLIENT_VERSION,
|
|
|
|
WALLET_EXCHANGE_PROTOCOL_VERSION,
|
2021-03-17 17:56:37 +01:00
|
|
|
} from "./versions.js";
|
2021-06-02 13:23:51 +02:00
|
|
|
import { HttpRequestLibrary } from "../util/http.js";
|
|
|
|
import { CryptoApi } from "../crypto/workers/cryptoApi.js";
|
|
|
|
import { TransactionHandle } from "../util/query.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,
|
|
|
|
denomIn: Denomination,
|
2021-06-02 13:23:51 +02:00
|
|
|
): DenominationRecord {
|
|
|
|
const denomPubHash = encodeCrock(hash(decodeCrock(denomIn.denom_pub)));
|
2019-12-02 00:42:40 +01:00
|
|
|
const d: DenominationRecord = {
|
|
|
|
denomPub: denomIn.denom_pub,
|
|
|
|
denomPubHash,
|
|
|
|
exchangeBaseUrl,
|
|
|
|
feeDeposit: Amounts.parseOrThrow(denomIn.fee_deposit),
|
|
|
|
feeRefresh: Amounts.parseOrThrow(denomIn.fee_refresh),
|
|
|
|
feeRefund: Amounts.parseOrThrow(denomIn.fee_refund),
|
|
|
|
feeWithdraw: Amounts.parseOrThrow(denomIn.fee_withdraw),
|
|
|
|
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,
|
2019-12-02 00:42:40 +01:00
|
|
|
status: DenominationStatus.Unverified,
|
|
|
|
value: Amounts.parseOrThrow(denomIn.value),
|
|
|
|
};
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2020-09-02 11:14:36 +02:00
|
|
|
async function handleExchangeUpdateError(
|
2019-12-02 00:42:40 +01:00
|
|
|
ws: InternalWalletState,
|
|
|
|
baseUrl: string,
|
2020-09-01 14:57:22 +02:00
|
|
|
err: TalerErrorDetails,
|
2019-12-02 00:42:40 +01:00
|
|
|
): Promise<void> {
|
2020-09-02 11:14:36 +02:00
|
|
|
await ws.db.runWithWriteTransaction([Stores.exchanges], async (tx) => {
|
|
|
|
const exchange = await tx.get(Stores.exchanges, baseUrl);
|
|
|
|
if (!exchange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
exchange.retryInfo.retryCounter++;
|
|
|
|
updateRetryInfoTimeout(exchange.retryInfo);
|
2019-12-02 00:42:40 +01:00
|
|
|
exchange.lastError = err;
|
2020-09-02 11:14:36 +02:00
|
|
|
});
|
|
|
|
if (err) {
|
|
|
|
ws.notify({ type: NotificationType.ExchangeOperationError, error: err });
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 12:57:20 +02:00
|
|
|
function getExchangeRequestTimeout(e: ExchangeRecord): Duration {
|
|
|
|
return { d_ms: 5000 };
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
interface ExchangeTosDownloadResult {
|
|
|
|
tosText: string;
|
|
|
|
tosEtag: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadExchangeWithTermsOfService(
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
timeout: Duration,
|
|
|
|
): Promise<ExchangeTosDownloadResult> {
|
|
|
|
const reqUrl = new URL("terms", exchangeBaseUrl);
|
|
|
|
reqUrl.searchParams.set("cacheBreaker", WALLET_CACHE_BREAKER_CLIENT_VERSION);
|
|
|
|
const headers = {
|
|
|
|
Accept: "text/plain",
|
|
|
|
};
|
|
|
|
|
|
|
|
const resp = await http.get(reqUrl.href, {
|
|
|
|
headers,
|
|
|
|
timeout,
|
|
|
|
});
|
|
|
|
const tosText = await readSuccessResponseTextOrThrow(resp);
|
|
|
|
const tosEtag = resp.headers.get("etag") || "unknown";
|
|
|
|
|
|
|
|
return { tosText, tosEtag };
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExchangeDetails(
|
|
|
|
tx: TransactionHandle<
|
|
|
|
typeof Stores.exchanges | typeof Stores.exchangeDetails
|
|
|
|
>,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
): Promise<ExchangeDetailsRecord | undefined> {
|
|
|
|
const r = await tx.get(Stores.exchanges, exchangeBaseUrl);
|
|
|
|
if (!r) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const dp = r.detailsPointer;
|
|
|
|
if (!dp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { currency, masterPublicKey } = dp;
|
|
|
|
return await tx.get(Stores.exchangeDetails, [
|
|
|
|
r.baseUrl,
|
|
|
|
currency,
|
|
|
|
masterPublicKey,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function acceptExchangeTermsOfService(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
etag: string | undefined,
|
|
|
|
): Promise<void> {
|
|
|
|
await ws.db.runWithWriteTransaction(
|
|
|
|
[Stores.exchanges, Stores.exchangeDetails],
|
|
|
|
async (tx) => {
|
|
|
|
const d = await getExchangeDetails(tx, exchangeBaseUrl);
|
|
|
|
if (d) {
|
|
|
|
d.termsOfServiceAcceptedEtag = etag;
|
|
|
|
await tx.put(Stores.exchangeDetails, d);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validateWireInfo(
|
|
|
|
wireInfo: ExchangeWireJson,
|
|
|
|
masterPublicKey: string,
|
|
|
|
cryptoApi: CryptoApi,
|
|
|
|
): Promise<WireInfo> {
|
|
|
|
for (const a of wireInfo.accounts) {
|
|
|
|
logger.trace("validating exchange acct");
|
|
|
|
const isValid = await cryptoApi.isValidWireAccount(
|
|
|
|
a.payto_uri,
|
|
|
|
a.master_sig,
|
|
|
|
masterPublicKey,
|
|
|
|
);
|
|
|
|
if (!isValid) {
|
|
|
|
throw Error("exchange acct signature invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const feesForType: { [wireMethod: string]: WireFee[] } = {};
|
|
|
|
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 = {
|
|
|
|
closingFee: Amounts.parseOrThrow(x.closing_fee),
|
|
|
|
endStamp,
|
|
|
|
sig: x.sig,
|
|
|
|
startStamp,
|
|
|
|
wireFee: Amounts.parseOrThrow(x.wire_fee),
|
|
|
|
};
|
|
|
|
const isValid = await cryptoApi.isValidWireFee(
|
|
|
|
wireMethod,
|
|
|
|
fee,
|
|
|
|
masterPublicKey,
|
|
|
|
);
|
|
|
|
if (!isValid) {
|
|
|
|
throw Error("exchange wire fee signature invalid");
|
|
|
|
}
|
|
|
|
feeList.push(fee);
|
|
|
|
}
|
|
|
|
feesForType[wireMethod] = feeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
accounts: wireInfo.accounts,
|
|
|
|
feesForType,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2021-06-02 13:23:51 +02:00
|
|
|
async function downloadExchangeWithWireInfo(
|
|
|
|
exchangeBaseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
timeout: Duration,
|
|
|
|
): Promise<ExchangeWireJson> {
|
|
|
|
const reqUrl = new URL("wire", exchangeBaseUrl);
|
|
|
|
reqUrl.searchParams.set("cacheBreaker", WALLET_CACHE_BREAKER_CLIENT_VERSION);
|
|
|
|
|
|
|
|
const resp = await http.get(reqUrl.href, {
|
|
|
|
timeout,
|
|
|
|
});
|
|
|
|
const wireInfo = await readSuccessResponseJsonOrThrow(
|
|
|
|
resp,
|
|
|
|
codecForExchangeWireJson(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return wireInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateExchangeFromUrl(
|
2019-12-02 00:42:40 +01:00
|
|
|
ws: InternalWalletState,
|
|
|
|
baseUrl: string,
|
2021-06-02 13:23:51 +02:00
|
|
|
forceNow = false,
|
|
|
|
): Promise<{
|
|
|
|
exchange: ExchangeRecord;
|
|
|
|
exchangeDetails: ExchangeDetailsRecord;
|
|
|
|
}> {
|
|
|
|
const onOpErr = (e: TalerErrorDetails): Promise<void> =>
|
|
|
|
handleExchangeUpdateError(ws, baseUrl, e);
|
|
|
|
return await guardOperationException(
|
|
|
|
() => updateExchangeFromUrlImpl(ws, baseUrl, forceNow),
|
|
|
|
onOpErr,
|
|
|
|
);
|
|
|
|
}
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
async function provideExchangeRecord(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
baseUrl: string,
|
|
|
|
now: Timestamp,
|
|
|
|
): Promise<ExchangeRecord> {
|
|
|
|
let r = await ws.db.get(Stores.exchanges, baseUrl);
|
|
|
|
if (!r) {
|
|
|
|
const newExchangeRecord: ExchangeRecord = {
|
|
|
|
permanent: true,
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
updateStatus: ExchangeUpdateStatus.FetchKeys,
|
|
|
|
updateStarted: now,
|
|
|
|
updateReason: ExchangeUpdateReason.Initial,
|
|
|
|
retryInfo: initRetryInfo(false),
|
|
|
|
detailsPointer: undefined,
|
|
|
|
};
|
|
|
|
await ws.db.put(Stores.exchanges, newExchangeRecord);
|
|
|
|
r = newExchangeRecord;
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
2021-06-02 13:23:51 +02:00
|
|
|
return r;
|
|
|
|
}
|
2020-07-22 10:52:03 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
interface ExchangeKeysDownloadResult {
|
|
|
|
masterPublicKey: string;
|
|
|
|
currency: string;
|
|
|
|
auditors: Auditor[];
|
|
|
|
currentDenominations: DenominationRecord[];
|
|
|
|
protocolVersion: string;
|
|
|
|
signingKeys: ExchangeSignKeyJson[];
|
|
|
|
reserveClosingDelay: Duration;
|
|
|
|
expiry: Timestamp;
|
|
|
|
recoup: Recoup[];
|
|
|
|
}
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
/**
|
|
|
|
* Download and validate an exchange's /keys data.
|
|
|
|
*/
|
|
|
|
async function downloadKeysInfo(
|
|
|
|
baseUrl: string,
|
|
|
|
http: HttpRequestLibrary,
|
|
|
|
timeout: Duration,
|
|
|
|
): Promise<ExchangeKeysDownloadResult> {
|
2019-12-02 00:42:40 +01:00
|
|
|
const keysUrl = new URL("keys", baseUrl);
|
|
|
|
keysUrl.searchParams.set("cacheBreaker", WALLET_CACHE_BREAKER_CLIENT_VERSION);
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const resp = await http.get(keysUrl.href, {
|
|
|
|
timeout,
|
2020-08-20 12:57:20 +02:00
|
|
|
});
|
2020-07-22 10:52:03 +02:00
|
|
|
const exchangeKeysJson = await readSuccessResponseJsonOrThrow(
|
|
|
|
resp,
|
|
|
|
codecForExchangeKeysJson(),
|
|
|
|
);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2020-08-05 21:00:36 +02:00
|
|
|
logger.info("received /keys response");
|
2021-01-14 18:00:00 +01:00
|
|
|
logger.trace(j2s(exchangeKeysJson));
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2019-12-02 00:42:40 +01:00
|
|
|
if (exchangeKeysJson.denoms.length === 0) {
|
2020-07-22 10:52:03 +02:00
|
|
|
const opErr = makeErrorDetails(
|
|
|
|
TalerErrorCode.WALLET_EXCHANGE_DENOMINATIONS_INSUFFICIENT,
|
|
|
|
"exchange doesn't offer any denominations",
|
|
|
|
{
|
|
|
|
exchangeBaseUrl: baseUrl,
|
|
|
|
},
|
|
|
|
);
|
2021-06-02 13:23:51 +02:00
|
|
|
throw new OperationFailedError(opErr);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const protocolVersion = exchangeKeysJson.version;
|
|
|
|
|
2020-03-09 09:59:13 +01:00
|
|
|
const versionRes = compare(WALLET_EXCHANGE_PROTOCOL_VERSION, protocolVersion);
|
|
|
|
if (versionRes?.compatible != true) {
|
2020-07-22 10:52:03 +02:00
|
|
|
const opErr = makeErrorDetails(
|
|
|
|
TalerErrorCode.WALLET_EXCHANGE_PROTOCOL_VERSION_INCOMPATIBLE,
|
|
|
|
"exchange protocol version not compatible with wallet",
|
|
|
|
{
|
2020-03-09 09:59:13 +01:00
|
|
|
exchangeProtocolVersion: protocolVersion,
|
|
|
|
walletProtocolVersion: WALLET_EXCHANGE_PROTOCOL_VERSION,
|
|
|
|
},
|
2020-07-22 10:52:03 +02:00
|
|
|
);
|
2021-06-02 13:23:51 +02:00
|
|
|
throw new OperationFailedError(opErr);
|
2020-03-09 09:59:13 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const currency = Amounts.parseOrThrow(
|
|
|
|
exchangeKeysJson.denoms[0].value,
|
|
|
|
).currency.toUpperCase();
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
return {
|
|
|
|
masterPublicKey: exchangeKeysJson.master_public_key,
|
|
|
|
currency,
|
|
|
|
auditors: exchangeKeysJson.auditors,
|
|
|
|
currentDenominations: exchangeKeysJson.denoms.map((d) =>
|
|
|
|
denominationRecordFromKeys(baseUrl, d),
|
2019-12-02 00:42:40 +01:00
|
|
|
),
|
2021-06-02 13:23:51 +02:00
|
|
|
protocolVersion: exchangeKeysJson.version,
|
|
|
|
signingKeys: exchangeKeysJson.signkeys,
|
|
|
|
reserveClosingDelay: exchangeKeysJson.reserve_closing_delay,
|
|
|
|
expiry: getExpiryTimestamp(resp, {
|
|
|
|
minDuration: durationFromSpec({ hours: 1 }),
|
|
|
|
}),
|
|
|
|
recoup: exchangeKeysJson.recoup ?? [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
async function updateExchangeFromUrlImpl(
|
|
|
|
ws: InternalWalletState,
|
|
|
|
baseUrl: string,
|
|
|
|
forceNow = false,
|
|
|
|
): Promise<{
|
|
|
|
exchange: ExchangeRecord;
|
|
|
|
exchangeDetails: ExchangeDetailsRecord;
|
|
|
|
}> {
|
|
|
|
logger.trace(`updating exchange info for ${baseUrl}`);
|
|
|
|
const now = getTimestampNow();
|
|
|
|
baseUrl = canonicalizeBaseUrl(baseUrl);
|
|
|
|
|
|
|
|
const r = await provideExchangeRecord(ws, baseUrl, now);
|
|
|
|
|
|
|
|
logger.info("updating exchange /keys info");
|
|
|
|
|
|
|
|
const timeout = getExchangeRequestTimeout(r);
|
|
|
|
|
|
|
|
const keysInfo = await downloadKeysInfo(baseUrl, ws.http, timeout);
|
|
|
|
|
|
|
|
const wireInfoDownload = await downloadExchangeWithWireInfo(
|
|
|
|
baseUrl,
|
|
|
|
ws.http,
|
|
|
|
timeout,
|
2019-12-02 00:42:40 +01:00
|
|
|
);
|
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const wireInfo = await validateWireInfo(
|
|
|
|
wireInfoDownload,
|
|
|
|
keysInfo.masterPublicKey,
|
|
|
|
ws.cryptoApi,
|
|
|
|
);
|
2020-08-05 21:00:36 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const tosDownload = await downloadExchangeWithTermsOfService(
|
|
|
|
baseUrl,
|
|
|
|
ws.http,
|
|
|
|
timeout,
|
|
|
|
);
|
2020-07-22 10:52:03 +02:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
let recoupGroupId: string | undefined = undefined;
|
2020-03-11 20:14:28 +01:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
const updated = await ws.db.runWithWriteTransaction(
|
|
|
|
[
|
|
|
|
Stores.exchanges,
|
|
|
|
Stores.exchangeDetails,
|
|
|
|
Stores.denominations,
|
|
|
|
Stores.recoupGroups,
|
|
|
|
Stores.coins,
|
|
|
|
],
|
2020-03-30 12:39:32 +02:00
|
|
|
async (tx) => {
|
2019-12-02 00:42:40 +01:00
|
|
|
const r = await tx.get(Stores.exchanges, baseUrl);
|
|
|
|
if (!r) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.warn(`exchange ${baseUrl} no longer present`);
|
2019-12-02 00:42:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-06-02 13:23:51 +02:00
|
|
|
let details = await getExchangeDetails(tx, r.baseUrl);
|
|
|
|
if (details) {
|
2019-12-02 00:42:40 +01:00
|
|
|
// FIXME: We need to do some consistency checks!
|
|
|
|
}
|
2020-03-13 14:34:16 +01:00
|
|
|
// FIXME: validate signing keys and merge with old set
|
2021-06-02 13:23:51 +02:00
|
|
|
details = {
|
|
|
|
auditors: keysInfo.auditors,
|
|
|
|
currency: keysInfo.currency,
|
|
|
|
lastUpdateTime: now,
|
|
|
|
masterPublicKey: keysInfo.masterPublicKey,
|
|
|
|
protocolVersion: keysInfo.protocolVersion,
|
|
|
|
signingKeys: keysInfo.signingKeys,
|
|
|
|
nextUpdateTime: keysInfo.expiry,
|
|
|
|
reserveClosingDelay: keysInfo.reserveClosingDelay,
|
|
|
|
exchangeBaseUrl: r.baseUrl,
|
|
|
|
wireInfo,
|
|
|
|
termsOfServiceText: tosDownload.tosText,
|
|
|
|
termsOfServiceAcceptedEtag: undefined,
|
|
|
|
termsOfServiceLastEtag: tosDownload.tosEtag,
|
2019-12-02 00:42:40 +01:00
|
|
|
};
|
2019-12-16 12:53:22 +01:00
|
|
|
r.updateStatus = ExchangeUpdateStatus.FetchWire;
|
2021-06-02 13:23:51 +02:00
|
|
|
// FIXME: only update if pointer got updated
|
2019-12-02 00:42:40 +01:00
|
|
|
r.lastError = undefined;
|
2020-09-02 11:14:36 +02:00
|
|
|
r.retryInfo = initRetryInfo(false);
|
2021-06-02 13:23:51 +02:00
|
|
|
// New denominations might be available.
|
|
|
|
r.nextRefreshCheck = undefined;
|
|
|
|
r.detailsPointer = {
|
|
|
|
currency: details.currency,
|
|
|
|
masterPublicKey: details.masterPublicKey,
|
|
|
|
// FIXME: only change if pointer really changed
|
|
|
|
updateClock: getTimestampNow(),
|
|
|
|
};
|
2019-12-02 00:42:40 +01:00
|
|
|
await tx.put(Stores.exchanges, r);
|
2021-06-02 13:23:51 +02:00
|
|
|
await tx.put(Stores.exchangeDetails, details);
|
2019-12-02 00:42:40 +01:00
|
|
|
|
2021-06-02 13:23:51 +02:00
|
|
|
for (const currentDenom of keysInfo.currentDenominations) {
|
2019-12-02 00:42:40 +01:00
|
|
|
const oldDenom = await tx.get(Stores.denominations, [
|
|
|
|
baseUrl,
|
2021-06-02 13:23:51 +02:00
|
|
|
currentDenom.denomPubHash,
|
2019-12-02 00:42:40 +01:00
|
|
|
]);
|
|
|
|
if (oldDenom) {
|
|
|
|
// FIXME: Do consistency check
|
|
|
|
} else {
|
2021-06-02 13:23:51 +02:00
|
|
|
await tx.put(Stores.denominations, currentDenom);
|
2019-12-02 00:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2020-09-08 17:33:10 +02:00
|
|
|
const oldDenom = await tx.get(Stores.denominations, [
|
|
|
|
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;
|
|
|
|
}
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.trace("revoking denom", recoupInfo.h_denom_pub);
|
2020-03-11 20:14:28 +01:00
|
|
|
oldDenom.isRevoked = true;
|
|
|
|
await tx.put(Stores.denominations, oldDenom);
|
|
|
|
const affectedCoins = await tx
|
2020-03-12 14:55:38 +01:00
|
|
|
.iterIndexed(Stores.coins.denomPubHashIndex, 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) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.trace("recouping coins", newlyRevokedCoinPubs);
|
2021-06-02 13:23:51 +02:00
|
|
|
recoupGroupId = await createRecoupGroup(ws, tx, newlyRevokedCoinPubs);
|
2020-03-11 20:14:28 +01:00
|
|
|
}
|
2021-06-02 13:23:51 +02:00
|
|
|
return {
|
|
|
|
exchange: r,
|
|
|
|
exchangeDetails: details,
|
|
|
|
};
|
2019-12-02 00:42:40 +01: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.
|
2020-03-30 12:39:32 +02:00
|
|
|
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-06-02 13:23:51 +02:00
|
|
|
return {
|
|
|
|
exchange: updated.exchange,
|
|
|
|
exchangeDetails: updated.exchangeDetails,
|
2019-12-09 19:59:08 +01:00
|
|
|
};
|
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-02 13:23:51 +02:00
|
|
|
const details = await ws.db.runWithReadTransaction(
|
|
|
|
[Stores.exchangeDetails, Stores.exchanges],
|
|
|
|
async (tx) => {
|
|
|
|
return getExchangeDetails(tx, exchangeBaseUrl);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
throw Error("no matching exchange account found");
|
|
|
|
}
|