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

Commit

Permalink
feat: Syncronize serializer to future MAM API spec
Browse files Browse the repository at this point in the history
APIs relates to MAM is under refactorin. The request/response format is
different from the old ones.
  • Loading branch information
howjmay committed Oct 29, 2019
1 parent 79c5765 commit aa219b2
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 107 deletions.
29 changes: 24 additions & 5 deletions request/ta_send_mam.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
ta_send_mam_req_t* send_mam_req_new() {
ta_send_mam_req_t* req = (ta_send_mam_req_t*)malloc(sizeof(ta_send_mam_req_t));
if (req) {
req->prng[0] = 0;
req->payload = NULL;
req->seed = NULL;
req->message = NULL;
req->channel_ord = 0;
req->ch_mss_depth = 6;
req->ep_mss_depth = 6;
req->psk = NULL;
req->ntru_pk = NULL;
}

return req;
Expand All @@ -24,9 +28,24 @@ void send_mam_req_free(ta_send_mam_req_t** req) {
return;
}

if ((*req)->payload) {
free((*req)->payload);
(*req)->payload = NULL;
if ((*req)->seed) {
free((*req)->seed);
(*req)->seed = NULL;
}

if ((*req)->message) {
free((*req)->message);
(*req)->message = NULL;
}

if ((*req)->ntru_pk) {
free((*req)->ntru_pk);
(*req)->ntru_pk = NULL;
}

if ((*req)->psk) {
free((*req)->psk);
(*req)->psk = NULL;
}

free(*req);
Expand Down
15 changes: 13 additions & 2 deletions request/ta_send_mam.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@ extern "C" {

/** struct of ta_send_transfer_req_t */
typedef struct send_mam_req_s {
tryte_t prng[NUM_TRYTES_ADDRESS + 1];
char* payload;
/** Optional. MAM channel seed */
tryte_t* seed;
/** Optional. The channel ordinal. */
int32_t channel_ord;
/** Required. The message will be append to the channel. */
char* message;
/** Optional. The depth of channel merkle tree. */
int32_t ch_mss_depth;
/** Optional. The depth of endpoint merkle tree. */
int32_t ep_mss_depth;
/** Optional. The pre-shared key to encrypt the message. Length: 81 trytes. Default: NULL. */
tryte_t* psk;
/** Optional. The NTRU public key to encrypt the message. Length: 1024 trytes. Default: NULL. */
tryte_t* ntru_pk;
} ta_send_mam_req_t;

/**
Expand Down
1 change: 1 addition & 0 deletions response/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cc_library(
"//accelerator:ta_errors",
"@entangled//common:errors",
"@entangled//common/model:transaction",
"@entangled//mam/mam:message",
"@entangled//utils/containers/hash:hash243_queue",
"@entangled//utils/containers/hash:hash243_stack",
],
Expand Down
44 changes: 42 additions & 2 deletions response/ta_send_mam.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,48 @@ status_t send_mam_res_set_channel_id(ta_send_mam_res_t* res, const tryte_t* chan
return SC_RES_NULL;
}

memcpy(res->channel_id, channel_id, NUM_TRYTES_HASH);
res->channel_id[NUM_TRYTES_HASH] = '\0';
memcpy(res->chid, channel_id, NUM_TRYTES_HASH);
res->chid[NUM_TRYTES_HASH] = '\0';
return SC_OK;
}

status_t send_mam_res_set_endpoint_id(ta_send_mam_res_t* res, const tryte_t* endpoint_id) {
if (!endpoint_id || !res) {
return SC_RES_NULL;
}

memcpy(res->epid, endpoint_id, NUM_TRYTES_HASH);
res->epid[NUM_TRYTES_HASH] = '\0';
return SC_OK;
}

status_t send_mam_res_set_msg_id(ta_send_mam_res_t* res, const tryte_t* msg_id) {
if (!msg_id || !res) {
return SC_RES_NULL;
}

memcpy(res->msg_id, msg_id, NUM_TRYTES_MAM_MSG_ID);
res->msg_id[NUM_TRYTES_MAM_MSG_ID] = '\0';
return SC_OK;
}

status_t send_mam_res_set_announcement_bundle_hash(ta_send_mam_res_t* res, const tryte_t* announcement_bundle_hash) {
if (!announcement_bundle_hash || !res) {
return SC_RES_NULL;
}

memcpy(res->announcement_bundle_hash, announcement_bundle_hash, NUM_TRYTES_HASH);
res->announcement_bundle_hash[NUM_TRYTES_HASH] = '\0';
return SC_OK;
}

status_t send_mam_res_set_chid1(ta_send_mam_res_t* res, const tryte_t* chid1) {
if (!chid1 || !res) {
return SC_RES_NULL;
}

memcpy(res->chid1, chid1, NUM_TRYTES_HASH);
res->chid[NUM_TRYTES_HASH] = '\0';
return SC_OK;
}

Expand Down
69 changes: 67 additions & 2 deletions response/ta_send_mam.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "accelerator/errors.h"
#include "common/model/transaction.h"
#include "common/trinary/tryte.h"
#include "mam/mam/message.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -21,14 +22,22 @@ extern "C" {
* @file response/ta_send_mam.h
*/

#define NUM_TRYTES_MAM_MSG_ID MAM_MSG_ID_SIZE / 3

/** struct of ta_send_mam_res_t */
typedef struct send_mam_res_s {
/** ascii string bundle hash */
char bundle_hash[NUM_TRYTES_HASH + 1];
/** ascii string channel id */
char channel_id[NUM_TRYTES_HASH + 1];
char chid[NUM_TRYTES_HASH + 1];
/** ascii string endpoint id */
char epid[NUM_TRYTES_HASH + 1];
/** channel ordinal which is the number of channel we generated */
int32_t channel_ord;
char msg_id[NUM_TRYTES_MAM_MSG_ID + 1];
/** bundle hash of announcement bundle */
char announcement_bundle_hash[NUM_TRYTES_HASH + 1];
/** ascii string the next channel id */
char chid1[NUM_TRYTES_HASH + 1];
} ta_send_mam_res_t;

/**
Expand Down Expand Up @@ -72,6 +81,62 @@ status_t send_mam_res_set_bundle_hash(ta_send_mam_res_t* res, const tryte_t* bun
*/
status_t send_mam_res_set_channel_id(ta_send_mam_res_t* res, const tryte_t* channel_id);

/**
* @brief Set the endpoint_id field of send_mam_res object.
*
* @param[in] res ta_send_mam_res_t struct object
* @param[in] endpoint_id endpoint id decoded in trytes string
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t send_mam_res_set_endpoint_id(ta_send_mam_res_t* res, const tryte_t* endpoint_id);

/**
* @brief Set the msgl_id field of send_mam_res object.
*
* @param[in] res ta_send_mam_res_t struct object
* @param[in] msg_id Message id decoded in trytes string
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t send_mam_res_set_msg_id(ta_send_mam_res_t* res, const tryte_t* msg_id);

/**
* @brief Set the announcement_bundle_hash field of send_mam_res object.
*
* Receive a announcement bundle hash tryte_t array which is 81 trytes long,
* and convert (instead of decoding) the received announcement bundle hash to ascii string.
* After conversion, set the announcement_bundle_hash field of send_mam_res object.
*
* @param[in] res ta_send_mam_res_t struct object
* @param[in] announcement_bundle_hash announcement bundle hash decoded in trytes string
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t send_mam_res_set_announcement_bundle_hash(ta_send_mam_res_t* res, const tryte_t* announcement_bundle_hash);

/**
* @brief Set the next channel_id field of send_mam_res object.
*
* Receive a chid1 tryte_t array which is 81 trytes long,
* and convert (instead of decoding) the received chid1 to ascii string.
* After conversion, set the chid1 field of send_mam_res object.
*
* @param[in] res ta_send_mam_res_t struct object
* @param[in] chid1 chid1 decoded in trytes string
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t send_mam_res_set_chid1(ta_send_mam_res_t* res, const tryte_t* chid1);

/**
* Free memory of ta_send_mam_res_t
*
Expand Down
Loading

0 comments on commit aa219b2

Please sign in to comment.