Skip to content

Commit 31b3a59

Browse files
committed
add ui and cpp tests
1 parent ccc4334 commit 31b3a59

22 files changed

Lines changed: 2210 additions & 223 deletions

app/src/borsh.c

Lines changed: 112 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,103 +18,132 @@
1818
#include "parser_impl.h"
1919
#include "zxformat.h"
2020

21-
// void print_buffer(bytes_t *buffer, const char *title) {
22-
// #if defined(LEDGER_SPECIFIC)
23-
// ZEMU_LOGF(50, "%s\n", title);
24-
// char print[1000] = {0};
25-
// array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
26-
// ZEMU_LOGF(1000, "%s\n", print);
27-
// #else
28-
// printf("%s %d: ", title, buffer->len);
29-
// for (uint16_t i = 0; i < buffer->len; i++) {
30-
// printf("%02x", buffer->ptr[i]);
31-
// }
32-
// printf("\n");
33-
// #endif
34-
// }
21+
void print_buffer(bytes_t *buffer, const char *title) {
22+
#if defined(LEDGER_SPECIFIC)
23+
char print[1000] = {0};
24+
MEMCPY(print, title, strlen(title));
25+
ZEMU_LOGF(50, "%s\n", print);
26+
MEMZERO(print, sizeof(print));
27+
array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
28+
ZEMU_LOGF(1000, "%s\n", print);
29+
#else
30+
printf("%s %d: ", title, buffer->len);
31+
for (uint16_t i = 0; i < buffer->len; i++) {
32+
printf("%02x", buffer->ptr[i]);
33+
}
34+
printf("\n");
35+
#endif
36+
}
3537

36-
// void print_buffer_u8(bytes_t *buffer, const char *title) {
37-
// #if defined(LEDGER_SPECIFIC)
38-
// ZEMU_LOGF(50, "%s\n", title);
39-
// char print[1000] = {0};
40-
// array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
41-
// ZEMU_LOGF(1000, "%s\n", print);
42-
// #else
43-
// printf("%s %d: [", title, buffer->len);
44-
// for (uint16_t i = 0; i < buffer->len; i++) {
45-
// printf("%d, ", buffer->ptr[i]);
46-
// }
47-
// printf("]\n");
48-
// #endif
49-
// }
38+
void print_buffer_u8(bytes_t *buffer, const char *title) {
39+
#if defined(LEDGER_SPECIFIC)
40+
char print[1000] = {0};
41+
MEMCPY(print, title, strlen(title));
42+
ZEMU_LOGF(50, "%s\n", print);
43+
MEMZERO(print, sizeof(print));
44+
array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
45+
ZEMU_LOGF(1000, "%s\n", print);
46+
#else
47+
printf("%s %d: [", title, buffer->len);
48+
for (uint16_t i = 0; i < buffer->len; i++) {
49+
printf("%d, ", buffer->ptr[i]);
50+
}
51+
printf("]\n");
52+
#endif
53+
}
5054

51-
// void print_buffer_str(bytes_t *buffer, const char *title) {
52-
// #if defined(LEDGER_SPECIFIC)
53-
// ZEMU_LOGF(50, "%s\n", title);
54-
// char print[1000] = {0};
55-
// array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
56-
// ZEMU_LOGF(1000, "%s\n", print);
57-
// #else
58-
// uint8_t buff[1000] = {0};
59-
// memcpy(buff, buffer->ptr, buffer->len);
60-
// printf("%s %s\n", title, buff);
61-
// #endif
62-
// }
55+
void print_buffer_str(bytes_t *buffer, const char *title) {
56+
#if defined(LEDGER_SPECIFIC)
57+
char print[1000] = {0};
58+
MEMCPY(print, title, strlen(title));
59+
ZEMU_LOGF(50, "%s\n", print);
60+
MEMZERO(print, sizeof(print));
61+
array_to_hexstr(print, sizeof(print), buffer->ptr, buffer->len);
62+
ZEMU_LOGF(1000, "%s\n", print);
63+
#else
64+
uint8_t buff[1000] = {0};
65+
memcpy(buff, buffer->ptr, buffer->len);
66+
printf("%s %s\n", title, buff);
67+
#endif
68+
}
6369

6470
void print_string(const char *str) {
6571
#if defined(LEDGER_SPECIFIC)
6672
char print[1000] = {0};
6773
MEMCPY(print, str, strlen(str));
68-
ZEMU_LOGF(100, "%s\n", str);
74+
ZEMU_LOGF(100, "%s\n", print);
6975
#else
7076
printf("%s\n", str);
7177
#endif
7278
}
7379

74-
// void print_string_title(const char *str, const char *title) {
75-
// #if defined(LEDGER_SPECIFIC)
76-
// ZEMU_LOGF(100, "%s: %s\n", title, str);
77-
// #else
78-
// printf("%s: %s\n", title, str);
79-
// #endif
80-
// }
80+
void print_string_title(const char *str, const char *title) {
81+
#if defined(LEDGER_SPECIFIC)
82+
char print[1000] = {0};
83+
MEMCPY(print, title, strlen(title));
84+
MEMCPY(print + strlen(title), str, strlen(str));
85+
ZEMU_LOGF(100, "%s\n", print);
86+
#else
87+
printf("%s: %s\n", title, str);
88+
#endif
89+
}
8190

82-
// void print_u8(const char *str, uint8_t val) {
83-
// #if defined(LEDGER_SPECIFIC)
84-
// ZEMU_LOGF(100, "%s: %d\n", str, val);
85-
// #else
86-
// printf("%s: %d\n", str, val);
87-
// #endif
88-
// }
91+
void print_u8(const char *str, uint8_t val) {
92+
#if defined(LEDGER_SPECIFIC)
93+
char print[1000] = {0};
94+
MEMCPY(print, str, strlen(str));
95+
MEMCPY(print + strlen(str), ": ", 2);
96+
MEMCPY(print + strlen(str) + 2, &val, sizeof(val));
97+
ZEMU_LOGF(100, "%s\n", print);
98+
#else
99+
printf("%s: %d\n", str, val);
100+
#endif
101+
}
89102

90-
// void print_u16(const char *str, uint16_t val) {
91-
// #if defined(LEDGER_SPECIFIC)
92-
// ZEMU_LOGF(100, "%s: %d\n", str, val);
93-
// #else
94-
// printf("%s: %d\n", str, val);
95-
// #endif
96-
// }
103+
void print_u16(const char *str, uint16_t val) {
104+
#if defined(LEDGER_SPECIFIC)
105+
char print[1000] = {0};
106+
MEMCPY(print, str, strlen(str));
107+
MEMCPY(print + strlen(str), ": ", 2);
108+
MEMCPY(print + strlen(str) + 2, &val, sizeof(val));
109+
ZEMU_LOGF(100, "%s\n", print);
110+
#else
111+
printf("%s: %d\n", str, val);
112+
#endif
113+
}
97114

98-
// void print_u32(const char *str, uint32_t val) {
99-
// #if defined(LEDGER_SPECIFIC)
100-
// ZEMU_LOGF(100, "%s: %d\n", str, val);
101-
// #else
102-
// printf("%s: %u\n", str, val);
103-
// #endif
104-
// }
115+
void print_u32(const char *str, uint32_t val) {
116+
#if defined(LEDGER_SPECIFIC)
117+
char print[1000] = {0};
118+
MEMCPY(print, str, strlen(str));
119+
MEMCPY(print + strlen(str), ": ", 2);
120+
MEMCPY(print + strlen(str) + 2, &val, sizeof(val));
121+
ZEMU_LOGF(100, "%s\n", print);
122+
#else
123+
printf("%s: %u\n", str, val);
124+
#endif
125+
}
105126

106-
// void print_u64(const char *str, uint64_t val) {
107-
// #if defined(LEDGER_SPECIFIC)
108-
// ZEMU_LOGF(100, "%s: %lu\n", str, val);
109-
// #else
110-
// printf("%s: %llu\n", str, val);
111-
// #endif
112-
// }
127+
void print_u64(const char *str, uint64_t val) {
128+
#if defined(LEDGER_SPECIFIC)
129+
char print[1000] = {0};
130+
MEMCPY(print, str, strlen(str));
131+
MEMCPY(print + strlen(str), ": ", 2);
132+
MEMCPY(print + strlen(str) + 2, &val, sizeof(val));
133+
ZEMU_LOGF(100, "%s\n", print);
134+
#else
135+
printf("%s: %llu\n", str, val);
136+
#endif
137+
}
113138

114-
// void print_u64_hex(const char *str, uint64_t val) {
115-
// #if defined(LEDGER_SPECIFIC)
116-
// ZEMU_LOGF(100, "%s: %lu\n", str, val);
117-
// #else
118-
// printf("%s: 0x%llx\n", str, val);
119-
// #endif
120-
// }
139+
void print_u64_hex(const char *str, uint64_t val) {
140+
#if defined(LEDGER_SPECIFIC)
141+
char print[1000] = {0};
142+
MEMCPY(print, str, strlen(str));
143+
MEMCPY(print + strlen(str), ": ", 2);
144+
MEMCPY(print + strlen(str) + 2, &val, sizeof(val));
145+
ZEMU_LOGF(100, "%s\n", print);
146+
#else
147+
printf("%s: 0x%llx\n", str, val);
148+
#endif
149+
}

app/src/borsh.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ DEFINE_READ_UINT(64)
4747
#undef DEFINE_READ_UINT
4848

4949
// TODO: Remove these functions
50-
// void print_buffer(bytes_t *buffer, const char *title);
51-
// void print_buffer_str(bytes_t *buffer, const char *title);
52-
// void print_buffer_u8(bytes_t *buffer, const char *title);
50+
void print_buffer(bytes_t *buffer, const char *title);
51+
void print_buffer_str(bytes_t *buffer, const char *title);
52+
void print_buffer_u8(bytes_t *buffer, const char *title);
5353
void print_string(const char *str);
54-
// void print_string_title(const char *str, const char *title);
55-
// void print_u8(const char *str, uint8_t val);
56-
// void print_u16(const char *str, uint16_t val);
57-
// void print_u32(const char *str, uint32_t val);
58-
// void print_u64(const char *str, uint64_t val);
59-
// void print_u64_hex(const char *str, uint64_t val);
54+
void print_string_title(const char *str, const char *title);
55+
void print_u8(const char *str, uint8_t val);
56+
void print_u16(const char *str, uint16_t val);
57+
void print_u32(const char *str, uint32_t val);
58+
void print_u64(const char *str, uint64_t val);
59+
void print_u64_hex(const char *str, uint64_t val);
6060

6161
#ifdef __cplusplus
6262
}

app/src/common/actions.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#include <stdint.h>
2020

2121
#include "apdu_codes.h"
22+
#include "borsh.h"
2223
#include "coin.h"
24+
#include "common_txdef.h"
2325
#include "crypto.h"
2426
#include "tx.h"
2527
#include "zxerror.h"
@@ -41,8 +43,8 @@ __Z_INLINE zxerr_t app_fill_address() {
4143
}
4244

4345
__Z_INLINE void app_sign() {
44-
const uint8_t *message = tx_get_buffer();
45-
const uint16_t messageLength = tx_get_buffer_length();
46+
const uint8_t *message = get_txn_raw();
47+
const uint16_t messageLength = get_txn_len() + CX_SHA256_SIZE;
4648

4749
const zxerr_t err = crypto_sign(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 3, message, messageLength);
4850

app/src/common/parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ const char *parser_getMsgPackTypeDescription(uint8_t type);
2929
parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, parser_tx_t *tx_obj);
3030

3131
//// verifies tx fields
32-
parser_error_t parser_validate(parser_context_t *ctx);
32+
parser_error_t parser_validate(parser_tx_t *txObj);
3333

3434
//// returns the number of items in the current parsing context
35-
parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_items);
35+
parser_error_t parser_getNumItems(const parser_tx_t *txObj, uint8_t *num_items);
3636

3737
// retrieves a readable output for each field / page
38-
parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, char *outKey, uint16_t outKeyLen,
39-
char *outVal, uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount);
38+
parser_error_t parser_getItem(const parser_tx_t *txObj, uint8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outVal,
39+
uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount);
4040

4141
#ifdef __cplusplus
4242
}

app/src/common/tx.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "zxmacros.h"
2525

2626
#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) || defined(TARGET_STAX) || defined(TARGET_FLEX)
27-
#define RAM_BUFFER_SIZE 8192
27+
#define RAM_BUFFER_SIZE 4096
2828
#define FLASH_BUFFER_SIZE 16384
2929
#elif defined(TARGET_NANOS)
3030
#define RAM_BUFFER_SIZE 256
@@ -44,8 +44,8 @@ storage_t NV_CONST N_appdata_impl __attribute__((aligned(64)));
4444
#define N_appdata (*(NV_VOLATILE storage_t *)PIC(&N_appdata_impl))
4545
#endif
4646

47-
static parser_tx_t tx_obj;
48-
static parser_context_t ctx_parsed_tx;
47+
static parser_tx_t tx_obj = {0};
48+
static parser_context_t ctx_parsed_tx = {0};
4949

5050
void tx_initialize() {
5151
buffering_init(ram_buffer, sizeof(ram_buffer), (uint8_t *)N_appdata.buffer, sizeof(N_appdata.buffer));
@@ -70,7 +70,7 @@ const char *tx_parse() {
7070
return parser_getErrorDescription(err);
7171
}
7272

73-
err = parser_validate(&ctx_parsed_tx);
73+
err = parser_validate(&tx_obj);
7474
CHECK_APP_CANARY()
7575

7676
if (err != parser_ok) {
@@ -83,7 +83,7 @@ const char *tx_parse() {
8383
void tx_parse_reset() { MEMZERO(&tx_obj, sizeof(tx_obj)); }
8484

8585
zxerr_t tx_getNumItems(uint8_t *num_items) {
86-
parser_error_t err = parser_getNumItems(&ctx_parsed_tx, num_items);
86+
parser_error_t err = parser_getNumItems(&tx_obj, num_items);
8787

8888
if (err != parser_ok) {
8989
return zxerr_unknown;
@@ -102,8 +102,7 @@ zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *ou
102102
return zxerr_no_data;
103103
}
104104

105-
parser_error_t err =
106-
parser_getItem(&ctx_parsed_tx, displayIdx, outKey, outKeyLen, outVal, outValLen, pageIdx, pageCount);
105+
parser_error_t err = parser_getItem(&tx_obj, displayIdx, outKey, outKeyLen, outVal, outValLen, pageIdx, pageCount);
107106

108107
// Convert error codes
109108
if (err == parser_no_data || err == parser_display_idx_out_of_range || err == parser_display_page_out_of_range)
@@ -113,3 +112,7 @@ zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *ou
113112

114113
return zxerr_ok;
115114
}
115+
116+
const uint8_t *get_txn_raw() { return tx_obj.unsigned_transaction_raw.buffer.ptr; }
117+
118+
uint16_t get_txn_len() { return tx_obj.unsigned_transaction_raw.buffer.len; }

app/src/common/tx.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ zxerr_t tx_getNumItems(uint8_t *num_items);
5050
/// Gets an specific item from the transaction (including paging)
5151
zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outValue, uint16_t outValueLen,
5252
uint8_t pageIdx, uint8_t *pageCount);
53+
54+
/// Get the transaction raw bytes
55+
/// \return
56+
const uint8_t *get_txn_raw();
57+
58+
/// Get the transaction length
59+
/// \return
60+
uint16_t get_txn_len();

0 commit comments

Comments
 (0)