aboutsummaryrefslogtreecommitdiff
path: root/src/backend-lib/taler-merchant-httpd_contract.c
blob: 319ca07c1ebaae362eeda533b443e823ee4e59bb (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
#include "platform.h"
#include <jansson.h>
#include <taler/taler_signatures.h>
#include <gnunet/gnunet_util_lib.h>
#include "merchant.h"
#include "merchant_db.h"
#include "taler_merchant_contract_lib.h"


/* TODO: make this file a library, and programmatically call the following
 * functions */

/**
 * Macro to round microseconds to seconds in GNUNET_TIME_* structs.
 */
#define ROUND_TO_SECS(name,us_field) name.us_field -= name.us_field % (1000 * 1000)

/**
 * Shorthand for exit jumps.
 */
#define EXITIF(cond)                                              \
  do {                                                            \
    if (cond) { GNUNET_break (0); goto EXITIF_exit; }             \
  } while (0)

/**
* Generate the hash containing the information (= a nounce + merchant's IBAN) to
* redeem money from mint in a subsequent /deposit operation
* @param nounce the nounce
* @param wire the merchant's wire details
* @return the hash to be included in the contract's blob
*
*/

static struct GNUNET_HashCode
hash_wireformat (uint64_t nounce, const struct MERCHANT_WIREFORMAT_Sepa *wire)
{
  struct GNUNET_HashContext *hc;
  struct GNUNET_HashCode hash;

  hc = GNUNET_CRYPTO_hash_context_start ();
  GNUNET_CRYPTO_hash_context_read (hc, wire->iban, strlen (wire->iban));
  GNUNET_CRYPTO_hash_context_read (hc, wire->name, strlen (wire->name));
  GNUNET_CRYPTO_hash_context_read (hc, wire->bic, strlen (wire->bic));
  nounce = GNUNET_htonll (nounce);
  GNUNET_CRYPTO_hash_context_read (hc, &nounce, sizeof (nounce));
  GNUNET_CRYPTO_hash_context_finish (hc, &hash);
  return hash;
}

/**
* Take from the frontend the (partly) generated contract and fill
* the missing values in it; for example, the SEPA-aware values.
* Moreover, it stores the contract in the DB.
* @param j_contract parsed contract, originated by the frontend
* @param db_conn the handle to the local DB
* @param wire merchant's bank's details
* @param where to store the (subset of the) contract to be (still) signed
* @return pointer to the complete JSON; NULL upon errors
*/

/**
* TODO: inspection of reference counting and, accordingly, free those json_t*(s)
* still allocated */

json_t *
MERCHANT_handle_contract (json_t *j_contract,
                          PGconn *db_conn,
                          const struct MERCHANT_WIREFORMAT_Sepa *wire,
			  struct ContractNBO *contract)
{
  json_t *root;
  json_t *j_details;
  json_t *j_timestamp;
  json_t *jh_wire;
  json_t *j_amount;
  json_t *j_mints;
  json_t *j_max_fee;
  json_int_t j_trans_id;

  uint64_t nounce;
  json_t *j_product_id;
  json_t *j_items_tmp;
  char *a;
  char *h_wire_enc;
  #ifdef DEBUG
  char *str;
  #endif
  struct GNUNET_HashCode h_wire;
  struct GNUNET_TIME_Absolute timestamp;
  struct TALER_Amount amount;
  struct TALER_AmountNBO amount_nbo;

  nounce = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
  // timing mgmt
  timestamp = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
                                     GNUNET_TIME_UNIT_DAYS);
  ROUND_TO_SECS (timestamp, abs_value_us);
  j_timestamp = TALER_json_from_abs (timestamp);

  // wireformat mgmt
  h_wire = hash_wireformat (nounce, wire);
  h_wire_enc = GNUNET_STRINGS_data_to_string_alloc (&h_wire,
                                                    sizeof (struct GNUNET_HashCode));
  jh_wire = json_string (h_wire_enc);
  if (-1 == json_unpack (j_contract, "{s:o, s:o, s:I, s:o, s:o}",
                         "amount", &j_amount,
                         "max fee", &j_max_fee,
                         "trans_id", &j_trans_id,
			 "mints", &j_mints,
                         "details", &j_details))

    return NULL;

  /* needed for DB stuff */
  TALER_json_to_amount (j_amount, &amount);
  j_items_tmp = json_object_get (j_details, "items");
  j_product_id = json_object_get (j_items_tmp, "product_id");

  #ifdef DEBUG
  printf ("prod id is at %p, eval to %d\n", j_product_id, json_integer_value (j_product_id));
  return NULL;
  #endif




  /* adding the generated values in this JSON */
  root = json_pack ("{s:o, s:o, s:I, s:s, s:o, s:o, s:o}",
             "amount", j_amount,
             "max fee", j_max_fee,
	     "trans_id", j_trans_id,
	     "h_wire", jh_wire,
	     "timestamp", j_timestamp,
             "mints", j_mints,
	     "details", j_details);

  a = json_dumps (root, JSON_COMPACT | JSON_PRESERVE_ORDER);

  // DB mgmt
  if (GNUNET_SYSERR == MERCHANT_DB_contract_create (db_conn,
                                                    &timestamp,
                                                    &amount,
 					            (uint64_t) j_trans_id, // safe?
                                                    a,
                                                    nounce,
                                                    json_integer_value (j_product_id)));
  contract->h_wire = h_wire;
  TALER_amount_hton (&amount_nbo, &amount);
  contract->amount = amount_nbo;
  contract->t = GNUNET_TIME_absolute_hton (timestamp);
  contract->m = GNUNET_htonll ((uint64_t) j_trans_id); // safe?
  GNUNET_CRYPTO_hash (a, strlen (a) + 1, &contract->h_contract_details);
  free (a);
  contract->purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_CONTRACT);
  contract->purpose.size = htonl (sizeof (struct ContractNBO));

  return root;
}