Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(serialzer): Add message format in send transfer
Browse files Browse the repository at this point in the history
Allow send transfer in ASCII format message and set default to ascii.
Current message format are trytes and ascii, so we check only "trytes"
to differ than default.
  • Loading branch information
Yu Wei Wu committed Apr 22, 2019
1 parent 5fb0d5f commit ccb7a8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
27 changes: 22 additions & 5 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ status_t ta_send_transfer_req_deserialize(const char* const obj,
cJSON* json_result = NULL;
flex_trit_t tag_trits[NUM_TRITS_TAG], address_trits[NUM_TRITS_HASH];
int msg_len = 0, tag_len = 0;
bool raw_message = true;
status_t ret = SC_OK;

if (json_obj == NULL) {
Expand Down Expand Up @@ -288,13 +289,29 @@ status_t ta_send_transfer_req_deserialize(const char* const obj,
goto done;
}

json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message_format");
if (json_result != NULL) {
strncmp("trytes", json_result->valuestring, 6);
raw_message = false;
}

json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message");
if (json_result != NULL) {
msg_len = strlen(json_result->valuestring);
req->msg_len = msg_len * 3;
flex_trits_from_trytes(req->message, req->msg_len,
(const tryte_t*)json_result->valuestring, msg_len,
msg_len);
if (raw_message) {
msg_len = strlen(json_result->valuestring) * 2;
req->msg_len = msg_len * 3;
tryte_t trytes_buffer[msg_len];

ascii_to_trytes(json_result->valuestring, trytes_buffer);
flex_trits_from_trytes(req->message, req->msg_len, trytes_buffer, msg_len,
msg_len);
} else {
msg_len = strlen(json_result->valuestring);
req->msg_len = msg_len * 3;
flex_trits_from_trytes(req->message, req->msg_len,
(const tryte_t*)json_result->valuestring, msg_len,
msg_len);
}
} else {
// 'message' does not exists, set to DEFAULT_MSG
req->msg_len = DEFAULT_MSG_LEN * 3;
Expand Down
1 change: 1 addition & 0 deletions tests/test_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void test_serialize_ta_generate_address(void) {
void test_deserialize_ta_send_transfer(void) {
const char* json =
"{\"value\":100,"
"\"message_format\":\"trytes\","
"\"message\":\"" TAG_MSG "\",\"tag\":\"" TAG_MSG
"\","
"\"address\":\"" TRYTES_81_1 "\"}";
Expand Down

0 comments on commit ccb7a8b

Please sign in to comment.