aboutsummaryrefslogtreecommitdiff
path: root/lib/wallet/wallet.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-05-24 01:53:56 +0200
committerFlorian Dold <florian.dold@gmail.com>2016-05-24 01:53:56 +0200
commit741dff270689ce8a16359e116f8e7dd02dd74c09 (patch)
treefa0c89fa4c23b1bd8d60625c4fa30522939cfaf6 /lib/wallet/wallet.ts
parent50fb0f2beb5e5a2ac6a08b694fbec9e4300fe734 (diff)
downloadwallet-core-741dff270689ce8a16359e116f8e7dd02dd74c09.tar.xz
check for re-payment (fix #4525)
Diffstat (limited to 'lib/wallet/wallet.ts')
-rw-r--r--lib/wallet/wallet.ts103
1 files changed, 68 insertions, 35 deletions
diff --git a/lib/wallet/wallet.ts b/lib/wallet/wallet.ts
index bae7873f1..d9b529b26 100644
--- a/lib/wallet/wallet.ts
+++ b/lib/wallet/wallet.ts
@@ -21,7 +21,14 @@
* @author Florian Dold
*/
-import {AmountJson, CreateReserveResponse, IExchangeInfo, Denomination, Notifier, WireInfo} from "./types";
+import {
+ AmountJson,
+ CreateReserveResponse,
+ IExchangeInfo,
+ Denomination,
+ Notifier,
+ WireInfo
+} from "./types";
import {HttpResponse, RequestException} from "./http";
import {Query} from "./query";
import {Checkable} from "./checkable";
@@ -139,7 +146,10 @@ class ExchangeInfo implements IExchangeInfo {
.isValidDenom(newDenom, this.masterPublicKey)
.then((valid) => {
if (!valid) {
- console.error("invalid denomination", newDenom, "with key", this.masterPublicKey);
+ console.error("invalid denomination",
+ newDenom,
+ "with key",
+ this.masterPublicKey);
throw Error("signature on denomination invalid");
}
return cryptoApi.hashRsaPub(newDenom.denom_pub);
@@ -422,10 +432,12 @@ export class Wallet {
}
handledExchanges.add(info.url);
console.log("Checking for merchant's exchange", JSON.stringify(info));
- return [Query(this.db)
- .iter("exchanges", {indexName: "pubKey", only: info.master_pub})
- .indexJoin("coins", "exchangeBaseUrl", (exchange) => exchange.baseUrl)
- .reduce((x) => storeExchangeCoin(x, info.url))];
+ return [
+ Query(this.db)
+ .iter("exchanges", {indexName: "pubKey", only: info.master_pub})
+ .indexJoin("coins", "exchangeBaseUrl", (exchange) => exchange.baseUrl)
+ .reduce((x) => storeExchangeCoin(x, info.url))
+ ];
});
return Promise.all(ps).then(() => {
@@ -536,23 +548,32 @@ export class Wallet {
*/
confirmPay(offer: Offer): Promise<any> {
console.log("executing confirmPay");
- return Promise.resolve().then(() => {
- return this.getPossibleExchangeCoins(offer.contract.amount,
- offer.contract.max_fee,
- offer.contract.exchanges)
- }).then((mcs) => {
- if (Object.keys(mcs).length == 0) {
- console.log("not confirming payment, insufficient coins");
- return {
- error: "coins-insufficient",
- };
- }
- let exchangeUrl = Object.keys(mcs)[0];
- return this.cryptoApi.signDeposit(offer, mcs[exchangeUrl])
- .then((ds) => this.recordConfirmPay(offer, ds, exchangeUrl))
- .then(() => ({}));
- });
+ return Query(this.db)
+ .get("transactions", offer.H_contract)
+ .then((transaction) => {
+ if (transaction) {
+ // Already payed ...
+ return {};
+ }
+ return Promise.resolve().then(() => {
+ return this.getPossibleExchangeCoins(offer.contract.amount,
+ offer.contract.max_fee,
+ offer.contract.exchanges)
+ }).then((mcs) => {
+ if (Object.keys(mcs).length == 0) {
+ console.log("not confirming payment, insufficient coins");
+ return {
+ error: "coins-insufficient",
+ };
+ }
+ let exchangeUrl = Object.keys(mcs)[0];
+
+ return this.cryptoApi.signDeposit(offer, mcs[exchangeUrl])
+ .then((ds) => this.recordConfirmPay(offer, ds, exchangeUrl))
+ .then(() => ({}));
+ });
+ });
}
@@ -562,19 +583,31 @@ export class Wallet {
*/
checkPay(offer: Offer): Promise<any> {
console.log("executing checkPay");
- return Promise.resolve().then(() => {
- return this.getPossibleExchangeCoins(offer.contract.amount,
- offer.contract.max_fee,
- offer.contract.exchanges)
- }).then((mcs) => {
- if (Object.keys(mcs).length == 0) {
- console.log("not confirming payment, insufficient coins");
- return {
- error: "coins-insufficient",
- };
- }
- return {};
- });
+
+ // First check if we already payed for it.
+ return Query(this.db)
+ .get("transactions", offer.H_contract)
+ .then((transaction) => {
+ if (transaction) {
+ return {isPayed: true};
+ }
+
+ // If not already payed, check if we could pay for it.
+ return Promise.resolve().then(() => {
+ return this.getPossibleExchangeCoins(offer.contract.amount,
+ offer.contract.max_fee,
+ offer.contract.exchanges)
+ }).then((mcs) => {
+ if (Object.keys(mcs).length == 0) {
+ console.log("not confirming payment, insufficient coins");
+ return {
+ error: "coins-insufficient",
+ };
+ }
+ return {isPayed: false};
+ });
+
+ });
}