Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
mobile: android bindings implementation for trinity (#810)
Browse files Browse the repository at this point in the history
* common/helpers: clean sign

* common/helpers: trits version of iota_sign_address_gen

* common/helpers: trits version of iota_sign_signature_gen

* mobile: link libmath

* mobile: add iota_digest binding

* mobile: implement pow_bundle binding

* mobile: fix function name

* helpers: cast callocs for c++/objC compilation

* common: cast all allocations of entangled for c++/objC compilation

* common: more casts

* common/helpers: sign clean

* common/helpers: sanity checks
  • Loading branch information
thibault-martinez authored Feb 15, 2019
1 parent 08b63e0 commit 3c2eb5c
Show file tree
Hide file tree
Showing 31 changed files with 439 additions and 213 deletions.
2 changes: 1 addition & 1 deletion cclient/request/get_transactions_to_approve.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void get_transactions_to_approve_req_set_reference(
get_transactions_to_approve_req_t* const req,
flex_trit_t const* const reference) {
if (req->reference == NULL) {
req->reference = malloc(FLEX_TRIT_SIZE_243);
req->reference = (flex_trit_t*)malloc(FLEX_TRIT_SIZE_243);
}
memcpy(req->reference, reference, FLEX_TRIT_SIZE_243);
}
3 changes: 2 additions & 1 deletion ciri/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ static struct option* build_options() {
while (cli_arguments_g[nbr].desc) {
nbr++;
}
struct option* options = malloc((nbr + 1) * sizeof(struct option));
struct option* options =
(struct option*)malloc((nbr + 1) * sizeof(struct option));
size_t i;
for (i = 0; i < nbr; i++) {
options[i].name = cli_arguments_g[i].name;
Expand Down
8 changes: 5 additions & 3 deletions common/curl-p/pearl_diver.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ PearlDiverStatus pd_search(Curl *const ctx, unsigned short const offset,
return PEARL_DIVER_ERROR;
}

SearchInstance *inst = calloc(n_procs, sizeof(SearchInstance));
thread_handle_t *tid = calloc(n_procs, sizeof(thread_handle_t));
SearchInstance *inst =
(SearchInstance *)calloc(n_procs, sizeof(SearchInstance));
thread_handle_t *tid =
(thread_handle_t *)calloc(n_procs, sizeof(thread_handle_t));

{
PCurl curl;
Expand All @@ -74,7 +76,7 @@ PearlDiverStatus pd_search(Curl *const ctx, unsigned short const offset,
if (tid[i] == 0) continue;

if (found_index == -1) {
thread_handle_join(tid[i], (void *)&found_index);
thread_handle_join(tid[i], (void **)&found_index);

if (found_index != -1) {
found_thread = i;
Expand Down
5 changes: 5 additions & 0 deletions common/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ extern "C" {

#define RC_MODULE_MAM2 (0xB0 << RC_SHIFT_MODULE)

#define RC_MODULE_HELPERS (0xB1 << RC_SHIFT_MODULE)

/* error code module specific */
#define RC_ERRORCODE_MASK 0x003F

Expand Down Expand Up @@ -475,6 +477,9 @@ enum retcode_t {
RC_MAM2_ENDPOINT_NOT_TRUSTED = 0x13 | RC_MODULE_MAM2 | RC_SEVERITY_MODERATE,
RC_MAM2_KEYLOAD_IRRELEVANT = 0x14 | RC_MODULE_MAM2 | RC_SEVERITY_MODERATE,
RC_MAM2_KEYLOAD_OVERLOADED = 0x15 | RC_MODULE_MAM2 | RC_SEVERITY_MODERATE,

// Helpers Module
RC_HELPERS_POW_INVALID_TX = 0x01 | RC_MODULE_HELPERS | RC_SEVERITY_MODERATE,
};

typedef enum retcode_t retcode_t;
Expand Down
4 changes: 2 additions & 2 deletions common/helpers/checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ IOTA_EXPORT char* iota_checksum(const char* input, const size_t input_length,
}

trit_t trits_hash[HASH_LENGTH_TRIT];
trit_t* trits = calloc(input_length * RADIX, sizeof(trit_t));
trit_t* trits = (trit_t*)calloc(input_length * RADIX, sizeof(trit_t));
if (!trits) {
return NULL;
}
trytes_to_trits((tryte_t*)input, trits, input_length);
kerl_hash(trits, input_length * RADIX, trits_hash, &kerl);
free(trits);

char* checksum_trytes = calloc(checksum_length + 1, sizeof(tryte_t));
char* checksum_trytes = (char*)calloc(checksum_length + 1, sizeof(tryte_t));
if (!checksum_trytes) {
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions common/helpers/digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ IOTA_EXPORT char* iota_digest(const char* trytes) {

trit_t trits_hash[HASH_LENGTH_TRIT];
size_t length = strnlen(trytes, TRYTE_LENGTH);
trit_t* trits = calloc(length * RADIX, sizeof(trit_t));
trit_t* trits = (trit_t*)calloc(length * RADIX, sizeof(trit_t));
if (!trits) {
return NULL;
}
trytes_to_trits((tryte_t*)trytes, trits, length);
curl_digest(trits, length * 3, trits_hash, &curl);
free(trits);

char* hash = calloc(HASH_LENGTH_TRYTE + 1, sizeof(trit_t));
char* hash = (char*)calloc(HASH_LENGTH_TRYTE + 1, sizeof(trit_t));
if (!hash) {
return NULL;
}
Expand Down
36 changes: 26 additions & 10 deletions common/helpers/pow.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,24 @@ IOTA_EXPORT retcode_t iota_pow_bundle(bundle_transactions_t *const bundle,
flex_trit_t const *const trunk,
flex_trit_t const *const branch,
uint8_t const mwm) {
iota_transaction_t *tx;
flex_trit_t *nonce, *txflex, *ctrunk;
iota_transaction_t *tx = NULL;
flex_trit_t *nonce = NULL;
flex_trit_t *txflex = NULL;
flex_trit_t *ctrunk = NULL;
size_t cur_idx = 0;

if (bundle == NULL || trunk == NULL || branch == NULL) {
return RC_NULL_PARAM;
}

if (bundle_transactions_size(bundle) == 0) {
return RC_OK;
}

tx = (iota_transaction_t *)utarray_front(bundle);
cur_idx = tx->essence.last_index + 1;

ctrunk = trunk;
ctrunk = (flex_trit_t *)trunk;

do {
cur_idx--;
Expand All @@ -103,22 +113,24 @@ IOTA_EXPORT retcode_t iota_pow_bundle(bundle_transactions_t *const bundle,
tx = (iota_transaction_t *)utarray_next(bundle, tx))
;

if (tx == NULL) {
return RC_HELPERS_POW_INVALID_TX;
}

// Set trunk & branch
transaction_set_trunk(tx, ctrunk);
transaction_set_branch(tx, branch);
transaction_set_attachment_timestamp(tx, current_timestamp_ms());
transaction_set_attachment_timestamp_lower(tx, 0);
transaction_set_attachment_timestamp_upper(tx, 3812798742493LL);

txflex = transaction_serialize(tx);

if (txflex == NULL) {
if ((txflex = transaction_serialize(tx)) == NULL) {
return RC_OOM;
}

// Do PoW
nonce = iota_pow_flex(txflex, NUM_TRITS_SERIALIZED_TRANSACTION, mwm);
if (nonce == NULL) {
if ((nonce = iota_pow_flex(txflex, NUM_TRITS_SERIALIZED_TRANSACTION,
mwm)) == NULL) {
return RC_OOM;
}
transaction_set_nonce(tx, nonce);
Expand All @@ -128,12 +140,16 @@ IOTA_EXPORT retcode_t iota_pow_bundle(bundle_transactions_t *const bundle,
free(ctrunk);
}

ctrunk = iota_flex_digest(txflex, NUM_TRITS_SERIALIZED_TRANSACTION);
if ((ctrunk = iota_flex_digest(txflex, NUM_TRITS_SERIALIZED_TRANSACTION)) ==
NULL) {
return RC_OOM;
}
free(txflex);
} while (cur_idx != 0);

if (ctrunk != trunk) {
free(ctrunk);
}

return RC_OK;
}
}
Loading

0 comments on commit 3c2eb5c

Please sign in to comment.