summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/operations
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2020-12-15 17:12:22 +0100
committerFlorian Dold <florian@dold.me>2020-12-15 17:12:22 +0100
commitc09c5bbe625566fc61c811160d2ccdab263327fa (patch)
tree4ce80e615bad8cf325b1b31aac957c6ce1ee0f05 /packages/taler-wallet-core/src/operations
parentf332d61fb68fbc394f31337ddeb7d1fc114772d0 (diff)
downloadwallet-core-c09c5bbe625566fc61c811160d2ccdab263327fa.tar.gz
wallet-core-c09c5bbe625566fc61c811160d2ccdab263327fa.tar.bz2
wallet-core-c09c5bbe625566fc61c811160d2ccdab263327fa.zip
derive tipping planchets from seed, implement backup further
Diffstat (limited to 'packages/taler-wallet-core/src/operations')
-rw-r--r--packages/taler-wallet-core/src/operations/backup.ts240
-rw-r--r--packages/taler-wallet-core/src/operations/refresh.ts1
-rw-r--r--packages/taler-wallet-core/src/operations/refund.ts6
-rw-r--r--packages/taler-wallet-core/src/operations/tip.ts71
-rw-r--r--packages/taler-wallet-core/src/operations/transactions.ts1
5 files changed, 262 insertions, 57 deletions
diff --git a/packages/taler-wallet-core/src/operations/backup.ts b/packages/taler-wallet-core/src/operations/backup.ts
index f609d4354..7ab908c46 100644
--- a/packages/taler-wallet-core/src/operations/backup.ts
+++ b/packages/taler-wallet-core/src/operations/backup.ts
@@ -26,20 +26,34 @@
*/
import { InternalWalletState } from "./state";
import {
+ BackupBackupProvider,
BackupCoin,
BackupCoinSource,
BackupCoinSourceType,
BackupDenomination,
BackupExchange,
BackupExchangeWireFee,
+ BackupProposal,
+ BackupProposalStatus,
+ BackupPurchase,
+ BackupRecoupGroup,
+ BackupRefreshGroup,
+ BackupRefreshOldCoin,
+ BackupRefreshSession,
+ BackupRefundItem,
+ BackupRefundState,
BackupReserve,
+ BackupTip,
WalletBackupContentV1,
} from "../types/backupTypes";
import { TransactionHandle } from "../util/query";
import {
+ AbortStatus,
CoinSourceType,
CoinStatus,
ConfigRecord,
+ ProposalStatus,
+ RefundState,
Stores,
} from "../types/dbTypes";
import { checkDbInvariant } from "../util/invariants";
@@ -56,7 +70,7 @@ import {
import { canonicalizeBaseUrl, canonicalJson, j2s } from "../util/helpers";
import { getTimestampNow, Timestamp } from "../util/time";
import { URL } from "../util/url";
-import { AmountString } from "../types/talerTypes";
+import { AmountString, TipResponse } from "../types/talerTypes";
import {
buildCodecForObject,
Codec,
@@ -146,16 +160,80 @@ export async function exportBackup(
): Promise<WalletBackupContentV1> {
await provideBackupState(ws);
return ws.db.runWithWriteTransaction(
- [Stores.config, Stores.exchanges, Stores.coins, Stores.denominations],
+ [
+ Stores.config,
+ Stores.exchanges,
+ Stores.coins,
+ Stores.denominations,
+ Stores.purchases,
+ Stores.proposals,
+ Stores.refreshGroups,
+ Stores.backupProviders,
+ Stores.tips,
+ Stores.recoupGroups,
+ Stores.reserves,
+ ],
async (tx) => {
const bs = await getWalletBackupState(ws, tx);
- const exchanges: BackupExchange[] = [];
- const coinsByDenom: { [dph: string]: BackupCoin[] } = {};
- const denominationsByExchange: {
+ const backupExchanges: BackupExchange[] = [];
+ const backupCoinsByDenom: { [dph: string]: BackupCoin[] } = {};
+ const backupDenominationsByExchange: {
[url: string]: BackupDenomination[];
} = {};
- const reservesByExchange: { [url: string]: BackupReserve[] } = {};
+ const backupReservesByExchange: { [url: string]: BackupReserve[] } = {};
+ const backupPurchases: BackupPurchase[] = [];
+ const backupProposals: BackupProposal[] = [];
+ const backupRefreshGroups: BackupRefreshGroup[] = [];
+ const backupBackupProviders: BackupBackupProvider[] = [];
+ const backupTips: BackupTip[] = [];
+ const backupRecoupGroups: BackupRecoupGroup[] = [];
+
+ await tx.iter(Stores.reserves).forEach((reserve) => {
+ // FIXME: implement
+ });
+
+ await tx.iter(Stores.tips).forEach((tip) => {
+ backupTips.push({
+ exchange_base_url: tip.exchangeBaseUrl,
+ merchant_base_url: tip.merchantBaseUrl,
+ merchant_tip_id: tip.merchantTipId,
+ wallet_tip_id: tip.walletTipId,
+ secret_seed: tip.secretSeed,
+ selected_denoms: tip.denomsSel.selectedDenoms.map((x) => ({
+ count: x.count,
+ denom_pub_hash: x.denomPubHash,
+ })),
+ timestam_picked_up: tip.pickedUpTimestamp,
+ timestamp_accepted: tip.acceptedTimestamp,
+ timestamp_created: tip.createdTimestamp,
+ timestamp_expiration: tip.tipExpiration,
+ tip_amount_raw: Amounts.stringify(tip.tipAmountRaw),
+ });
+ });
+
+ await tx.iter(Stores.recoupGroups).forEach((recoupGroup) => {
+ backupRecoupGroups.push({
+ recoup_group_id: recoupGroup.recoupGroupId,
+ timestamp_started: recoupGroup.timestampStarted,
+ timestamp_finished: recoupGroup.timestampFinished,
+ coins: recoupGroup.coinPubs.map((x, i) => ({
+ coin_pub: x,
+ recoup_finished: recoupGroup.recoupFinishedPerCoin[i],
+ old_amount: Amounts.stringify(recoupGroup.oldAmountPerCoin[i]),
+ })),
+ });
+ });
+
+ await tx.iter(Stores.backupProviders).forEach((bp) => {
+ backupBackupProviders.push({
+ annual_fee: Amounts.stringify(bp.annualFee),
+ base_url: canonicalizeBaseUrl(bp.baseUrl),
+ pay_proposal_ids: [],
+ storage_limit_in_megabytes: bp.storageLimitInMegabytes,
+ supported_protocol_version: bp.supportedProtocolVersion,
+ });
+ });
await tx.iter(Stores.coins).forEach((coin) => {
let bcs: BackupCoinSource;
@@ -183,7 +261,7 @@ export async function exportBackup(
break;
}
- const coins = (coinsByDenom[coin.denomPubHash] ??= []);
+ const coins = (backupCoinsByDenom[coin.denomPubHash] ??= []);
coins.push({
blinding_key: coin.blindingKey,
coin_priv: coin.coinPriv,
@@ -195,11 +273,11 @@ export async function exportBackup(
});
await tx.iter(Stores.denominations).forEach((denom) => {
- const backupDenoms = (denominationsByExchange[
+ const backupDenoms = (backupDenominationsByExchange[
denom.exchangeBaseUrl
] ??= []);
backupDenoms.push({
- coins: coinsByDenom[denom.denomPubHash] ?? [],
+ coins: backupCoinsByDenom[denom.denomPubHash] ?? [],
denom_pub: denom.denomPub,
fee_deposit: Amounts.stringify(denom.feeDeposit),
fee_refresh: Amounts.stringify(denom.feeRefresh),
@@ -247,7 +325,7 @@ export async function exportBackup(
}
});
- exchanges.push({
+ backupExchanges.push({
base_url: ex.baseUrl,
accounts: ex.wireInfo.accounts.map((x) => ({
payto_uri: x.payto_uri,
@@ -271,8 +349,132 @@ export async function exportBackup(
})),
tos_etag_accepted: ex.termsOfServiceAcceptedEtag,
tos_etag_last: ex.termsOfServiceLastEtag,
- denominations: denominationsByExchange[ex.baseUrl] ?? [],
- reserves: reservesByExchange[ex.baseUrl] ?? [],
+ denominations: backupDenominationsByExchange[ex.baseUrl] ?? [],
+ reserves: backupReservesByExchange[ex.baseUrl] ?? [],
+ });
+ });
+
+ const purchaseProposalIdSet = new Set<string>();
+
+ await tx.iter(Stores.purchases).forEach((purch) => {
+ const refunds: BackupRefundItem[] = [];
+ purchaseProposalIdSet.add(purch.proposalId);
+ for (const refundKey of Object.keys(purch.refunds)) {
+ const ri = purch.refunds[refundKey];
+ const common = {
+ coin_pub: ri.coinPub,
+ execution_time: ri.executionTime,
+ obtained_time: ri.obtainedTime,
+ refund_amount: Amounts.stringify(ri.refundAmount),
+ rtransaction_id: ri.rtransactionId,
+ total_refresh_cost_bound: Amounts.stringify(
+ ri.totalRefreshCostBound,
+ ),
+ };
+ switch (ri.type) {
+ case RefundState.Applied:
+ refunds.push({ type: BackupRefundState.Applied, ...common });
+ break;
+ case RefundState.Failed:
+ refunds.push({ type: BackupRefundState.Failed, ...common });
+ break;
+ case RefundState.Pending:
+ refunds.push({ type: BackupRefundState.Pending, ...common });
+ break;
+ }
+ }
+
+ backupPurchases.push({
+ clock_created: 1,
+ contract_terms_raw: purch.contractTermsRaw,
+ auto_refund_deadline: purch.autoRefundDeadline,
+ merchant_pay_sig: purch.merchantPaySig,
+ pay_coins: purch.payCoinSelection.coinPubs.map((x, i) => ({
+ coin_pub: x,
+ contribution: Amounts.stringify(
+ purch.payCoinSelection.coinContributions[i],
+ ),
+ })),
+ proposal_id: purch.proposalId,
+ refunds,
+ timestamp_accept: purch.timestampAccept,
+ timestamp_first_successful_pay: purch.timestampFirstSuccessfulPay,
+ timestamp_last_refund_status: purch.timestampLastRefundStatus,
+ abort_status:
+ purch.abortStatus === AbortStatus.None
+ ? undefined
+ : purch.abortStatus,
+ nonce_priv: purch.noncePriv,
+ });
+ });
+
+ await tx.iter(Stores.proposals).forEach((prop) => {
+ if (purchaseProposalIdSet.has(prop.proposalId)) {
+ return;
+ }
+ let propStatus: BackupProposalStatus;
+ switch (prop.proposalStatus) {
+ case ProposalStatus.ACCEPTED:
+ return;
+ case ProposalStatus.DOWNLOADING:
+ case ProposalStatus.PROPOSED:
+ propStatus = BackupProposalStatus.Proposed;
+ break;
+ case ProposalStatus.PERMANENTLY_FAILED:
+ propStatus = BackupProposalStatus.PermanentlyFailed;
+ break;
+ case ProposalStatus.REFUSED:
+ propStatus = BackupProposalStatus.Refused;
+ break;
+ case ProposalStatus.REPURCHASE:
+ propStatus = BackupProposalStatus.Repurchase;
+ break;
+ }
+ backupProposals.push({
+ claim_token: prop.claimToken,
+ nonce_priv: prop.noncePriv,
+ proposal_id: prop.noncePriv,
+ proposal_status: propStatus,
+ repurchase_proposal_id: prop.repurchaseProposalId,
+ timestamp: prop.timestamp,
+ contract_terms_raw: prop.download?.contractTermsRaw,
+ download_session_id: prop.downloadSessionId,
+ });
+ });
+
+ await tx.iter(Stores.refreshGroups).forEach((rg) => {
+ const oldCoins: BackupRefreshOldCoin[] = [];
+
+ for (let i = 0; i < rg.oldCoinPubs.length; i++) {
+ let refreshSession: BackupRefreshSession | undefined;
+ const s = rg.refreshSessionPerCoin[i];
+ if (s) {
+ refreshSession = {
+ new_denoms: s.newDenoms.map((x) => ({
+ count: x.count,
+ denom_pub_hash: x.denomPubHash,
+ })),
+ session_secret_seed: s.sessionSecretSeed,
+ noreveal_index: s.norevealIndex,
+ };
+ }
+ oldCoins.push({
+ coin_pub: rg.oldCoinPubs[i],
+ estimated_output_amount: Amounts.stringify(
+ rg.estimatedOutputPerCoin[i],
+ ),
+ finished: rg.finishedPerCoin[i],
+ input_amount: Amounts.stringify(rg.inputPerCoin[i]),
+ refresh_session: refreshSession,
+ });
+ }
+
+ backupRefreshGroups.push({
+ reason: rg.reason as any,
+ refresh_group_id: rg.refreshGroupId,
+ timestamp_started: rg.timestampCreated,
+ timestamp_finished: rg.timestampFinished,
+ old_coins: oldCoins,
});
});
@@ -284,16 +486,16 @@ export async function exportBackup(
schema_id: "gnu-taler-wallet-backup-content",
schema_version: 1,
clocks: bs.clocks,
- exchanges: exchanges,
+ exchanges: backupExchanges,
wallet_root_pub: bs.walletRootPub,
- backup_providers: [],
+ backup_providers: backupBackupProviders,
current_device_id: bs.deviceId,
- proposals: [],
+ proposals: backupProposals,
purchase_tombstones: [],
- purchases: [],
- recoup_groups: [],
- refresh_groups: [],
- tips: [],
+ purchases: backupPurchases,
+ recoup_groups: backupRecoupGroups,
+ refresh_groups: backupRefreshGroups,
+ tips: backupTips,
timestamp: bs.lastBackupTimestamp,
trusted_auditors: {},
trusted_exchanges: {},
diff --git a/packages/taler-wallet-core/src/operations/refresh.ts b/packages/taler-wallet-core/src/operations/refresh.ts
index 16b691ec8..71cc78fa9 100644
--- a/packages/taler-wallet-core/src/operations/refresh.ts
+++ b/packages/taler-wallet-core/src/operations/refresh.ts
@@ -711,6 +711,7 @@ export async function createRefreshGroup(
retryInfo: initRetryInfo(),
inputPerCoin,
estimatedOutputPerCoin,
+ timestampCreated: getTimestampNow(),
};
if (oldCoinPubs.length == 0) {
diff --git a/packages/taler-wallet-core/src/operations/refund.ts b/packages/taler-wallet-core/src/operations/refund.ts
index 36b21b232..367b644a2 100644
--- a/packages/taler-wallet-core/src/operations/refund.ts
+++ b/packages/taler-wallet-core/src/operations/refund.ts
@@ -158,6 +158,8 @@ async function applySuccessfulRefund(
refundAmount: Amounts.parseOrThrow(r.refund_amount),
refundFee: denom.feeRefund,
totalRefreshCostBound,
+ coinPub: r.coin_pub,
+ rtransactionId: r.rtransaction_id,
};
}
@@ -208,6 +210,8 @@ async function storePendingRefund(
refundAmount: Amounts.parseOrThrow(r.refund_amount),
refundFee: denom.feeRefund,
totalRefreshCostBound,
+ coinPub: r.coin_pub,
+ rtransactionId: r.rtransaction_id,
};
}
@@ -259,6 +263,8 @@ async function storeFailedRefund(
refundAmount: Amounts.parseOrThrow(r.refund_amount),
refundFee: denom.feeRefund,
totalRefreshCostBound,
+ coinPub: r.coin_pub,
+ rtransactionId: r.rtransaction_id,
};
if (p.abortStatus === AbortStatus.AbortRefund) {
diff --git a/packages/taler-wallet-core/src/operations/tip.ts b/packages/taler-wallet-core/src/operations/tip.ts
index a57963824..bc10e346d 100644
--- a/packages/taler-wallet-core/src/operations/tip.ts
+++ b/packages/taler-wallet-core/src/operations/tip.ts
@@ -25,10 +25,10 @@ import {
import * as Amounts from "../util/amounts";
import {
Stores,
- TipPlanchet,
CoinRecord,
CoinSourceType,
CoinStatus,
+ DenominationRecord,
} from "../types/dbTypes";
import {
getExchangeWithdrawalInfo,
@@ -50,6 +50,7 @@ import { checkDbInvariant } from "../util/invariants";
import { TalerErrorCode } from "../TalerErrorCode";
import { initRetryInfo, updateRetryInfoTimeout } from "../util/retries";
import { j2s } from "../util/helpers";
+import { DerivedTipPlanchet } from '../types/cryptoTypes';
const logger = new Logger("operations/tip.ts");
@@ -201,39 +202,34 @@ async function processTipImpl(
const denomsForWithdraw = tipRecord.denomsSel;
- if (!tipRecord.planchets) {
- const planchets: TipPlanchet[] = [];
-
- for (const sd of denomsForWithdraw.selectedDenoms) {
- const denom = await ws.db.get(Stores.denominations, [
- tipRecord.exchangeBaseUrl,
- sd.denomPubHash,
- ]);
- if (!denom) {
- throw Error("denom does not exist anymore");
- }
- for (let i = 0; i < sd.count; i++) {
- const r = await ws.cryptoApi.createTipPlanchet(denom);
- planchets.push(r);
- }
- }
- await ws.db.mutate(Stores.tips, walletTipId, (r) => {
- if (!r.planchets) {
- r.planchets = planchets;
- }
- return r;
- });
- }
-
tipRecord = await ws.db.get(Stores.tips, walletTipId);
checkDbInvariant(!!tipRecord, "tip record should be in database");
- checkDbInvariant(!!tipRecord.planchets, "tip record should have planchets");
+ const planchets: DerivedTipPlanchet[] = [];
// Planchets in the form that the merchant expects
- const planchetsDetail: TipPlanchetDetail[] = tipRecord.planchets.map((p) => ({
- coin_ev: p.coinEv,
- denom_pub_hash: p.denomPubHash,
- }));
+ const planchetsDetail: TipPlanchetDetail[] = [];
+ const denomForPlanchet: { [index: number]: DenominationRecord} = [];
+
+ for (const dh of denomsForWithdraw.selectedDenoms) {
+ const denom = await ws.db.get(Stores.denominations, [
+ tipRecord.exchangeBaseUrl,
+ dh.denomPubHash,
+ ]);
+ checkDbInvariant(!!denom, "denomination should be in database");
+ denomForPlanchet[planchets.length] = denom;
+ for (let i = 0; i < dh.count; i++) {
+ const p = await ws.cryptoApi.createTipPlanchet({
+ denomPub: dh.denomPubHash,
+ planchetIndex: planchets.length,
+ secretSeed: tipRecord.secretSeed,
+ });
+ planchets.push(p);
+ planchetsDetail.push({
+ coin_ev: p.coinEv,
+ denom_pub_hash: denom.denomPubHash,
+ });
+ }
+ }
const tipStatusUrl = new URL(
`tips/${tipRecord.merchantTipId}/pickup`,
@@ -264,7 +260,7 @@ async function processTipImpl(
codecForTipResponse(),
);
- if (response.blind_sigs.length !== tipRecord.planchets.length) {
+ if (response.blind_sigs.length !== planchets.length) {
throw Error("number of tip responses does not match requested planchets");
}
@@ -273,18 +269,19 @@ async function processTipImpl(
for (let i = 0; i < response.blind_sigs.length; i++) {
const blindedSig = response.blind_sigs[i].blind_sig;
- const planchet = tipRecord.planchets[i];
+ const denom = denomForPlanchet[i];
+ const planchet = planchets[i];
const denomSig = await ws.cryptoApi.rsaUnblind(
blindedSig,
planchet.blindingKey,
- planchet.denomPub,
+ denom.denomPub,
);
const isValid = await ws.cryptoApi.rsaVerify(
planchet.coinPub,
denomSig,
- planchet.denomPub,
+ denom.denomPub,
);
if (!isValid) {
@@ -312,9 +309,9 @@ async function processTipImpl(
coinIndex: i,
walletTipId: walletTipId,
},
- currentAmount: planchet.coinValue,
- denomPub: planchet.denomPub,
- denomPubHash: planchet.denomPubHash,
+ currentAmount: denom.value,
+ denomPub: denom.denomPub,
+ denomPubHash: denom.denomPubHash,
denomSig: denomSig,
exchangeBaseUrl: tipRecord.exchangeBaseUrl,
status: CoinStatus.Fresh,
diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts
index e4cbdd8c3..56e07a426 100644
--- a/packages/taler-wallet-core/src/operations/transactions.ts
+++ b/packages/taler-wallet-core/src/operations/transactions.ts
@@ -92,7 +92,6 @@ export async function getTransactions(
Stores.purchases,
Stores.refreshGroups,
Stores.reserves,
- Stores.reserveHistory,
Stores.tips,
Stores.withdrawalGroups,
Stores.planchets,