Skip to content

Commit

Permalink
Improved generic UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed Apr 19, 2019
1 parent 80e5693 commit f81404b
Show file tree
Hide file tree
Showing 30 changed files with 1,225 additions and 1,018 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ debug/app\.asm
bin/app\.hex

bin/app\.elf

src/glyphs\.h

src/glyphs\.c
75 changes: 50 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,76 @@
ifeq ($(BOLOS_SDK),)
$(error BOLOS_SDK is not set)
endif

dummy_submodules := $(shell git submodule update --init --recursive)

SCRIPT_LD:=$(CURDIR)/script.ld

include $(BOLOS_SDK)/Makefile.defines

# Main app configuration
APPNAME = "Cosmos"
APPVERSION_M=1
APPVERSION_N=3
APPVERSION_P=2
APPVERSION_N=5
APPVERSION_P=0

APP_LOAD_PARAMS = --appFlags 0x200 --delete $(COMMON_LOAD_PARAMS) --path "44'/118'"

ifeq ($(TARGET_NAME),TARGET_NANOS)
SCRIPT_LD:=$(CURDIR)/script.ld
ICONNAME:=$(CURDIR)/nanos_icon.gif
endif

APP_LOAD_PARAMS = --appFlags 0x00 --delete $(COMMON_LOAD_PARAMS) --path "44'/118'"
ICONNAME=$(CURDIR)/icon.gif
ifeq ($(TARGET_NAME),TARGET_NANOX)
ICONNAME:=$(CURDIR)/nanox_icon.gif
endif

ifndef ICONNAME
$(error ICONNAME is not set)
endif

all: default

############
# Platform

DEFINES += UNUSED\(x\)=\(void\)x
DEFINES += PRINTF\(...\)=

APPVERSION=$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)
DEFINES += APPVERSION=\"$(APPVERSION)\"

DEFINES += OS_IO_SEPROXYHAL IO_SEPROXYHAL_BUFFER_SIZE_B=128
DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=7 IO_HID_EP_LENGTH=64 HAVE_USB_APDU

DEFINES += OS_IO_SEPROXYHAL
DEFINES += HAVE_BAGL HAVE_SPRINTF
DEFINES += PRINTF\(...\)=
DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=7 IO_HID_EP_LENGTH=64 HAVE_USB_APDU

DEFINES += LEDGER_MAJOR_VERSION=$(APPVERSION_M) LEDGER_MINOR_VERSION=$(APPVERSION_N) LEDGER_PATCH_VERSION=$(APPVERSION_P)

SDK_SOURCE_PATH += lib_u2f
DEFINES += HAVE_U2F HAVE_IO_U2F
DEFINES += USB_SEGMENT_SIZE=64
DEFINES += U2F_PROXY_MAGIC=\"CSM\"
DEFINES += USB_SEGMENT_SIZE=64
DEFINES += U2F_MAX_MESSAGE_SIZE=264 #257+5+2

DEFINES += HAVE_BOLOS_APP_STACK_CANARY
DEFINES += LEDGER_SPECIFIC

ifeq ($(TARGET_NAME),TARGET_NANOX)
DEFINES += IO_SEPROXYHAL_BUFFER_SIZE_B=300

DEFINES += HAVE_GLO096
DEFINES += HAVE_BAGL BAGL_WIDTH=128 BAGL_HEIGHT=64
DEFINES += HAVE_BAGL_ELLIPSIS # long label truncation feature
DEFINES += HAVE_BAGL_FONT_OPEN_SANS_REGULAR_11PX
DEFINES += HAVE_BAGL_FONT_OPEN_SANS_EXTRABOLD_11PX
DEFINES += HAVE_BAGL_FONT_OPEN_SANS_LIGHT_16PX

DEFINES += HAVE_UX_FLOW

#SDK_SOURCE_PATH += lib_blewbxx lib_blewbxx_impl
SDK_SOURCE_PATH += lib_ux
else
# Assume Nano S
DEFINES += IO_SEPROXYHAL_BUFFER_SIZE_B=128
endif

# X specific

#Feature temporarily disabled
DEFINES += LEDGER_SPECIFIC
#DEFINES += TESTING_ENABLED
#DEFINES += FEATURE_ED25519

# Compiler, assembler, and linker

Expand Down Expand Up @@ -92,17 +117,19 @@ AS := $(GCCPATH)arm-none-eabi-gcc
AFLAGS +=

LD := $(GCCPATH)arm-none-eabi-gcc
LDFLAGS += -O3 -s
LDFLAGS += -O3 -Os
LDLIBS += -lm -lgcc -lc

##########################
include $(BOLOS_SDK)/Makefile.glyphs

APP_SOURCE_PATH += src deps/jsmn/src deps/ledger-zxlib/include deps/ledger-zxlib/src
SDK_SOURCE_PATH += lib_stusb lib_u2f lib_stusb_impl

#include $(BOLOS_SDK)/Makefile.glyphs

all: default
ifeq ($(TARGET_NAME),TARGET_NANOX)
#SDK_SOURCE_PATH += lib_blewbxx lib_blewbxx_impl
SDK_SOURCE_PATH += lib_ux
endif

load:
python -m ledgerblue.loadApp $(APP_LOAD_PARAMS)
Expand All @@ -114,12 +141,10 @@ package:
./pkgdemo.sh ${APPNAME} ${APPVERSION} ${ICONNAME}

# Import generic rules from the SDK

include $(BOLOS_SDK)/Makefile.rules

#add dependency on custom makefile filename
dep/%.d: %.c Makefile.genericwallet

dep/%.d: %.c Makefile

listvariants:
@echo VARIANTS COIN cosmos
Binary file added glyphs/icon_crossmark.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glyphs/icon_eye.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glyphs/icon_validate_14.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added nanox_icon.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ MEMORY
}

PAGE_SIZE = 64;
STACK_SIZE = 880;
STACK_SIZE = 870;
END_STACK = ORIGIN(SRAM) + LENGTH(SRAM);

SECTIONS
Expand Down
137 changes: 66 additions & 71 deletions src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
********************************************************************************/

#include "app_main.h"
#include "view.h"
#include "lib/transaction.h"
#include "signature.h"
#include "zxmacros.h"
#include "bech32.h"

#include <string.h>
#include <os_io_seproxyhal.h>
#include <os.h>

#include <string.h>
#include <zxmacros.h>
#include <bech32.h>

#include "lib/transaction.h"
#include "lib/tx_display.h"
#include "view.h"
#include "signature.h"

#ifdef TESTING_ENABLED
// Generate using always the same private data
Expand All @@ -38,6 +40,13 @@ const uint8_t privateKeyDataTest[] = {
};
#endif

#if defined(TARGET_NANOX)
#define IS_UX_ALLOWED (G_ux_params.len != BOLOS_UX_IGNORE && G_ux_params.len != BOLOS_UX_CONTINUE)
#else
#define IS_UX_ALLOWED (ux.params.len != BOLOS_UX_IGNORE && ux.params.len != BOLOS_UX_CONTINUE)
#endif


uint8_t bip32_depth;
uint32_t bip32_path[10];
sigtype_t current_sigtype;
Expand Down Expand Up @@ -98,8 +107,7 @@ unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) {
return 0; // nothing received from the master so far (it's a tx
// transaction)
} else {
return io_seproxyhal_spi_recv(G_io_apdu_buffer,
sizeof(G_io_apdu_buffer), 0);
return io_seproxyhal_spi_recv(G_io_apdu_buffer, sizeof(G_io_apdu_buffer), 0);
}

default:
Expand Down Expand Up @@ -201,32 +209,33 @@ bool process_chunk(volatile uint32_t *tx, uint32_t rx, bool getBip32) {

//region View Transaction Handlers

int tx_getData(
char *title, int max_title_length,
char *key, int max_key_length,
char *value, int max_value_length,
int page_index,
int chunk_index,
int *page_count_out,
int *chunk_count_out) {
int16_t tx_getData(char *title, int16_t max_title_length,
char *key, int16_t max_key_length,
char *value, int16_t max_value_length,
int16_t page_index,
int16_t chunk_index,
int16_t *page_count_out,
int16_t *chunk_count_out) {
*page_count_out = tx_display_num_pages();
*chunk_count_out = 0;

if (*page_count_out > 0) {
switch (current_sigtype) {
case SECP256K1:
snprintf(title, max_title_length, "SECP256K1 %02d/%02d", page_index + 1, *page_count_out);
break;
default:
snprintf(title, max_title_length, "INVALID!");
break;
}

*page_count_out = transaction_get_display_pages();
INIT_QUERY(key, max_key_length, value, max_value_length, chunk_index)
*chunk_count_out = tx_display_get_item(page_index);

switch (current_sigtype) {
case SECP256K1:
snprintf(title, max_title_length, "SECP256K1 %02d/%02d", page_index + 1, *page_count_out);
break;
default:
snprintf(title, max_title_length, "INVALID!");
break;
tx_display_make_friendly();
}

*chunk_count_out = transaction_get_display_key_value(
key, max_key_length,
value, max_value_length,
page_index, chunk_index);

return 0;
return *chunk_count_out;
}

void tx_accept_sign() {
Expand Down Expand Up @@ -262,11 +271,11 @@ void tx_accept_sign() {
if (result == 1) {
set_code(G_io_apdu_buffer, length, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, length + 2);
view_display_signing_success();
view_idle(0);
} else {
set_code(G_io_apdu_buffer, length, APDU_CODE_SIGN_VERIFY_ERROR);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, length + 2);
view_display_signing_error();
view_idle(0);
}
}

Expand Down Expand Up @@ -297,16 +306,18 @@ void get_pk_compressed(uint8_t *pkc) {
memcpy(pkc, publicKey.W, PK_COMPRESSED_LEN);
}

int addr_getData(char *title, int max_title_length,
char *key, int max_key_length,
char *value, int max_value_length,
int page_index,
int chunk_index,
int *page_count_out,
int *chunk_count_out) {
int16_t addr_getData(char *title, int16_t max_title_length,
char *key, int16_t max_key_length,
char *value, int16_t max_value_length,
int16_t page_index,
int16_t chunk_index,
int16_t *page_count_out,
int16_t *chunk_count_out) {

*page_count_out = 0x7FFFFFFF;
*chunk_count_out = 1;
if (page_count_out)
*page_count_out = 1;
if (chunk_count_out)
*chunk_count_out = 1;

snprintf(title, max_title_length, "Account %d", bip32_path[2] & 0x7FFFFFF);
snprintf(key, max_key_length, "index %d", page_index);
Expand All @@ -327,14 +338,19 @@ int addr_getData(char *title, int max_title_length,
}

void addr_accept() {
int pos = 0;
// Send pubkey
get_pk_compressed(G_io_apdu_buffer + pos);
pos += PK_COMPRESSED_LEN;
uint8_t *pk = G_io_apdu_buffer;
get_pk_compressed(pk);
int pos = PK_COMPRESSED_LEN;

// Send bech32 addr
strcpy((char *) (G_io_apdu_buffer + pos), (char *) viewctl_DataValue);
pos += strlen((char *) viewctl_DataValue);
// Convert pubkey to bech32 address
const char *bech32_out = (char*)(G_io_apdu_buffer + pos);
uint8_t hashed_pk[CX_RIPEMD160_SIZE];
cx_hash_sha256(pk, PK_COMPRESSED_LEN, pk, CX_SHA256_SIZE);
ripemd160_32(hashed_pk, pk);

bech32EncodeFromBytes( bech32_out, bech32_hrp, hashed_pk, CX_RIPEMD160_SIZE);
pos += strlen(bech32_out);

set_code(G_io_apdu_buffer, pos, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, pos + 2);
Expand Down Expand Up @@ -366,8 +382,6 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {

switch (G_io_apdu_buffer[OFFSET_INS]) {
case INS_GET_VERSION: {
unsigned int UX_ALLOWED = (ux.params.len != BOLOS_UX_IGNORE && ux.params.len != BOLOS_UX_CONTINUE);

#ifdef TESTING_ENABLED
G_io_apdu_buffer[0] = 0xFF;
#else
Expand All @@ -376,7 +390,7 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
G_io_apdu_buffer[1] = LEDGER_MAJOR_VERSION;
G_io_apdu_buffer[2] = LEDGER_MINOR_VERSION;
G_io_apdu_buffer[3] = LEDGER_PATCH_VERSION;
G_io_apdu_buffer[4] = !UX_ALLOWED;
G_io_apdu_buffer[4] = !IS_UX_ALLOWED;

*tx += 5;
THROW(APDU_CODE_OK);
Expand All @@ -403,27 +417,6 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}

case INS_SHOW_ADDR_SECP256K1: {
// Parse arguments
if (!extractHRP(&bech32_hrp_len, bech32_hrp, rx, OFFSET_DATA)) {
THROW(APDU_CODE_DATA_INVALID);
}

if (!extractBip32(&bip32_depth, bip32_path, rx, OFFSET_DATA + bech32_hrp_len + 1)) {
THROW(APDU_CODE_DATA_INVALID);
}

if (!validateCosmosPath(bip32_depth, bip32_path)) {
THROW(APDU_CODE_DATA_INVALID);
}

view_set_handlers(addr_getData, NULL, NULL);
view_addr_show(bip32_path[4] & 0x7FFFFFF);

*flags |= IO_ASYNCH_REPLY;
break;
}

case INS_GET_ADDR_SECP256K1: {
// Parse arguments
if (!extractHRP(&bech32_hrp_len, bech32_hrp, rx, OFFSET_DATA)) {
Expand Down Expand Up @@ -458,6 +451,8 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
THROW(APDU_CODE_BAD_KEY_HANDLE);
}

tx_display_index_root();

view_set_handlers(tx_getData, tx_accept_sign, tx_reject);
view_tx_show(0);

Expand Down
2 changes: 1 addition & 1 deletion src/app_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define INS_GET_VERSION 0
#define INS_PUBLIC_KEY_SECP256K1 1 // It will be deprecated in the near future
#define INS_SIGN_SECP256K1 2
#define INS_SHOW_ADDR_SECP256K1 3
//#define INS_SHOW_ADDR_SECP256K1 3 DEPRECATED
#define INS_GET_ADDR_SECP256K1 4

#ifdef TESTING_ENABLED
Expand Down
Loading

0 comments on commit f81404b

Please sign in to comment.