|
1 | 1 | #include "apis.h" |
2 | 2 |
|
3 | | -// TODO: Generate actual pre shared keys |
4 | | -static mam_psk_t const psk = { |
5 | | - .id = {1, 0, -1, -1, 0, -1, -1, 0, 0, 1, -1, 0, 1, 0, 0, 1, 1, |
6 | | - 1, -1, 1, 1, 0, 1, 1, 0, 0, -1, 1, -1, -1, -1, -1, -1, -1, |
7 | | - -1, 1, -1, -1, 0, -1, -1, 1, 0, -1, -1, -1, 1, 1, 1, 0, 0, |
8 | | - -1, 1, -1, -1, -1, 0, -1, 1, -1, -1, -1, 1, 1, -1, 1, 0, 0, |
9 | | - 1, 1, 1, -1, -1, 0, 0, -1, -1, 1, 0, -1, 1}, |
10 | | - .key = {-1, 1, -1, -1, 1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1, -1, -1, |
11 | | - -1, 0, 0, 0, 0, -1, -1, 1, 1, 1, 0, -1, -1, -1, 0, 0, 0, |
12 | | - -1, -1, 1, -1, 0, 0, 1, 0, 0, -1, 1, 1, 0, -1, 0, 0, 1, |
13 | | - -1, 1, 0, 1, 0, 0, -1, 1, 1, -1, 1, 0, -1, 0, -1, 1, -1, |
14 | | - -1, -1, 0, -1, -1, 0, -1, -1, 0, 0, -1, -1, 1, -1, 0, 0, -1, |
15 | | - -1, -1, -1, 0, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 0, 1, |
16 | | - 0, 1, -1, 0, 0, 1, 0, 1, 0, 0, 1, 0, -1, 0, 1, 1, 0, |
17 | | - 0, -1, -1, 1, 1, 0, 0, 1, -1, 1, 1, 1, 0, 1, 1, 1, 0, |
18 | | - 0, -1, -1, -1, -1, 1, 1, 1, 0, 0, -1, 0, 1, -1, 1, 1, 1, |
19 | | - 0, 0, 1, -1, -1, 0, -1, 1, -1, 1, 0, 0, 1, -1, 0, 1, -1, |
20 | | - 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, -1, 1, -1, 1, 0, 1, 1, |
21 | | - 1, -1, 0, 0, -1, 1, 1, 0, -1, -1, 0, 0, -1, 1, 0, 1, -1, |
22 | | - 0, 0, -1, 1, -1, 1, 1, 1, -1, 0, 1, 1, 0, 0, -1, -1, -1, |
23 | | - 0, 0, 1, 0, 1, 0, -1, 1, -1, 0, 1, 0, -1, 1, 1, -1, -1, |
24 | | - 0, 0, -1, 0, -1}}; |
25 | | - |
26 | 3 | status_t api_get_tips(const iota_client_service_t* const service, |
27 | 4 | char** json_result) { |
28 | 5 | status_t ret = SC_OK; |
@@ -54,7 +31,7 @@ status_t api_get_tips_pair(const iota_config_t* const tangle, |
54 | 31 | goto done; |
55 | 32 | } |
56 | 33 |
|
57 | | - ret = cclient_get_txn_to_approve(service, tangle->depth, res); |
| 34 | + ret = cclient_get_txn_to_approve(service, tangle->milestone_depth, res); |
58 | 35 | if (ret) { |
59 | 36 | goto done; |
60 | 37 | } |
@@ -178,7 +155,6 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, |
178 | 155 | } |
179 | 156 |
|
180 | 157 | // Set first transaction's address as chid, if no `chid` specified |
181 | | - mam_psk_t_set_add(&mam.psks, &psk); |
182 | 158 | iota_transaction_t* curr_tx = (iota_transaction_t*)utarray_eltptr(bundle, 0); |
183 | 159 | none_chid_trytes = (tryte_t*)malloc(sizeof(tryte_t) * NUM_TRYTES_ADDRESS); |
184 | 160 | flex_trits_to_trytes(none_chid_trytes, NUM_TRYTES_ADDRESS, |
@@ -221,6 +197,83 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, |
221 | 197 | return ret; |
222 | 198 | } |
223 | 199 |
|
| 200 | +status_t api_mam_send_message(const iota_config_t* const tangle, |
| 201 | + const iota_client_service_t* const service, |
| 202 | + char const* const payload, char** json_result) { |
| 203 | + status_t ret = SC_OK; |
| 204 | + retcode_t rc = RC_OK; |
| 205 | + mam_api_t mam; |
| 206 | + const bool last_packet = true; |
| 207 | + bundle_transactions_t* bundle = NULL; |
| 208 | + bundle_transactions_new(&bundle); |
| 209 | + tryte_t channel_id[MAM_CHANNEL_ID_SIZE]; |
| 210 | + trit_t msg_id[MAM_MSG_ID_SIZE]; |
| 211 | + send_mam_req_t* req = send_mam_req_new(); |
| 212 | + send_mam_res_t* res = send_mam_res_new(); |
| 213 | + |
| 214 | + // Loading and creating MAM API |
| 215 | + if ((rc = mam_api_load(tangle->mam_file, &mam)) == |
| 216 | + RC_UTILS_FAILED_TO_OPEN_FILE) { |
| 217 | + if (mam_api_init(&mam, (tryte_t*)SEED)) { |
| 218 | + ret = SC_MAM_FAILED_INIT; |
| 219 | + goto done; |
| 220 | + } |
| 221 | + } else if (rc != RC_OK) { |
| 222 | + ret = SC_MAM_FAILED_INIT; |
| 223 | + goto done; |
| 224 | + } |
| 225 | + |
| 226 | + ret = send_mam_req_deserialize(payload, req); |
| 227 | + if (ret) { |
| 228 | + goto done; |
| 229 | + } |
| 230 | + |
| 231 | + // Create mam channel |
| 232 | + if (mam_channel_t_set_size(mam.channels) == 0) { |
| 233 | + mam_api_create_channel(&mam, tangle->mss_depth, channel_id); |
| 234 | + } else { |
| 235 | + mam_channel_t* channel = &mam.channels->value; |
| 236 | + trits_to_trytes(trits_begin(mam_channel_id(channel)), channel_id, |
| 237 | + NUM_TRITS_ADDRESS); |
| 238 | + } |
| 239 | + |
| 240 | + // Write header and packet |
| 241 | + if (mam_api_bundle_write_header_on_channel(&mam, channel_id, NULL, NULL, 0, |
| 242 | + bundle, msg_id) != RC_OK) { |
| 243 | + ret = SC_MAM_FAILED_WRITE; |
| 244 | + goto done; |
| 245 | + } |
| 246 | + if (mam_api_bundle_write_packet(&mam, msg_id, req->payload_trytes, |
| 247 | + req->payload_trytes_size, 0, last_packet, |
| 248 | + bundle) != RC_OK) { |
| 249 | + ret = SC_MAM_FAILED_WRITE; |
| 250 | + goto done; |
| 251 | + } |
| 252 | + send_mam_res_set_channel_id(res, channel_id); |
| 253 | + |
| 254 | + // Sending bundle |
| 255 | + if (ta_send_bundle(tangle, service, bundle) != SC_OK) { |
| 256 | + ret = SC_MAM_FAILED_RESPONSE; |
| 257 | + goto done; |
| 258 | + } |
| 259 | + send_mam_res_set_bundle_hash( |
| 260 | + res, transaction_bundle((iota_transaction_t*)utarray_front(bundle))); |
| 261 | + |
| 262 | + ret = send_mam_res_serialize(json_result, res); |
| 263 | + |
| 264 | +done: |
| 265 | + // Save and destroying MAM API |
| 266 | + if (ret != SC_MAM_FAILED_INIT) { |
| 267 | + if (mam_api_save(&mam, tangle->mam_file) || mam_api_destroy(&mam)) { |
| 268 | + ret = SC_MAM_FAILED_DESTROYED; |
| 269 | + } |
| 270 | + } |
| 271 | + bundle_transactions_free(&bundle); |
| 272 | + send_mam_req_free(&req); |
| 273 | + send_mam_res_free(&res); |
| 274 | + return ret; |
| 275 | +} |
| 276 | + |
224 | 277 | status_t api_send_transfer(const iota_config_t* const tangle, |
225 | 278 | const iota_client_service_t* const service, |
226 | 279 | const char* const obj, char** json_result) { |
|
0 commit comments