Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7d47926
feat: Core proto options
TejasvOnly Oct 22, 2024
67c0bf4
feat: Include common submodule
TejasvOnly Oct 23, 2024
e781002
feat: Options file for get public key proto
TejasvOnly Oct 24, 2024
771c910
feat: Options file for generic txn signing
TejasvOnly Oct 25, 2024
36d5c08
feat: Define icp apis for device
TejasvOnly Oct 29, 2024
d9e1550
feat: Define context for icp
TejasvOnly Oct 31, 2024
ea99e9e
feat: Create icp helpers
TejasvOnly Nov 2, 2024
bfac06d
feat: Create private headers
TejasvOnly Nov 6, 2024
abbb67a
feat: Icp publick key implementation
TejasvOnly Nov 8, 2024
9cf9292
feat: Icp generic transaction implementation
TejasvOnly Nov 12, 2024
27aa48b
feat: Create main entry point for icp app
TejasvOnly Nov 14, 2024
52f999b
chore: Remove aboslete code and use correct hasher
TejasvOnly Nov 16, 2024
e4b0a03
fix: Remove obsolete code
TejasvOnly Nov 21, 2024
a705124
fix: Update obsolete macros
TejasvOnly Nov 22, 2024
285dd0d
fix: Incorrect derivation path
TejasvOnly Nov 22, 2024
23a793e
feat: Display principal and account id
TejasvOnly Nov 23, 2024
5f3f7f3
feat: Add ui text for icp public key generation
TejasvOnly Nov 23, 2024
7d737a2
feat: Added icp app to core app registry
muzaffarbhat07 Feb 3, 2025
0a3ce68
fix: Icp principal, account id and principal id derivation
muzaffarbhat07 Feb 3, 2025
e7bd35f
fix: Added icp app to cmake
muzaffarbhat07 Mar 1, 2025
fb8493c
feat: Include common submodule
TejasvOnly Oct 23, 2024
c69aca6
feat: Add ui text for icp public key generation
TejasvOnly Nov 23, 2024
fd8f8bc
feat: Added icp transfer txn parser
muzaffarbhat07 Feb 13, 2025
c514606
feat: Icp hash and sign txn request data
muzaffarbhat07 Feb 15, 2025
1b8a9cd
fix: Icp sign txn input data format
muzaffarbhat07 Feb 16, 2025
283510b
feat: Added read_state_request signature to icp sign txn
muzaffarbhat07 Feb 27, 2025
48f6676
fix: Resolve review comments
muzaffarbhat07 Feb 28, 2025
4e2acaf
fix: Update upload-artifact version to fix ci
muzaffarbhat07 Mar 1, 2025
83760dd
fix: Resolved review comments
muzaffarbhat07 Mar 1, 2025
f34ca71
Merge pull request #626 from Cypherock/poc/icp/sign-txn
muzaffarbhat07 Mar 1, 2025
e84157e
fix: Code formatting
muzaffarbhat07 Mar 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
bash ./utilities/ci/static-analyzer.sh ${SHA_BASE}
fi
- name: Upload result
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: clang-tidy-result
path: anaylysis.results
Expand Down
196 changes: 196 additions & 0 deletions apps/icp_app/icp_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* @file icp_api.c
* @author Cypherock X1 Team
* @brief Defines helpers apis for ICP app.
* @copyright Copyright (c) 2024 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 2024 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 "icp_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_icp_query(const uint8_t *data,
uint16_t data_size,
icp_query_t *query_out) {
if (NULL == data || NULL == query_out || 0 == data_size) {
icp_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(icp_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, ICP_QUERY_FIELDS, query_out);

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

return status;
}

bool encode_icp_result(const icp_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, ICP_RESULT_FIELDS, result);

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

return status;
}

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

icp_result_t init_icp_result(pb_size_t result_tag) {
icp_result_t result = ICP_RESULT_INIT_ZERO;
result.which_response = result_tag;
return result;
}

void icp_send_error(pb_size_t which_error, uint32_t error_code) {
icp_result_t result = init_icp_result(ICP_RESULT_COMMON_ERROR_TAG);
result.common_error = init_common_error(which_error, error_code);
icp_send_result(&result);
}

void icp_send_result(const icp_result_t *result) {
// TODO: Set all option files
uint8_t buffer[1700] = {0};
size_t bytes_encoded = 0;
ASSERT(encode_icp_result(result, buffer, sizeof(buffer), &bytes_encoded));
send_response_to_host(&buffer[0], bytes_encoded);
}

bool icp_get_query(icp_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_icp_query(
event.usb_event.p_msg, event.usb_event.msg_size, query)) {
return false;
}

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

return true;
}
113 changes: 113 additions & 0 deletions apps/icp_app/icp_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @file icp_api.h
* @author Cypherock X1 Team
* @brief Header file to export some helper functions for the ICP app
* @copyright Copyright (c) 2024 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 ICP_API_H
#define ICP_API_H

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

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

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

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

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

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

/**
* @brief API to decode query from host with `ICP_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 icp_query_t obj to copy the decoded result to
* @return bool True if decoding was successful, else false
*/
bool decode_icp_query(const uint8_t *data,
uint16_t data_size,
icp_query_t *query_out);

/**
* @brief Encodes the ICP result with `ICP_RESULT_FIELDS` to byte-stream
*
* @param[in] result: object of populated @ref icp_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_icp_result(const icp_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
* `icp_query_t` matches against the expected tag.
*
* @param query The query of type `icp_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_icp_query(const icp_query_t *query, pb_size_t exp_query_tag);

/**
* @brief Returns zero initialized object of type
* icp_result_t result_tag set in result.which_response field
*
* @param result_tag Result tag to be set in the icp_result_t result
* @return icp_result_t Result object of type icp_result_t
*/
icp_result_t init_icp_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 icp_send_error(pb_size_t which_error, uint32_t error_code);

/**
* @brief This API encodes icp_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_icp_result internally.
*
* @param result The result which needs to be sent to the host.
*/
void icp_send_result(const icp_result_t *result);

/**
* @brief This API receives request of type icp_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 icp_get_query(icp_query_t *query, pb_size_t exp_query_tag);

#endif
Loading
Loading