Commit ae708b41 authored by xHire's avatar xHire
Browse files

[2019-05-21] saving the progress so far

parent 370bb1a7
......@@ -456,7 +456,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
{
void *data;
char *json_payload;
char *json_payload_data;
json_t *json_payload_data;
enum trade_step next_step;
enum payload_type payload_type;
int res;
......@@ -468,14 +468,14 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
identity->keypair.secret_key,
&json_payload)) {
log_debug("process_encrypted - decrypting message payload has "
"failed. Payload:\n%s", encrypted_payload->payload);
"failed. Payload:\n%s\n", encrypted_payload->payload);
return 1;
}
if (decode_payload_type(json_payload,
&payload_type,
&json_payload_data)) {
log_debug("process_encrypted - decoding decrypted payload "
"has failed. Payload:\n%s", json_payload);
"has failed. Payload:\n%s\n", json_payload);
free(json_payload);
return 1;
}
......@@ -489,7 +489,7 @@ static int process_encrypted(const encrypted_t *encrypted_payload,
payload_type,
&data)) {
log_debug("process_encrypted - decoding payload's data "
"has failed. Data:\n%s", json_payload_data);
"has failed. Data:\n%s\n", json_payload);
free(json_payload_data);
return 1;
}
......
......@@ -470,8 +470,7 @@ int decode_message_data(const char *json_data,
return 0;
}
case P2P_PEERS_ADV:
{
case P2P_PEERS_ADV: {
p2p_peers_adv_t *decoded;
if (json_data == NULL) {
......@@ -529,8 +528,7 @@ int decode_message_data(const char *json_data,
return 0;
case P2P_ROUTE_ADV:
return 0;
case P2P_ROUTE_SOL:
{
case P2P_ROUTE_SOL: {
p2p_route_sol_t *decoded;
if (json_data == NULL) {
......@@ -651,7 +649,6 @@ int decode_payload_type(const char *json_payload,
return 1;
}
/* TODO: Implement */
/**
* Decode JSON payload into an internal representation.
*
......@@ -663,18 +660,76 @@ int decode_payload_type(const char *json_payload,
* @return 0 Successfully decoded.
* @return 1 Failure.
*/
int decode_payload_data(const char *json_data,
int decode_payload_data(const json_t *json_data,
enum payload_type type,
void **data)
{
return 0;
/* NOTE: TRADE_EXECUTION is decoded in its own function */
switch (type) {
case TRADE_PROPOSAL: {
trade_proposal_t *decoded;
if (json_data == NULL) {
return 1;
}
*data = malloc(sizeof(trade_proposal_t));
if (!*data) {
log_error("decode_payload_data[trade_proposal]:"
" allocating container");
return 1;
}
decoded = (trade_proposal_t *) *data;
/* parse json_data */
/* FIXME: implement a switch */
decoded->protocol = TT_BASIC;
value = json_object_get(json_data, "order");
if (!json_is_string(value)) {
log_debug("decode_message_data[encrypted]: "
"invalid message - `order` is not a "
"string");
json_decref(value);
free(*data);
return 1;
}
strncpy((char *) decoded->order,
json_string_value(value),
sizeof(decoded->order));
json_decref(value);
value = json_object_get(json_data, "commitment");
if (!json_is_string(value)) {
log_debug("decode_message_data[encrypted]: "
"invalid message - `commitment` is "
"not a string");
json_decref(value);
free(*data);
return 1;
}
strncpy((char *) decoded->commitment,
json_string_value(value),
sizeof(decoded->commitment));
json_decref(value);
return 0;
}
case TRADE_REJECT: {
/* TODO: implement */
return 1;
}
}
return 1;
}
/* TODO: Implement */
/**
* Decode JSON trade.execution of type basic into internal representation.
*
* @param json_data The JSON trade.execution.
* @param json_data The JSON of trade.execution.
* @param step Trade step to be decoded.
* @param execution The internal representation.
*
......@@ -686,6 +741,8 @@ static int decode_trade_basic(const char *json_data,
trade_execution_t *execution)
{
trade_execution_basic_t *data;
json_t *root, *value;
json_error_t json_error;
data = (trade_execution_basic_t *) malloc(
sizeof(trade_execution_basic_t));
......@@ -694,15 +751,136 @@ static int decode_trade_basic(const char *json_data,
return 1;
}
execution->data = data;
/* parse json_payload */
if (!(root = json_loads(json_data, 0, &json_error))) {
log_debug("decode_trade_basic - invalid json \"%s\"; error on "
"line %d: %s", json_data, json_error.line,
json_error.text);
free(data);
return 1;
}
if (!json_is_object(root)) {
log_debug("decode_trade_basic - invalid message \"%s\"",
json_data);
json_decref(root);
free(data);
return 1;
}
switch (step) {
case TS_PROPOSAL:
value = json_object_get(root, "commitment");
if (!json_is_string(value)) {
log_debug("decode_trade_basic - invalid message"
" \"%s\" - `commitment` is not a "
"string", json_data);
json_decref(value);
json_decref(root);
free(data);
return 1;
}
strncpy((char *) data->commitment,
json_string_value(value),
sizeof(data->commitment));
json_decref(value);
break;
case TS_COMMITMENT:
value = json_object_get(root, "committed");
if (!json_is_string(value)) {
log_debug("decode_trade_basic - invalid message"
" \"%s\" - `committed` is not a "
"string", json_data);
json_decref(value);
json_decref(root);
free(data);
return 1;
}
strncpy((char *) data->committed,
json_string_value(value),
sizeof(data->committed));
json_decref(value);
value = json_object_get(root, "pubkey");
if (!json_is_string(value)) {
log_debug("decode_trade_basic - invalid message"
" \"%s\" - `pubkey` is not a string",
json_data);
json_decref(value);
json_decref(root);
free(data);
return 1;
}
strncpy((char *) data->pubkey,
json_string_value(value),
sizeof(data->pubkey));
json_decref(value);
break;
case TS_KEY_AND_COMMITTED_EXCHANGE:
/* if json_data doesn't contain script, set data->script
* to NULL, otherwise it must also contain 'hx' */
value = json_object_get(root, "script");
if (value == NULL) {
data->script = NULL;
} else if (!json_is_string(value)) {
log_debug("decode_trade_basic - invalid message"
" \"%s\" - `script` is not a string",
json_data);
json_decref(value);
json_decref(root);
return 1;
} else {
data->script = strdup(json_string_value(value));
json_decref(value);
}
value = json_object_get(root, "hx");
if (value && !json_is_string(value)) {
log_debug("decode_trade_basic - invalid message"
" \"%s\" - `hx` is not a string",
json_data);
json_decref(value);
json_decref(root);
return 1;
} else {
strncpy((char *) data->hx,
json_string_value(value),
sizeof(data->hx));
json_decref(value);
}
break;
case TS_SCRIPT_ORIGIN:
value = json_object_get(root, "script");
if (!json_is_string(value)) {
log_debug("decode_trade_basic - invalid message"
" \"%s\" - `script` is not a string",
json_data);
json_decref(value);
json_decref(root);
return 1;
}
data->script = strdup(json_string_value(value));
json_decref(value);
break;
default:
log_error("decode_trade_basic - unexpected message, "
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
"\"%s\"", json_data);
}
json_decref(root);
execution->data = data;
return 0;
}
......@@ -747,9 +925,9 @@ int decode_trade_execution(const char *json_data,
/* TODO: Implement */
/**
* Decode a JSON trade.
* Decode a JSON serialisation of a persisted trade.
*
* @param json_trade Encoded JSON trade.
* @param json_trade JSON-encoded trade.
* @param trade Internal trade representation, dynamically
* allocated in here.
*
......@@ -806,6 +984,9 @@ int encode_message(const message_t *message, char **json_message)
/* from_hex, to_hex, sig_hex are initialized now, ready to be
* included in output 'json_message' */
/* TODO: has to call encode_message_body */
/* NOTE: pouzit fce json_integer, json_string ap.; JSON_COMPACT do json_dumps */
return 0;
}
......@@ -822,10 +1003,57 @@ int encode_message(const message_t *message, char **json_message)
*/
int encode_message_body(const message_body_t *body, char **json_body)
{
char to_hex[65];
char to_null = 0;
int i;
json_t *json_data = json_object();
if (json_data == NULL) {
log_error("[encode_message_body] json_object() for reject "
"failed");
return 1;
}
for (i = 0; i < sizeof(body->to); i++) {
if (body->to[i]) {
to_null = 1;
}
}
if (!to_null) {
/* sodium_bin2hex also appends '\0' */
/* if converting 'order' to hexadecimal failed */
if (!sodium_bin2hex(to_hex,
sizeof(to_hex),
body->to,
sizeof(body->to))) {
log_debug("[encode_message_body] 'to' to hex");
return 1;
}
/* TODO: hex + opravit tenhle copy-paste */
ret = json_object_set_new(json_data, "protocol",
json_integer(proposal->protocol));
if (ret == -1) {
log_error("[encode_message_body] setting 'to' "
"failed");
return 1;
}
} else {
ret = json_object_set_new(json_data, "to", json_null());
if (ret == -1) {
log_error("[encode_message_body] setting 'to' to NULL "
"failed");
return 1;
}
}
/* TODO: zbytek poli */
return 0;
}
/* TODO: Implement */
/**
* Encode a payload into JSON.
*
......@@ -838,6 +1066,113 @@ int encode_message_body(const message_body_t *body, char **json_body)
*/
int encode_payload(enum payload_type type, const void *data, char **encoded)
{
switch (type) {
case TRADE_PROPOSAL: {
char order_hex[65];
char commitment_hex[65];
trade_proposal_t *proposal = (trade_proposal_t *) data;
json_t *json_data; = json_object();
if (json_data == NULL) {
log_error("[encode_payload] json_object() "
"failed");
return 1;
}
/* sodium_bin2hex also appends '\0' */
/* if converting 'order' to hexadecimal failed */
if (!sodium_bin2hex(order_hex,
sizeof(order_hex),
proposal->order,
sizeof(proposal->order))) {
log_debug("[encode_payload] 'order' to hex");
return 1;
}
/* if converting 'commitment' to hexadecimal failed */
if (!sodium_bin2hex(commitment_hex,
sizeof(commitment_hex),
proposal->commitment,
sizeof(proposal->commitment))) {
log_debug("[encode_payload] 'commitment' to "
"hex");
return 1;
}
ret = json_object_set_new(json_data, "protocol",
json_integer(proposal->protocol));
if (ret == -1) {
log_error("[encode_payload] setting protocol "
"failed");
return 1;
}
ret = json_object_set_new(json_data, "order",
json_string(order_hex));
if (ret == -1) {
log_error("[encode_payload] setting order "
"failed");
json_object_clear(json_data);
return 1;
}
ret = json_object_set_new(json_data, "commitment",
json_string(commitment_hex));
if (ret == -1) {
log_error("[encode_payload] setting order "
"failed");
json_object_clear(json_data);
return 1;
}
*encoded = json_dumps(json_data, JSON_COMPACT);
break;
}
case TRADE_REJECT: {
char order_hex[65];
trade_reject_t *reject = (trade_reject *) data;
json_t *json_data = json_object();
/* sodium_bin2hex also appends '\0' */
/* if converting 'order' to hexadecimal failed */
if (!sodium_bin2hex(order_hex,
sizeof(order_hex),
proposal->order,
sizeof(proposal->order))) {
log_debug("[encode_payload] 'order' to hex");
return 1;
}
if (json_data == NULL) {
log_error("[encode_payload] json_object() for "
"reject failed");
return 1;
}
ret = json_object_set_new(json_data, "order",
json_string(order_hex));
if (ret == -1) {
log_error("[encode_payload] setting order for "
"reject failed");
return 1;
}
*encoded = json_dumps(json_data, JSON_COMPACT);
break;
}
default:
log_error("[encode_payload] Unknown type to encode: %d",
type);
return 1;
}
return 0;
}
......@@ -865,7 +1200,7 @@ int encode_trade_execution(const trade_execution_t *trade_execution,
/* TODO: Implement */
/**
* Encode a trade into JSON.
* Encode a trade into JSON for the purpose of internal storage.
*
* @param trade Trade to encode.
* @param json_trade Encoded JSON trade.
......@@ -875,5 +1210,7 @@ int encode_trade_execution(const trade_execution_t *trade_execution,
*/
int encode_trade(const trade_t *trade, char **json_trade)
{
*json_trade = NULL;
return 0;
}
......@@ -63,7 +63,7 @@ int decode_message_data(const char *json_data,
int decode_payload_type(const char *json_payload,
enum payload_type *type,
json_t **json_data);
int decode_payload_data(const char *json_data,
int decode_payload_data(const json_t *json_data,
enum payload_type type,
void **data);
int decode_trade_execution(const char *json_data,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment