diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-09-23 17:16:11 +0200 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-09-23 17:16:11 +0200 |
commit | 1e04bb2610bd142aebeb0fd040e649045ec785df (patch) | |
tree | 220e1afb2d2cd833c57a4e4750d080449368d4a0 /src/backend-lib/taler-merchant-httpd_contract.c | |
parent | 5453309a27e2d3621225ac10785707b02459b51e (diff) | |
download | merchant-1e04bb2610bd142aebeb0fd040e649045ec785df.tar.gz merchant-1e04bb2610bd142aebeb0fd040e649045ec785df.tar.bz2 merchant-1e04bb2610bd142aebeb0fd040e649045ec785df.zip |
adding further columns to the "contracts" table: e.g. "edate",
"nounce", "timestamp". Plus, adding a function for querying the DB
in order to get the contract (nonce,edate) by looking for its hashed
stringification. Plus adjusting accordingly the testcase.
Diffstat (limited to 'src/backend-lib/taler-merchant-httpd_contract.c')
-rw-r--r-- | src/backend-lib/taler-merchant-httpd_contract.c | 142 |
1 files changed, 72 insertions, 70 deletions
diff --git a/src/backend-lib/taler-merchant-httpd_contract.c b/src/backend-lib/taler-merchant-httpd_contract.c index 6a008455..7cfda21c 100644 --- a/src/backend-lib/taler-merchant-httpd_contract.c +++ b/src/backend-lib/taler-merchant-httpd_contract.c @@ -49,14 +49,57 @@ hash_wireformat (uint64_t nounce, const struct MERCHANT_WIREFORMAT_Sepa *wire) } /** + * Take the global wire details and return a JSON containing them, + * compliantly with the Taler's API. + * @param wire the merchant's wire details + * @param nounce the nounce for hashing the wire details with + * @param edate when the beneficiary wants this transfer to take place + * @return JSON representation of the wire details, NULL upon errors + */ + +json_t * +MERCHANT_get_wire_json (const struct MERCHANT_WIREFORMAT_Sepa *wire, + uint64_t nounce, + const struct GNUNET_TIME_Absolute edate) + +{ + + json_t *root; + json_t *j_edate; + json_t *j_nounce; + + nounce = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); + j_nounce = json_integer (nounce); + j_edate = TALER_json_from_abs (edate); + + if (NULL == (root = json_pack ("{s:s, s:s, s:s, s:s, s:o}", + "type", "SEPA", + "IBAN", wire->iban, + "name", wire->name, + "BIC", wire->bic, + "edate", j_edate, + "r", json_integer_value (j_nounce)))) + return NULL; + + return root; +} + + + +/** * Take from the frontend the (partly) generated contract and fill * the missing values in it; for example, the SEPA details. * Moreover, it stores the contract in the DB. * @param j_contract parsed contract, originated by the frontend. It will be * hold the new values. * @param db_conn the handle to the local DB -* @param wire merchant's bank's details * @param contract where to store the (subset of the) contract to be (still) signed +* @param timestamp contract's timestamp (shall be generated by the merchant) +* @param expiry the time when the contract will expire +* @param edate when the merchant wants to receive the wire transfer corresponding +* to this deal (this value is also a field inside the 'wire' JSON format) +* @param nounce the nounce used to hash the wire details +* @param contract_str where to store the (stringified) contract * @return GNUNET_OK on success; GNUNET_SYSERR upon errors */ @@ -65,97 +108,56 @@ hash_wireformat (uint64_t nounce, const struct MERCHANT_WIREFORMAT_Sepa *wire) * still allocated */ uint32_t -MERCHANT_handle_contract (json_t *j_contract, +MERCHANT_handle_contract (const json_t *j_contract, PGconn *db_conn, - const struct MERCHANT_WIREFORMAT_Sepa *wire, - struct Contract *contract) + struct Contract *contract, + struct GNUNET_TIME_Absolute timestamp, + struct GNUNET_TIME_Absolute expiry, + struct GNUNET_TIME_Absolute edate, + uint64_t nounce, + const char *contract_str) { - json_t *j_tmp; - 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_product_id; json_int_t j_trans_id; - uint64_t nounce; - json_t *j_product_id; - json_t *j_items_tmp; - char *a; - #define DaEBUG - #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); - jh_wire = TALER_json_from_data (&h_wire, sizeof (struct GNUNET_HashCode)); - /* adding the generated values in this JSON */ - if (NULL == (j_tmp = json_pack ("{s:o, s:o}", - "h_wire", jh_wire, - "timestamp", j_timestamp))) - return GNUNET_SYSERR; - if (-1 == json_object_update (j_contract, j_tmp)) + + /* Extracting values useful for DB work. Only gettable from the JSON + since they are generated by the frontend */ + if (-1 == json_unpack (j_contract, "{s:o, s:I, s:{s:{s:I}}}", + "amount", &j_amount, + "trans_id", &j_trans_id, + "details", "items", + "product_id", &j_product_id)) + { + printf ("no unpacking\n"); return GNUNET_SYSERR; + } + /* needed for DB work */ - j_amount = json_object_get (j_contract, "amount"); TALER_json_to_amount (j_amount, &amount); // produces a WARNING.. - a = json_dumps (j_contract, JSON_COMPACT | JSON_PRESERVE_ORDER); - - #ifdef DEBUG - str = json_dumps (j_amount, JSON_COMPACT | JSON_PRESERVE_ORDER); - printf ("amount : \n%s", str); - return GNUNET_SYSERR; - #endif - - /* TODO - - Add a further field to the 'contract' table, indicating the timestamp - of this contract being finalized - - */ - - GNUNET_CRYPTO_hash (a, strlen (a) + 1, &contract->h_contract_details); + contract_str = json_dumps (j_contract, JSON_COMPACT | JSON_PRESERVE_ORDER); + GNUNET_CRYPTO_hash (contract_str, strlen (contract_str) + 1, + &contract->h_contract_details); contract->purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_CONTRACT); contract->purpose.size = htonl (sizeof (struct Contract)); // DB mgmt if (GNUNET_SYSERR == MERCHANT_DB_contract_create (db_conn, - ×tamp, + timestamp, + expiry, + edate, &amount, &contract->h_contract_details, (uint64_t) j_trans_id, // safe? - a, + contract_str, nounce, - json_integer_value (j_product_id))) + (uint64_t) j_product_id)) return GNUNET_SYSERR; - free (a); - - #ifdef OBSOLETE - 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? - #endif - - #ifdef OBSOLETE - contract->purpose.size = htonl (sizeof (struct ContractNBO)); - #endif - return GNUNET_OK; } |