Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
address_book_common.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2026 Ledger SAS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
28
29#include <string.h>
30#include "address_book_common.h"
31#include "os_utils.h"
32#include "os_print.h"
33#include "buffer.h"
34#include "tlv_library.h"
35#include "bip32.h"
36#include "address_book.h"
37#include "io.h"
38#include "status_words.h"
39#include "nbgl_use_case.h"
40
41#ifdef HAVE_ADDRESS_BOOK
42
45
54{
55 buffer_t path_buffer = {0};
56
57 if (!get_buffer_from_tlv_data(data, &path_buffer, 1, MAX_BIP32_PATH * 4 + 1)) {
58 PRINTF("DERIVATION_PATH: failed to extract\n");
59 return false;
60 }
61
62 // Parse BIP32 path using the standard parser
63 if (buffer_get_path_bip32(&path_buffer, bip32_path) == false) {
64 PRINTF("DERIVATION_PATH: failed to parse BIP32 path\n");
65 return false;
66 }
67
68 return true;
69}
70
78bool address_book_handle_chain_id(const tlv_data_t *data, uint64_t *chain_id)
79{
80 if (!get_uint64_t_from_tlv_data(data, chain_id)) {
81 PRINTF("CHAIN_ID: failed to extract\n");
82 return false;
83 }
84 return true;
85}
86
95{
96 uint8_t value = 0;
97 if (!get_uint8_t_from_tlv_data(data, &value)) {
98 PRINTF("UINT8: failed to extract\n");
99 return false;
100 }
101 if (value >= FAMILY_COUNT) {
102 PRINTF("BLOCKCHAIN_FAMILY: Value out of range (%d, max %d)!\n", value, FAMILY_COUNT - 1);
103 return false;
104 }
105 *family = (blockchain_family_e) value;
106 return true;
107}
108
122 char *output_buffer,
123 size_t buffer_size)
124{
125 // Extract the string (with null terminator added by get_string_from_tlv_data)
126 if (!get_string_from_tlv_data(data, output_buffer, 1, buffer_size)) {
127 PRINTF("String extraction failed\n");
128 return false;
129 }
130 if (!is_displayable_string(output_buffer, strlen(output_buffer))) {
131 PRINTF("String contains non-printable characters or is blank\n");
132 return false;
133 }
134 return true;
135}
136
150 const char *reviewTitle,
151 const char *confirmText,
152 nbgl_choiceCallback_t choiceCallback)
153{
154 g_ab_ui.list.wrapping = true;
155 // NB: no subtitle for address book reviews, as the content is already quite long
156 // Indicate the flag ADDRESS_BOOK_OPERATION to force wording in review screens
158 &g_ab_ui.list,
159 icon,
160 reviewTitle,
161 NULL,
162 confirmText,
163 choiceCallback);
164}
165
175{
176 io_send_sw(SWO_INCORRECT_DATA);
177 nbgl_useCaseStatus("Canceled", false, finalize);
178}
179
194 const char *successMsg,
195 const char *errorMsg,
196 nbgl_callback_t finalize)
197{
198 if (success) {
199 nbgl_useCaseStatus(successMsg, true, finalize);
200 }
201 else {
202 io_send_sw(SWO_INCORRECT_DATA);
203 nbgl_useCaseStatus(errorMsg, false, finalize);
204 }
205}
206
207#endif // HAVE_ADDRESS_BOOK
blockchain_family_e
@ FAMILY_COUNT
void address_book_finalize_review(bool success, const char *successMsg, const char *errorMsg, nbgl_callback_t finalize)
Common handling of a confirmed Address Book review's outcome.
void address_book_handle_review_rejected(nbgl_callback_t finalize)
Common handling of a rejected Address Book review.
bool address_book_handle_blockchain_family(const tlv_data_t *data, blockchain_family_e *family)
Generic handler for blockchain family.
bool address_book_handle_derivation_path(const tlv_data_t *data, path_bip32_t *bip32_path)
Generic handler for BIP32 derivation path.
bool address_book_handle_printable_string(const tlv_data_t *data, char *output_buffer, size_t buffer_size)
Generic handler for printable string extraction.
bool address_book_handle_chain_id(const tlv_data_t *data, uint64_t *chain_id)
Generic handler for chain ID.
void address_book_display_review(const nbgl_icon_details_t *icon, const char *reviewTitle, const char *confirmText, nbgl_choiceCallback_t choiceCallback)
Display a confirmation review for an Address Book operation.
ab_ui_t g_ab_ui
ab_payload_u g_ab_payload
#define MAX_BIP32_PATH
Definition bip32.h:15
bool buffer_get_path_bip32(buffer_t *buffer, path_bip32_t *bip32)
Definition buffer.c:177
static int io_send_sw(uint16_t sw)
Definition io.h:112
struct PACKED__ nbgl_icon_details_s nbgl_icon_details_t
Represents all information about an icon.
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
void(* nbgl_callback_t)(void)
prototype of generic callback function
void(* nbgl_choiceCallback_t)(bool confirm)
prototype of choice callback function
void nbgl_useCaseStatus(const char *message, bool isSuccess, nbgl_callback_t quitCallback)
void nbgl_useCaseReviewLight(nbgl_operationType_t operationType, const nbgl_contentTagValueList_t *tagValueList, const nbgl_icon_details_t *icon, const char *reviewTitle, const char *reviewSubTitle, const char *finishTitle, nbgl_choiceCallback_t choiceCallback)
#define ADDRESS_BOOK_OPERATION
This is to use in nbgl_operationType_t when the operation is related to "AddressBook" to adapt wordin...
@ TYPE_OPERATION
For other types of operation (generic type).
bool get_string_from_tlv_data(const tlv_data_t *data, char *out, uint16_t min_length, uint16_t out_size)
Copy a NUL-terminated string from TLV data.
bool get_uint64_t_from_tlv_data(const tlv_data_t *data, uint64_t *value)
Extract a uint64_t value from TLV data.
Definition tlv_library.c:64
bool get_uint8_t_from_tlv_data(const tlv_data_t *data, uint8_t *value)
Extract a uint8_t value from TLV data.
bool get_buffer_from_tlv_data(const tlv_data_t *data, buffer_t *out, uint16_t min_size, uint16_t max_size)
Extract a sized buffer from TLV data (zero-copy).
struct tlv_data_s tlv_data_t