From aec2c1301edd6c62b7665d4bfbf2087927f9419b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 23 Jan 2018 16:19:03 +0100 Subject: refactor submitPay --- src/wallet.ts | 70 +++++++++++++++++++++++++---------------------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/src/wallet.ts b/src/wallet.ts index 01db8c612..8167556f8 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -745,7 +745,36 @@ export class Wallet { fu.addSearch("session_sig", merchantResp.session_sig); await this.q().put(Stores.purchases, purchase).finish(); } - await this.paymentSucceeded(purchase.contractTermsHash, merchantResp.sig); + + const merchantPub = purchase.contractTerms.merchant_pub; + const valid: boolean = await ( + this.cryptoApi.isValidPaymentSignature(merchantResp.sig, contractTermsHash, merchantPub) + ); + if (!valid) { + console.error("merchant payment signature invalid"); + // FIXME: properly display error + throw Error("merchant payment signature invalid"); + } + purchase.finished = true; + const modifiedCoins: CoinRecord[] = []; + for (const pc of purchase.payReq.coins) { + const c = await this.q().get(Stores.coins, pc.coin_pub); + if (!c) { + console.error("coin not found"); + throw Error("coin used in payment not found"); + } + c.status = CoinStatus.Dirty; + modifiedCoins.push(c); + } + + await this.q() + .putAll(Stores.coins, modifiedCoins) + .put(Stores.purchases, purchase) + .finish(); + for (const c of purchase.payReq.coins) { + this.refresh(c.coin_pub); + } + const nextUrl = fu.href(); this.cachedNextUrl[purchase.contractTerms.fulfillment_url] = { nextUrl, lastSessionId: sessionId }; return { nextUrl }; @@ -2245,45 +2274,6 @@ export class Wallet { } - private async paymentSucceeded(contractTermsHash: string, merchantSig: string): Promise { - const doPaymentSucceeded = async() => { - const t = await this.q().get(Stores.purchases, - contractTermsHash); - if (!t) { - console.error("contract not found"); - return; - } - const merchantPub = t.contractTerms.merchant_pub; - const valid = this.cryptoApi.isValidPaymentSignature(merchantSig, contractTermsHash, merchantPub); - if (!valid) { - console.error("merchant payment signature invalid"); - // FIXME: properly display error - return; - } - t.finished = true; - const modifiedCoins: CoinRecord[] = []; - for (const pc of t.payReq.coins) { - const c = await this.q().get(Stores.coins, pc.coin_pub); - if (!c) { - console.error("coin not found"); - return; - } - c.status = CoinStatus.Dirty; - modifiedCoins.push(c); - } - - await this.q() - .putAll(Stores.coins, modifiedCoins) - .put(Stores.purchases, t) - .finish(); - for (const c of t.payReq.coins) { - this.refresh(c.coin_pub); - } - }; - doPaymentSucceeded(); - return; - } - async payback(coinPub: string): Promise { let coin = await this.q().get(Stores.coins, coinPub); if (!coin) { -- cgit v1.2.3