Skip to content

Commit

Permalink
Merge pull request #10 from Zondax/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
chcmedeiros authored Mar 26, 2024
2 parents 1a940f4 + 37123ca commit c82cfae
Show file tree
Hide file tree
Showing 29 changed files with 91 additions and 54 deletions.
2 changes: 1 addition & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $(info COIN = [$(COIN)])

ifeq ($(COIN),FLR)
APPNAME = "Flare Network"
APPPATH = "44'/9000'" --path "44'/60'"
APPPATH = "44'/60'"
else
define error_message

Expand Down
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the minor version
APPVERSION_N=0
# This is the patch version
APPVERSION_P=11
APPVERSION_P=12
Binary file modified app/glyphs/icon_stax_32.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 modified app/glyphs/icon_stax_64.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 42 additions & 32 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "actions.h"
#include "addr.h"
#include "app_main.h"
#include "app_mode.h"
#include "coin.h"
#include "crypto.h"
#include "crypto_helper.h"
Expand All @@ -34,6 +35,9 @@
#include "view_internal.h"
#include "zxmacros.h"

static const char *msg_error1 = "Expert Mode";
static const char *msg_error2 = "Required";

static bool tx_initialized = false;

void extractHDPath(uint32_t rx, uint32_t offset) {
Expand All @@ -45,7 +49,7 @@ void extractHDPath(uint32_t rx, uint32_t offset) {

memcpy(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * HDPATH_LEN_DEFAULT);

if (hdPath[0] != HDPATH_0_DEFAULT || ((hdPath[1] != HDPATH_1_DEFAULT) && (hdPath[1] != HDPATH_ETH_1_DEFAULT))) {
if (hdPath[0] != HDPATH_ETH_0_DEFAULT || (hdPath[1] != HDPATH_ETH_1_DEFAULT)) {
THROW(APDU_CODE_DATA_INVALID);
}
hdPath_len = HDPATH_LEN_DEFAULT;
Expand Down Expand Up @@ -319,6 +323,12 @@ __Z_INLINE void handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx,
THROW(APDU_CODE_OK);
}

if (!app_mode_expert()) {
*flags |= IO_ASYNCH_REPLY;
view_custom_error_show(PIC(msg_error1), PIC(msg_error2));
THROW(APDU_CODE_DATA_INVALID);
}

const char *error_msg = hash_parse();
CHECK_APP_CANARY()
if (error_msg != NULL) {
Expand Down Expand Up @@ -401,45 +411,45 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {

const uint8_t instruction = G_io_apdu_buffer[OFFSET_INS];

// Handle this case as ins number is the same as normal fil sign
// Handle this case as ins number is the same as normal flr sign
// instruction
if (instruction == INS_GET_ADDR_ETH && cla == CLA_ETH) {
handleGetAddrEth(flags, tx, rx);
}

switch (instruction) {
case INS_GET_VERSION: {
handle_getversion(flags, tx);
break;
}
} else {
switch (instruction) {
case INS_GET_VERSION: {
handle_getversion(flags, tx);
break;
}

case INS_GET_ADDR: {
CHECK_PIN_VALIDATED()
handleGetAddr(flags, tx, rx);
break;
}
case INS_GET_ADDR: {
CHECK_PIN_VALIDATED()
handleGetAddr(flags, tx, rx);
break;
}

case INS_SIGN: {
CHECK_PIN_VALIDATED()
handleSign(flags, tx, rx);
break;
}
case INS_SIGN: {
CHECK_PIN_VALIDATED()
handleSign(flags, tx, rx);
break;
}

case INS_SIGN_HASH: {
CHECK_PIN_VALIDATED()
handleSignHash(flags, tx, rx);
break;
}
case INS_SIGN_ETH: {
CHECK_PIN_VALIDATED()
if (cla != CLA_ETH) {
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
case INS_SIGN_HASH: {
CHECK_PIN_VALIDATED()
handleSignHash(flags, tx, rx);
break;
}
handleSignEth(flags, tx, rx);
break;
case INS_SIGN_ETH: {
CHECK_PIN_VALIDATED()
if (cla != CLA_ETH) {
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
}
handleSignEth(flags, tx, rx);
break;
}
default:
THROW(APDU_CODE_INS_NOT_SUPPORTED);
}
default:
THROW(APDU_CODE_INS_NOT_SUPPORTED);
}
}
CATCH(EXCEPTION_IO_RESET) { THROW(EXCEPTION_IO_RESET); }
Expand Down
3 changes: 0 additions & 3 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ typedef enum {
#define MAX_BIP32_PATH 10
#define HDPATH_LEN_DEFAULT 5

#define HDPATH_0_DEFAULT (0x80000000u | 0x2c) // 44
#define HDPATH_1_DEFAULT (0x80000000u | 0x2328) // 9000

#define HDPATH_ETH_0_DEFAULT (0x80000000u | 0x2cu)
#define HDPATH_ETH_1_DEFAULT (0x80000000u | 0x3cu)

Expand Down
3 changes: 1 addition & 2 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ zxerr_t crypto_sign(uint8_t *signature, uint16_t signatureMaxlen, uint16_t *sigS
if (err_c != no_error) {
error = zxerr_unknown;
} else {
*sigSize =
sizeof_field(signature_t, r) + sizeof_field(signature_t, s) + sizeof_field(signature_t, v) + signatureLength;
*sigSize = sizeof_field(signature_t, r) + sizeof_field(signature_t, s) + sizeof_field(signature_t, v);
error = zxerr_ok;
}

Expand Down
42 changes: 35 additions & 7 deletions docs/APDUSPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ The general structure of commands and responses is as follows:

## Command definition

### GET_DEVICE_INFO

#### Command

| Field | Type | Content | Expected |
| ----- | -------- | ---------------------- | -------- |
| CLA | byte (1) | Application Identifier | 0xE0 |
| INS | byte (1) | Instruction ID | 0x01 |
| P1 | byte (1) | Parameter 1 | 0x00 |
| P2 | byte (1) | Parameter 2 | 0x00 |
| L | byte (1) | Bytes in payload | 0x00 |

#### Response

| Field | Type | Content | Note |
| --------- | -------- | ------------------ | ------------------------ |
| TARGET_ID | byte (4) | Target Id | |
| OS_LEN | byte (1) | OS version length | 0..64 |
| OS | byte (?) | OS version | Non terminated string |
| FLAGS_LEN | byte (1) | Flags length | 0 |
| MCU_LEN | byte (1) | MCU version length | 0..64 |
| MCU | byte (?) | MCU version | Non terminated string |
| SW1-SW2 | byte (2) | Return code | see list of return codes |

---

### GET_VERSION

#### Command
Expand All @@ -64,8 +90,8 @@ The general structure of commands and responses is as follows:
| MAJOR | byte (2) | Version Major | 0..65535 |
| MINOR | byte (2) | Version Minor | 0..65535 |
| PATCH | byte (2) | Version Patch | 0..65535 |
| LOCKED | byte (1) | Device is locked | It'll always be 0 |
| TARGET_ID | byte (4) | Target Id | |
| LOCKED | byte (1) | Device is locked | |
| SW1-SW2 | byte (2) | Return code | see list of return codes |

---
Expand Down Expand Up @@ -194,7 +220,7 @@ All other packets/chunks contain data chunks that are described below
| CLA | byte (1) | Application Identifier | 0xE0 |
| INS | byte (1) | Instruction ID | 0x02 |
| P1 | byte (1) | Request User confirmation | No = 0 |
| P2 | byte (1) | Parameter 2 | ignored |
| P2 | byte (1) | Chain code | no chain code - 0x0 / chain code - 0x01 |
| L | byte (1) | Bytes in payload | (depends) |
| Path[0] | byte (4) | Derivation Path Data | 0x8000002c |
| Path[1] | byte (4) | Derivation Path Data | 0x8000003c |
Expand All @@ -204,11 +230,13 @@ All other packets/chunks contain data chunks that are described below

#### Response

| Field | Type | Content | Note |
| ------- | --------- | ----------- | ------------------------ |
| PK | byte (33) | Public Key | Compressed public key |
| ADDR | byte (??) | address | Bech32 encoded address |
| SW1-SW2 | byte (2) | Return code | see list of return codes |
| Field | Type | Content | Note |
| ------- | --------- | --------------- | ---------------------------------- |
| PK LEN | byte | Public Key Len | |
| PK | byte (??) | Public Key | |
| ADDR LEN| byte | Address Len | |
| ADDR | byte (??) | address | Hex representation of eth address |
| SW1-SW2 | byte (2) | Return code | see list of return codes |

---

Expand Down
Binary file modified tests_zemu/snapshots/s-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-show_address/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-show_address/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-show_address_reject/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-show_address_reject/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-show_address/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-show_address_reject/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-show_address/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-show_address_reject/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address_reject/00001.png
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 tests_zemu/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const APP_PATH_X = resolve('../app/output/app_x.elf')
const APP_PATH_SP = resolve('../app/output/app_s2.elf')
const APP_PATH_ST = resolve('../app/output/app_stax.elf')

export const hdpath = `m/44'/9000'/0/0/0`
export const hdpath = `m/44'/60'/0/0/0`
export const PATH_TESTNET = "m/44'/1'/0'/0/1"
export const ETH_PATH = "m/44'/60'/0'/0'/5"
export const EXPECTED_PUBLIC_KEY = '024f1dd50f180bfd546339e75410b127331469837fa618d950f7cfb8be351b0020'
Expand Down
10 changes: 5 additions & 5 deletions tests_zemu/tests/eth_tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import Zemu, { ButtonKind } from '@zondax/zemu'
// @ts-ignore
import FilecoinApp from '@zondax/ledger-flare'
import { models, defaultOptions, ETH_PATH, EXPECTED_ETH_ADDRESS, EXPECTED_ETH_PK } from './common'
import { ec } from 'elliptic'
import FlareApp from '@zondax/ledger-flare'

const sha3 = require('js-sha3')
type NftInfo = {
Expand Down Expand Up @@ -89,7 +89,7 @@ describe.each(models)('ETH', function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new FilecoinApp(sim.getTransport())
const app = new FlareApp(sim.getTransport())
const msg = data.op

// Put the app in expert mode
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('EthAddress', function () {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new FilecoinApp(sim.getTransport())
const app = new FlareApp(sim.getTransport())

const resp = await app.getEVMAddress(ETH_PATH, false, true)

Expand All @@ -157,9 +157,9 @@ describe('EthAddress', function () {
approveKeyword: m.name === 'stax' ? 'QR' : '',
approveAction: ButtonKind.ApproveTapButton,
})
const app = new FilecoinApp(sim.getTransport())
const app = new FlareApp(sim.getTransport())

const resp = app.getEVMAddress(ETH_PATH, true)
const resp = app.getEVMAddress(ETH_PATH, true, true)

await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())

Expand Down
4 changes: 2 additions & 2 deletions tests_zemu/tests/standard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('Standard', function () {
expect(resp.returnCode).toEqual(0x9000)
expect(resp.errorMessage).toEqual('No errors')

const expected_address = 'flare1yh62d5xdyzu5w2nc6qpyymsjzqc5qzaumcf0jy'
const expected_pk = '0226525208673808e006c9efbc1bce812b21c67aa286eb550b3c0dd208095cc3a7'
const expected_address = 'flare1yalwlqksjyhcf7jsp7mx9tvxc62fwqzdnuen5c'
const expected_pk = '028b4bfd1f5fbf8575d7b2c9fc7dc0e587ec417b9f29dff89be57759adc8cdd11d'

expect(resp.compressed_pk!.toString('hex')).toEqual(expected_pk)
expect(resp.bech32_address).toEqual(expected_address)
Expand Down
3 changes: 3 additions & 0 deletions tests_zemu/tests/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ describe.each(models)('Transactions', function (m) {
await sim.start({ ...defaultOptions, model: m.name })
const app = new FlareApp(sim.getTransport())

//Change to expert mode so we can skip fields
await sim.toggleExpertMode()

const responseAddr = await app.getAddressAndPubKey(hdpath)
expect(responseAddr.returnCode).toEqual(0x9000)
console.log(responseAddr)
Expand Down

0 comments on commit c82cfae

Please sign in to comment.