Skip to content

Commit bcd9066

Browse files
authored
Merge pull request #542 from Cypherock/feat/inheritance/single-card-tap
feat(app): Add single card tap
2 parents e9c75d7 + 8f4a09c commit bcd9066

File tree

6 files changed

+118
-14
lines changed

6 files changed

+118
-14
lines changed

apps/inheritance_app/inheritance_auth_wallet.c

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919

2020
#include "bip39.h"
2121
#include "card_fetch_data.h"
22+
#include "card_pair.h"
23+
#include "core_error.h"
2224
#include "inheritance/core.pb.h"
2325
#include "inheritance_api.h"
2426
#include "inheritance_priv.h"
2527
#include "reconstruct_wallet_flow.h"
2628
#include "status_api.h"
2729
#include "ui_delay.h"
28-
2930
/*****************************************************************************
3031
* EXTERN VARIABLES
3132
*****************************************************************************/
@@ -60,8 +61,39 @@ static auth_wallet_config_t *auth = NULL;
6061
static bool verify_auth_wallet_inputs();
6162

6263
/**
63-
* @brief Retrieves encrypted data (entropy) from the card based on the wallet
64-
* ID.
64+
* @brief Retrieves and stores seed-based entropy for authentication.
65+
*
66+
* Generates entropy from the seed based on the wallet ID and stores it in the
67+
* auth structure.
68+
*
69+
* @return true if seed-based entropy is successfully generated and stored,
70+
* false otherwise.
71+
*/
72+
static bool auth_wallet_get_seed_entropy();
73+
74+
/**
75+
* @brief Pairs the card with the device.
76+
*
77+
* Initiates the card pairing process and updates the app flow status upon
78+
* success.
79+
*
80+
* @return true if the card is successfully paired, false otherwise.
81+
*/
82+
static bool auth_wallet_pair_card();
83+
84+
/**
85+
* @brief Retrieves and stores wallet-based entropy from the card.
86+
*
87+
* Fetches encrypted wallet ID data from the card to generate entropy.
88+
*
89+
* @return true if wallet-based entropy is successfully fetched and stored,
90+
* false otherwise.
91+
*/
92+
static bool auth_wallet_get_wallet_entropy();
93+
94+
/**
95+
* @brief Retrieves encrypted data (entropy) from the card based on the type
96+
* requested from the host (seed based and/or wallet_id based)
6597
*
6698
* This function initializes a secure_data_t structure, fetches encrypted data
6799
* from the card, and stores the result in the auth structure. It checks if the
@@ -126,19 +158,15 @@ static bool verify_auth_wallet_inputs() {
126158
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
127159
return false;
128160
}
129-
130161
return true;
131162
}
132163

133-
static bool auth_wallet_get_entropy() {
164+
static bool auth_wallet_get_seed_entropy() {
134165
if (auth->do_seed_based) {
135166
uint8_t seed[SIZE_SEED] = {0};
136167
if (!reconstruct_seed_without_passphrase(
137168
auth->data.wallet_id, seed, inheritance_send_error)) {
138169
memzero(seed, sizeof(seed));
139-
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
140-
ERROR_DATA_FLOW_INVALID_QUERY);
141-
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
142170
return false;
143171
}
144172
memcpy((void *)auth->seed_based_data.entropy, seed, SIZE_SEED);
@@ -148,18 +176,30 @@ static bool auth_wallet_get_entropy() {
148176
// seed generation complete
149177
set_app_flow_status(INHERITANCE_AUTH_WALLET_STATUS_SEED_BASED_CARD_TAPPED);
150178
}
179+
return true;
180+
}
181+
182+
static bool auth_wallet_pair_card() {
183+
// Pair the card first
184+
card_error_type_e status = single_card_pair_operation(
185+
(char *)ui_text_tap_the_card, ui_text_place_card_below);
186+
if (status != CARD_OPERATION_SUCCESS) {
187+
return false;
188+
}
189+
set_app_flow_status(INHERITANCE_AUTH_WALLET_STATUS_PAIRING_CARD_TAPPED);
190+
return true;
191+
}
192+
193+
static bool auth_wallet_get_wallet_entropy() {
151194
if (auth->do_wallet_based) {
152195
secure_data_t msgs[1] = {0};
153196
msgs[0].plain_data_size = WALLET_ID_SIZE;
154197
memcpy(msgs[0].plain_data, auth->data.wallet_id, WALLET_ID_SIZE);
155-
198+
// fetch encrypted wallet_id
156199
card_error_type_e status =
157200
card_fetch_encrypt_data(auth->data.wallet_id, msgs, 1);
158201
if (status != CARD_OPERATION_SUCCESS ||
159202
msgs[0].encrypted_data_size > ENTROPY_SIZE_LIMIT) {
160-
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
161-
ERROR_DATA_FLOW_INVALID_DATA);
162-
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
163203
return false;
164204
}
165205
memcpy((void *)auth->wallet_based_data.entropy,
@@ -171,9 +211,18 @@ static bool auth_wallet_get_entropy() {
171211
set_app_flow_status(
172212
INHERITANCE_AUTH_WALLET_STATUS_WALLET_BASED_CARD_TAPPED);
173213
}
214+
return true;
215+
}
174216

217+
static bool auth_wallet_get_entropy() {
218+
if (!auth_wallet_get_seed_entropy() || !auth_wallet_pair_card() ||
219+
!auth_wallet_get_wallet_entropy()) {
220+
inheritance_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
221+
ERROR_DATA_FLOW_INVALID_DATA);
222+
delay_scr_init(ui_text_inheritance_wallet_auth_fail, DELAY_TIME);
223+
return false;
224+
}
175225
delay_scr_init(ui_text_inheritance_wallet_authenticating, DELAY_SHORT);
176-
177226
return true;
178227
}
179228

src/card_operations/card_operation_typedefs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ typedef enum card_errors_type {
4646
can be associated to @ref
4747
card_error_status_word_e errors or PN532
4848
errors */
49-
5049
CARD_OPERATION_DEFAULT_INVALID = 0xFF, /** Default invalid value */
5150
} card_error_type_e;
5251

src/card_operations/card_pair.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,54 @@ card_error_type_e card_pair_operation(uint8_t card_number,
484484
break;
485485
}
486486

487+
nfc_deselect_card();
488+
return card_data.error_type;
489+
}
490+
491+
card_error_type_e single_card_pair_operation(char *heading,
492+
const char *message) {
493+
// Need to handle how assign new card its number
494+
card_operation_data_t card_data = {0};
495+
card_pairing_data_t pair_data = {0};
496+
497+
if (SUCCESS != pair_card_preprocess(&pair_data)) {
498+
return CARD_OPERATION_ABORT_OPERATION;
499+
}
500+
501+
instruction_scr_init(message, heading);
502+
card_data.nfc_data.retries = 5;
503+
504+
while (1) {
505+
// Initialize card tap config
506+
card_data.nfc_data.acceptable_cards = ACCEPTABLE_CARDS_ALL;
507+
508+
init_and_pair_card(&card_data, &pair_data);
509+
510+
if (CARD_OPERATION_SUCCESS == card_data.error_type) {
511+
if (SW_NO_ERROR != handle_pairing_success(&card_data, &pair_data)) {
512+
card_data.error_type = CARD_OPERATION_ABORT_OPERATION;
513+
break;
514+
}
515+
516+
buzzer_start(BUZZER_DURATION);
517+
wait_for_card_removal();
518+
519+
break;
520+
}
521+
522+
if (CARD_OPERATION_CARD_REMOVED == card_data.error_type ||
523+
CARD_OPERATION_RETAP_BY_USER_REQUIRED == card_data.error_type) {
524+
const char *error_msg = card_data.error_message;
525+
if (CARD_OPERATION_SUCCESS == indicate_card_error(error_msg)) {
526+
// Re-render the instruction screen
527+
instruction_scr_init(message, heading);
528+
continue;
529+
}
530+
}
531+
532+
break;
533+
}
534+
487535
nfc_deselect_card();
488536
return card_data.error_type;
489537
}

src/card_operations/card_pair.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,10 @@ card_error_type_e card_pair_without_retap(uint8_t card_number,
6262
card_error_type_e card_pair_operation(uint8_t card_number,
6363
char *heading,
6464
const char *message);
65+
// To Improvise:
66+
// Single card pairing operation,
67+
// Should allow single or extra card to be paired if not already,
68+
// for any required flow
69+
card_error_type_e single_card_pair_operation(char *heading,
70+
const char *message);
6571
#endif

src/constant_texts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ const char *ui_text_inheritance_wallet_auth_success =
536536
"Wallet\nauthentication\nsuccessfully";
537537
const char *ui_text_inheritance_wallet_auth_fail =
538538
"Wallet\nauthentication\nfailed";
539+
const char *ui_text_tap_the_card = "Tap the card";
539540

540541
const char *ui_text_inheritance_encryption_flow_confirmation =
541542
"Proceed to encrypt data for %s?";

src/constant_texts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ extern const char *ui_critical_card_health_migrate_data;
376376
extern const char *ui_text_inheritance_wallet_authenticating;
377377
extern const char *ui_text_inheritance_wallet_auth_success;
378378
extern const char *ui_text_inheritance_wallet_auth_fail;
379+
extern const char *ui_text_tap_the_card;
379380

380381
extern const char *ui_text_inheritance_encryption_flow_confirmation;
381382
extern const char *ui_text_inheritance_encryption_flow_success;

0 commit comments

Comments
 (0)