diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-25 19:11:20 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-25 19:11:20 +0100 |
commit | adebfab94e76ee5d34a4f22d15fc085daef9ae00 (patch) | |
tree | 2dd0f233661fc32d2e5c2ee83750b3616d421359 /src/crypto/workers/cryptoImplementation.ts | |
parent | 54f7999c63292ca63f5f584c49bdef0b55627d71 (diff) | |
download | wallet-core-adebfab94e76ee5d34a4f22d15fc085daef9ae00.tar.xz |
fix and simplify coin selection
Diffstat (limited to 'src/crypto/workers/cryptoImplementation.ts')
-rw-r--r-- | src/crypto/workers/cryptoImplementation.ts | 103 |
1 files changed, 24 insertions, 79 deletions
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; } /** |