From f1d86b7ec238b3b130582cf58d78eb8a25ef17c7 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Sat, 7 Mar 2015 13:56:26 +0100 Subject: [PATCH] db: Implement insert_collectable_blindcoin --- src/mint/mint_db.c | 71 +++++++++++++++++------------------------ src/mint/test_mint_db.c | 57 +++++++++++++++++++++++++++++---- 2 files changed, 81 insertions(+), 47 deletions(-) diff --git a/src/mint/mint_db.c b/src/mint/mint_db.c index ec48c6a80..750ff207b 100644 --- a/src/mint/mint_db.c +++ b/src/mint/mint_db.c @@ -179,9 +179,9 @@ TALER_MINT_DB_create_tables (int temporary) SQLEXEC ("CREATE TABLE IF NOT EXISTS collectable_blindcoins" "(" "blind_ev BYTEA PRIMARY KEY" - ",denom_pub BYTEA NOT NULL" - ",reserve_sig BYTEA NOT NULL" + ",denom_pub BYTEA NOT NULL" /* FIXME: Make this a foreign key? */ ",reserve_pub BYTEA REFERENCES reserves (reserve_pub) ON DELETE CASCADE" + ",reserve_sig BYTEA NOT NULL" ");"); SQLEXEC ("CREATE INDEX collectable_blindcoins_reserve_pub_index ON" " collectable_blindcoins (reserve_pub)"); @@ -330,10 +330,10 @@ TALER_MINT_DB_prepare (PGconn *db_conn) 4, NULL); PREPARE ("insert_collectable_blindcoins", "INSERT INTO collectable_blindcoins ( " - " blind_ev, blind_ev_sig " + " blind_ev" ",denom_pub, reserve_pub, reserve_sig) " - "VALUES ($1, $2, $3, $4, $5)", - 6, NULL); + "VALUES ($1, $2, $3, $4)", + 4, NULL); PREPARE ("get_collectable_blindcoins", "SELECT " "blind_ev_sig, denom_pub, reserve_sig, reserve_pub " @@ -987,46 +987,35 @@ TALER_MINT_DB_insert_collectable_blindcoin (PGconn *db_conn, const struct GNUNET_HashCode *h_blind, const struct CollectableBlindcoin *collectable) { - // FIXME: check logic! PGresult *result; - char *sig_buf; - size_t sig_buf_size; + char *denom_pub_enc = NULL; + size_t denom_pub_enc_size; + denom_pub_enc_size = + GNUNET_CRYPTO_rsa_public_key_encode (collectable->denom_pub, + &denom_pub_enc); + struct TALER_DB_QueryParam params[] = { + TALER_DB_QUERY_PARAM_PTR (h_blind), + TALER_DB_QUERY_PARAM_PTR_SIZED (denom_pub_enc, denom_pub_enc_size - 1), /* DB doesn't like the trailing \0 */ + TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_pub), + TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_sig), + TALER_DB_QUERY_PARAM_END + }; + int ret; - sig_buf_size = GNUNET_CRYPTO_rsa_signature_encode (collectable->sig, - &sig_buf); + result = TALER_DB_exec_prepared (db_conn, + "insert_collectable_blindcoins", + params); + if (PGRES_COMMAND_OK != PQresultStatus (result)) { - struct TALER_DB_QueryParam params[] = { - TALER_DB_QUERY_PARAM_PTR (&h_blind), - TALER_DB_QUERY_PARAM_PTR_SIZED (sig_buf, sig_buf_size), - TALER_DB_QUERY_PARAM_PTR (&collectable->denom_pub), - TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_pub), - TALER_DB_QUERY_PARAM_PTR (&collectable->reserve_sig), - TALER_DB_QUERY_PARAM_END - }; - - result = TALER_DB_exec_prepared (db_conn, - "insert_collectable_blindcoins", - params); - if (PGRES_COMMAND_OK != PQresultStatus (result)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Query failed: %s\n", - PQresultErrorMessage (result)); - PQclear (result); - return GNUNET_SYSERR; - } - - if (0 != strcmp ("1", PQcmdTuples (result))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Insert failed (updated '%s' tupes instead of '1')\n", - PQcmdTuples (result)); - PQclear (result); - return GNUNET_SYSERR; - } - PQclear (result); + ret = GNUNET_SYSERR; + goto cleanup; } - return GNUNET_OK; + ret = GNUNET_OK; + + cleanup: + PQclear (result); + GNUNET_free_non_null (denom_pub_enc); + return ret; } diff --git a/src/mint/test_mint_db.c b/src/mint/test_mint_db.c index fcd8311de..320005833 100644 --- a/src/mint/test_mint_db.c +++ b/src/mint/test_mint_db.c @@ -33,6 +33,10 @@ static int result; } while (0) +#define RND_BLK(ptr) \ + GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (*ptr)) + + /** * Checks if the given reserve has the given amount of balance and expiry * @@ -66,6 +70,31 @@ check_reserve (PGconn *db, } +struct DenomKeyPair +{ + struct GNUNET_CRYPTO_rsa_PrivateKey *priv; + struct GNUNET_CRYPTO_rsa_PublicKey *pub; +}; + +struct DenomKeyPair * +create_denom_key_pair (unsigned int size) +{ + struct DenomKeyPair *dkp; + + dkp = GNUNET_new (struct DenomKeyPair); + dkp->priv = GNUNET_CRYPTO_rsa_private_key_create (size); + GNUNET_assert (NULL != dkp->priv); + dkp->pub = GNUNET_CRYPTO_rsa_private_key_get_public (dkp->priv); + return dkp; +} + +destroy_denon_key_pair (struct DenomKeyPair *dkp) +{ + GNUNET_CRYPTO_rsa_public_key_free (dkp->pub); + GNUNET_CRYPTO_rsa_private_key_free (dkp->priv); + GNUNET_free (dkp); +} + /** * Main function that will be run by the scheduler. * @@ -79,12 +108,16 @@ run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { PGconn *db; - struct GNUNET_CRYPTO_EddsaPublicKey pub; + struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub; struct Reserve reserve; struct GNUNET_TIME_Absolute expiry; struct TALER_Amount amount; + struct DenomKeyPair *dkp; + struct GNUNET_HashCode *h_blind; + struct CollectableBlindcoin cbc; db = NULL; + dkp = NULL; if (GNUNET_OK != TALER_MINT_DB_init ("postgres:///taler")) { result = 1; @@ -100,9 +133,8 @@ run (void *cls, char *const *args, const char *cfgfile, result = 3; goto drop; } - GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, - &pub, sizeof (pub)); - reserve.pub = &pub; + RND_BLK (&reserve_pub); + reserve.pub = &reserve_pub; amount.value = 1; amount.fraction = 1; strcpy (amount.currency, "EUR"); @@ -114,7 +146,7 @@ run (void *cls, char *const *args, const char *cfgfile, amount, expiry)); FAILIF (GNUNET_OK != check_reserve (db, - &pub, + &reserve_pub, amount.value, amount.fraction, amount.currency, @@ -124,15 +156,28 @@ run (void *cls, char *const *args, const char *cfgfile, amount, expiry)); FAILIF (GNUNET_OK != check_reserve (db, - &pub, + &reserve_pub, ++amount.value, ++amount.fraction, amount.currency, expiry.abs_value_us)); + dkp = create_denom_key_pair (1024); + RND_BLK(&h_blind); + RND_BLK(&cbc.reserve_sig); + cbc.denom_pub = dkp->pub; + cbc.sig = NULL; + memcpy (&cbc.reserve_pub, &reserve_pub, sizeof (reserve_pub)); + TALER_MINT_DB_insert_collectable_blindcoin (db, + &h_blind, + &cbc); result = 0; + drop: if (NULL != db) GNUNET_break (GNUNET_OK == TALER_MINT_DB_drop_temporary (db)); + if (NULL != dkp) + destroy_denon_key_pair (dkp); + dkp = NULL; }