Skip to content

Commit

Permalink
Merge pull request #526 from Cypherock/release/v0.6.768+
Browse files Browse the repository at this point in the history
Release/v0.6.768+
  • Loading branch information
ParnikaGupta9 authored Jun 29, 2024
2 parents 38f2eff + 7ad67f2 commit 1e2c340
Show file tree
Hide file tree
Showing 35 changed files with 3,601 additions and 9 deletions.
26 changes: 22 additions & 4 deletions apps/btc_family/btc_txn_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ static void update_locktime(btc_verify_input_t *verify_input_data,
return;
}
}

// TODO: Add chunking condition for varint decode
// refer: https://app.clickup.com/t/9002019994/PRF-7288
static int64_t varint_decode(const uint8_t *raw_txn_chunk, int32_t *offset) {
uint8_t first_byte = raw_txn_chunk[*offset];
if (first_byte < 0xFD) {
return first_byte;
} else {
// TODO: var-int varies between 1-9 bytes
// current implementation supports decoding
// upto 3 bytes only
uint8_t result[2];
memcpy(result, raw_txn_chunk + *offset + 1, 2);
*offset += 2;
return U16_READ_LE_ARRAY(result);
}
}
/*****************************************************************************
* GLOBAL FUNCTIONS
*****************************************************************************/
Expand All @@ -414,7 +431,7 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
// store the number of inputs in the raw_txn
verify_input_data->count = raw_txn_chunk[offset++];
// TODO: Improve varint decode.
// size of variable containing ip-count/op-count
// size of variable containing script size and ip-count/op-count
// varies (1-9 Bytes) depending on its value.
// refer:
// https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
Expand Down Expand Up @@ -442,17 +459,18 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
}

case SCRIPT_LENGTH_CASE: {
if (offset + raw_txn_chunk[offset] + 1 + 4 > CHUNK_SIZE) {
int64_t script_length = varint_decode(raw_txn_chunk, &offset);
if (offset + script_length + 1 + 4 > CHUNK_SIZE) {
verify_input_data->prev_offset =
(offset + raw_txn_chunk[offset] + 1 + 4) - CHUNK_SIZE;
(offset + script_length + 1 + 4) - CHUNK_SIZE;
update_hash(
verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE);
verify_input_data->input_parse =
PREVIOUS_TX_HASH_PLUS_OP_INDEX_CASE;
verify_input_data->input_index++;
return 4;
} else {
offset += (raw_txn_chunk[offset] + 1 + 4);
offset += (script_length + 1 + 4);
}
break;
}
Expand Down
197 changes: 197 additions & 0 deletions apps/tron_app/tron_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@

/**
* @file tron_api.c
* @author Cypherock X1 Team
* @brief Defines helpers apis for tron app.
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
*target=_blank>https://mitcc.org/</a>
*
******************************************************************************
* @attention
*
* (c) Copyright 2023 by HODL TECH PTE LTD
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License,
* as defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of
* rights under the License will not include, and the License does not
* grant to you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all
* of the rights granted to you under the License to provide to third
* parties, for a fee or other consideration (including without
* limitation fees for hosting or consulting/ support services related
* to the Software), a product or service whose value derives, entirely
* or substantially, from the functionality of the Software. Any license
* notice or attribution required by the License must also include
* this Commons Clause License Condition notice.
*
* Software: All X1Wallet associated files.
* License: MIT
* Licensor: HODL TECH PTE LTD
*
******************************************************************************
*/

/*****************************************************************************
* INCLUDES
*****************************************************************************/

#include "tron_api.h"

#include <pb_decode.h>
#include <pb_encode.h>

#include "common_error.h"
#include "core_api.h"
#include "events.h"

/*****************************************************************************
* EXTERN VARIABLES
*****************************************************************************/

/*****************************************************************************
* PRIVATE MACROS AND DEFINES
*****************************************************************************/

/*****************************************************************************
* PRIVATE TYPEDEFS
*****************************************************************************/

/*****************************************************************************
* STATIC VARIABLES
*****************************************************************************/

/*****************************************************************************
* GLOBAL VARIABLES
*****************************************************************************/

/*****************************************************************************
* STATIC FUNCTION PROTOTYPES
*****************************************************************************/

/*****************************************************************************
* STATIC FUNCTIONS
*****************************************************************************/

/*****************************************************************************
* GLOBAL FUNCTIONS
*****************************************************************************/
bool decode_tron_query(const uint8_t *data,
uint16_t data_size,
tron_query_t *query_out) {
if (NULL == data || NULL == query_out || 0 == data_size) {
tron_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_DECODING_FAILED);
return false;
}

// zeroise for safety from garbage in the query reference
memzero(query_out, sizeof(tron_query_t));

/* Create a stream that reads from the buffer. */
pb_istream_t stream = pb_istream_from_buffer(data, data_size);

/* Now we are ready to decode the message. */
bool status = pb_decode(&stream, TRON_QUERY_FIELDS, query_out);

/* Send error to host if status is false*/
if (false == status) {
tron_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_DECODING_FAILED);
}

return status;
}

bool encode_tron_result(const tron_result_t *result,
uint8_t *buffer,
uint16_t max_buffer_len,
size_t *bytes_written_out) {
if (NULL == result || NULL == buffer || NULL == bytes_written_out)
return false;

/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, max_buffer_len);

/* Now we are ready to encode the message! */
bool status = pb_encode(&stream, TRON_RESULT_FIELDS, result);

if (true == status) {
*bytes_written_out = stream.bytes_written;
}

return status;
}

bool check_tron_query(const tron_query_t *query, pb_size_t exp_query_tag) {
if ((NULL == query) || (exp_query_tag != query->which_request)) {
tron_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_INVALID_QUERY);
return false;
}
return true;
}

tron_result_t init_tron_result(pb_size_t result_tag) {
tron_result_t result = TRON_RESULT_INIT_ZERO;
result.which_response = result_tag;
return result;
}

void tron_send_error(pb_size_t which_error, uint32_t error_code) {
tron_result_t result = init_tron_result(TRON_RESULT_COMMON_ERROR_TAG);
result.common_error = init_common_error(which_error, error_code);
tron_send_result(&result);
}

void tron_send_result(const tron_result_t *result) {
// TODO: Set the options file for all
uint8_t buffer[1700] = {0};
size_t bytes_encoded = 0;
ASSERT(encode_tron_result(result, buffer, sizeof(buffer), &bytes_encoded));
send_response_to_host(&buffer[0], bytes_encoded);
}

bool tron_get_query(tron_query_t *query, pb_size_t exp_query_tag) {
evt_status_t event = get_events(EVENT_CONFIG_USB, MAX_INACTIVITY_TIMEOUT);

if (true == event.p0_event.flag) {
return false;
}

if (!decode_tron_query(
event.usb_event.p_msg, event.usb_event.msg_size, query)) {
return false;
}

if (!check_tron_query(query, exp_query_tag)) {
return false;
}

return true;
}
114 changes: 114 additions & 0 deletions apps/tron_app/tron_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

/**
* @file tron_api.h
* @author Cypherock X1 Team
* @brief Header file to export some helper functions for the TRON app
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
* target=_blank>https://mitcc.org/</a>
*/
#ifndef TRON_API_H
#define TRON_API_H

/*****************************************************************************
* INCLUDES
*****************************************************************************/

#include <stdint.h>
#include <tron/core.pb.h>

/*****************************************************************************
* MACROS AND DEFINES
*****************************************************************************/

/*****************************************************************************
* TYPEDEFS
*****************************************************************************/

/*****************************************************************************
* EXPORTED VARIABLES
*****************************************************************************/

/*****************************************************************************
* GLOBAL FUNCTION PROTOTYPES
*****************************************************************************/

/**
* @brief API to decode query from host with `TRON_QUERY_FIELDS`
*
* @param[in] data: PB encoded bytestream received from host
* @param[in] data_size: size of pb encoded bytestream
* @param[out] query_out: @ref tron_query_t obj to copy the decoded result to
* @return bool True if decoding was successful, else false
*/
bool decode_tron_query(const uint8_t *data,
uint16_t data_size,
tron_query_t *query_out);

/**
* @brief Encodes the TRON result with `TRON_RESULT_FIELDS` to byte-stream
*
* @param[in] result: object of populated @ref tron_result_t to be encoded
* @param[out] buffer: buffer to fill byte-stream into
* @param[in] max_buffer_len: Max length allowed for writing bytestream to
* buffer
* @param[out] bytes_written_out: bytes written to bytestream
* @return bool True if decoding was successful, else false
*/
bool encode_tron_result(const tron_result_t *result,
uint8_t *buffer,
uint16_t max_buffer_len,
size_t *bytes_written_out);

/**
* @brief This API checks if the `which_request` field of the query of type
* `tron_query_t` matches against the expected tag.
*
* @param query The query of type `tron_query_t` to be checked
* @param exp_query_tag The expected tag of the query
* @return true If the query tag matches the expected tag
* @return false If the query tag does not match the expected tag
*/
bool check_tron_query(const tron_query_t *query, pb_size_t exp_query_tag);

/**
* @brief Returns zero initialized object of type
* tron_result_t result_tag set in result.which_response field
*
* @param result_tag Result tag to be set in the tron_result_t result
* @return tron_result_t Result object of type tron_result_t
*/
tron_result_t init_tron_result(pb_size_t result_tag);

/**
* @brief Send the error to the host.
*
* @param which_error The error type to be sent
* @param error_code The error code to sent to the host
*/
void tron_send_error(pb_size_t which_error, uint32_t error_code);

/**
* @brief This API encodes tron_result_t in protobuf structure.
* @details If the encoding is successful, then it sends the corresponding
* result to the host.
*
* The function ASSERTs the result of encode_tron_result internally.
*
* @param result The result which needs to be sent to the host.
*/
void tron_send_result(const tron_result_t *result);

/**
* @brief This API receives request of type tron_query_t of type
* exp_query_tag from the host.
*
* @param query The reference to which the query needs to be populated
* @param exp_query_tag The expected tag of the query
* @return true If the query was recieved from the host matching the tag
* @return false If the request timed out or the recieved request did not match
* the tag
*/
bool tron_get_query(tron_query_t *query, pb_size_t exp_query_tag);

#endif
Loading

0 comments on commit 1e2c340

Please sign in to comment.