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

Commit ccb7a8b

Browse files
author
Yu Wei Wu
committed
feat(serialzer): Add message format in send transfer
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.
1 parent 5fb0d5f commit ccb7a8b

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

serializer/serializer.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ status_t ta_send_transfer_req_deserialize(const char* const obj,
243243
cJSON* json_result = NULL;
244244
flex_trit_t tag_trits[NUM_TRITS_TAG], address_trits[NUM_TRITS_HASH];
245245
int msg_len = 0, tag_len = 0;
246+
bool raw_message = true;
246247
status_t ret = SC_OK;
247248

248249
if (json_obj == NULL) {
@@ -288,13 +289,29 @@ status_t ta_send_transfer_req_deserialize(const char* const obj,
288289
goto done;
289290
}
290291

292+
json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message_format");
293+
if (json_result != NULL) {
294+
strncmp("trytes", json_result->valuestring, 6);
295+
raw_message = false;
296+
}
297+
291298
json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message");
292299
if (json_result != NULL) {
293-
msg_len = strlen(json_result->valuestring);
294-
req->msg_len = msg_len * 3;
295-
flex_trits_from_trytes(req->message, req->msg_len,
296-
(const tryte_t*)json_result->valuestring, msg_len,
297-
msg_len);
300+
if (raw_message) {
301+
msg_len = strlen(json_result->valuestring) * 2;
302+
req->msg_len = msg_len * 3;
303+
tryte_t trytes_buffer[msg_len];
304+
305+
ascii_to_trytes(json_result->valuestring, trytes_buffer);
306+
flex_trits_from_trytes(req->message, req->msg_len, trytes_buffer, msg_len,
307+
msg_len);
308+
} else {
309+
msg_len = strlen(json_result->valuestring);
310+
req->msg_len = msg_len * 3;
311+
flex_trits_from_trytes(req->message, req->msg_len,
312+
(const tryte_t*)json_result->valuestring, msg_len,
313+
msg_len);
314+
}
298315
} else {
299316
// 'message' does not exists, set to DEFAULT_MSG
300317
req->msg_len = DEFAULT_MSG_LEN * 3;

tests/test_serializer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void test_serialize_ta_generate_address(void) {
4747
void test_deserialize_ta_send_transfer(void) {
4848
const char* json =
4949
"{\"value\":100,"
50+
"\"message_format\":\"trytes\","
5051
"\"message\":\"" TAG_MSG "\",\"tag\":\"" TAG_MSG
5152
"\","
5253
"\"address\":\"" TRYTES_81_1 "\"}";

0 commit comments

Comments
 (0)