summaryrefslogtreecommitdiff
path: root/core/api-exchange.rst
blob: 9e4f24a349fd7d0ea9ce5d4735b55c5475702c7a (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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
..
  This file is part of GNU TALER.
  Copyright (C) 2014-2020 Taler Systems SA

  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 2.1, or (at your option) any later version.

  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 Lesser General Public License for more details.

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

  @author Christian Grothoff

=============================
The Exchange RESTful JSON API
=============================

The API specified here follows the :ref:`general conventions <http-common>`
for all details not specified in the individual requests.
The `glossary <https://docs.taler.net/glossary.html#glossary>`_
defines all specific terms used in this section.

.. _keys:

--------------------
Terms of service API
--------------------

These APIs allow merchants and wallets to obtian the terms of service
and the privacy policy of an exchange.


.. http:get:: /terms

  Get the terms of service of the exchange.
  The exchange will consider the "Accept" and "Accept-Language" and
  "Accept-Encoding" headers when generating a response. Specifically,
  it will try to find a response with an acceptable mime-type, then
  pick the version in the most preferred language of the user, and
  finally apply compression if that is allowed by the client and
  deemed beneficial.

  The exchange will set an "Etag", and subsequent requests of the
  same client should provide the tag in an "If-None-Match" header
  to detect if the terms of service have changed.  If not, a
  "204 Not Modified" response will be returned.

  If the "Etag" is missing, the client should not cache the response and instead prompt the user again at the next opportunity. This is usually only the case if the terms of service were not configured correctly.


.. http:get:: /privacy

  Get the privacy policy of the exchange.
  The exchange will consider the "Accept" and "Accept-Language" and
  "Accept-Encoding" headers when generating a response. Specifically,
  it will try to find a response with an acceptable mime-type, then
  pick the version in the most preferred language of the user, and
  finally apply compression if that is allowed by the client and
  deemed beneficial.

  The exchange will set an "Etag", and subsequent requests of the
  same client should provide the tag in an "If-None-Match" header
  to detect if the privacy policy has changed.  If not, a
  "204 Not Modified" response will be returned.

  If the "Etag" is missing, the client should not cache the response and instead prompt the user again at the next opportunity. This is usually only the case if the privacy policy was not configured correctly.

---------------------------
Exchange status information
---------------------------

This API is used by wallets and merchants to obtain global information about
the exchange, such as online signing keys, available denominations and the fee
structure.  This is typically the first call any exchange client makes, as it
returns information required to process all of the other interactions with the
exchange.  The returned information is secured by (1) signature(s) from the exchange,
especially the long-term offline signing key of the exchange, which clients should
cache; (2) signature(s) from auditors, and the auditor keys should be
hard-coded into the wallet as they are the trust anchors for Taler; (3)
possibly by using HTTPS.


.. http:get:: /seed

  Return an entropy seed. The exchange will return a high-entropy
  value that will differ for every call.  The response is NOT in
  JSON, but simply high-entropy binary data in the HTTP body.
  This API should be used by wallets to guard themselves against
  running on low-entropy (bad PRNG) hardware. Naturally, the entropy
  returned MUST be mixed with locally generated entropy.


.. http:get:: /keys

  Get a list of all denomination keys offered by the exchange,
  as well as the exchange's current online signing key.

  **Request:**

  :query last_issue_date: optional argument specifying the maximum value of any of the "stamp_start" members of the denomination keys of a "/keys" response that is already known to the client. Allows the exchange to only return keys that have changed since that timestamp.  The given value must be an unsigned 64-bit integer representing seconds after 1970.  If the timestamp does not exactly match the "stamp_start" of one of the denomination keys, all keys are returned.

  **Response:**

  :http:statuscode:`200 OK`:
    The exchange responds with a `ExchangeKeysResponse` object. This request should
    virtually always be successful. It only fails if the exchange is misconfigured or
    has not yet been provisioned with key signatures via `taler-exchange-offline`.

  **Details:**

  .. ts:def:: ExchangeKeysResponse

    interface ExchangeKeysResponse {
      // libtool-style representation of the Exchange protocol version, see
      // https://www.gnu.org/software/libtool/manual/html_node/Versioning.html#Versioning
      // The format is "current:revision:age".
      version: string;

      // EdDSA master public key of the exchange, used to sign entries in 'denoms' and 'signkeys'
      master_public_key: EddsaPublicKey;

      // Relative duration until inactive reserves are closed; not signed, expressed as
      // a string in relative time in microseconds, i.e. "/Delay(1000)/" for 1 second.
      reserve_closing_delay: RelativeTime;

      // Denominations offered by this exchange.
      denoms: Denom[];

      // Denominations for which the exchange currently offers/requests recoup.
      recoup: Recoup[];

      // The date when the denomination keys were last updated.
      list_issue_date: Timestamp;

      // Auditors of the exchange.
      auditors: AuditorKeys[];

      // The exchange's signing keys.
      signkeys: SignKey[];

      // compact EdDSA `signature` (binary-only) over the SHA-512 hash of the
      // concatenation of all SHA-512 hashes of the RSA denomination public keys
      // in ``denoms`` in the same order as they were in ``denoms``.  Note that for
      // hashing, the binary format of the RSA public keys is used, and not their
      // `base32 encoding <base32>`.  Wallets cannot do much with this signature by itself;
      // it is only useful when multiple clients need to establish that the exchange
      // is sabotaging end-user anonymity by giving disjoint denomination keys to
      // different users.  If a exchange were to do this, this signature allows the
      // clients to demonstrate to the public that the exchange is dishonest.
      eddsa_sig: EddsaSignature;

      // Public EdDSA key of the exchange that was used to generate the signature.
      // Should match one of the exchange's signing keys from /keys.  It is given
      // explicitly as the client might otherwise be confused by clock skew as to
      // which signing key was used.
      eddsa_pub: EddsaPublicKey;
    }

  .. ts:def:: Denom

    interface Denom {
      // How much are coins of this denomination worth?
      value: Amount;

      // When does the denomination key become valid?
      stamp_start: Timestamp;

      // When is it no longer possible to deposit coins
      // of this denomination?
      stamp_expire_withdraw: Timestamp;

      // Timestamp indicating by when legal disputes relating to these coins must
      // be settled, as the exchange will afterwards destroy its evidence relating to
      // transactions involving this coin.
      stamp_expire_legal: Timestamp;

      // Public (RSA) key for the denomination.
      denom_pub: RsaPublicKey;

      // Fee charged by the exchange for withdrawing a coin of this denomination
      fee_withdraw: Amount;

      // Fee charged by the exchange for depositing a coin of this denomination
      fee_deposit: Amount;

      // Fee charged by the exchange for refreshing a coin of this denomination
      fee_refresh: Amount;

      // Fee charged by the exchange for refunding a coin of this denomination
      fee_refund: Amount;

      // Signature of `TALER_DenominationKeyValidityPS`
      master_sig: EddsaSignature;
    }

  Fees for any of the operations can be zero, but the fields must still be
  present. The currency of the ``fee_deposit``, ``fee_refresh`` and ``fee_refund`` must match the
  currency of the ``value``.  Theoretically, the ``fee_withdraw`` could be in a
  different currency, but this is not currently supported by the
  implementation.

  .. ts:def:: Recoup

    interface Recoup {
      // hash of the public key of the denomination that is being revoked under
      // emergency protocol (see /recoup).
      h_denom_pub: HashCode;

      // We do not include any signature here, as the primary use-case for
      // this emergency involves the exchange having lost its signing keys,
      // so such a signature here would be pretty worthless.  However, the
      // exchange will not honor /recoup requests unless they are for
      // denomination keys listed here.
    }

  A signing key in the ``signkeys`` list is a JSON object with the following fields:

  .. ts:def:: SignKey

    interface SignKey {
      // The actual exchange's EdDSA signing public key.
      key: EddsaPublicKey;

      // Initial validity date for the signing key.
      stamp_start: Timestamp;

      // Date when the exchange will stop using the signing key, allowed to overlap
      // slightly with the next signing key's validity to allow for clock skew.
      stamp_expire: Timestamp;

      // Date when all signatures made by the signing key expire and should
      // henceforth no longer be considered valid in legal disputes.
      stamp_end: Timestamp;

      // Signature over ``key`` and ``stamp_expire`` by the exchange master key.
      // Must have purpose TALER_SIGNATURE_MASTER_SIGNING_KEY_VALIDITY.
      master_sig: EddsaSignature;
    }

  An entry in the ``auditors`` list is a JSON object with the following fields:

  .. ts:def:: AuditorKeys

    interface AuditorKeys {
      // The auditor's EdDSA signing public key.
      auditor_pub: EddsaPublicKey;

      // The auditor's URL.
      auditor_url: string;

      // The auditor's name (for humans)
      auditor_name: string;

      // An array of denomination keys the auditor affirms with its signature.
      // Note that the message only includes the hash of the public key, while the
      // signature is actually over the expanded information including expiration
      // times and fees.  The exact format is described below.
      denomination_keys: AuditorDenominationKey[];
    }

  .. ts:def:: AuditorDenominationKey

    interface AuditorDenominationKey {
      // hash of the public RSA key used to sign coins of the respective
      // denomination.  Note that the auditor's signature covers more than just
      // the hash, but this other information is already provided in ``denoms`` and
      // thus not repeated here.
      denom_pub_h: HashCode;

      // Signature of `TALER_ExchangeKeyValidityPS`
      auditor_sig: EddsaSignature;
    }

  The same auditor may appear multiple times in the array for different subsets
  of denomination keys, and the same denomination key hash may be listed
  multiple times for the same or different auditors.  The wallet or merchant
  just should check that the denomination keys they use are in the set for at
  least one of the auditors that they accept.

  .. note::

    Both the individual denominations *and* the denomination list is signed,
    allowing customers to prove that they received an inconsistent list.

.. _wire-req:

.. http:get:: /wire

  Returns a list of payment methods supported by the exchange.  The idea is that wallets may use this information to instruct users on how to perform wire transfers to top up their wallets.

  **Response:**

  :http:statuscode:`200 Ok`:
     The exchange responds with a `WireResponse` object. This request should virtually always be successful.

  **Details:**

  .. ts:def:: WireResponse

    interface WireResponse {

      // Master public key of the exchange, must match the key returned in /keys.
      master_public_key: EddsaPublicKey;

      // Array of wire accounts operated by the exchange for
      // incoming wire transfers.
      accounts: WireAccount[];

      // Object mapping names of wire methods (i.e. "sepa" or "x-taler-bank")
      // to wire fees.
      fees: { method : AggregateTransferFee };
    }

  The specification for the account object is:

  .. ts:def:: WireAccount

    interface WireAccount {
      // payto:// URL identifying the account and wire method
      url: string;

      // Signature using the exchange's offline key
      // with purpose TALER_SIGNATURE_MASTER_WIRE_DETAILS.
      master_sig: EddsaSignature;
    }

  Aggregate wire transfer fees representing the fees the exchange
  charges per wire transfer to a merchant must be specified as an
  array in all wire transfer response objects under ``fees``.  The
  respective array contains objects with the following members:

  .. ts:def:: AggregateTransferFee

    interface AggregateTransferFee {
      // Per transfer wire transfer fee.
      wire_fee: Amount;

      // Per transfer closing fee.
      closing_fee: Amount;

      // What date (inclusive) does this fee go into effect?
      // The different fees must cover the full time period in which
      // any of the denomination keys are valid without overlap.
      start_date: Timestamp;

      // What date (exclusive) does this fee stop going into effect?
      // The different fees must cover the full time period in which
      // any of the denomination keys are valid without overlap.
      end_date: Timestamp;

      // Signature of `TALER_MasterWireFeePS` with purpose TALER_SIGNATURE_MASTER_WIRE_FEES
      sig: EddsaSignature;
    }


----------------------------------------------
Management operations authorized by master key
----------------------------------------------

.. http:get:: /management/keys

  Get a list of future public keys to be used by the exchange.  Only to be
  used by the exchange's offline key management team. Not useful for anyone
  else (but also not secret, so access is public).

  **Response:**

  :http:statuscode:`200 OK`:
    The exchange responds with a `FutureKeysResponse` object. This request should
    virtually always be successful.

  **Details:**

  .. ts:def:: FutureKeysResponse

    interface FutureKeysResponse {

      // Future denominations to be offered by this exchange
      // (only those lacking a master signature).
      future_denoms: FutureDenom[];

      // The exchange's future signing keys (only those lacking a master signature).
      future_signkeys: FutureSignKey[];

      // Master public key expected by this exchange (provided so that the
      // offline signing tool can check that it has the right key).
      master_pub: EddsaPublicKey;

      // Public key of the denomination security module.
      denom_secmod_public_key: EddsaPublicKey;

      // Public key of the signkey security module.
      signkey_secmod_public_key: EddsaPublicKey;

    }

  .. ts:def:: FutureDenom

    interface FutureDenom {
      // Name in the configuration file that defines this denomination.
      section_name: string;

      // How much are coins of this denomination worth?
      value: Amount;

      // When does the denomination key become valid?
      stamp_start: Timestamp;

      // When is it no longer possible to withdraw coins
      // of this denomination?
      stamp_expire_withdraw: Timestamp;

      // When is it no longer possible to deposit coins
      // of this denomination?
      stamp_expire_deposit: Timestamp;

      // Timestamp indicating by when legal disputes relating to these coins must
      // be settled, as the exchange will afterwards destroy its evidence relating to
      // transactions involving this coin.
      stamp_expire_legal: Timestamp;

      // Public (RSA) key for the denomination.
      denom_pub: RsaPublicKey;

      // Fee charged by the exchange for withdrawing a coin of this denomination
      fee_withdraw: Amount;

      // Fee charged by the exchange for depositing a coin of this denomination
      fee_deposit: Amount;

      // Fee charged by the exchange for refreshing a coin of this denomination
      fee_refresh: Amount;

      // Fee charged by the exchange for refunding a coin of this denomination
      fee_refund: Amount;

      // Signature over this denomination by the denomination security module.
      denom_secmod_sig: EddsaSignature;

    }

  .. ts:def:: FutureSignKey

    interface SignKey {
      // The actual exchange's EdDSA signing public key.
      key: EddsaPublicKey;

      // Initial validity date for the signing key.
      stamp_start: Timestamp;

      // Date when the exchange will stop using the signing key, allowed to overlap
      // slightly with the next signing key's validity to allow for clock skew.
      stamp_expire: Timestamp;

      // Date when all signatures made by the signing key expire and should
      // henceforth no longer be considered valid in legal disputes.
      stamp_end: Timestamp;

      // Signature over this signing key by the signkey security module.
      signkey_secmod_sig: EddsaSignature;
    }


.. http:post:: /management/keys

  Provide master signatures for future public keys to be used by the exchange.
  Only to be used by the exchange's offline key management team. Not useful
  for anyone else.

  **Request:** The request body must be a `MasterSignatures` object.

  **Response:**

  :http:statuscode:`204 No content`:
    The request was successfully processed.
  :http:statuscode:`403 Forbidden`:
    A provided signature is invalid.
  :http:statuscode:`404 Not found`:
    One of the keys for which a signature was provided is unknown to the exchange.

  **Details:**

  .. ts:def:: MasterSignatures

    interface MasterSignatures {

      // Provided master signatures for future denomination keys.
      denom_sigs: DenomSignature[];

      // Provided master signatures for future online signing keys.
      signkey_sigs: SignKeySignature[];

    }

  .. ts:def:: DenomSignature

    interface DenomSignature {

      // Hash of the pbulic (RSA) key of the denomination.
      h_denom_pub: HashCode;

      // Signature of `TALER_DenominationKeyValidityPS`
      master_sig: EddsaSignature;

    }

  .. ts:def:: SignKeySignature

    interface SignKeySignature {
      // The actual exchange's EdDSA signing public key.
      key: EddsaPublicKey;

      // Signature by the exchange master key.
      // Must have purpose TALER_SIGNATURE_MASTER_SIGNING_KEY_VALIDITY.
      master_sig: EddsaSignature;

    }


.. http:post:: /management/denominations/$H_DENOM_PUB/revoke

  Revoke denomination key, prevending further use by the exchange.
  Only to be used by the exchange's offline key management team. Not useful
  for anyone else.

  **Request:** The request body must be a `DenomRevocationSignature` object.

  **Response:**

  :http:statuscode:`204 No content`:
    The request was successfully processed.
  :http:statuscode:`403 Forbidden`:
    The provided signature is invalid.

  **Details:**

  .. ts:def:: DenomRevocationSignature

    interface DenomRevocationSignature {

      // Signature by the exchange master key.
      // Must have purpose TALER_SIGNATURE_MASTER_DENOMINATION_KEY_REVOKED
      master_sig: EddsaSignature;

    }

.. http:post:: /management/signkeys/$EXCHANGE_PUB/revoke

  Revoke exchange online signing key, prevending further use by the exchange.
  Only to be used by the exchange's offline key management team. Not useful
  for anyone else.

  **Request:** The request body must be a `SignkeyRevocationSignature` object.

  **Response:**

  :http:statuscode:`204 No content`:
    The request was successfully processed.
  :http:statuscode:`403 Forbidden`:
    The provided signature is invalid.

  **Details:**

  .. ts:def:: SignkeyRevocationSignature

    interface SignkeyRevocationSignature {

      // Signature by the exchange master key.
      // Must have purpose TALER_SIGNATURE_MASTER_SIGN_KEY_REVOKED
      master_sig: EddsaSignature;

    }


.. http:post:: /management/auditors

  This request will be used to enable an auditor.

  **Request:**

  The request must be a `AuditorSetupMessage`.

  **Response:**

  :http:statuscode:`204 No content`:
    The auditor was successfully enabled.
  :http:statuscode:`403 Forbidden`:
    The master signature is invalid.
  :http:statuscode:`409 Conflict`:
    The exchange has a more recent request related to this auditor key (replay detected).

  **Details:**

  .. ts:def:: AuditorSetupMessage

    interface AuditorSetupMessage {

      // base URL of the auditor
      auditor_url: string;

      // human-readable name of the auditor
      auditor_name: string;

      // The auditor's EdDSA signing public key.
      auditor_pub: EddsaPublicKey;

      // Signature by the exchange master key.
      // Must have purpose TALER_SIGNATURE_MASTER_AUDITOR_ADD.
      master_sig: EddsaSignature;

      // When does the auditor become active?
      // Should be the time when the signature was created,
      // using the (monotonic!) local time of the system
      // with the offline master public key. Note that
      // even if the time is in the future, the auditor will
      // become active immediately! Used ONLY to detect replay attacks.
      validity_start: Timestamp;

    }

.. http:post:: /management/auditors/$AUDITOR_PUB/disable

  This request will be used to disable the use of the given auditor.
  We use POST instead of DELETE because the exchange will retain state
  about the auditor (specifically the end date) to prevent replay
  attacks abusing the `AuditorSetupMessage`.  Also, DELETE would not
  support a body, which is needed to provide the signature authorizing
  the operation.

  **Request:**

  The request must be a `AuditorTeardownMessage`.

  **Response**

  :http:statuscode:`204 No content`:
    The auditor has successfully disabled the auditor. The body is empty.
  :http:statuscode:`403 Forbidden`:
    The signature is invalid.
  :http:statuscode:`404 Not found`:
    The auditor is unknown to the exchange.
  :http:statuscode:`409 Conflict`:
    The exchange has a more recent request related to this auditor key (replay detected).

  **Details:**

  .. ts:def:: AuditorTeardownMessage

    interface AuditorTeardownMessage {

      // Signature by the exchange master key.
      // Must have purpose TALER_SIGNATURE_MASTER_AUDITOR_DEL.
      master_sig: EddsaSignature;

      // When does the auditor become inactive?
      // Should be the time when the signature was created,
      // using the (monotonic!) local time of the system
      // with the offline master public key.  Note that
      // even if the time is in the future, the auditor will
      // become inactive immediately! Used ONLY to detect replay attacks.
      validity_end: Timestamp;

    }


.. http:post:: /management/wire-fee

  This request will be used to configure wire fees.

  **Request:**

  The request must be a `WireFeeSetupMessage`.

  **Response:**

  :http:statuscode:`204 No content`:
    The wire fee was successfully configured.
  :http:statuscode:`403 Forbidden`:
    The master signature is invalid.
  :http:statuscode:`409 Conflict`:
    The exchange has a conflicting wire fee already setup.
  :http:statuscode:`412 Precondition failed`:
    The currency used is different from the currency of the exchange.

  **Details:**

  .. ts:def:: WireFeeSetupMessage

    interface WireFeeSetupMessage {

      // wire method the fee applies to
      wire_method: string;

      // Signature using the exchange's offline key
      // with purpose TALER_SIGNATURE_MASTER_WIRE_FEES.
      master_sig_wire: EddsaSignature;

      // When does the wire fee validity period start?
      fee_start: Timestamp;

      // When does the wire fee validity period end (exclusive).
      fee_end: Timestamp;

      // Closing fee to charge during that time period for this wire method.
      closing_fee: Amount;

      // Wire fee to charge during that time period for this wire method.
      wire_fee: Amount;

    }



.. http:post:: /management/wire

  This request will be used to enable a wire method (exchange bank account).

  **Request:**

  The request must be a `WireSetupMessage`.

  **Response:**

  :http:statuscode:`204 No content`:
    The wire method was successfully enabled.
  :http:statuscode:`403 Forbidden`:
    The master signature is invalid.
  :http:statuscode:`409 Conflict`:
    The exchange has a more recent request related to this wire method (replay detected).

  **Details:**

  .. ts:def:: WireSetupMessage

    interface WireSetupMessage {

      // payto:// URL identifying the account and wire method
      payto_uri: string;

      // Signature using the exchange's offline key
      // with purpose TALER_SIGNATURE_MASTER_WIRE_DETAILS.
      master_sig_wire: EddsaSignature;

      // Signature using the exchange's offline key
      // with purpose TALER_SIGNATURE_MASTER_WIRE_ADD.
      master_sig_add: EddsaSignature;

      // When does the wire method become active?
      // Should be the time when the signature was created,
      // using the (monotonic!) local time of the system
      // with the offline master public key. Note that
      // even if the time is in the future, the wire method will
      // become active immediately! Used ONLY to detect replay attacks.
      validity_start: Timestamp;

    }

.. http:post:: /management/wire/disable

  This request will be used to disable the use of the given wire method.
  We use POST instead of DELETE because the exchange will retain state
  about the wire method (specifically the end date) to prevent replay
  attacks abusing the `WireSetupMessage`.  Also, DELETE would not
  support a body, which is needed to provide the signature authorizing
  the operation.

  **Request:**

  The request must be a `WireTeardownMessage`.

  **Response**

  :http:statuscode:`204 No content`:
    The auditor has successfully disabled the wire method. The body is empty.
  :http:statuscode:`403 Forbidden`:
    The signature is invalid.
  :http:statuscode:`404 Not found`:
    The wire method is unknown to the exchange.
  :http:statuscode:`409 Conflict`:
    The exchange has a more recent request related to this wire method (replay detected).

  **Details:**

  .. ts:def:: WireTeardownMessage

    interface WireTeardownMessage {

      // payto:// URL identifying the account and wire method
      payto_uri: string;

      // Signature using the exchange's offline key
      // with purpose TALER_SIGNATURE_MASTER_WIRE_DEL.
      master_sig_del: EddsaSignature;

      // Should be the time when the signature was created,
      // using the (monotonic!) local time of the system
      // with the offline master public key.  Note that
      // even if the time is in the future, the wire method will
      // become inactive immediately! Used ONLY to detect replay attacks.
      validity_end: Timestamp;

    }


---------------
Auditor actions
---------------

.. _auditor_action:

This part of the API is for the use by auditors interacting with the exchange.


.. http:post:: /auditors/$AUDITOR_PUB/$H_DENOM_PUB

  This is used to add an auditor signature to the ``/keys`` response. It
  affirms to wallets and merchants that this auditor is indeed auditing
  the coins issued by the respective denomination.  There is no "delete"
  operation for this, as auditors can only stop auditing a denomination
  when it expires.

  **Request:**

  The request must be a `AuditorSignatureAddMessage`.

  **Response:**

  :http:statuscode:`204 No content`:
    The backend has successfully stored the auditor signature.
  :http:statuscode:`403 Forbidden`:
    The auditor signature is invalid.
  :http:statuscode:`404 Not found`:
    The denomination key for which the auditor is providing a signature is unknown.
  :http:statuscode:`410 Gone`:
    This auditor is no longer supported by the exchange.
  :http:statuscode:`412 Precondition failed`:
    This auditor is not yet known to the exchange.

  **Details:**

  .. ts:def:: AuditorSignatureAddMessage

    interface AuditorSignatureAddMessage {

      // Signature by the auditor.
      // Must have purpose TALER_SIGNATURE_AUDITOR_XXX.
      auditor_sig: EddsaSignature;

    }


----------
Withdrawal
----------

This API is used by the wallet to obtain digital coins.

When transferring money to the exchange such as via SEPA transfers, the exchange creates
a *reserve*, which keeps the money from the customer.  The customer must
specify an EdDSA reserve public key as part of the transfer, and can then
withdraw digital coins using the corresponding private key.  All incoming and
outgoing transactions are recorded under the corresponding public key by the
exchange.

.. note::

   Eventually the exchange will need to advertise a policy for how long it will
   keep transaction histories for inactive or even fully drained reserves.  We
   will therefore need some additional handler similar to ``/keys`` to
   advertise those terms of service.


.. http:get:: /reserves/$RESERVE_PUB

  Request information about a reserve.

  .. note::
    The client currently does not have to demonstrate knowledge of the private
    key of the reserve to make this request, which makes the reserve's public
    key privileged information known only to the client, their bank, and the
    exchange.  In future, we might wish to revisit this decision to improve
    security, such as by having the client EdDSA-sign an ECDHE key to be used
    to derive a symmetric key to encrypt the response.  This would be useful if
    for example HTTPS were not used for communication with the exchange.

  **Request:**

  **Response:**

  :http:statuscode:`200 OK`:
    The exchange responds with a `ReserveStatus` object;  the reserve was known to the exchange,
  :http:statuscode:`404 Not found`:
    The reserve key does not belong to a reserve known to the exchange.

  **Details:**

  .. ts:def:: ReserveStatus

    interface ReserveStatus {
      // Balance left in the reserve.
      balance: Amount;

      // Transaction history for this reserve
      history: TransactionHistoryItem[];
    }

  Objects in the transaction history have the following format:

  .. ts:def:: TransactionHistoryItem

    // Union discriminated by the "type" field.
    type ReserveTransaction =
      | ReserveWithdrawTransaction
      | ReserveCreditTransaction
      | ReserveClosingTransaction
      | ReserveRecoupTransaction;

  .. ts:def:: ReserveWithdrawTransaction

    interface ReserveWithdrawTransaction {
      type: "WITHDRAW";

      // Amount withdrawn.
      amount: Amount;

      // Hash of the denomination public key of the coin.
      h_denom_pub: HashCode;

      // Hash of the blinded coin to be signed
      h_coin_envelope: HashCode;

      // Signature of 'TALER_WithdrawRequestPS' created with the reserves's
      // private key.
      reserve_sig: EddsaSignature;

      // Fee that is charged for withdraw.
      withdraw_fee: Amount;
     }


  .. ts:def:: ReserveCreditTransaction

    interface ReserveCreditTransaction {
      type: "CREDIT";

      // Amount deposited.
      amount: Amount;

      // Sender account payto://-URL
      sender_account_url: string;

      // Opaque identifier internal to the exchange that
      // uniquely identifies the wire transfer that credited the reserve.
      wire_reference: Base32;

      // Timestamp of the incoming wire transfer.
      timestamp: Timestamp;
    }


  .. ts:def:: ReserveClosingTransaction

    interface ReserveClosingTransaction {
      type: "CLOSING";

      // Closing balance.
      amount: Amount;

      // Closing fee charged by the exchange.
      closing_fee: Amount;

      // Wire transfer subject.
      wtid: string;

      // payto:// URI of the wire account into which the funds were returned to.
      receiver_account_details: string;

      // This is a signature over a
      // struct TALER_ReserveCloseConfirmationPS with purpose
      // TALER_SIGNATURE_EXCHANGE_RESERVE_CLOSED.
      exchange_sig: EddsaSignature;

      // Public key used to create exchange_sig.
      exchange_pub: EddsaPublicKey;

      // Time when the reserve was closed.
      timestamp: Timestamp;
    }


  .. ts:def:: ReserveRecoupTransaction

    interface ReserveRecoupTransaction {
      type: "RECOUP";

      // Amount paid back.
      amount: Amount;

      // This is a signature over
      // a struct TALER_PaybackConfirmationPS with purpose
      // TALER_SIGNATURE_EXCHANGE_CONFIRM_PAYBACK.
      exchange_sig: EddsaSignature;

      // Public key used to create exchange_sig.
      exchange_pub: EddsaPublicKey;

      // Time when the funds were paid back into the reserve.
      timestamp: Timestamp;

      // Public key of the coin that was paid back.
      coin_pub: CoinPublicKey;
    }

.. http:post:: /reserves/$RESERVE_PUB/withdraw

  Withdraw a coin of the specified denomination.  Note that the client should
  commit all of the request details, including the private key of the coin and
  the blinding factor, to disk *before* issuing this request, so that it can
  recover the information if necessary in case of transient failures, like
  power outage, network outage, etc.

  **Request:** The request body must be a `WithdrawRequest` object.

  **Response:**

  :http:statuscode:`200 OK`:
    The request was successful, and the response is a `WithdrawResponse`.  Note that repeating exactly the same request
    will again yield the same response, so if the network goes down during the
    transaction or before the client can commit the coin signature to disk, the
    coin is not lost.
  :http:statuscode:`403 Forbidden`:
    The signature is invalid.
  :http:statuscode:`404 Not found`:
    The denomination key or the reserve are not known to the exchange.  If the
    denomination key is unknown, this suggests a bug in the wallet as the
    wallet should have used current denomination keys from ``/keys``.  If the
    reserve is unknown, the wallet should not report a hard error yet, but
    instead simply wait for up to a day, as the wire transaction might simply
    not yet have completed and might be known to the exchange in the near future.
    In this case, the wallet should repeat the exact same request later again
    using exactly the same blinded coin.
  :http:statuscode:`409 Conflict`:
    The balance of the reserve is not sufficient to withdraw a coin of the indicated denomination.
    The response is `WithdrawError` object.
  :http:statuscode:`410 Gone`:
    The requested denomination key is no longer valid. It either expired or was revoked.
  :http:statuscode:`412 Precondition failed`:
    The requested denomination key is not yet valid. It is too early to withdraw.

  **Details:**

  .. ts:def:: WithdrawRequest

    interface WithdrawRequest {
      // Hash of a denomination public key (RSA), specifying the type of coin the client
      // would like the exchange to create.
      denom_pub_hash: HashCode;

      // coin's blinded public key, should be (blindly) signed by the exchange's
      // denomination private key
      coin_ev: CoinEnvelope;

      // Signature of `TALER_WithdrawRequestPS` created with the `reserves's private key <reserve-priv>`
      reserve_sig: EddsaSignature;

    }


  .. ts:def:: WithdrawResponse

    interface WithdrawResponse {
      // The blinded RSA signature over the ``coin_ev``, affirms the coin's
      // validity after unblinding.
      ev_sig: BlindedRsaSignature;

    }

  .. ts:def:: WithdrawError

    interface WithdrawError {
      // Text describing the error
      hint: string;

      // Detailed error code
      code: Integer;

      // Amount left in the reserve
      balance: Amount;

      // History of the reserve's activity, in the same format as returned by /reserve/status.
      history: TransactionHistoryItem[]
    }

.. _deposit-par:

-------
Deposit
-------

Deposit operations are requested by a merchant during a transaction. For the
deposit operation, the merchant has to obtain the deposit permission for a coin
from their customer who owns the coin.  When depositing a coin, the merchant is
credited an amount specified in the deposit permission, possibly a fraction of
the total coin's value, minus the deposit fee as specified by the coin's
denomination.


.. _deposit:

.. http:POST:: /coins/$COIN_PUB/deposit

  Deposit the given coin and ask the exchange to transfer the given :ref:`amount`
  to the merchants bank account.  This API is used by the merchant to redeem
  the digital coins.

  The base URL for "/coins/"-requests may differ from the main base URL of the
  exchange. The exchange MUST return a 307 or 308 redirection to the correct
  base URL if this is the case.

  The request should contain a JSON object with the
  following fields:

  **Request:** The request body must be a `DepositRequest` object.

  **Response:**

  :http:statuscode:`200 OK`:
    The operation succeeded, the exchange confirms that no double-spending took
    place.  The response will include a `DepositSuccess` object.
  :http:statuscode:`401 Unauthorized`:
    One of the signatures is invalid.
  :http:statuscode:`404 Not found`:
    Either the denomination key is not recognized (expired or invalid) or
    the wire type is not recognized.
  :http:statuscode:`409 Conflict`:
    The deposit operation has either failed because the coin has insufficient
    residual value, or because the same public key of the coin has been
    previously used with a different denomination.  Which case it is
    can be decided by looking at the error code
    (``TALER_EC_EXCHANGE_DEPOSIT_INSUFFICIENT_FUNDS`` or ``TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY``).
    The fields of the response are the same in both cases.
    The request should not be repeated again with this coin.
    In this case, the response is a `DepositDoubleSpendError`.

  **Details:**

  .. ts:def:: DepositRequest

    interface DepositRequest {
      // Amount to be deposited, can be a fraction of the
      // coin's total value.
      contribution: Amount;

      // The merchant's account details. This must be a JSON object whose format
      // must correspond to one of the supported wire transfer formats of the exchange.
      // See `wireformats`.
      wire: object;

      // SHA-512 hash of the merchant's payment details from ``wire``.  Although
      // strictly speaking redundant, this helps detect inconsistencies.
      h_wire: HashCode;

      // SHA-512 hash of the contact of the merchant with the customer.  Further
      // details are never disclosed to the exchange.
      h_contract_terms: HashCode;

      // Hash of denomination RSA key with which the coin is signed
      denom_pub_hash: HashCode;

      // exchange's unblinded RSA signature of the coin
      ub_sig: RsaSignature;

      // timestamp when the contract was finalized.
      timestamp: Timestamp;

      // indicative time by which the exchange undertakes to transfer the funds to
      // the merchant, in case of successful payment.
      wire_deadline: Timestamp;

      // EdDSA `public key of the merchant <merchant-pub>`, so that the client can identify the
      // merchant for refund requests.
      merchant_pub: EddsaPublicKey;

      // date until which the merchant can issue a refund to the customer via the
      // exchange, possibly zero if refunds are not allowed.
      refund_deadline: Timestamp;

      // Signature of `TALER_DepositRequestPS`, made by the customer with the
      // `coin's private key <coin-priv>`
      coin_sig: EddsaSignature;
    }

  The deposit operation succeeds if the coin is valid for making a deposit and
  has enough residual value that has not already been deposited or melted.

  .. ts:def:: DepositSuccess

     interface DepositSuccess {
      // Optional base URL of the exchange for looking up wire transfers
      // associated with this transaction.  If not given,
      // the base URL is the same as the one used for this request.
      // Can be used if the base URL for /transactions/ differs from that
      // for /coins/, i.e. for load balancing.  Clients SHOULD
      // respect the transaction_base_url if provided.  Any HTTP server
      // belonging to an exchange MUST generate a 307 or 308 redirection
      // to the correct base URL should a client uses the wrong base
      // URL, or if the base URL has changed since the deposit.
      transaction_base_url?: string;

      // timestamp when the deposit was received by the exchange.
      exchange_timestamp: Timestamp;

      // the EdDSA signature of `TALER_DepositConfirmationPS` using a current
      // `signing key of the exchange <sign-key-priv>` affirming the successful
      // deposit and that the exchange will transfer the funds after the refund
      // deadline, or as soon as possible if the refund deadline is zero.
      exchange_sig: EddsaSignature;

      // `public EdDSA key of the exchange <sign-key-pub>` that was used to
      // generate the signature.
      // Should match one of the exchange's signing keys from /keys.  It is given
      // explicitly as the client might otherwise be confused by clock skew as to
      // which signing key was used.
      exchange_pub: EddsaPublicKey;
    }

  .. ts:def:: DepositDoubleSpendError

    interface DepositDoubleSpendError {
      // The string constant "insufficient funds"
      hint: string;

      // Transaction history for the coin that is
      // being double-spended
      history: CoinSpendHistoryItem[];
    }

  .. ts:def:: CoinSpendHistoryItem

    interface CoinSpendHistoryItem {
      // Either "DEPOSIT", "MELT", "REFUND", "RECOUP",
      // "OLD-COIN-RECOUP" or "RECOUP-REFRESH"
      type: string;

      // The total amount of the coin's value absorbed (or restored in the
      // case of a refund) by this transaction.
      // Note that for deposit and melt this means the amount given includes
      // the transaction fee, while for refunds the amount given excludes
      // the transaction fee. The current coin value can thus be computed by
      // subtracting deposit and melt amounts and adding refund amounts from
      // the coin's denomination value.
      amount: Amount;

      // Deposit fee in case of type "DEPOSIT".
      deposit_fee: Amount;

      // public key of the merchant, for "DEPOSIT" operations.
      merchant_pub?: EddsaPublicKey;

      // date when the operation was made.
      // Only for "DEPOSIT", "RECOUP", "OLD-COIN-RECOUP" and
      // "RECOUP-REFRESH" operations.
      timestamp?: Timestamp;

      // date until which the merchant can issue a refund to the customer via the
      // exchange, possibly zero if refunds are not allowed. Only for "DEPOSIT" operations.
      refund_deadline?: Timestamp;

      // Signature by the coin, only present if ``type`` is "DEPOSIT", "MELT", "RECOUP",
      // or "RECOUP-REFRESH"
      coin_sig?: EddsaSignature;

      // Deposit fee in case of type "MELT".
      melt_fee: Amount;

      // Commitment from the melt operation, only for "MELT".
      rc?: TALER_RefreshCommitmentP;

      // Hash of the bank account from where we received the funds,
      // only present if ``type`` is "DEPOSIT"
      h_wire?: HashCode;

      // Hash of the public denomination key used to sign the coin.
      // only present if ``type`` is "DEPOSIT", "RECOUP",
      // "RECOUP-REFRESH", or "MELT".
      h_denom_pub?: HashCode;

      // Deposit fee in case of type "REFUND".
      refund_fee?: Amount;

      // Hash over the proposal data of the contract that
      // is being paid (if type is "DEPOSIT") or refunded (if
      // ``type`` is "REFUND"); otherwise absent.
      h_contract_terms?: HashCode;

      // Refund transaction ID.  Only present if ``type`` is
      // "REFUND"
      rtransaction_id?: Integer;

      // Coin blinding key.  Only present if ``type`` is
      // "RECOUP" or "RECOUP-REFRESH"
      coin_blind?: DenominationBlindingKeyP;

      // Reserve receiving the recoup.  Only present if ``type`` is
      // "RECOUP"
      reserve_pub?: EddsaPublicKey;

      // `EdDSA Signature <eddsa-sig>` authorizing the REFUND. Made with
      // the `public key of the merchant <merchant-pub>`.
      // Only present if ``type`` is "REFUND"
      merchant_sig?: EddsaSignature;

      // public key of the reserve that will receive the funds, for "RECOUP" operations.
      reserve_pub?: EddsaPublicKey;

      // Signature by the exchange, only present if ``type`` is "RECOUP",
      // "OLD-COIN-RECOUP" or "RECOUP-REFRESH".  Signature is
      // of type TALER_SIGNATURE_EXCHANGE_CONFIRM_RECOUP for "RECOUP",
      // and of type TALER_SIGNATURE_EXCHANGE_CONFIRM_RECOUP_REFRESH otherwise.
      exchange_sig?: EddsaSignature;

      // public key used to sign ``exchange_sig``,
      // only present if ``exchange_sig`` present.
      exchange_pub?: EddsaPublicKey;

      // Blinding factor of the revoked new coin,
      // only present if ``type`` is "REFRESH_RECOUP".
      new_coin_blinding_secret: RsaBlindingKeySecret;

      // Blinded public key of the revoked new coin,
      // only present if ``type`` is "REFRESH_RECOUP".
      new_coin_ev: RsaBlindingKeySecret;
    }

----------
Refreshing
----------

Refreshing creates ``n`` new coins from ``m`` old coins, where the sum of
denominations of the new coins must be smaller than the sum of the old coins'
denominations plus melting (refresh) and withdrawal fees charged by the exchange.
The refreshing API can be used by wallets to melt partially spent coins, making
transactions with the freshly exchangeed coins unlinkabe to previous transactions
by anyone except the wallet itself.

However, the new coins are linkable from the private keys of all old coins
using the /refresh/link request.  While /refresh/link must be implemented by
the exchange to achieve taxability, wallets do not really ever need that part of
the API during normal operation.

.. _refresh:
.. http:post:: /coins/$COIN_PUB/melt

  "Melts" a coin.  Invalidates the coins and prepares for exchangeing of fresh
  coins.  Taler uses a global parameter ``kappa`` for the cut-and-choose
  component of the protocol, for which this request is the commitment.  Thus,
  various arguments are given ``kappa``-times in this step.  At present ``kappa``
  is always 3.

  The base URL for "/coins/"-requests may differ from the main base URL of the
  exchange. The exchange MUST return a 307 or 308 redirection to the correct
  base URL if this is the case.

  :http:statuscode:`200 OK`:
    The request was successful.  The response body is `MeltResponse` in this case.
  :http:statuscode:`403 Forbidden`:
    One of the signatures is invalid.
  :http:statuscode:`404 Not found`:
    the exchange does not recognize the denomination key as belonging to the exchange,
    or it has expired
  :http:statuscode:`409 Conflict`:
    The operation is not allowed as the coin has insufficient
    residual value, or because the same public key of the coin has been
    previously used with a different denomination.  Which case it is
    can be decided by looking at the error code
    (``TALER_EC_EXCHANGE_MELT_INSUFFICIENT_FUNDS`` or ``TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY``).
    The response is `MeltForbiddenResponse` in both cases.

  **Details:**


  .. ts:def:: MeltRequest

    interface MeltRequest {

      // Hash of the denomination public key, to determine total coin value.
      denom_pub_hash: HashCode;

      // Signature over the `coin public key <eddsa-coin-pub>` by the denomination.
      denom_sig: RsaSignature;

      // Signature by the `coin <coin-priv>` over the melt commitment.
      confirm_sig: EddsaSignature;

      // Amount of the value of the coin that should be melted as part of
      // this refresh operation, including melting fee.
      value_with_fee: Amount;

      // Melt commitment.  Hash over the various coins to be withdrawn.
      // See also ``TALER_refresh_get_commitment()``
      rc: TALER_RefreshCommitmentP;

    }

  For details about the HKDF used to derive the new coin private keys and
  the blinding factors from ECDHE between the transfer public keys and
  the private key of the melted coin, please refer to the
  implementation in ``libtalerutil``.

  .. ts:def:: MeltResponse

    interface MeltResponse {
      // Which of the ``kappa`` indices does the client not have to reveal.
      noreveal_index: number;

      // Signature of `TALER_RefreshMeltConfirmationPS` whereby the exchange
      // affirms the successful melt and confirming the ``noreveal_index``
      exchange_sig: EddsaSignature;

      // `public EdDSA key <sign-key-pub>` of the exchange that was used to generate the signature.
      // Should match one of the exchange's signing keys from /keys.  Again given
      // explicitly as the client might otherwise be confused by clock skew as to
      // which signing key was used.
      exchange_pub: EddsaPublicKey;

      // Base URL to use for operations on the refresh context
      // (so the reveal operation).  If not given,
      // the base URL is the same as the one used for this request.
      // Can be used if the base URL for /refreshes/ differs from that
      // for /coins/, i.e. for load balancing.  Clients SHOULD
      // respect the refresh_base_url if provided.  Any HTTP server
      // belonging to an exchange MUST generate a 307 or 308 redirection
      // to the correct base URL should a client uses the wrong base
      // URL, or if the base URL has changed since the melt.
      //
      // When melting the same coin twice (technically allowed
      // as the response might have been lost on the network),
      // the exchange may return different values for the refresh_base_url.
      refresh_base_url?: string;

    }


  .. ts:def:: MeltForbiddenResponse

    interface MeltForbiddenResponse {
      // Text describing the error.
      hint: string;

      // Detailed error code
      code: Integer;

      // public key of a melted coin that had insufficient funds
      coin_pub: EddsaPublicKey;

      // original total value of the coin
      original_value: Amount;

      // remaining value of the coin
      residual_value: Amount;

      // amount of the coin's value that was to be melted
      requested_value: Amount;

      // The transaction list of the respective coin that failed to have sufficient funds left.
      // Note that only the transaction history for one bogus coin is given,
      // even if multiple coins would have failed the check.
      history: CoinSpendHistoryItem[];
    }


.. http:post:: /refreshes/$RCH/reveal

  Reveal previously committed values to the exchange, except for the values
  corresponding to the ``noreveal_index`` returned by the /coins/-melt step.

  The $RCH is the hash over the refresh commitment from the /coins/-melt step
  (note that the value is calculated independently by both sides and has never
  appeared *explicitly* in the protocol before).

  The base URL for "/refreshes/"-requests may differ from the main base URL of
  the exchange. Clients SHOULD respect the "refresh_base_url" returned for the
  coin during melt operations. The exchange MUST return a
  307 or 308 redirection to the correct base URL if the client failed to
  respect the "refresh_base_url" or if the allocation has changed.

  Errors such as failing to do proper arithmetic when it comes to calculating
  the total of the coin values and fees are simply reported as bad requests.
  This includes issues such as melting the same coin twice in the same session,
  which is simply not allowed.  However, theoretically it is possible to melt a
  coin twice, as long as the ``value_with_fee`` of the two melting operations is
  not larger than the total remaining value of the coin before the melting
  operations. Nevertheless, this is not really useful.

  :http:statuscode:`200 OK`:
    The transfer private keys matched the commitment and the original request was well-formed.
    The response body is a `RevealResponse`
  :http:statuscode:`409 Conflict`:
    There is a problem between the original commitment and the revealed private
    keys.  The returned information is proof of the mismatch, and therefore
    rather verbose, as it includes most of the original /refresh/melt request,
    but of course expected to be primarily used for diagnostics.
    The response body is a `RevealConflictResponse`.

  **Details:**

  Request body contains a JSON object with the following fields:

  .. ts:def:: RevealRequest

    interface RevealRequest {

      // Array of ``n`` new hash codes of denomination public keys to order.
      new_denoms_h: HashCode[];

      // Array of ``n`` entries with blinded coins,
      // matching the respective entries in ``new_denoms``.
      coin_evs: CoinEnvelope[];

      // ``kappa - 1`` transfer private keys (ephemeral ECDHE keys)
      transfer_privs: EddsaPrivateKey[];

      // transfer public key at the ``noreveal_index``.
      transfer_pub: EddsaPublicKey;

      // Array of ``n`` signatures made by the wallet using the old coin's private key,
      // used later to verify the /refresh/link response from the exchange.
      // Signs over a `TALER_CoinLinkSignaturePS`
      link_sigs: EddsaSignature[];

    }


  .. ts:def:: RevealResponse

    interface RevealResponse {
      // List of the exchange's blinded RSA signatures on the new coins.
      ev_sigs : Array<{ ev_sig: BlindedRsaSignature }>;
    }


  .. ts:def:: RevealConflictResponse

    interface RevealConflictResponse {
      // Text describing the error
      hint: string;

      // Detailed error code
      code: Integer;

      // Commitment as calculated by the exchange from the revealed data.
      rc_expected: HashCode;

    }


.. http:get:: /coins/$COIN_PUB/link

  Link the old public key of a melted coin to the coin(s) that were exchangeed during the refresh operation.

  **Request:**

  **Response:**

  :http:statuscode:`200 OK`:
    All commitments were revealed successfully.  The exchange returns an array,
    typically consisting of only one element, in which each each element contains
    information about a melting session that the coin was used in.
  :http:statuscode:`404 Not found`:
    The exchange has no linkage data for the given public key, as the coin has not
    yet been involved in a refresh operation.

  **Details:**

  .. ts:def:: LinkResponse

    interface LinkResponse {
      // transfer ECDHE public key corresponding to the ``coin_pub``, used to
      // compute the blinding factor and private key of the fresh coins.
      transfer_pub: EcdhePublicKey;

      // array with (encrypted/blinded) information for each of the coins
      // exchangeed in the refresh operation.
      new_coins: NewCoinInfo[];
    }

  .. ts:def:: NewCoinInfo

    interface NewCoinInfo {
      // RSA public key of the exchangeed coin.
      denom_pub: RsaPublicKey;

      // Exchange's blinded signature over the fresh coin.
      ev_sig: BlindedRsaSignature;

      // Blinded coin.
      coin_ev : CoinEnvelope;

      // Signature made by the old coin over the refresh request.
      // Signs over a `TALER_CoinLinkSignaturePS`
      link_sig: EddsaSignature;
    }


------
Recoup
------

This API is only used if the exchange is either about to go out of
business or has had its private signing keys compromised (so in
either case, the protocol is only used in **abnormal**
situations).  In the above cases, the exchange signals to the
wallets that the emergency cash back protocol has been activated
by putting the affected denomination keys into the cash-back
part of the /keys response.  If and only if this has happened,
coins that were signed with those denomination keys can be cashed
in using this API.

.. http:post:: /coins/$COIN_PUB/recoup

  Demand that a coin be refunded via wire transfer to the original owner.

  The base URL for "/coins/"-requests may differ from the main base URL of the
  exchange. The exchange MUST return a 307 or 308 redirection to the correct
  base URL if this is the case.

  Depending whether ``$COIN_PUB`` is a withdrawn coin or a refreshed coin,
  the remaining amount on the coin will be credited either on the reserve or
  the old coin that ``$COIN_PUB`` was withdrawn/refreshed from.

  Note that the original withdrawal/refresh fees will **not** be recouped.


  **Request:** The request body must be a `RecoupRequest` object.

  **Response:**

  :http:statuscode:`200 OK`:
    The request was successful, and the response is a `RecoupConfirmation`.
    Note that repeating exactly the same request
    will again yield the same response, so if the network goes down during the
    transaction or before the client can commit the coin signature to disk, the
    coin is not lost.
  :http:statuscode:`401 Unauthorized`:
    The coin's signature is invalid.
  :http:statuscode:`404 Not found`:
    The denomination key is not in the set of denomination
    keys where emergency pay back is enabled, or the blinded
    coin is not known to have been withdrawn.
  :http:statuscode:`409 Conflict`:
    The operation is not allowed as the coin has insufficient
    residual value, or because the same public key of the coin has been
    previously used with a different denomination.  Which case it is
    can be decided by looking at the error code
    (``TALER_EC_EXCHANGE_RECOUP_COIN_BALANCE_ZERO`` or ``TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY``).
    The response is a `DepositDoubleSpendError`.

  **Details:**

  .. ts:def:: RecoupRequest

    interface RecoupRequest {
      // Hash of denomination public key (RSA), specifying the type of coin the client
      // would like the exchange to pay back.
      denom_pub_hash: HashCode;

      // Signature over the `coin public key <eddsa-coin-pub>` by the denomination.
      denom_sig: RsaSignature;

      // coin's blinding factor
      coin_blind_key_secret: RsaBlindingKeySecret;

      // Signature of `TALER_RecoupRequestPS` created with the `coin's private key <coin-priv>`
      coin_sig: EddsaSignature;

      // Was the coin refreshed (and thus the recoup should go to the old coin)?
      // While this information is technically redundant, it helps the exchange
      // to respond faster.
      // *Optional* (for backwards compatibility); if absent, "false" is assumed
      refreshed?: boolean;
    }


  .. ts:def:: RecoupConfirmation

    type RecoupConfirmation = | RecoupRefreshConfirmation
                              | RecoupWithdrawalConfirmation;

  .. ts:def:: RecoupWithdrawalConfirmation

    interface RecoupWithdrawalConfirmation {
      // Tag to distinguish the RecoupConfirmation response type
      refreshed: false;
      // public key of the reserve that will receive the recoup,
      reserve_pub: EddsaPublicKey;
    }

  .. ts:def:: RecoupRefreshConfirmation

    interface RecoupRefreshConfirmation {
      // Tag to distinguish the RecoupConfirmation response type
      refreshed: true;
      // public key of the old coin that will receive the recoup
      old_coin_pub: EddsaPublicKey;
    }


-----------------------
Tracking wire transfers
-----------------------

This API is used by merchants that need to find out which wire
transfers (from the exchange to the merchant) correspond to which deposit
operations.  Typically, a merchant will receive a wire transfer with a
**wire transfer identifier** and want to know the set of deposit
operations that correspond to this wire transfer.  This is the
preferred query that merchants should make for each wire transfer they
receive.  If a merchant needs to investigate a specific deposit
operation (i.e. because it seems that it was not paid), then the
merchant can also request the wire transfer identifier for a deposit
operation.

Sufficient information is returned to verify that the coin signatures
are correct. This also allows governments to use this API when doing
a tax audit on merchants.

Naturally, the returned information may be sensitive for the merchant.
We do not require the merchant to sign the request, as the same requests
may also be performed by the government auditing a merchant.
However, wire transfer identifiers should have sufficient entropy to
ensure that obtaining a successful reply by brute-force is not practical.
Nevertheless, the merchant should protect the wire transfer identifiers
from his bank statements against unauthorized access, least his income
situation is revealed to an adversary. (This is not a major issue, as
an adversary that has access to the line-items of bank statements can
typically also view the balance.)


.. http:get:: /transfers/$WTID

  Provides deposits associated with a given wire transfer.  The
  wire transfer identifier (WTID) and the base URL for tracking
  the wire transfer are both given in the wire transfer subject.

  **Request:**

  **Response:**

  :http:statuscode:`200 OK`:
    The wire transfer is known to the exchange, details about it follow in the body.
    The body of the response is a `TrackTransferResponse`.
  :http:statuscode:`404 Not found`:
    The wire transfer identifier is unknown to the exchange.

  .. ts:def:: TrackTransferResponse

    interface TrackTransferResponse {
      // Actual amount of the wire transfer, excluding the wire fee.
      total: Amount;

      // Applicable wire fee that was charged
      wire_fee: Amount;

      // public key of the merchant (identical for all deposits)
      merchant_pub: EddsaPublicKey;

      // hash of the wire details (identical for all deposits)
      h_wire: HashCode;

      // Time of the execution of the wire transfer by the exchange
      execution_time: Timestamp;

      // details about the deposits
      deposits: TrackTransferDetail[];

      // signature from the exchange made with purpose
      // `TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE_DEPOSIT`
      exchange_sig: EddsaSignature;

      // public EdDSA key of the exchange that was used to generate the signature.
      // Should match one of the exchange's signing keys from /keys.  Again given
      // explicitly as the client might otherwise be confused by clock skew as to
      // which signing key was used.
      exchange_pub: EddsaSignature;
    }

  .. ts:def:: TrackTransferDetail

    interface TrackTransferDetail {
      // SHA-512 hash of the contact of the merchant with the customer.
      h_contract_terms: HashCode;

      // coin's public key, both ECDHE and EdDSA.
      coin_pub: CoinPublicKey;

      // The total amount the original deposit was worth.
      deposit_value: Amount;

      // applicable fees for the deposit
      deposit_fee: Amount;

    }

.. http:get:: /deposits/$H_WIRE/$MERCHANT_PUB/$H_CONTRACT_TERMS/$COIN_PUB

  Provide the wire transfer identifier associated with an (existing) deposit operation.
  The arguments are the hash of the merchant's payment details (H_WIRE), the
  merchant's public key (EdDSA), the hash of the contract terms that were paid
  (H_CONTRACT_TERMS) and the public key of the coin used for the payment (COIN_PUB).

  **Request:**

  :query merchant_sig: EdDSA signature of the merchant made with purpose ``TALER_SIGNATURE_MERCHANT_TRACK_TRANSACTION`` , affirming that it is really the merchant who requires obtaining the wire transfer identifier.

  **Response:**

  :http:statuscode:`200 OK`:
    The deposit has been executed by the exchange and we have a wire transfer identifier.
    The response body is a `TrackTransactionResponse` object.
  :http:statuscode:`202 Accepted`:
    The deposit request has been accepted for processing, but was not yet
    executed.  Hence the exchange does not yet have a wire transfer identifier.  The
    merchant should come back later and ask again.
    The response body is a `TrackTransactionAcceptedResponse`.
  :http:statuscode:`401 Unauthorized`:
    The signature is invalid.
  :http:statuscode:`404 Not found`:
    The deposit operation is unknown to the exchange

  **Details:**

  .. ts:def:: TrackTransactionResponse

    interface TrackTransactionResponse {
      // raw wire transfer identifier of the deposit.
      wtid: Base32;

      // when was the wire transfer given to the bank.
      execution_time: Timestamp;

      // The contribution of this coin to the total (without fees)
      coin_contribution: Amount;

      // Total amount transferred
      total_amount: Amount;

      // binary-only Signature_ for purpose `TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE`
      // whereby the exchange affirms the successful wire transfer.
      exchange_sig: EddsaSignature;

      // public EdDSA key of the exchange that was used to generate the signature.
      // Should match one of the exchange's signing keys from /keys.  Again given
      // explicitly as the client might otherwise be confused by clock skew as to
      // which signing key was used.
      exchange_pub: EddsaPublicKey;
    }

  .. ts:def:: TrackTransactionAcceptedResponse

    interface TrackTransactionAcceptedResponse {
      // time by which the exchange currently thinks the deposit will be executed.
      execution_time: Timestamp;
    }


.. _exchange_refund:

-------
Refunds
-------

.. http:POST:: /coins/$COIN_PUB/refund

  Undo deposit of the given coin, restoring its value.

  **Request:** The request body must be a `RefundRequest` object.

  **Response:**

  :http:statuscode:`200 OK`:
    The operation succeeded, the exchange confirms that the coin can now be refreshed.  The response will include a `RefundSuccess` object.
  :http:statuscode:`401 Unauthorized`:
    Merchant signature is invalid.
    This response comes with a standard `ErrorDetail` response.
  :http:statuscode:`404 Not found`:
    The refund operation failed as we could not find a matching deposit operation (coin, contract, transaction ID and merchant public key must all match).
    This response comes with a standard `ErrorDetail` response.
  :http:statuscode:`409 Conflict`:
    The exchange has previously received a refund request for the same coin, merchant and contract, but specifying a different amount for the same refund transaction ID.  The response will be a `RefundFailure` object.
  :http:statuscode:`410 Gone`:
    It is too late for a refund by the exchange, the money was already sent to the merchant.
    This response comes with a standard `ErrorDetail` response.
  :http:statuscode:`412 Precondition failed`:
    The request transaction ID is identical to a previous refund request by the same
    merchant for the same coin and contract, but the refund amount differs. (The
    failed precondition is that the ``rtransaction_id`` is not unique.)
    The response will be a `RefundFailure` object with the conflicting refund request.

  **Details:**

  .. ts:def:: RefundRequest

     interface RefundRequest {

      // Amount to be refunded, can be a fraction of the
      // coin's total deposit value (including deposit fee);
      // must be larger than the refund fee.
      refund_amount: Amount;

      // SHA-512 hash of the contact of the merchant with the customer.
      h_contract_terms: HashCode;

      // 64-bit transaction id of the refund transaction between merchant and customer
      rtransaction_id: number;

      // EdDSA public key of the merchant.
      merchant_pub: EddsaPublicKey;

      // EdDSA signature of the merchant affirming the refund.
      merchant_sig: EddsaPublicKey;

    }

  .. ts:def:: RefundSuccess

    interface RefundSuccess {

      // the EdDSA :ref:`signature` (binary-only) with purpose
      // `TALER_SIGNATURE_EXCHANGE_CONFIRM_REFUND` using a current signing key of the
      // exchange affirming the successful refund
      exchange_sig: EddsaSignature;

      // public EdDSA key of the exchange that was used to generate the signature.
      // Should match one of the exchange's signing keys from /keys.  It is given
      // explicitly as the client might otherwise be confused by clock skew as to
      // which signing key was used.
      exchange_pub: EddsaPublicKey;
   }

  .. ts:def:: RefundFailure

    interface RefundFailure {

      // Numeric error code unique to the condition, which can be either
      // related to the deposit value being insufficient for the requested
      // refund(s), or the requested refund conflicting due to refund
      // transaction number re-use (with different amounts).
      code: number;

      // Human-readable description of the error message
      hint: string;

      // Information about the conflicting refund request(s).
      // This will not be the full history of the coin, but only
      // the relevant subset of the transactions.
      history: CoinSpendHistoryItem[];
    }