moving low-level JSON encoding logic to json.c

This commit is contained in:
Christian Grothoff 2015-03-15 16:39:06 +01:00
parent 76dda24c11
commit 53b189868e
4 changed files with 73 additions and 37 deletions

View File

@ -76,6 +76,26 @@ TALER_JSON_from_ecdsa_sig (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpo
const struct GNUNET_CRYPTO_EcdsaSignature *signature); const struct GNUNET_CRYPTO_EcdsaSignature *signature);
/**
* Convert RSA public key to JSON.
*
* @param pk public key to convert
* @return corresponding JSON encoding
*/
json_t *
TALER_JSON_from_rsa_public_key (struct GNUNET_CRYPTO_rsa_PublicKey *pk);
/**
* Convert RSA signature to JSON.
*
* @param sig signature to convert
* @return corresponding JSON encoding
*/
json_t *
TALER_JSON_from_rsa_signature (struct GNUNET_CRYPTO_rsa_Signature *sig);
/** /**
* Convert binary data to a JSON string * Convert binary data to a JSON string
* with the base32crockford encoding. * with the base32crockford encoding.

View File

@ -116,8 +116,6 @@ static int reload_pipe[2];
static json_t * static json_t *
denom_key_issue_to_json (const struct TALER_MINT_DenomKeyIssue *dki) denom_key_issue_to_json (const struct TALER_MINT_DenomKeyIssue *dki)
{ {
char *buf;
size_t buf_len;
json_t *dk_json = json_object (); json_t *dk_json = json_object ();
json_object_set_new (dk_json, json_object_set_new (dk_json,
@ -134,13 +132,9 @@ denom_key_issue_to_json (const struct TALER_MINT_DenomKeyIssue *dki)
"stamp_expire_deposit", "stamp_expire_deposit",
TALER_JSON_from_abs (GNUNET_TIME_absolute_ntoh (dki->expire_spend))); TALER_JSON_from_abs (GNUNET_TIME_absolute_ntoh (dki->expire_spend)));
buf_len = GNUNET_CRYPTO_rsa_public_key_encode (dki->denom_pub,
&buf);
json_object_set_new (dk_json, json_object_set_new (dk_json,
"denom_pub", "denom_pub",
TALER_JSON_from_data (buf, TALER_JSON_from_rsa_public_key (dki->denom_pub));
buf_len));
GNUNET_free (buf);
json_object_set_new (dk_json, json_object_set_new (dk_json,
"value", "value",
TALER_JSON_from_amount (TALER_amount_ntoh (dki->value))); TALER_JSON_from_amount (TALER_amount_ntoh (dki->value)));

View File

@ -581,15 +581,9 @@ TALER_MINT_reply_withdraw_sign_success (struct MHD_Connection *connection,
const struct CollectableBlindcoin *collectable) const struct CollectableBlindcoin *collectable)
{ {
json_t *sig_json; json_t *sig_json;
size_t sig_buf_size;
char *sig_buf;
int ret; int ret;
sig_buf_size = GNUNET_CRYPTO_rsa_signature_encode (collectable->sig, sig_json = TALER_JSON_from_rsa_signature (collectable->sig);
&sig_buf);
sig_json = TALER_JSON_from_data (sig_buf,
sig_buf_size);
GNUNET_free (sig_buf);
ret = TALER_MINT_reply_json_pack (connection, ret = TALER_MINT_reply_json_pack (connection,
MHD_HTTP_OK, MHD_HTTP_OK,
"{s:o}", "{s:o}",
@ -691,22 +685,14 @@ TALER_MINT_reply_refresh_reveal_success (struct MHD_Connection *connection,
int newcoin_index; int newcoin_index;
json_t *root; json_t *root;
json_t *list; json_t *list;
char *buf;
size_t buf_size;
int ret; int ret;
root = json_object (); root = json_object ();
list = json_array (); list = json_array ();
json_object_set_new (root, "ev_sigs", list); json_object_set_new (root, "ev_sigs", list);
for (newcoin_index = 0; newcoin_index < num_newcoins; newcoin_index++) for (newcoin_index = 0; newcoin_index < num_newcoins; newcoin_index++)
{
buf_size = GNUNET_CRYPTO_rsa_signature_encode (sigs[newcoin_index],
&buf);
json_array_append_new (list, json_array_append_new (list,
TALER_JSON_from_data (buf, TALER_JSON_from_rsa_signature (sigs[newcoin_index]));
buf_size));
GNUNET_free (buf);
}
ret = TALER_MINT_reply_json (connection, ret = TALER_MINT_reply_json (connection,
root, root,
MHD_HTTP_OK); MHD_HTTP_OK);
@ -772,26 +758,18 @@ TALER_MINT_reply_refresh_link_success (struct MHD_Connection *connection,
for (pos = ldl; NULL != pos; pos = pos->next) for (pos = ldl; NULL != pos; pos = pos->next)
{ {
json_t *obj; json_t *obj;
char *buf;
size_t buf_len;
obj = json_object (); obj = json_object ();
json_object_set_new (obj, "link_enc", json_object_set_new (obj, "link_enc",
TALER_JSON_from_data (ldl->link_data_enc->coin_priv_enc, TALER_JSON_from_data (ldl->link_data_enc->coin_priv_enc,
sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) +
ldl->link_data_enc->blinding_key_enc_size)); ldl->link_data_enc->blinding_key_enc_size));
buf_len = GNUNET_CRYPTO_rsa_public_key_encode (ldl->denom_pub, json_object_set_new (obj,
&buf); "denom_pub",
json_object_set_new (obj, "denom_pub", TALER_JSON_from_rsa_public_key (ldl->denom_pub));
TALER_JSON_from_data (buf, json_object_set_new (obj,
buf_len)); "ev_sig",
GNUNET_free (buf); TALER_JSON_from_rsa_signature (ldl->ev_sig));
buf_len = GNUNET_CRYPTO_rsa_signature_encode (ldl->ev_sig,
&buf);
json_object_set_new (obj, "ev_sig",
TALER_JSON_from_data (buf,
buf_len));
GNUNET_free (buf);
json_array_append_new (list, obj); json_array_append_new (list, obj);
} }

View File

@ -150,6 +150,50 @@ TALER_JSON_from_ecdsa_sig (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpo
} }
/**
* Convert RSA public key to JSON.
*
* @param pk public key to convert
* @return corresponding JSON encoding
*/
json_t *
TALER_JSON_from_rsa_public_key (struct GNUNET_CRYPTO_rsa_PublicKey *pk)
{
char *buf;
size_t buf_len;
json_t *ret;
buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
&buf);
ret = TALER_JSON_from_data (buf,
buf_len);
GNUNET_free (buf);
return ret;
}
/**
* Convert RSA signature to JSON.
*
* @param sig signature to convert
* @return corresponding JSON encoding
*/
json_t *
TALER_JSON_from_rsa_signature (struct GNUNET_CRYPTO_rsa_Signature *sig)
{
char *buf;
size_t buf_len;
json_t *ret;
buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
&buf);
ret = TALER_JSON_from_data (buf,
buf_len);
GNUNET_free (buf);
return ret;
}
/** /**
* Convert binary data to a JSON string * Convert binary data to a JSON string
* with the base32crockford encoding. * with the base32crockford encoding.