summaryrefslogtreecommitdiff
path: root/src/types/history.ts
blob: f4f3872ca6d05f6cbee7e7d5a151ee9402ac200d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 This file is part of GNU Taler
 (C) 2019 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Type and schema definitions for the wallet's history.
 */

/**
 * Imports.
 */
import { RefreshReason } from "./walletTypes";
import { ReserveTransaction } from "./ReserveTransaction";
import { WithdrawalSource } from "./dbTypes";
import { Timestamp } from "../util/time";

/**
 * Type tags for the history event types.
 */
export const enum HistoryEventType {
  AuditorComplaintSent = "auditor-complained-sent",
  AuditorComplaintProcessed = "auditor-complaint-processed",
  AuditorTrustAdded = "auditor-trust-added",
  AuditorTrustRemoved = "auditor-trust-removed",
  ExchangeAdded = "exchange-added",
  ExchangeTermsAccepted = "exchange-terms-accepted",
  ExchangePolicyChanged = "exchange-policy-changed",
  ExchangeTrustAdded = "exchange-trust-added",
  ExchangeTrustRemoved = "exchange-trust-removed",
  ExchangeUpdated = "exchange-updated",
  FundsDepositedToSelf = "funds-deposited-to-self",
  FundsRecouped = "funds-recouped",
  OrderAccepted = "order-accepted",
  OrderRedirected = "order-redirected",
  OrderRefused = "order-refused",
  PaymentAborted = "payment-aborted",
  PaymentSent = "payment-sent",
  Refreshed = "refreshed",
  Refund = "refund",
  ReserveBalanceUpdated = "reserve-balance-updated",
  ReserveCreated = "reserve-created",
  TipAccepted = "tip-accepted",
  TipDeclined = "tip-declined",
  Withdrawn = "withdrawn",
}

export const enum ReserveType {
  /**
   * Manually created.
   */
  Manual = "manual",
  /**
   * Withdrawn from a bank that has "tight" Taler integration
   */
  TalerBankWithdraw = "taler-bank-withdraw",
}

/**
 * Short info about a reserve.  Enough to display in a list view and
 * to query more information from the wallet.
 */
export interface ReserveShortInfo {
  /**
   * The exchange that the reserve will be at.
   */
  exchangeBaseUrl: string;

  /**
   * Key to query more details
   */
  reservePub: string;

  /**
   * Detail about how the reserve has been created.
   */
  reserveCreationDetail: ReserveCreationDetail;
}

export type ReserveCreationDetail =
  | { type: ReserveType.Manual }
  | { type: ReserveType.TalerBankWithdraw; bankUrl: string };

export interface HistoryReserveCreatedEvent {
  type: HistoryEventType.ReserveCreated;

  /**
   * Amount that the should appear in the reserve once its status
   * is requested from the exchange.
   */
  expectedAmount: string;

  /**
   * Condensed information about the reserve.
   */
  reserveShortInfo: ReserveShortInfo;
}

/**
 * This event is emitted every time we ask the exchange for the status
 * of the reserve, and the status has changed.
 */
export interface HistoryReserveBalanceUpdatedEvent {
  type: HistoryEventType.ReserveBalanceUpdated;

  /**
   * Point in time when the reserve was confirmed.
   */
  timestamp: Timestamp;

  newHistoryTransactions: ReserveTransaction[];

  /**
   * Condensed information about the reserve.
   */
  reserveShortInfo: ReserveShortInfo;

  /**
   * Amount currently left in the reserve.
   */
  amountReserveBalance: string;

  /**
   * Amount we expected to be in the reserve at that time,
   * considering ongoing withdrawals from that reserve.
   */
  amountExpected: string;
}

/**
 * History event to indicate that the user has accepted a tip.
 */
export interface HistoryTipAcceptedEvent {
  type: HistoryEventType.TipAccepted;

  /**
   * Point in time when the tip has been accepted.
   */
  timestamp: Timestamp;

  /**
   * Unique identifier for the tip to query more information.
   */
  tipId: string;

  /**
   * Raw amount of the tip, without extra fees that apply.
   */
  tipAmountRaw: string;
}

/**
 * History event to indicate that the user has accepted a tip.
 */
export interface HistoryTipDeclinedEvent {
  type: HistoryEventType.TipDeclined;

  /**
   * Point in time when the tip has been declined.
   */
  timestamp: Timestamp;

  /**
   * Unique identifier for the tip to query more information.
   */
  tipId: string;

  /**
   * Raw amount of the tip, without extra fees that apply.
   */
  tipAmountRaw: string;
}

/**
 * The wallet has send a complaint (typically with cryptographic evidence of
 * something having gone wrong) to the auditor.
 */
export interface HistoryAuditorComplaintSentEvent {
  type: HistoryEventType.AuditorComplaintSent;

  auditorComplaintId: string;

  /* FIXME: add fields once this feature is implemented */
}

/**
 * The wallet has received a response from the auditor to a complaint.
 */
export interface HistoryAuditorComplaintProcessedEvent {
  type: HistoryEventType.AuditorComplaintProcessed;

  auditorComplaintId: string;

  /* FIXME: add fields once this feature is implemented */
}

/**
 * The wallet has added an auditor as a trusted auditor.
 */
export interface HistoryAuditorTrustAddedEvent {
  type: HistoryEventType.AuditorTrustAdded;

  /**
   * Base URL of the auditor.
   */
  auditorBaseUrl: string;

  /**
   * If set to true, this auditor hasn't been added by the user,
   * but is part of the pre-set trusted auditors in the wallet.
   */
  builtIn: boolean;
}

/**
 * The wallet has added an auditor as a trusted auditor.
 */
export interface HistoryAuditorTrustRemovedEvent {
  type: HistoryEventType.AuditorTrustRemoved;

  /**
   * Base URL of the auditor.
   */
  auditorBaseUrl: string;
}

/**
 * An exchange has been added to the wallet.  The wallet only
 * downloads information about the exchange, but does not necessarily
 * trust it yet.
 */
export interface HistoryExchangeAddedEvent {
  type: HistoryEventType.ExchangeAdded;

  exchangeBaseUrl: string;

  /**
   * Set to true if the exchange was pre-configured
   * by the wallet.
   */
  builtIn: boolean;
}

/**
 * History event to indicate that the wallet now trusts
 * an exchange.
 */
export interface HistoryExchangeTrustAddedEvent {
  type: HistoryEventType.ExchangeTrustAdded;

  exchangeBaseUrl: string;

  /**
   * Set to true if the exchange was pre-configured
   * by the wallet.
   */
  builtIn: boolean;
}

/**
 * History event to indicate that the wallet doesn't trust
 * an exchange anymore.
 */
export interface HistoryExchangeTrustRemovedEvent {
  type: HistoryEventType.ExchangeTrustRemoved;

  exchangeBaseUrl: string;
}

/**
 * This event indicates that the user accepted the terms of service
 * of the exchange.
 */
export interface HistoryExchangeTermsAcceptedEvent {
  type: HistoryEventType.ExchangeTermsAccepted;

  exchangeBaseUrl: string;

  /**
   * Etag that the exchange sent with the terms of service.
   * Identifies the version of the terms of service.
   */
  termsEtag: string | undefined;
}

/**
 * The exchange has changed the terms of service or privacy
 * policy.
 */
export interface HistoryExchangePolicyChangedEvent {
  type: HistoryEventType.ExchangePolicyChanged;

  exchangeBaseUrl: string;

  /**
   * Etag that the exchange sent with the terms of service.
   * Identifies the version of the terms of service.
   */
  termsEtag: string | undefined;

  /**
   * Etag that the exchange sent with the privacy policy.
   * Identifies the version of the privacy policy.
   */
  privacyPolicyEtag: string | undefined;
}

/**
 * This history event indicates that the exchange has updated
 * the /keys or /wire information.  The event is only emitted if
 * this information changed since the last time the wallet checked it.
 */
export interface HistoryExchangeUpdatedEvent {
  type: HistoryEventType.ExchangeUpdated;

  exchangeBaseUrl: string;
}

/**
 * History event to indicate that the user sent back digital cash from
 * their wallet back to their own bank account (basically acting as a merchant).
 */
export interface HistoryFundsDepositedToSelfEvent {
  type: HistoryEventType.FundsDepositedToSelf;

  /**
   * Amount that got sent back.
   */
  amount: string;

  /**
   * Account that received the funds.
   */
  receiverPaytoUri: string;
}

/**
 * History event to indicate that the exchange marked
 * some denominations as "compromised", and the wallet has
 * converted funds in these denominations to new funds.
 */
export interface HistoryFundsRecoupedEvent {
  type: HistoryEventType.FundsRecouped;
  numCoinsRecouped: number;
}

/**
 * Condensed information about an order, enough to display in a list view
 * and to query more details from the wallet for a detail view.
 */
export interface OrderShortInfo {
  /**
   * Wallet-internal identifier of the proposal.
   */
  proposalId: string;

  /**
   * Order ID, uniquely identifies the order within a merchant instance.
   */
  orderId: string;

  /**
   * Base URL of the merchant.
   */
  merchantBaseUrl: string;

  /**
   * Amount that must be paid for the contract.
   */
  amount: string;

  /**
   * Summary of the proposal, given by the merchant.
   */
  summary: string;

  /**
   * URL of the fulfillment, given by the merchant.
   */
  fulfillmentUrl: string;
}

/**
 * The user has accepted purchasing something.
 */
export interface HistoryOrderAcceptedEvent {
  /**
   * Type tag.
   */
  type: HistoryEventType.OrderAccepted;

  /**
   * Condensed info about the order.
   */
  orderShortInfo: OrderShortInfo;
}

/**
 * The customer refused to pay.
 */
export interface HistoryOrderRefusedEvent {
  /**
   * Type tag.
   */
  type: HistoryEventType.OrderRefused;

  /**
   * Condensed info about the order.
   */
  orderShortInfo: OrderShortInfo;
}

/**
 * The wallet has claimed an order.
 */
export interface HistoryOrderRedirectedEvent {
  /**
   * Type tag.
   */
  type: HistoryEventType.OrderRedirected;

  /**
   * Condensed info about the new order that contains a
   * product (identified by the fulfillment URL) that we've already paid for.
   */
  newOrderShortInfo: OrderShortInfo;

  /**
   * Condensed info about the order that we already paid for.
   */
  alreadyPaidOrderShortInfo: OrderShortInfo;
}

/**
 * The user aborted a pending, partially submitted payment.
 */
export interface HistoryPaymentAbortedEvent {
  /**
   * Type tag.
   */
  type: HistoryEventType.PaymentAborted;

  /**
   * Condensed info about the order that we already paid for.
   */
  orderShortInfo: OrderShortInfo;

  /**
   * Amount that was lost due to refund and refreshing fees.
   */
  amountLost: string;
}

export interface VerbosePayCoinDetails {
  coins: {
    value: string;
    contribution: string;
    denomPub: string;
  }[];
}

/**
 * History event to indicate that a payment has been (re-)submitted
 * to the merchant.
 */
export interface HistoryPaymentSent {
  /**
   * Type tag.
   */
  type: HistoryEventType.PaymentSent;

  /**
   * Condensed info about the order that we already paid for.
   */
  orderShortInfo: OrderShortInfo;

  /**
   * Set to true if the payment has been previously sent
   * to the merchant successfully, possibly with a different session ID.
   */
  replay: boolean;

  /**
   * Number of coins that were involved in the payment.
   */
  numCoins: number;

  /**
   * Amount that was paid, including deposit and wire fees.
   */
  amountPaidWithFees: string;

  /**
   * Session ID that the payment was (re-)submitted under.
   */
  sessionId: string | undefined;

  verboseDetails?: VerbosePayCoinDetails;
}

/**
 * A refund has been applied.
 */
export interface HistoryRefunded {
  /**
   * Type tag.
   */
  type: HistoryEventType.Refund;

  timestamp: Timestamp;

  orderShortInfo: OrderShortInfo;

  /**
   * Unique identifier for this refund.
   * (Identifies multiple refund permissions that were obtained at once.)
   */
  refundGroupId: string;

  /**
   * Part of the refund that couldn't be applied because
   * the refund permissions were expired.
   */
  amountRefundedInvalid: string;

  /**
   * Amount that has been refunded by the merchant.
   */
  amountRefundedRaw: string;

  /**
   * Amount will be added to the wallet's balance after fees and refreshing.
   */
  amountRefundedEffective: string;
}

export interface VerboseRefreshDetails {
  outputCoins: {
    denomPub: string;
    value: string;
  }[];
}

/**
 * Event to indicate that a group of refresh sessions has completed.
 */
export interface HistoryRefreshedEvent {
  /**
   * Type tag.
   */
  type: HistoryEventType.Refreshed;

  /**
   * Amount that is now available again because it has
   * been refreshed.
   */
  amountRefreshedEffective: string;

  /**
   * Amount that we spent for refreshing.
   */
  amountRefreshedRaw: string;

  /**
   * Why was the refreshing done?
   */
  refreshReason: RefreshReason;

  numInputCoins: number;
  numRefreshedInputCoins: number;
  numOutputCoins: number;

  /**
   * Identifier for a refresh group, contains one or
   * more refresh session IDs.
   */
  refreshGroupId: string;

  verboseDetails?: VerboseRefreshDetails;
}

export interface VerboseWithdrawDetails {
  coins: {
    value: string;
    denomPub: string;
  }[];
}

/**
 * A withdrawal has completed.
 */
export interface HistoryWithdrawnEvent {
  type: HistoryEventType.Withdrawn;

  /**
   * Exchange that was withdrawn from.
   */
  exchangeBaseUrl: string;

  /**
   * Unique identifier for the withdrawal session, can be used to
   * query more detailed information from the wallet.
   */
  withdrawSessionId: string;

  withdrawalSource: WithdrawalSource;

  /**
   * Amount that has been subtracted from the reserve's balance
   * for this withdrawal.
   */
  amountWithdrawnRaw: string;

  /**
   * Amount that actually was added to the wallet's balance.
   */
  amountWithdrawnEffective: string;

  /**
   * Verbose details of the operations, only generated when requested.
   */
  verboseDetails?: VerboseWithdrawDetails;
}

/**
 * Common fields that all history events need to have.
 */
export interface HistoryEventBase {
  type: HistoryEventType;

  /**
   * Main timestamp of the history event.
   */
  timestamp: Timestamp;

  /**
   * Opaque unique ID for the event, used as a starting point
   * for paginating history queries and for invoking actions
   * on the event (e.g. hiding it from the history).
   */
  eventId: string;

  /**
   * Extra details for debugging.
   */
  verboseDetails?: any;
}

/**
 * Union of all history detail types, discriminated by their type.
 */
export type HistoryEvent = HistoryEventBase &
  (
    | HistoryAuditorComplaintSentEvent
    | HistoryAuditorComplaintProcessedEvent
    | HistoryAuditorTrustAddedEvent
    | HistoryAuditorTrustRemovedEvent
    | HistoryExchangeAddedEvent
    | HistoryExchangeTermsAcceptedEvent
    | HistoryExchangePolicyChangedEvent
    | HistoryExchangeTrustAddedEvent
    | HistoryExchangeTrustRemovedEvent
    | HistoryExchangeUpdatedEvent
    | HistoryFundsDepositedToSelfEvent
    | HistoryFundsRecoupedEvent
    | HistoryOrderAcceptedEvent
    | HistoryOrderRedirectedEvent
    | HistoryOrderRefusedEvent
    | HistoryPaymentAbortedEvent
    | HistoryPaymentSent
    | HistoryRefreshedEvent
    | HistoryRefunded
    | HistoryReserveBalanceUpdatedEvent
    | HistoryReserveCreatedEvent
    | HistoryTipAcceptedEvent
    | HistoryTipDeclinedEvent
    | HistoryWithdrawnEvent
  );

export interface HistoryQuery {
  /**
   * Output extra verbose details, intended for debugging
   * and not for end users.
   */
  extraDebug?: boolean;
}