-towards client-side support for merge and history requests in reserve history

This commit is contained in:
Christian Grothoff 2022-05-22 20:04:38 +02:00
parent 40daa209fb
commit 67535ebf65
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC

View File

@ -26,64 +26,71 @@
#include "taler_signatures.h" #include "taler_signatures.h"
// FIXME: refactor, use switching table instead /**
// of long if-then-else-else-else list! * UUID array for duplicate detection.
enum GNUNET_GenericReturnValue */
TALER_EXCHANGE_parse_reserve_history ( struct HistoryParseContext
struct TALER_EXCHANGE_Handle *exchange,
const json_t *history,
const struct TALER_ReservePublicKeyP *reserve_pub,
const char *currency,
struct TALER_Amount *total_in,
struct TALER_Amount *total_out,
unsigned int history_length,
struct TALER_EXCHANGE_ReserveHistoryEntry *rhistory)
{ {
struct GNUNET_HashCode uuid[history_length];
unsigned int uuid_off;
GNUNET_assert (GNUNET_OK == /**
TALER_amount_set_zero (currency, * Exchange we use.
total_in)); */
GNUNET_assert (GNUNET_OK == struct TALER_EXCHANGE_Handle *exchange;
TALER_amount_set_zero (currency,
total_out)); /**
uuid_off = 0; * Our reserve public key.
for (unsigned int off = 0; off<history_length; off++) */
{ const struct TALER_ReservePublicKeyP *reserve_pub;
struct TALER_EXCHANGE_ReserveHistoryEntry *rh = &rhistory[off];
json_t *transaction; /**
struct TALER_Amount amount; * Array of UUIDs.
const char *type; */
struct GNUNET_JSON_Specification hist_spec[] = { struct GNUNET_HashCode *uuids;
GNUNET_JSON_spec_string ("type",
&type), /**
TALER_JSON_spec_amount_any ("amount", * Where to sum up total inbound amounts.
&amount), */
/* 'wire' and 'signature' are optional depending on 'type'! */ struct TALER_Amount *total_in;
GNUNET_JSON_spec_end ()
/**
* Where to sum up total outbound amounts.
*/
struct TALER_Amount *total_out;
/**
* Number of entries already used in @e uuids.
*/
unsigned int uuid_off;
}; };
transaction = json_array_get (history,
off); /**
if (GNUNET_OK != * Type of a function called to parse a reserve history
GNUNET_JSON_parse (transaction, * entry @a rh.
hist_spec, *
NULL, NULL)) * @param[in,out] rh where to write the result
{ * @param[in,out] uc UUID context for duplicate detection
GNUNET_break_op (0); * @param transaction the transaction to parse
return GNUNET_SYSERR; * @return #GNUNET_OK on success
} */
rhistory[off].amount = amount; typedef enum GNUNET_GenericReturnValue
if (GNUNET_YES != (*ParseHelper)(struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
TALER_amount_cmp_currency (&amount, struct HistoryParseContext *uc,
total_in)) const json_t *transaction);
{
GNUNET_break_op (0);
return GNUNET_SYSERR; /**
} * Parse "credit" reserve history entry.
if (0 == strcasecmp (type, *
"CREDIT")) * @param[in,out] rh entry to parse
* @param uc our context
* @param transaction the transaction to parse
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
parse_credit (struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
struct HistoryParseContext *uc,
const json_t *transaction)
{ {
const char *wire_url; const char *wire_url;
uint64_t wire_reference; uint64_t wire_reference;
@ -100,9 +107,9 @@ TALER_EXCHANGE_parse_reserve_history (
rh->type = TALER_EXCHANGE_RTT_CREDIT; rh->type = TALER_EXCHANGE_RTT_CREDIT;
if (0 > if (0 >
TALER_amount_add (total_in, TALER_amount_add (uc->total_in,
total_in, uc->total_in,
&amount)) &rh->amount))
{ {
/* overflow in history already!? inconceivable! Bad exchange! */ /* overflow in history already!? inconceivable! Bad exchange! */
GNUNET_break_op (0); GNUNET_break_op (0);
@ -119,10 +126,22 @@ TALER_EXCHANGE_parse_reserve_history (
rh->details.in_details.sender_url = GNUNET_strdup (wire_url); rh->details.in_details.sender_url = GNUNET_strdup (wire_url);
rh->details.in_details.wire_reference = wire_reference; rh->details.in_details.wire_reference = wire_reference;
rh->details.in_details.timestamp = timestamp; rh->details.in_details.timestamp = timestamp;
/* end type==DEPOSIT */ return GNUNET_OK;
} }
else if (0 == strcasecmp (type,
"WITHDRAW"))
/**
* Parse "credit" reserve history entry.
*
* @param[in,out] rh entry to parse
* @param uc our context
* @param transaction the transaction to parse
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
parse_withdraw (struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
struct HistoryParseContext *uc,
const json_t *transaction)
{ {
struct TALER_ReserveSignatureP sig; struct TALER_ReserveSignatureP sig;
struct TALER_DenominationHashP h_denom_pub; struct TALER_DenominationHashP h_denom_pub;
@ -153,9 +172,9 @@ TALER_EXCHANGE_parse_reserve_history (
/* Check that the signature is a valid withdraw request */ /* Check that the signature is a valid withdraw request */
if (GNUNET_OK != if (GNUNET_OK !=
TALER_wallet_withdraw_verify (&h_denom_pub, TALER_wallet_withdraw_verify (&h_denom_pub,
&amount, &rh->amount,
&bch, &bch,
reserve_pub, uc->reserve_pub,
&sig)) &sig))
{ {
GNUNET_break_op (0); GNUNET_break_op (0);
@ -167,7 +186,7 @@ TALER_EXCHANGE_parse_reserve_history (
const struct TALER_EXCHANGE_Keys *key_state; const struct TALER_EXCHANGE_Keys *key_state;
const struct TALER_EXCHANGE_DenomPublicKey *dki; const struct TALER_EXCHANGE_DenomPublicKey *dki;
key_state = TALER_EXCHANGE_get_keys (exchange); key_state = TALER_EXCHANGE_get_keys (uc->exchange);
dki = TALER_EXCHANGE_get_denomination_key_by_hash (key_state, dki = TALER_EXCHANGE_get_denomination_key_by_hash (key_state,
&h_denom_pub); &h_denom_pub);
if ( (GNUNET_YES != if ( (GNUNET_YES !=
@ -193,33 +212,45 @@ TALER_EXCHANGE_parse_reserve_history (
duplicates. */ duplicates. */
GNUNET_CRYPTO_hash (&sig, GNUNET_CRYPTO_hash (&sig,
sizeof (sig), sizeof (sig),
&uuid[uuid_off]); &uc->uuids[uc->uuid_off]);
for (unsigned int i = 0; i<uuid_off; i++) for (unsigned int i = 0; i<uc->uuid_off; i++)
{ {
if (0 == GNUNET_memcmp (&uuid[uuid_off], if (0 == GNUNET_memcmp (&uc->uuids[uc->uuid_off],
&uuid[i])) &uc->uuids[i]))
{ {
GNUNET_break_op (0); GNUNET_break_op (0);
GNUNET_JSON_parse_free (withdraw_spec); GNUNET_JSON_parse_free (withdraw_spec);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
} }
uuid_off++; uc->uuid_off++;
if (0 > if (0 >
TALER_amount_add (total_out, TALER_amount_add (uc->total_out,
total_out, uc->total_out,
&amount)) &rh->amount))
{ {
/* overflow in history already!? inconceivable! Bad exchange! */ /* overflow in history already!? inconceivable! Bad exchange! */
GNUNET_break_op (0); GNUNET_break_op (0);
GNUNET_JSON_parse_free (withdraw_spec); GNUNET_JSON_parse_free (withdraw_spec);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
/* end type==WITHDRAW */ return GNUNET_OK;
} }
else if (0 == strcasecmp (type,
"RECOUP"))
/**
* Parse "recoup" reserve history entry.
*
* @param[in,out] rh entry to parse
* @param uc our context
* @param transaction the transaction to parse
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
parse_recoup (struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
struct HistoryParseContext *uc,
const json_t *transaction)
{ {
const struct TALER_EXCHANGE_Keys *key_state; const struct TALER_EXCHANGE_Keys *key_state;
struct GNUNET_JSON_Specification recoup_spec[] = { struct GNUNET_JSON_Specification recoup_spec[] = {
@ -235,7 +266,6 @@ TALER_EXCHANGE_parse_reserve_history (
}; };
rh->type = TALER_EXCHANGE_RTT_RECOUP; rh->type = TALER_EXCHANGE_RTT_RECOUP;
rh->amount = amount;
if (GNUNET_OK != if (GNUNET_OK !=
GNUNET_JSON_parse (transaction, GNUNET_JSON_parse (transaction,
recoup_spec, recoup_spec,
@ -244,7 +274,7 @@ TALER_EXCHANGE_parse_reserve_history (
GNUNET_break_op (0); GNUNET_break_op (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
key_state = TALER_EXCHANGE_get_keys (exchange); key_state = TALER_EXCHANGE_get_keys (uc->exchange);
if (GNUNET_OK != if (GNUNET_OK !=
TALER_EXCHANGE_test_signing_key (key_state, TALER_EXCHANGE_test_signing_key (key_state,
&rh->details. &rh->details.
@ -256,9 +286,9 @@ TALER_EXCHANGE_parse_reserve_history (
if (GNUNET_OK != if (GNUNET_OK !=
TALER_exchange_online_confirm_recoup_verify ( TALER_exchange_online_confirm_recoup_verify (
rh->details.recoup_details.timestamp, rh->details.recoup_details.timestamp,
&amount, &rh->amount,
&rh->details.recoup_details.coin_pub, &rh->details.recoup_details.coin_pub,
reserve_pub, uc->reserve_pub,
&rh->details.recoup_details.exchange_pub, &rh->details.recoup_details.exchange_pub,
&rh->details.recoup_details.exchange_sig)) &rh->details.recoup_details.exchange_sig))
{ {
@ -266,18 +296,30 @@ TALER_EXCHANGE_parse_reserve_history (
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (0 > if (0 >
TALER_amount_add (total_in, TALER_amount_add (uc->total_in,
total_in, uc->total_in,
&rh->amount)) &rh->amount))
{ {
/* overflow in history already!? inconceivable! Bad exchange! */ /* overflow in history already!? inconceivable! Bad exchange! */
GNUNET_break_op (0); GNUNET_break_op (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
/* end type==RECOUP */ return GNUNET_OK;
} }
else if (0 == strcasecmp (type,
"CLOSING"))
/**
* Parse "closing" reserve history entry.
*
* @param[in,out] rh entry to parse
* @param uc our context
* @param transaction the transaction to parse
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
parse_closing (struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
struct HistoryParseContext *uc,
const json_t *transaction)
{ {
const struct TALER_EXCHANGE_Keys *key_state; const struct TALER_EXCHANGE_Keys *key_state;
struct GNUNET_JSON_Specification closing_spec[] = { struct GNUNET_JSON_Specification closing_spec[] = {
@ -298,7 +340,6 @@ TALER_EXCHANGE_parse_reserve_history (
}; };
rh->type = TALER_EXCHANGE_RTT_CLOSE; rh->type = TALER_EXCHANGE_RTT_CLOSE;
rh->amount = amount;
if (GNUNET_OK != if (GNUNET_OK !=
GNUNET_JSON_parse (transaction, GNUNET_JSON_parse (transaction,
closing_spec, closing_spec,
@ -307,8 +348,7 @@ TALER_EXCHANGE_parse_reserve_history (
GNUNET_break_op (0); GNUNET_break_op (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
key_state = TALER_EXCHANGE_get_keys (uc->exchange);
key_state = TALER_EXCHANGE_get_keys (exchange);
if (GNUNET_OK != if (GNUNET_OK !=
TALER_EXCHANGE_test_signing_key ( TALER_EXCHANGE_test_signing_key (
key_state, key_state,
@ -320,11 +360,11 @@ TALER_EXCHANGE_parse_reserve_history (
if (GNUNET_OK != if (GNUNET_OK !=
TALER_exchange_online_reserve_closed_verify ( TALER_exchange_online_reserve_closed_verify (
rh->details.close_details.timestamp, rh->details.close_details.timestamp,
&amount, &rh->amount,
&rh->details.close_details.fee, &rh->details.close_details.fee,
rh->details.close_details.receiver_account_details, rh->details.close_details.receiver_account_details,
&rh->details.close_details.wtid, &rh->details.close_details.wtid,
reserve_pub, uc->reserve_pub,
&rh->details.close_details.exchange_pub, &rh->details.close_details.exchange_pub,
&rh->details.close_details.exchange_sig)) &rh->details.close_details.exchange_sig))
{ {
@ -332,45 +372,213 @@ TALER_EXCHANGE_parse_reserve_history (
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
if (0 > if (0 >
TALER_amount_add (total_out, TALER_amount_add (uc->total_out,
total_out, uc->total_out,
&rh->amount)) &rh->amount))
{ {
/* overflow in history already!? inconceivable! Bad exchange! */ /* overflow in history already!? inconceivable! Bad exchange! */
GNUNET_break_op (0); GNUNET_break_op (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
/* end type==CLOSING */ return GNUNET_OK;
} }
else if (0 == strcasecmp (type,
"MERGE"))
/**
* Parse "merge" reserve history entry.
*
* @param[in,out] rh entry to parse
* @param uc our context
* @param transaction the transaction to parse
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
parse_merge (struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
struct HistoryParseContext *uc,
const json_t *transaction)
{ {
GNUNET_break (0); // FIXME: implement! struct GNUNET_JSON_Specification merge_spec[] = {
GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
&rh->details.merge_details.h_contract_terms),
GNUNET_JSON_spec_fixed_auto ("merge_pub",
&rh->details.merge_details.merge_pub),
GNUNET_JSON_spec_fixed_auto ("purse_sig",
&rh->details.merge_details.purse_sig),
GNUNET_JSON_spec_fixed_auto ("purse_pub",
&rh->details.merge_details.purse_pub),
GNUNET_JSON_spec_fixed_auto ("merge_sig",
&rh->details.merge_details.merge_sig),
GNUNET_JSON_spec_fixed_auto ("reserve_sig",
&rh->details.merge_details.reserve_sig),
TALER_JSON_spec_amount_any ("purse_fee",
&rh->details.merge_details.purse_fee),
GNUNET_JSON_spec_timestamp ("merge_timestamp",
&rh->details.merge_details.merge_timestamp),
GNUNET_JSON_spec_end ()
};
rh->type = TALER_EXCHANGE_RTT_MERGE;
if (GNUNET_OK !=
GNUNET_JSON_parse (transaction,
merge_spec,
NULL, NULL))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
GNUNET_break (0); // FIXME: verify signatures!
if (0 > if (0 >
TALER_amount_add (total_in, TALER_amount_add (uc->total_in,
total_in, uc->total_in,
&rh->amount)) &rh->amount))
{ {
/* overflow in history already!? inconceivable! Bad exchange! */ /* overflow in history already!? inconceivable! Bad exchange! */
GNUNET_break_op (0); GNUNET_break_op (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
return GNUNET_OK;
} }
else if (0 == strcasecmp (type,
"HISTORY"))
/**
* Parse "history" reserve history entry.
*
* @param[in,out] rh entry to parse
* @param uc our context
* @param transaction the transaction to parse
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
parse_history (struct TALER_EXCHANGE_ReserveHistoryEntry *rh,
struct HistoryParseContext *uc,
const json_t *transaction)
{ {
GNUNET_break (0); // FIXME: implement! struct GNUNET_JSON_Specification history_spec[] = {
GNUNET_JSON_spec_fixed_auto ("reserve_sig",
&rh->details.history_details.reserve_sig),
TALER_JSON_spec_amount_any ("history_fee",
&rh->details.history_details.history_fee),
GNUNET_JSON_spec_timestamp ("request_timestamp",
&rh->details.history_details.request_timestamp),
GNUNET_JSON_spec_end ()
};
rh->type = TALER_EXCHANGE_RTT_HISTORY;
if (GNUNET_OK !=
GNUNET_JSON_parse (transaction,
history_spec,
NULL, NULL))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
GNUNET_break (0); // FIXME: verify signature!
if (0 > if (0 >
TALER_amount_add (total_out, TALER_amount_add (uc->total_out,
total_out, uc->total_out,
&amount)) &rh->amount))
{ {
/* overflow in history already!? inconceivable! Bad exchange! */ /* overflow in history already!? inconceivable! Bad exchange! */
GNUNET_break_op (0); GNUNET_break_op (0);
return GNUNET_SYSERR; return GNUNET_SYSERR;
} }
return GNUNET_OK;
} }
else
enum GNUNET_GenericReturnValue
TALER_EXCHANGE_parse_reserve_history (
struct TALER_EXCHANGE_Handle *exchange,
const json_t *history,
const struct TALER_ReservePublicKeyP *reserve_pub,
const char *currency,
struct TALER_Amount *total_in,
struct TALER_Amount *total_out,
unsigned int history_length,
struct TALER_EXCHANGE_ReserveHistoryEntry *rhistory)
{
const struct
{
const char *type;
ParseHelper helper;
} map[] = {
{ "CREDIT", &parse_credit },
{ "WITHDRAW", &parse_withdraw },
{ "RECOUP", &parse_recoup },
{ "MERGE", &parse_merge },
{ "CLOSING", &parse_closing },
{ "HISTORY", &parse_history },
{ NULL, NULL }
};
struct GNUNET_HashCode uuid[history_length];
struct HistoryParseContext uc = {
.exchange = exchange,
.reserve_pub = reserve_pub,
.uuids = uuid,
.total_in = total_in,
.total_in = total_out
};
GNUNET_assert (GNUNET_OK ==
TALER_amount_set_zero (currency,
total_in));
GNUNET_assert (GNUNET_OK ==
TALER_amount_set_zero (currency,
total_out));
for (unsigned int off = 0; off<history_length; off++)
{
struct TALER_EXCHANGE_ReserveHistoryEntry *rh = &rhistory[off];
json_t *transaction;
struct TALER_Amount amount;
const char *type;
struct GNUNET_JSON_Specification hist_spec[] = {
GNUNET_JSON_spec_string ("type",
&type),
TALER_JSON_spec_amount_any ("amount",
&amount),
/* 'wire' and 'signature' are optional depending on 'type'! */
GNUNET_JSON_spec_end ()
};
bool found = false;
transaction = json_array_get (history,
off);
if (GNUNET_OK !=
GNUNET_JSON_parse (transaction,
hist_spec,
NULL, NULL))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
rh->amount = amount;
if (GNUNET_YES !=
TALER_amount_cmp_currency (&amount,
total_in))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
for (unsigned int i = 0; NULL != map[i].type; i++)
{
if (0 == strcasecmp (map[i].type,
type))
{
found = true;
if (GNUNET_OK !=
map[i].helper (rh,
&uc,
transaction))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
break;
}
}
if (! found)
{ {
/* unexpected 'type', protocol incompatibility, complain! */ /* unexpected 'type', protocol incompatibility, complain! */
GNUNET_break_op (0); GNUNET_break_op (0);
@ -409,6 +617,7 @@ TALER_EXCHANGE_free_reserve_history (
} }
// FIMXE: also transform with helpers...
enum GNUNET_GenericReturnValue enum GNUNET_GenericReturnValue
TALER_EXCHANGE_verify_coin_history ( TALER_EXCHANGE_verify_coin_history (
const struct TALER_EXCHANGE_DenomPublicKey *dk, const struct TALER_EXCHANGE_DenomPublicKey *dk,
@ -598,7 +807,6 @@ TALER_EXCHANGE_verify_coin_history (
} }
} }
if (GNUNET_OK != if (GNUNET_OK !=
TALER_wallet_melt_verify ( TALER_wallet_melt_verify (
&amount, &amount,