Skip to content

Commit

Permalink
Zxlib restructure (#1)
Browse files Browse the repository at this point in the history
* moving to common structure
* restructure tests
* Upgrading zxlib
  • Loading branch information
jleni committed May 2, 2020
1 parent 667c629 commit fb165cd
Show file tree
Hide file tree
Showing 26 changed files with 5,660 additions and 270 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ cmake-build-fuzz/
!\deps/BLAKE
/build
yarn.lock
!/tests_zemu/yarn.lock
!/tests_zemu/yarn-error.log
35 changes: 3 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,11 @@
# BOLOS_SDK IS DEFINED We use the plain Makefile for Ledger
# BOLOS_SDK NOT DEFINED We use a containerized build approach

#TESTS_ZEMU_JS_PACKAGE = "@zondax/ledger-zcash"
#TESTS_ZEMU_JS_DIR = $(CURDIR)/js

ifeq ($(BOLOS_SDK),)
include $(CURDIR)/deps/ledger-zxlib/cmake/dockerized_build.mk

zemu_install:
# First unlink everything
cd js && yarn unlink || true
cd tests/zemu && yarn unlink @zondax/ledger-zcash || true
# Now build and link
cd js && yarn install && yarn build || true
cd js && yarn link || true
cd tests/zemu && yarn link @zondax/ledger-zcash || true
# and now install everything
cd tests/zemu && yarn install

zemu_upgrade:
# and now install everything
cd tests/zemu && yarn install && yarn upgrade --all --latest

zemu_test:
cd tests/zemu && yarn test

zemu_debug:
cd tests/zemu/tools && node debug.mjs debug

zemu:
cd tests/zemu/tools && node debug.mjs

rust_test:
cd app/rust && cargo test

cpp_test:
mkdir -p build && cd build && cmake -DDISABLE_DOCKER_BUILDS=ON -DCMAKE_BUILD_TYPE=Debug .. && make
cd build && GTEST_COLOR=1 ASAN_OPTIONS=detect_leaks=0 ctest -VV

else
default:
$(MAKE) -C app
Expand Down
143 changes: 91 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

**This app is still work in progress!**

## ATTENTION

Please:

- **Do not use in production**
Expand All @@ -15,7 +17,6 @@ Tip:

- In releases, you will find a precompiled test app. If you are just curious, you can run `zxtool.sh` and avoid building.


## Preconditions

- Be sure you checkout submodules too:
Expand All @@ -24,12 +25,26 @@ Tip:
git submodule update --init --recursive
```
- Install `node > v13.0`. We typically recommend using `n`
- Install Docker CE
- Instructions can be found here: https://docs.docker.com/install/
- We only officially support Ubuntu. Install the following packages:
```
sudo apt update && apt-get -y install build-essential git wget cmake \
libssl-dev libgmp-dev autoconf libtool
```
- Install `node > v13.0`. We typically recommend using `n`
- You will need python 3 and then run
- `make deps`
- This project requires Ledger firmware 1.6
- The current repository keeps track of Ledger's SDK but it is possible to override it by changing the git submodule.
*Warning*: Some IDEs may not use the same python interpreter or virtual enviroment as the one you used when running `pip`.
If you see conan is not found, check that you installed the package in the same interpreter as the one that launches `cmake`.
## How to build ?
> We like clion or vscode but let's have some reproducible command line steps
Expand All @@ -42,18 +57,20 @@ Tip:
make
```
## Running tests
- Running rust tests (x64)
If you installed the what is described above, just run:
```bash
make rust_tests
make rust_test
```
- Running C/C++ tests (x64)
If you installed the what is described above, just run:
```bash
make cpp_tests
make cpp_test
```
- Running device emulation+integration tests!!
Expand All @@ -62,51 +79,6 @@ Tip:
Use Zemu! Explained below!
```
## How to prepare your DEVELOPMENT! device:
> You can use an emulated device for development. This is only required if you are using a physical device
>
> **Please do not use a Ledger device with funds for development purposes.**
>>
> **Have a separate and marked device that is used ONLY for development and testing**
There are a few additional steps that increase reproducibility and simplify development:
**1 - Ensure your device works in your OS**
- In Linux hosts it might be necessary to adjust udev rules, etc.
Refer to Ledger documentation: https://support.ledger.com/hc/en-us/articles/115005165269-Fix-connection-issues
**2 - Set a test mnemonic**
Many of our integration tests expect the device to be configured with a known test mnemonic.
- Plug your device while pressing the right button
- Your device will show "Recovery" in the screen
- Double click
- Run `make dev_init`. This will take about 2 minutes. The device will be initialized to:
```
PIN: 5555
Mnemonic: equip will roof matter pink blind book anxiety banner elbow sun young
```
**3 - Add a development certificate**
- Plug your device while pressing the right button
- Your device will show "Recovery" in the screen
- Click both buttons at the same time
- Enter your pin if necessary
- Run `make dev_ca`. The device will receive a development certificate to avoid constant manual confirmations.
## How to test with Zemu?
> What is Zemu?? Great you asked!!
Expand Down Expand Up @@ -156,15 +128,15 @@ There are a few things to take into account when enabling Ledger App debugging:

1. Build your app

```Makefile
```bash
make
```

2. Define your debug scenario

Open `tests/zemu/tools/debug.mjs` and look for the line:

```
```bash
/// TIP you can use zemu commands here to take the app ...
```

Expand All @@ -176,7 +148,7 @@ There are a few things to take into account when enabling Ledger App debugging:

> If you didnt install Zemu yet (previous section), then run `make zemu_install`

```Makefile
```bash
make zemu_debug
```

Expand All @@ -196,3 +168,70 @@ There are a few things to take into account when enabling Ledger App debugging:
Add breakpoints in other places and continue.

Enjoy :)

## Using a real device

### How to prepare your DEVELOPMENT! device:

> You can use an emulated device for development. This is only required if you are using a physical device
>
> **Please do not use a Ledger device with funds for development purposes.**
>>
> **Have a separate and marked device that is used ONLY for development and testing**

There are a few additional steps that increase reproducibility and simplify development:

**1 - Ensure your device works in your OS**
- In Linux hosts it might be necessary to adjust udev rules, etc.

Refer to Ledger documentation: https://support.ledger.com/hc/en-us/articles/115005165269-Fix-connection-issues

**2 - Set a test mnemonic**

Many of our integration tests expect the device to be configured with a known test mnemonic.

- Plug your device while pressing the right button

- Your device will show "Recovery" in the screen

- Double click

- Run `make dev_init`. This will take about 2 minutes. The device will be initialized to:

```
PIN: 5555
Mnemonic: equip will roof matter pink blind book anxiety banner elbow sun young
```

**3 - Add a development certificate**

- Plug your device while pressing the right button

- Your device will show "Recovery" in the screen

- Click both buttons at the same time

- Enter your pin if necessary

- Run `make dev_ca`. The device will receive a development certificate to avoid constant manual confirmations.


### Loading into your development device

The Makefile will build the firmware in a docker container and leave the binary in the correct directory.

- Build

```
make # Builds the app
```

- Upload to a device
The following command will upload the application to the ledger. _Warning: The application will be deleted before uploading._
```
make load # Builds and loads the app to the device
```

## APDU Specifications

- [APDU Protocol](docs/APDUSPEC.md)
9 changes: 9 additions & 0 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extern "C" {
#define HDPATH_0_TESTNET (0x80000000u | 0x2cu)
#define HDPATH_1_TESTNET (0x80000000u | 0x1u)

#define HDPATH_LEN_DEFAULT 5

#define COIN_AMOUNT_DECIMAL_PLACES 18

typedef enum {
Expand All @@ -44,12 +46,19 @@ typedef enum {
// sapling address [11+32]
#define SAPLING_PK_LEN 43u

#define VIEW_ADDRESS_OFFSET_SECP256K1 SECP256K1_PK_LEN
#define VIEW_ADDRESS_OFFSET_SAPLING SAPLING_PK_LEN
#define VIEW_ADDRESS_ITEM_COUNT 2

#define MENU_MAIN_APP_LINE1 "Zcash"
#ifdef TESTING_ENABLED
#define MENU_MAIN_APP_LINE2 "DO NOT USE!"
#else
#define MENU_MAIN_APP_LINE2 "DO NOT USE!"
#endif
#define APPVERSION_LINE2 ""

#define VIEW_ADDRESS_LAST_PAGE_DEFAULT 255;

#ifdef __cplusplus
}
Expand Down
36 changes: 18 additions & 18 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ void crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t *p
BEGIN_TRY
{
TRY {
SAFE_HEARTBEAT(os_perso_derive_node_bip32(CX_CURVE_256K1,
os_perso_derive_node_bip32(CX_CURVE_256K1,
path,
HDPATH_LEN_DEFAULT,
privateKeyData, NULL));
privateKeyData, NULL);

SAFE_HEARTBEAT(cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey));
SAFE_HEARTBEAT(cx_ecfp_init_public_key(CX_CURVE_256K1, NULL, 0, &cx_publicKey));
SAFE_HEARTBEAT(cx_ecfp_generate_pair(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1));
cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey);
cx_ecfp_init_public_key(CX_CURVE_256K1, NULL, 0, &cx_publicKey);
cx_ecfp_generate_pair(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1);
}
FINALLY {
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
Expand Down Expand Up @@ -178,22 +178,22 @@ uint16_t crypto_sign(uint8_t *buffer, uint16_t signatureMaxlen, const uint8_t *m
TRY
{
// Generate keys
SAFE_HEARTBEAT(os_perso_derive_node_bip32(CX_CURVE_256K1,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData, NULL));
os_perso_derive_node_bip32(CX_CURVE_256K1,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData, NULL);

SAFE_HEARTBEAT(cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey));
cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey);

// Sign
SAFE_HEARTBEAT(
signatureLength = cx_ecdsa_sign(&cx_privateKey,
CX_RND_RFC6979 | CX_LAST,
CX_SHA256,
message_digest,
CX_SHA256_SIZE, signature->der_signature,
sizeof_field(signature_t, der_signature),
&info));
signatureLength = cx_ecdsa_sign(&cx_privateKey,
CX_RND_RFC6979 | CX_LAST,
CX_SHA256,
message_digest,
CX_SHA256_SIZE,
signature->der_signature,
sizeof_field(signature_t, der_signature),
&info);

}
FINALLY {
Expand Down
2 changes: 0 additions & 2 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
extern "C" {
#endif

#define HDPATH_LEN_DEFAULT 5u

#define CHECKSUM_LENGTH 4

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];
Expand Down
Loading

0 comments on commit fb165cd

Please sign in to comment.