Skip to content

Commit

Permalink
Merge pull request #19 from ElrondNetwork/add-multiple-accounts-support
Browse files Browse the repository at this point in the history
add support for multiple accounts / address indexes
  • Loading branch information
mihaib79 authored Oct 13, 2020
2 parents a144aa8 + f75b2c2 commit 159dbc9
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 284 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ matrix:
file: $TRAVIS_BUILD_DIR/testApp/cmd/testApp/$FILE_NAME
skip_cleanup: true
draft: true

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ Note that `python3` and `pip3` are required in order to install `ledgerblue`.
Download the latest `*.hex` file from our [releases page](https://github.com/ElrondNetwork/ledger-elrond/releases). If `wget` is available on your machine, then:

```
export APP_VERSION=1.0.3
export APP_VERSION=1.0.7
wget https://github.com/ElrondNetwork/ledger-elrond/releases/latest/download/elrond-ledger-app-v${APP_VERSION}.hex
```

Now that you've downloaded the app and `ledgerblue` package is available, let's load the app on the device:

```
export APP_VERSION=1.0.3
export APP_VERSION=1.0.7
python3 -m ledgerblue.loadApp --curve ed25519 --path "44'/508'" --appFlags 0x240 --tlv --targetId 0x31100004 --targetVersion=1.6.0 --delete --appName Elrond --appVersion ${APP_VERSION} --fileName elrond-ledger-app-v${APP_VERSION}.hex --dataSize 64 --icon "010000000000ffffffffffffffffff37ecdffbeff7f7eff7eff7eff7efeff7dffb37ecffffffffffff"
```
Expand Down
1 change: 0 additions & 1 deletion src/getAddress.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ static uint8_t setResultGetAddress() {
}

void handleGetAddress(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, volatile unsigned int *flags, volatile unsigned int *tx) {
UNUSED(dataLength);
UNUSED(p2);
uint8_t publicKey[32];
uint32_t account, index;
Expand Down
4 changes: 4 additions & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ bolos_ux_params_t G_ux_params;
unsigned int ux_step;
unsigned int ux_step_count;
const internalStorage_t N_storage_real;

// selected account global variables
unsigned char bip32_account;
unsigned char bip32_address_index;
22 changes: 12 additions & 10 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#define P1_MORE 0x80

#define FULL_ADDRESS_LENGTH 65 // hex address is 64 characters + \0 = 65
#define BIP32_PATH 5
#define COIN_TYPE_eGLD 508UL
#define TICKER_MAINNET "eGLD"
#define TICKER_TESTNET "XeGLD"
#define HRP "erd"
#define DECIMAL_PLACES 18
#define MAINNET_CHAIN_ID "1"
#define TX_VERSION 1
#define BIP32_PATH 5
#define COIN_TYPE_EGLD 508UL
#define TICKER_MAINNET "eGLD"
#define TICKER_TESTNET "XeGLD"
#define HRP "erd"
#define DECIMAL_PLACES 18
#define MAINNET_CHAIN_ID "1"
#define TX_VERSION 1

typedef enum {
NETWORK_MAINNET = 0,
Expand Down Expand Up @@ -57,10 +57,12 @@ extern ux_state_t ux;
extern unsigned int ux_step;
extern unsigned int ux_step_count;

// selected account global variables
extern unsigned char bip32_account;
extern unsigned char bip32_address_index;

typedef struct internalStorage_t {
unsigned char setting_contract_data;
unsigned char setting_account;
unsigned char setting_address_index;
uint8_t initialized;
} internalStorage_t;

Expand Down
24 changes: 20 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "utils.h"
#include "getAddress.h"
#include "setAddress.h"
#include "signTx.h"
#include "signMsg.h"
#include "menu.h"
#include "globals.h"

Expand All @@ -27,6 +29,8 @@
#define INS_GET_APP_CONFIGURATION 0x02
#define INS_GET_ADDR 0x03
#define INS_SIGN_TX 0x04
#define INS_SET_ADDR 0x05
#define INS_SIGN_MSG 0x06

#define OFFSET_CLA 0
#define OFFSET_INS 1
Expand Down Expand Up @@ -64,8 +68,8 @@ void handleApdu(volatile unsigned int *flags, volatile unsigned int *tx) {

case INS_GET_APP_CONFIGURATION:
G_io_apdu_buffer[0] = (N_storage.setting_contract_data ? 0x01 : 0x00);
G_io_apdu_buffer[1] = N_storage.setting_account;
G_io_apdu_buffer[2] = N_storage.setting_address_index;
G_io_apdu_buffer[1] = bip32_account;
G_io_apdu_buffer[2] = bip32_address_index;
G_io_apdu_buffer[3] = LEDGER_MAJOR_VERSION;
G_io_apdu_buffer[4] = LEDGER_MINOR_VERSION;
G_io_apdu_buffer[5] = LEDGER_PATCH_VERSION;
Expand All @@ -77,10 +81,19 @@ void handleApdu(volatile unsigned int *flags, volatile unsigned int *tx) {
handleGetAddress(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, G_io_apdu_buffer[OFFSET_LC], flags, tx);
break;

case INS_SET_ADDR:
handleSetAddress(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, G_io_apdu_buffer[OFFSET_LC], flags, tx);
THROW(MSG_OK);
break;

case INS_SIGN_TX:
handleSignTx(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, G_io_apdu_buffer[OFFSET_LC], flags, tx);
break;

case INS_SIGN_MSG:
handleSignMsg(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, G_io_apdu_buffer[OFFSET_LC], flags, tx);
break;

default:
THROW(ERR_UNKNOWN_INSTRUCTION);
break;
Expand Down Expand Up @@ -119,6 +132,11 @@ void elrond_main(void) {
volatile unsigned int tx = 0;
volatile unsigned int flags = 0;

bip32_account = 0;
bip32_address_index = 0;

msg_context.state = APP_STATE_IDLE;

// DESIGN NOTE: the bootloader ignores the way APDU are fetched. The only
// goal is to retrieve APDU.
// When APDU are to be fetched from multiple IOs, like NFC+USB+BLE, make
Expand Down Expand Up @@ -278,8 +296,6 @@ void nv_app_state_init() {
if (N_storage.initialized != 0x01) {
internalStorage_t storage;
storage.setting_contract_data = DEFAULT_CONTRACT_DATA;
storage.setting_account = 0;
storage.setting_address_index = 0;
storage.initialized = 0x01;
nvm_write((internalStorage_t*)&N_storage, (void*)&storage, sizeof(internalStorage_t));
}
Expand Down
10 changes: 0 additions & 10 deletions src/menu.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#include "menu.h"
#include "os.h"
#include "viewAddress.h"
#include "viewAppVersion.h"
#include "selectAccount.h"

const char* const setting_contract_data_getter_values[] = {
"No",
"Yes",
"Back"
};
const char* const settings_submenu_getter_values[] = {
"Select account",
"Contract data",
"Back",
};
const char* const info_submenu_getter_values[] = {
"View address",
"App version",
"Back",
};
Expand Down Expand Up @@ -106,9 +102,6 @@ const char* settings_submenu_getter(unsigned int idx) {
void settings_submenu_selector(unsigned int idx) {
switch(idx) {
case 0:
selectAccount();
break;
case 1:
ux_menulist_init_select(0, setting_contract_data_getter, setting_contract_data_selector, N_storage.setting_contract_data);
break;
default:
Expand All @@ -127,9 +120,6 @@ const char* info_submenu_getter(unsigned int idx) {
void info_submenu_selector(unsigned int idx) {
switch(idx) {
case 0:
viewAddressAsBech32(N_storage.setting_account, N_storage.setting_address_index);
break;
case 1:
viewAppVersion();
break;
default:
Expand Down
164 changes: 0 additions & 164 deletions src/selectAccount.c

This file was deleted.

6 changes: 0 additions & 6 deletions src/selectAccount.h

This file was deleted.

28 changes: 28 additions & 0 deletions src/setAddress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "utils.h"
#include "os.h"
#include "ux.h"
#include "setAddress.h"

// set the account and address index for the derivation path
void handleSetAddress(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, volatile unsigned int *flags, volatile unsigned int *tx) {
UNUSED(p1);
UNUSED(p2);

if (dataLength != sizeof(uint32_t) * 2) {
THROW(ERR_INVALID_ARGUMENTS);
return;
}

uint32_t account, address_index;

account = readUint32BE(dataBuffer);
address_index = readUint32BE(dataBuffer + sizeof(uint32_t));

if ((account > __UINT8_MAX__) || (address_index > __UINT8_MAX__)) {
THROW(ERR_INVALID_ARGUMENTS);
return;
}

bip32_account = account;
bip32_address_index = address_index;
}
8 changes: 8 additions & 0 deletions src/setAddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "os.h"

#ifndef _SET_ADDRESS_H_
#define _SET_ADDRESS_H_

void handleSetAddress(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, volatile unsigned int *flags, volatile unsigned int *tx);

#endif
Loading

0 comments on commit 159dbc9

Please sign in to comment.