aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-12-25 19:11:20 +0100
committerFlorian Dold <florian.dold@gmail.com>2019-12-25 19:11:20 +0100
commitadebfab94e76ee5d34a4f22d15fc085daef9ae00 (patch)
tree2dd0f233661fc32d2e5c2ee83750b3616d421359 /src/crypto
parent54f7999c63292ca63f5f584c49bdef0b55627d71 (diff)
fix and simplify coin selection
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/workers/cryptoApi.ts23
-rw-r--r--src/crypto/workers/cryptoImplementation.ts103
2 files changed, 32 insertions, 94 deletions
diff --git a/src/crypto/workers/cryptoApi.ts b/src/crypto/workers/cryptoApi.ts
index 1c54d286a..489d56f5c 100644
--- a/src/crypto/workers/cryptoApi.ts
+++ b/src/crypto/workers/cryptoApi.ts
@@ -35,14 +35,13 @@ import {
import { CryptoWorker } from "./cryptoWorker";
-import { ContractTerms, PaybackRequest } from "../../types/talerTypes";
+import { ContractTerms, PaybackRequest, CoinDepositPermission } from "../../types/talerTypes";
import {
BenchmarkResult,
- CoinWithDenom,
- PaySigInfo,
PlanchetCreationResult,
PlanchetCreationRequest,
+ DepositInfo,
} from "../../types/walletTypes";
import * as timer from "../../util/timer";
@@ -384,19 +383,13 @@ export class CryptoApi {
);
}
- signDeposit(
- contractTermsRaw: string,
- contractData: WalletContractData,
- cds: CoinWithDenom[],
- totalAmount: AmountJson,
- ): Promise<PaySigInfo> {
- return this.doRpc<PaySigInfo>(
- "signDeposit",
+ signDepositPermission(
+ depositInfo: DepositInfo
+ ): Promise<CoinDepositPermission> {
+ return this.doRpc<CoinDepositPermission>(
+ "signDepositPermission",
3,
- contractTermsRaw,
- contractData,
- cds,
- totalAmount,
+ depositInfo
);
}
diff --git a/src/crypto/workers/cryptoImplementation.ts b/src/crypto/workers/cryptoImplementation.ts
index 043711864..d3295e749 100644
--- a/src/crypto/workers/cryptoImplementation.ts
+++ b/src/crypto/workers/cryptoImplementation.ts
@@ -36,14 +36,12 @@ import {
WalletContractData,
} from "../../types/dbTypes";
-import { CoinPaySig, ContractTerms, PaybackRequest } from "../../types/talerTypes";
+import { CoinDepositPermission, ContractTerms, PaybackRequest } from "../../types/talerTypes";
import {
BenchmarkResult,
- CoinWithDenom,
- PaySigInfo,
PlanchetCreationResult,
PlanchetCreationRequest,
- CoinPayInfo,
+ DepositInfo,
} from "../../types/walletTypes";
import { canonicalJson } from "../../util/helpers";
import { AmountJson } from "../../util/amounts";
@@ -331,82 +329,29 @@ export class CryptoImplementation {
* Generate updated coins (to store in the database)
* and deposit permissions for each given coin.
*/
- signDeposit(
- contractTermsRaw: string,
- contractData: WalletContractData,
- cds: CoinWithDenom[],
- totalAmount: AmountJson,
- ): PaySigInfo {
- const ret: PaySigInfo = {
- coinInfo: [],
- };
-
- const contractTermsHash = this.hashString(canonicalJson(JSON.parse(contractTermsRaw)));
-
- const feeList: AmountJson[] = cds.map(x => x.denom.feeDeposit);
- let fees = Amounts.add(Amounts.getZero(feeList[0].currency), ...feeList)
- .amount;
- // okay if saturates
- fees = Amounts.sub(fees, contractData.maxDepositFee).amount;
- const total = Amounts.add(fees, totalAmount).amount;
-
- let amountSpent = Amounts.getZero(cds[0].coin.currentAmount.currency);
- let amountRemaining = total;
-
- for (const cd of cds) {
- if (amountRemaining.value === 0 && amountRemaining.fraction === 0) {
- break;
- }
-
- let coinSpend: AmountJson;
- if (Amounts.cmp(amountRemaining, cd.coin.currentAmount) < 0) {
- coinSpend = amountRemaining;
- } else {
- coinSpend = cd.coin.currentAmount;
- }
-
- amountSpent = Amounts.add(amountSpent, coinSpend).amount;
-
- const feeDeposit = cd.denom.feeDeposit;
-
- // Give the merchant at least the deposit fee, otherwise it'll reject
- // the coin.
-
- if (Amounts.cmp(coinSpend, feeDeposit) < 0) {
- coinSpend = feeDeposit;
- }
+ signDepositPermission(depositInfo: DepositInfo): CoinDepositPermission {
+
+ const d = buildSigPS(SignaturePurpose.WALLET_COIN_DEPOSIT)
+ .put(decodeCrock(depositInfo.contractTermsHash))
+ .put(decodeCrock(depositInfo.wireInfoHash))
+ .put(timestampToBuffer(depositInfo.timestamp))
+ .put(timestampToBuffer(depositInfo.refundDeadline))
+ .put(amountToBuffer(depositInfo.spendAmount))
+ .put(amountToBuffer(depositInfo.feeDeposit))
+ .put(decodeCrock(depositInfo.merchantPub))
+ .put(decodeCrock(depositInfo.coinPub))
+ .build();
+ const coinSig = eddsaSign(d, decodeCrock(depositInfo.coinPriv));
- const newAmount = Amounts.sub(cd.coin.currentAmount, coinSpend).amount;
- cd.coin.currentAmount = newAmount;
-
- const d = buildSigPS(SignaturePurpose.WALLET_COIN_DEPOSIT)
- .put(decodeCrock(contractTermsHash))
- .put(decodeCrock(contractData.wireInfoHash))
- .put(timestampToBuffer(contractData.timestamp))
- .put(timestampToBuffer(contractData.refundDeadline))
- .put(amountToBuffer(coinSpend))
- .put(amountToBuffer(cd.denom.feeDeposit))
- .put(decodeCrock(contractData.merchantPub))
- .put(decodeCrock(cd.coin.coinPub))
- .build();
- const coinSig = eddsaSign(d, decodeCrock(cd.coin.coinPriv));
-
- const s: CoinPaySig = {
- coin_pub: cd.coin.coinPub,
- coin_sig: encodeCrock(coinSig),
- contribution: Amounts.toString(coinSpend),
- denom_pub: cd.coin.denomPub,
- exchange_url: cd.denom.exchangeBaseUrl,
- ub_sig: cd.coin.denomSig,
- };
- const coinInfo: CoinPayInfo = {
- sig: s,
- coinPub: cd.coin.coinPub,
- subtractedAmount: coinSpend,
- };
- ret.coinInfo.push(coinInfo);
- }
- return ret;
+ const s: CoinDepositPermission = {
+ coin_pub: depositInfo.coinPub,
+ coin_sig: encodeCrock(coinSig),
+ contribution: Amounts.toString(depositInfo.spendAmount),
+ denom_pub: depositInfo.denomPub,
+ exchange_url: depositInfo.exchangeBaseUrl,
+ ub_sig: depositInfo.denomSig,
+ };
+ return s;
}
/**