Skip to content

Commit 465ec3f

Browse files
Merge pull request #655 from Cypherock/develop
Develop
2 parents 7ebc5ca + 5422597 commit 465ec3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4740
-30
lines changed

apps/btc_family/btc_pub_key.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,17 @@
6060
* INCLUDES
6161
*****************************************************************************/
6262

63+
#include <string.h>
64+
6365
#include "bip32.h"
6466
#include "btc_api.h"
67+
#include "btc_app.h"
6568
#include "btc_helpers.h"
6669
#include "btc_priv.h"
6770
#include "coin_utils.h"
71+
#include "composable_app_queue.h"
6872
#include "curves.h"
73+
#include "exchange_main.h"
6974
#include "reconstruct_wallet_flow.h"
7075
#include "status_api.h"
7176
#include "ui_core_confirm.h"
@@ -152,6 +157,7 @@ static void send_public_key(const uint8_t *public_key);
152157
/*****************************************************************************
153158
* STATIC VARIABLES
154159
*****************************************************************************/
160+
static bool sign_address = false;
155161

156162
static bool check_which_request(const btc_query_t *query,
157163
pb_size_t which_request) {
@@ -173,6 +179,17 @@ static bool validate_request_data(btc_get_public_key_request_t *request) {
173179
ERROR_DATA_FLOW_INVALID_DATA);
174180
status = false;
175181
}
182+
183+
caq_node_data_t data = {.applet_id = get_btc_app_desc()->id};
184+
185+
memzero(data.params, sizeof(data.params));
186+
memcpy(data.params,
187+
request->initiate.wallet_id,
188+
sizeof(request->initiate.wallet_id));
189+
data.params[32] = EXCHANGE_FLOW_TAG_RECEIVE;
190+
191+
sign_address = exchange_app_validate_caq(data);
192+
176193
return status;
177194
}
178195

@@ -283,6 +300,11 @@ void btc_get_pub_key(btc_query_t *query) {
283300
delay_scr_init(ui_text_processing, DELAY_SHORT);
284301
size_t length = btc_get_address(seed, path, path_length, public_key, msg);
285302
memzero(seed, sizeof(seed));
303+
304+
if (sign_address) {
305+
exchange_sign_address(msg, sizeof(msg));
306+
}
307+
286308
if (0 < length &&
287309
true == core_scroll_page(ui_text_receive_on, msg, btc_send_error)) {
288310
set_app_flow_status(BTC_GET_PUBLIC_KEY_STATUS_VERIFY);

apps/btc_family/btc_txn.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
#include "bip32.h"
6868
#include "btc_api.h"
69+
#include "btc_app.h"
6970
#include "btc_helpers.h"
7071
#include "btc_inputs_validator.h"
7172
#include "btc_priv.h"
@@ -74,8 +75,10 @@
7475
#include "byte_stream.h"
7576
#include "coin_utils.h"
7677
#include "common.pb.h"
78+
#include "composable_app_queue.h"
7779
#include "constant_texts.h"
7880
#include "curves.h"
81+
#include "exchange_main.h"
7982
#include "reconstruct_wallet_flow.h"
8083
#include "status_api.h"
8184
#include "ui_core_confirm.h"
@@ -274,7 +277,7 @@ static bool send_script_sig(btc_query_t *query, const scrip_sig_t *sigs);
274277
/*****************************************************************************
275278
* STATIC VARIABLES
276279
*****************************************************************************/
277-
280+
static bool use_signature_verification = false;
278281
static btc_txn_context_t *btc_txn_context = NULL;
279282

280283
/*****************************************************************************
@@ -305,6 +308,17 @@ static bool validate_request_data(const btc_sign_txn_request_t *request) {
305308
ERROR_DATA_FLOW_INVALID_DATA);
306309
status = false;
307310
}
311+
312+
caq_node_data_t data = {.applet_id = get_btc_app_desc()->id};
313+
314+
memzero(data.params, sizeof(data.params));
315+
memcpy(data.params,
316+
request->initiate.wallet_id,
317+
sizeof(request->initiate.wallet_id));
318+
data.params[32] = EXCHANGE_FLOW_TAG_SEND;
319+
320+
use_signature_verification = exchange_app_validate_caq(data);
321+
308322
return status;
309323
}
310324

@@ -572,6 +586,14 @@ static bool get_user_verification() {
572586
btc_send_error(ERROR_COMMON_ERROR_UNKNOWN_ERROR_TAG, status);
573587
return false;
574588
}
589+
590+
if (use_signature_verification) {
591+
if (!exchange_validate_stored_signature(address, sizeof(address))) {
592+
btc_send_error(ERROR_COMMON_ERROR_UNKNOWN_ERROR_TAG, status);
593+
return false;
594+
}
595+
}
596+
575597
if (!core_scroll_page(title, address, btc_send_error) ||
576598
!core_scroll_page(title, value, btc_send_error)) {
577599
return false;
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/**
2+
* @file constellation_api.c
3+
* @author Cypherock X1 Team
4+
* @brief Defines helpers apis for CONSTELLATION app.
5+
* @copyright Copyright (c) 2025 HODL TECH PTE LTD
6+
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
7+
*target=_blank>https://mitcc.org/</a>
8+
*
9+
******************************************************************************
10+
* @attention
11+
*
12+
* (c) Copyright 2025 by HODL TECH PTE LTD
13+
*
14+
* Permission is hereby granted, free of charge, to any person obtaining
15+
* a copy of this software and associated documentation files (the
16+
* "Software"), to deal in the Software without restriction, including
17+
* without limitation the rights to use, copy, modify, merge, publish,
18+
* distribute, sublicense, and/or sell copies of the Software, and to
19+
* permit persons to whom the Software is furnished to do so, subject
20+
* to the following conditions:
21+
*
22+
* The above copyright notice and this permission notice shall be
23+
* included in all copies or substantial portions of the Software.
24+
*
25+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29+
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30+
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32+
*
33+
*
34+
* "Commons Clause" License Condition v1.0
35+
*
36+
* The Software is provided to you by the Licensor under the License,
37+
* as defined below, subject to the following condition.
38+
*
39+
* Without limiting other conditions in the License, the grant of
40+
* rights under the License will not include, and the License does not
41+
* grant to you, the right to Sell the Software.
42+
*
43+
* For purposes of the foregoing, "Sell" means practicing any or all
44+
* of the rights granted to you under the License to provide to third
45+
* parties, for a fee or other consideration (including without
46+
* limitation fees for hosting or consulting/ support services related
47+
* to the Software), a product or service whose value derives, entirely
48+
* or substantially, from the functionality of the Software. Any license
49+
* notice or attribution required by the License must also include
50+
* this Commons Clause License Condition notice.
51+
*
52+
* Software: All X1Wallet associated files.
53+
* License: MIT
54+
* Licensor: HODL TECH PTE LTD
55+
*
56+
******************************************************************************
57+
*/
58+
59+
/*****************************************************************************
60+
* INCLUDES
61+
*****************************************************************************/
62+
63+
#include "constellation_api.h"
64+
65+
#include <pb_decode.h>
66+
#include <pb_encode.h>
67+
68+
#include "common_error.h"
69+
#include "core_api.h"
70+
#include "events.h"
71+
72+
/*****************************************************************************
73+
* EXTERN VARIABLES
74+
*****************************************************************************/
75+
76+
/*****************************************************************************
77+
* PRIVATE MACROS AND DEFINES
78+
*****************************************************************************/
79+
80+
/*****************************************************************************
81+
* PRIVATE TYPEDEFS
82+
*****************************************************************************/
83+
84+
/*****************************************************************************
85+
* STATIC VARIABLES
86+
*****************************************************************************/
87+
88+
/*****************************************************************************
89+
* GLOBAL VARIABLES
90+
*****************************************************************************/
91+
92+
/*****************************************************************************
93+
* STATIC FUNCTION PROTOTYPES
94+
*****************************************************************************/
95+
96+
/*****************************************************************************
97+
* STATIC FUNCTIONS
98+
*****************************************************************************/
99+
100+
/*****************************************************************************
101+
* GLOBAL FUNCTIONS
102+
*****************************************************************************/
103+
bool decode_constellation_query(const uint8_t *data,
104+
uint16_t data_size,
105+
constellation_query_t *query_out) {
106+
if (NULL == data || NULL == query_out || 0 == data_size) {
107+
constellation_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
108+
ERROR_DATA_FLOW_DECODING_FAILED);
109+
return false;
110+
}
111+
112+
// zeroise for safety from garbage in the query reference
113+
memzero(query_out, sizeof(constellation_query_t));
114+
115+
/* Create a stream that reads from the buffer. */
116+
pb_istream_t stream = pb_istream_from_buffer(data, data_size);
117+
118+
/* Now we are ready to decode the message. */
119+
bool status = pb_decode(&stream, CONSTELLATION_QUERY_FIELDS, query_out);
120+
121+
/* Send error to host if status is false*/
122+
if (false == status) {
123+
constellation_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
124+
ERROR_DATA_FLOW_DECODING_FAILED);
125+
}
126+
127+
return status;
128+
}
129+
130+
bool encode_constellation_result(const constellation_result_t *result,
131+
uint8_t *buffer,
132+
uint16_t max_buffer_len,
133+
size_t *bytes_written_out) {
134+
if (NULL == result || NULL == buffer || NULL == bytes_written_out)
135+
return false;
136+
137+
/* Create a stream that will write to our buffer. */
138+
pb_ostream_t stream = pb_ostream_from_buffer(buffer, max_buffer_len);
139+
140+
/* Now we are ready to encode the message! */
141+
bool status = pb_encode(&stream, CONSTELLATION_RESULT_FIELDS, result);
142+
143+
if (true == status) {
144+
*bytes_written_out = stream.bytes_written;
145+
}
146+
147+
return status;
148+
}
149+
150+
bool check_constellation_query(const constellation_query_t *query,
151+
pb_size_t exp_query_tag) {
152+
if ((NULL == query) || (exp_query_tag != query->which_request)) {
153+
constellation_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
154+
ERROR_DATA_FLOW_INVALID_QUERY);
155+
return false;
156+
}
157+
return true;
158+
}
159+
160+
constellation_result_t init_constellation_result(pb_size_t result_tag) {
161+
constellation_result_t result = CONSTELLATION_RESULT_INIT_ZERO;
162+
result.which_response = result_tag;
163+
return result;
164+
}
165+
166+
void constellation_send_error(pb_size_t which_error, uint32_t error_code) {
167+
constellation_result_t result =
168+
init_constellation_result(CONSTELLATION_RESULT_COMMON_ERROR_TAG);
169+
result.common_error = init_common_error(which_error, error_code);
170+
constellation_send_result(&result);
171+
}
172+
173+
void constellation_send_result(const constellation_result_t *result) {
174+
// TODO: Set all option files
175+
uint8_t buffer[1700] = {0};
176+
size_t bytes_encoded = 0;
177+
ASSERT(encode_constellation_result(
178+
result, buffer, sizeof(buffer), &bytes_encoded));
179+
send_response_to_host(&buffer[0], bytes_encoded);
180+
}
181+
182+
bool constellation_get_query(constellation_query_t *query,
183+
pb_size_t exp_query_tag) {
184+
evt_status_t event = get_events(EVENT_CONFIG_USB, MAX_INACTIVITY_TIMEOUT);
185+
186+
if (true == event.p0_event.flag) {
187+
return false;
188+
}
189+
190+
if (!decode_constellation_query(
191+
event.usb_event.p_msg, event.usb_event.msg_size, query)) {
192+
return false;
193+
}
194+
195+
if (!check_constellation_query(query, exp_query_tag)) {
196+
return false;
197+
}
198+
199+
return true;
200+
}

0 commit comments

Comments
 (0)