From 21f67e8241e451a593b44562237cf9ae754c4eef Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 17 Dec 2023 22:17:02 +0800 Subject: -expand data returned from /config by merchant backend --- src/backend/taler-merchant-httpd_config.c | 38 +++++++++++++++++++++++ src/backend/taler-merchant-httpd_exchanges.c | 8 +++++ src/backend/taler-merchant-httpd_exchanges.h | 13 ++++++++ src/include/taler_merchant_service.h | 34 +++++++++++++++++++++ src/lib/merchant_api_get_config.c | 45 ++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+) (limited to 'src') diff --git a/src/backend/taler-merchant-httpd_config.c b/src/backend/taler-merchant-httpd_config.c index 448b3dec..d53554f6 100644 --- a/src/backend/taler-merchant-httpd_config.c +++ b/src/backend/taler-merchant-httpd_config.c @@ -45,6 +45,38 @@ #define MERCHANT_PROTOCOL_VERSION "6:0:2" + +/** + * Callback on an exchange known to us. Does not warrant + * that the "keys" information is actually available for + * @a exchange. + * + * @param cls closure with `json_t *` array to expand + * @param url base URL of the exchange + * @param exchange internal handle for the exchange + */ +static void +add_exchange (void *cls, + const char *url, + const struct TMH_Exchange *exchange) +{ + json_t *xa = cls; + json_t *xi; + + xi = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_data_auto ("master_pub", + TMH_EXCHANGES_get_master_pub (exchange)), + GNUNET_JSON_pack_string ("currency", + TMH_EXCHANGES_get_currency (exchange)), + GNUNET_JSON_pack_string ("base_url", + url)); + GNUNET_assert (NULL != xi); + GNUNET_assert (0 == + json_array_append_new (xa, + xi)); +} + + MHD_RESULT MH_handler_config (struct TMH_RequestHandler *rh, struct MHD_Connection *connection, @@ -57,8 +89,12 @@ MH_handler_config (struct TMH_RequestHandler *rh, if (NULL == response) { json_t *specs = json_object (); + json_t *exchanges = json_array (); GNUNET_assert (NULL != specs); + GNUNET_assert (NULL != exchanges); + TMH_exchange_get_trusted (&add_exchange, + exchanges); for (unsigned int i=0;icurrency; +} + + /** * Free data structures within @a ea, but not @a ea * pointer itself. diff --git a/src/backend/taler-merchant-httpd_exchanges.h b/src/backend/taler-merchant-httpd_exchanges.h index 86c8374d..892843f6 100644 --- a/src/backend/taler-merchant-httpd_exchanges.h +++ b/src/backend/taler-merchant-httpd_exchanges.h @@ -139,6 +139,19 @@ TMH_EXCHANGES_get_master_pub ( const struct TMH_Exchange *exchange); +/** + * Return the currency of the given @a exchange. + * Will be returned from configuration for trusted + * exchanges. + * + * @param exchange exchange to get master public key for + * @return the currency of @a exchange + */ +const char * +TMH_EXCHANGES_get_currency ( + const struct TMH_Exchange *exchange); + + /** * Lookup current wire fee by @a exchange_url and @a wire_method. * diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h index e8aad3d0..aeddefc8 100644 --- a/src/include/taler_merchant_service.h +++ b/src/include/taler_merchant_service.h @@ -309,6 +309,29 @@ struct TALER_MERCHANT_ConfigInformation }; +/** + * Information about an exchange the merchant backend trusts. + */ +struct TALER_MERCHANT_ExchangeConfigInfo +{ + /** + * Base URL of the exchange REST API. + */ + const char *base_url; + + /** + * Currency for which the merchant is configured to + * trust the exchange. + */ + const char *currency; + + /** + * Master public key of the exchange. + */ + struct TALER_MasterPublicKeyP master_pub; + +}; + /** * Response to /config request. */ @@ -351,6 +374,17 @@ struct TALER_MERCHANT_ConfigResponse */ const struct TALER_CurrencySpecification *cspecs; + /** + * Length of the @e exchanges array. + */ + unsigned int num_exchanges; + + /** + * Array details about exchanges trusted + * by this merchant backend. + */ + const struct TALER_MERCHANT_ExchangeConfigInfo *exchanges; + } ok; } details; }; diff --git a/src/lib/merchant_api_get_config.c b/src/lib/merchant_api_get_config.c index 1b289a9a..401d4829 100644 --- a/src/lib/merchant_api_get_config.c +++ b/src/lib/merchant_api_get_config.c @@ -105,10 +105,19 @@ handle_config_finished (void *cls, case MHD_HTTP_OK: { const json_t *jcs; + const json_t *exchanges = NULL; + struct TALER_MERCHANT_ExchangeConfigInfo *eci = NULL; + unsigned int num_eci = 0; struct TALER_JSON_ProtocolVersion pv; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_object_const ("currencies", &jcs), + /* Optional for v5 compatibility, remove + once we reduce age! */ + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_array_const ("exchanges", + &exchanges), + NULL), GNUNET_JSON_spec_string ("currency", &cr.details.ok.ci.currency), TALER_JSON_spec_version ("version", @@ -124,6 +133,7 @@ handle_config_finished (void *cls, spec, NULL, NULL)) { + GNUNET_break_op (0); cr.hr.http_status = 0; cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; } @@ -143,6 +153,38 @@ handle_config_finished (void *cls, cr.details.ok.compat |= TALER_MERCHANT_VC_INCOMPATIBLE; } } + if (NULL != exchanges) + { + num_eci = json_object_size (exchanges); + eci = GNUNET_new_array (num_eci, + struct TALER_MERCHANT_ExchangeConfigInfo); + for (unsigned int i=0;icurrency), + GNUNET_JSON_spec_string ("base_url", + &ei->base_url), + GNUNET_JSON_spec_fixed_auto ("master_pub", + &ei->master_pub), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (ej, + ispec, + NULL, NULL)) + { + GNUNET_break_op (0); + cr.hr.http_status = 0; + cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + } + } + } { unsigned int nspec = json_object_size (jcs); struct TALER_CurrencySpecification *cspecs; @@ -154,6 +196,8 @@ handle_config_finished (void *cls, struct TALER_CurrencySpecification); cr.details.ok.num_cspecs = nspec; cr.details.ok.cspecs = cspecs; + cr.details.ok.num_exchanges = num_eci; + cr.details.ok.exchanges = eci; json_object_foreach ((json_t *) jcs, curr, obj) { struct TALER_CurrencySpecification *cs = &cspecs[off++]; @@ -168,6 +212,7 @@ handle_config_finished (void *cls, cspec, NULL, NULL)) { + GNUNET_break_op (0); cr.hr.http_status = 0; cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; -- cgit v1.2.3