From 882d6b3710f82d85b2129f09c63f9db45985ef64 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 24 Jan 2022 14:12:12 -0300 Subject: last activity -> pending; fix downloadTos --- .../taler-wallet-core/src/operations/exchanges.ts | 84 ++++++++++++++-------- packages/taler-wallet-core/src/wallet.ts | 35 +++++++-- 2 files changed, 86 insertions(+), 33 deletions(-) (limited to 'packages/taler-wallet-core') diff --git a/packages/taler-wallet-core/src/operations/exchanges.ts b/packages/taler-wallet-core/src/operations/exchanges.ts index d73d593e8..87200c2f9 100644 --- a/packages/taler-wallet-core/src/operations/exchanges.ts +++ b/packages/taler-wallet-core/src/operations/exchanges.ts @@ -138,7 +138,7 @@ async function handleExchangeUpdateError( } } -function getExchangeRequestTimeout(e: ExchangeRecord): Duration { +export function getExchangeRequestTimeout(): Duration { return { d_ms: 5000 }; } @@ -199,6 +199,27 @@ getExchangeDetails.makeContext = (db: DbAccess) => exchangeDetails: x.exchangeDetails, })); +export async function updateExchangeTermsOfService( + ws: InternalWalletState, + exchangeBaseUrl: string, + tos: ExchangeTosDownloadResult, +): Promise { + await ws.db + .mktx((x) => ({ + exchanges: x.exchanges, + exchangeDetails: x.exchangeDetails, + })) + .runReadWrite(async (tx) => { + const d = await getExchangeDetails(tx, exchangeBaseUrl); + if (d) { + d.termsOfServiceText = tos.tosText; + d.termsOfServiceContentType = tos.tosContentType; + d.termsOfServiceLastEtag = tos.tosEtag; + await tx.exchangeDetails.put(d); + } + }); +} + export async function acceptExchangeTermsOfService( ws: InternalWalletState, exchangeBaseUrl: string, @@ -434,6 +455,36 @@ async function downloadKeysInfo( }; } +export async function downloadTosFromAcceptedFormat( + ws: InternalWalletState, + baseUrl: string, + timeout: Duration, + acceptedFormat?: string[]): Promise { + 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; + } + } + if (tosFound !== undefined) return tosFound + // If none of the specified format was found try text/plain + return await downloadExchangeWithTermsOfService( + baseUrl, + ws.http, + timeout, + "text/plain", + ); +} + /** * Update or add exchange DB entry by fetching the /keys and /wire information. * Optionally link the reserve entry to the new or existing @@ -479,7 +530,7 @@ async function updateExchangeFromUrlImpl( logger.info("updating exchange /keys info"); - const timeout = getExchangeRequestTimeout(r); + const timeout = getExchangeRequestTimeout(); const keysInfo = await downloadKeysInfo(baseUrl, ws.http, timeout); @@ -507,33 +558,10 @@ async function updateExchangeFromUrlImpl( logger.info("finished validating exchange /wire info"); - 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; - } - } - // If none of the specified format was found try text/plain - const tosDownload = - tosFound !== undefined - ? tosFound - : await downloadExchangeWithTermsOfService( - baseUrl, - ws.http, - timeout, - "text/plain", - ); - let recoupGroupId: string | undefined = undefined; + const tosDownload = await downloadTosFromAcceptedFormat(ws, baseUrl, timeout, acceptedFormat) + + let recoupGroupId: string | undefined; logger.trace("updating exchange info in database"); diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index 182453ff2..b53ba24c4 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -89,9 +89,12 @@ import { } from "./operations/deposits.js"; import { acceptExchangeTermsOfService, + downloadTosFromAcceptedFormat, getExchangeDetails, + getExchangeRequestTimeout, getExchangeTrust, - updateExchangeFromUrl + updateExchangeFromUrl, + updateExchangeTermsOfService } from "./operations/exchanges.js"; import { getMerchantInfo } from "./operations/merchants.js"; import { @@ -441,7 +444,6 @@ async function getExchangeTos( ws, exchangeBaseUrl, acceptedFormat, - true, ); const content = exchangeDetails.termsOfServiceText; const currentEtag = exchangeDetails.termsOfServiceLastEtag; @@ -453,12 +455,34 @@ async function getExchangeTos( ) { throw Error("exchange is in invalid state"); } + if (acceptedFormat && acceptedFormat.findIndex(f => f === contentType) !== -1) { + return { + acceptedEtag: exchangeDetails.termsOfServiceAcceptedEtag, + currentEtag, + content, + contentType, + }; + } + + const tosDownload = await downloadTosFromAcceptedFormat(ws, exchangeBaseUrl, getExchangeRequestTimeout(), acceptedFormat); + + if (tosDownload.tosContentType === contentType) { + return { + acceptedEtag: exchangeDetails.termsOfServiceAcceptedEtag, + currentEtag, + content, + contentType, + }; + } + await updateExchangeTermsOfService(ws, exchangeBaseUrl, tosDownload) + return { acceptedEtag: exchangeDetails.termsOfServiceAcceptedEtag, - currentEtag, - content, - contentType, + currentEtag: tosDownload.tosEtag, + content: tosDownload.tosText, + contentType: tosDownload.tosContentType, }; + } async function listKnownBankAccounts( @@ -1245,3 +1269,4 @@ class InternalWalletStateImpl implements InternalWalletState { } } } + -- cgit v1.2.3