diff options
Diffstat (limited to 'src/wallet-impl')
-rw-r--r-- | src/wallet-impl/exchanges.ts | 14 | ||||
-rw-r--r-- | src/wallet-impl/pay.ts | 22 | ||||
-rw-r--r-- | src/wallet-impl/payback.ts | 2 | ||||
-rw-r--r-- | src/wallet-impl/pending.ts | 2 | ||||
-rw-r--r-- | src/wallet-impl/refresh.ts | 13 | ||||
-rw-r--r-- | src/wallet-impl/reserves.ts | 29 | ||||
-rw-r--r-- | src/wallet-impl/return.ts | 2 | ||||
-rw-r--r-- | src/wallet-impl/tip.ts | 15 | ||||
-rw-r--r-- | src/wallet-impl/withdraw.ts | 13 |
9 files changed, 76 insertions, 36 deletions
diff --git a/src/wallet-impl/exchanges.ts b/src/wallet-impl/exchanges.ts index 42d626a71..9810b9b91 100644 --- a/src/wallet-impl/exchanges.ts +++ b/src/wallet-impl/exchanges.ts @@ -112,7 +112,11 @@ async function updateExchangeWithKeys( let keysResp; try { - keysResp = await ws.http.get(keysUrl.href); + const r = await ws.http.get(keysUrl.href); + if (r.status !== 200) { + throw Error(`unexpected status for keys: ${r.status}`); + } + keysResp = await r.json(); } catch (e) { const m = `Fetching keys failed: ${e.message}`; await setExchangeError(ws, baseUrl, { @@ -126,7 +130,7 @@ async function updateExchangeWithKeys( } let exchangeKeysJson: KeysJson; try { - exchangeKeysJson = KeysJson.checked(keysResp.responseJson); + exchangeKeysJson = KeysJson.checked(keysResp); } catch (e) { const m = `Parsing /keys response failed: ${e.message}`; await setExchangeError(ws, baseUrl, { @@ -242,8 +246,10 @@ async function updateExchangeWithWireInfo( reqUrl.searchParams.set("cacheBreaker", WALLET_CACHE_BREAKER_CLIENT_VERSION); const resp = await ws.http.get(reqUrl.href); - - const wiJson = resp.responseJson; + if (resp.status !== 200) { + throw Error(`/wire response has unexpected status code (${resp.status})`); + } + const wiJson = await resp.json(); if (!wiJson) { throw Error("/wire response malformed"); } diff --git a/src/wallet-impl/pay.ts b/src/wallet-impl/pay.ts index d100ad26c..89b124553 100644 --- a/src/wallet-impl/pay.ts +++ b/src/wallet-impl/pay.ts @@ -441,7 +441,11 @@ export async function abortFailedPayment( throw e; } - const refundResponse = MerchantRefundResponse.checked(resp.responseJson); + if (resp.status !== 200) { + throw Error(`unexpected status for /pay (${resp.status})`); + } + + const refundResponse = MerchantRefundResponse.checked(await resp.json()); await acceptRefundResponse(ws, purchase.proposalId, refundResponse); await runWithWriteTransaction(ws.db, [Stores.purchases], async tx => { @@ -597,7 +601,11 @@ async function processDownloadProposalImpl( throw e; } - const proposalResp = Proposal.checked(resp.responseJson); + if (resp.status !== 200) { + throw Error(`contract download failed with status ${resp.status}`); + } + + const proposalResp = Proposal.checked(await resp.json()); const contractTermsHash = await ws.cryptoApi.hashString( canonicalJson(proposalResp.contract_terms), @@ -717,7 +725,10 @@ export async function submitPay( console.log("payment failed", e); throw e; } - const merchantResp = resp.responseJson; + if (resp.status !== 200) { + throw Error(`unexpected status (${resp.status}) for /pay`); + } + const merchantResp = await resp.json(); console.log("got success from pay URL"); const merchantPub = purchase.contractTerms.merchant_pub; @@ -1317,8 +1328,11 @@ async function processPurchaseQueryRefundImpl( console.error("error downloading refund permission", e); throw e; } + if (resp.status !== 200) { + throw Error(`unexpected status code (${resp.status}) for /refund`); + } - const refundResponse = MerchantRefundResponse.checked(resp.responseJson); + const refundResponse = MerchantRefundResponse.checked(await resp.json()); await acceptRefundResponse(ws, proposalId, refundResponse); } diff --git a/src/wallet-impl/payback.ts b/src/wallet-impl/payback.ts index 56696d771..8cdfbf7ed 100644 --- a/src/wallet-impl/payback.ts +++ b/src/wallet-impl/payback.ts @@ -76,7 +76,7 @@ export async function payback( if (resp.status !== 200) { throw Error(); } - const paybackConfirmation = PaybackConfirmation.checked(resp.responseJson); + const paybackConfirmation = PaybackConfirmation.checked(await resp.json()); if (paybackConfirmation.reserve_pub !== coin.reservePub) { throw Error(`Coin's reserve doesn't match reserve on payback`); } diff --git a/src/wallet-impl/pending.ts b/src/wallet-impl/pending.ts index 729dcf125..022895e95 100644 --- a/src/wallet-impl/pending.ts +++ b/src/wallet-impl/pending.ts @@ -238,7 +238,7 @@ async function gatherCoinsPending( // Refreshing dirty coins is always due. await tx.iter(Stores.coins).forEach(coin => { if (coin.status == CoinStatus.Dirty) { - resp.nextRetryDelay.d_ms = 0; + resp.nextRetryDelay = { d_ms: 0 }; resp.pendingOperations.push({ givesLifeness: true, type: "dirty-coin", diff --git a/src/wallet-impl/refresh.ts b/src/wallet-impl/refresh.ts index a23f34324..a33511c34 100644 --- a/src/wallet-impl/refresh.ts +++ b/src/wallet-impl/refresh.ts @@ -118,16 +118,19 @@ async function refreshMelt( }; logger.trace("melt request:", meltReq); const resp = await ws.http.postJson(reqUrl.href, meltReq); + if (resp.status !== 200) { + throw Error(`unexpected status code ${resp.status} for refresh/melt`); + } - logger.trace("melt response:", resp.responseJson); + const respJson = await resp.json(); + + logger.trace("melt response:", respJson); if (resp.status !== 200) { - console.error(resp.responseJson); + console.error(respJson); throw Error("refresh failed"); } - const respJson = resp.responseJson; - const norevealIndex = respJson.noreveal_index; if (typeof norevealIndex !== "number") { @@ -228,7 +231,7 @@ async function refreshReveal( return; } - const respJson = resp.responseJson; + const respJson = await resp.json(); if (!respJson.ev_sigs || !Array.isArray(respJson.ev_sigs)) { console.error("/refresh/reveal did not contain ev_sigs"); diff --git a/src/wallet-impl/reserves.ts b/src/wallet-impl/reserves.ts index d6568bd30..504cf10f0 100644 --- a/src/wallet-impl/reserves.ts +++ b/src/wallet-impl/reserves.ts @@ -282,7 +282,10 @@ async function processReserveBankStatusImpl( let status: WithdrawOperationStatusResponse; try { const statusResp = await ws.http.get(bankStatusUrl); - status = WithdrawOperationStatusResponse.checked(statusResp.responseJson); + if (statusResp.status !== 200) { + throw Error(`unexpected status ${statusResp.status} for bank status query`); + } + status = WithdrawOperationStatusResponse.checked(await statusResp.json()); } catch (e) { throw e; } @@ -378,22 +381,24 @@ async function updateReserve( let resp; try { resp = await ws.http.get(reqUrl.href); - } catch (e) { - if (e.response?.status === 404) { + if (resp.status === 404) { const m = "The exchange does not know about this reserve (yet)."; await incrementReserveRetry(ws, reservePub, undefined); return; - } else { - const m = e.message; - await incrementReserveRetry(ws, reservePub, { - type: "network", - details: {}, - message: m, - }); - throw new OperationFailedAndReportedError(m); } + if (resp.status !== 200) { + throw Error(`unexpected status code ${resp.status} for reserve/status`) + } + } catch (e) { + const m = e.message; + await incrementReserveRetry(ws, reservePub, { + type: "network", + details: {}, + message: m, + }); + throw new OperationFailedAndReportedError(m); } - const reserveInfo = ReserveStatus.checked(resp.responseJson); + const reserveInfo = ReserveStatus.checked(await resp.json()); const balance = Amounts.parseOrThrow(reserveInfo.balance); await oneShotMutate(ws.db, Stores.reserves, reserve.reservePub, r => { if (r.reserveStatus !== ReserveRecordStatus.QUERYING_STATUS) { diff --git a/src/wallet-impl/return.ts b/src/wallet-impl/return.ts index ec19c00ae..0c142f9a6 100644 --- a/src/wallet-impl/return.ts +++ b/src/wallet-impl/return.ts @@ -238,7 +238,7 @@ async function depositReturnedCoins( console.error("deposit failed due to status code", resp); continue; } - const respJson = resp.responseJson; + const respJson = await resp.json(); if (respJson.status !== "DEPOSIT_OK") { console.error("deposit failed", resp); continue; diff --git a/src/wallet-impl/tip.ts b/src/wallet-impl/tip.ts index e11eb3b42..41463ab18 100644 --- a/src/wallet-impl/tip.ts +++ b/src/wallet-impl/tip.ts @@ -41,10 +41,12 @@ export async function getTipStatus( tipStatusUrl.searchParams.set("tip_id", res.merchantTipId); console.log("checking tip status from", tipStatusUrl.href); const merchantResp = await ws.http.get(tipStatusUrl.href); - console.log("resp:", merchantResp.responseJson); - const tipPickupStatus = TipPickupGetResponse.checked( - merchantResp.responseJson, - ); + if (merchantResp.status !== 200) { + throw Error(`unexpected status ${merchantResp.status} for tip-pickup`); + } + const respJson = await merchantResp.json(); + console.log("resp:", respJson); + const tipPickupStatus = TipPickupGetResponse.checked(respJson); console.log("status", tipPickupStatus); @@ -208,13 +210,16 @@ async function processTipImpl( try { const req = { planchets: planchetsDetail, tip_id: tipRecord.merchantTipId }; merchantResp = await ws.http.postJson(tipStatusUrl.href, req); + if (merchantResp.status !== 200) { + throw Error(`unexpected status ${merchantResp.status} for tip-pickup`); + } console.log("got merchant resp:", merchantResp); } catch (e) { console.log("tipping failed", e); throw e; } - const response = TipResponse.checked(merchantResp.responseJson); + const response = TipResponse.checked(await merchantResp.json()); if (response.reserve_sigs.length !== tipRecord.planchets.length) { throw Error("number of tip responses does not match requested planchets"); diff --git a/src/wallet-impl/withdraw.ts b/src/wallet-impl/withdraw.ts index 96055c9c5..cd3989972 100644 --- a/src/wallet-impl/withdraw.ts +++ b/src/wallet-impl/withdraw.ts @@ -117,8 +117,12 @@ export async function getWithdrawalInfo( throw Error("can't parse URL"); } const resp = await ws.http.get(uriResult.statusUrl); - console.log("resp:", resp.responseJson); - const status = WithdrawOperationStatusResponse.checked(resp.responseJson); + if (resp.status !== 200) { + throw Error(`unexpected status (${resp.status}) from bank for ${uriResult.statusUrl}`); + } + const respJson = await resp.json(); + console.log("resp:", respJson); + const status = WithdrawOperationStatusResponse.checked(respJson); return { amount: Amounts.parseOrThrow(status.amount), confirmTransferUrl: status.confirm_transfer_url, @@ -228,8 +232,11 @@ async function processPlanchet( wd.coin_ev = planchet.coinEv; const reqUrl = new URL("reserve/withdraw", exchange.baseUrl).href; const resp = await ws.http.postJson(reqUrl, wd); + if (resp.status !== 200) { + throw Error(`unexpected status ${resp.status} for withdraw`); + } - const r = resp.responseJson; + const r = await resp.json(); const denomSig = await ws.cryptoApi.rsaUnblind( r.ev_sig, |