diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-01-23 16:19:03 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-01-23 16:19:03 +0100 |
commit | aec2c1301edd6c62b7665d4bfbf2087927f9419b (patch) | |
tree | 469fc290e5a47dd004564a9615b80a1394554dc6 | |
parent | b517163eceff692c998d0bb402bde34bef277b08 (diff) |
refactor submitPay
-rw-r--r-- | src/wallet.ts | 70 |
1 files 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<CoinRecord>(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<any> { - const doPaymentSucceeded = async() => { - const t = await this.q().get<PurchaseRecord>(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<CoinRecord>(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<void> { let coin = await this.q().get(Stores.coins, coinPub); if (!coin) { |