Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/worskpace #93

Merged
merged 8 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
[target.nanos]
runner = "speculos -m nanos --display=headless"

[target.nanox]
runner = "speculos -m nanox -a 5 --display=headless"

[target.nanosplus]
runner = "speculos -m nanosp -a 1 --display=headless"

[unstable]
build-std = ["core"]
build-std-features = ["compiler-builtins-mem"]

[build]
target = "nanosplus"
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --target ./${{ matrix.target }}.json
args: -p ledger_device_rust_sdk --target ledger_device_rust_sdk/${{ matrix.target }}.json
fmt:
runs-on: ubuntu-latest
steps:
Expand All @@ -52,7 +52,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
args: -p ledger_device_rust_sdk --all -- --check

build:
runs-on: ubuntu-latest
Expand All @@ -78,7 +78,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --target ./${{ matrix.target }}.json
args: -p ledger_device_rust_sdk --target ledger_device_rust_sdk/${{ matrix.target }}.json

test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -107,4 +107,4 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --target ./${{ matrix.target }}.json --features speculos
args: -p ledger_device_rust_sdk --target ledger_device_rust_sdk/${{ matrix.target }}.json --features speculos
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Cargo.lock

# Editors
.idea/
.vscode/
.vscode/
44 changes: 15 additions & 29 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
[package]
name = "nanos_sdk"
version = "0.3.0"
authors = ["yhql"]
edition = "2021"

[dev-dependencies]
# enable the 'speculos' feature when testing
# https://github.com/rust-lang/cargo/issues/2911#issuecomment-749580481
nanos_sdk = { path = ".", features = ["speculos"] }

testmacro = { git = "https://github.com/yhql/testmacro"}

[dependencies]
ledger-sdk-sys = { git = "https://github.com/LedgerHQ/secure-sdk-rust" }
num-traits = { version = "0.2.14", default_features = false }
rand_core = { version = "0.6.3", default_features = false }
zeroize = { version = "1.6.0", default_features = false }

[profile.release]
opt-level = 's'
lto = true

[features]
speculos = []
pre1_54 = []
lib_bagl = []
ccid = []
pending_review_screen = []
[workspace]
members = [
"ledger_device_rust_sdk",
"ledger_device_ui_sdk",
"ledger_secure_sdk_sys",
"include_gif"
]
resolver = "2"

[workspace.package]
license = "Apache-2.0"
description = "Ledger Device Rust SDK crates"
repository = "https://github.com/LedgerHQ/ledger-device-rust-sdk"
readme = "README.md"
authors = ["Ledger"]
88 changes: 6 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,7 @@
# Ledger wallets SDK for Rust Applications
# Ledger Device Rust SDK
This workspace contains the 4 crates members of Ledger Device Rust SDK

Crate that allows developing Ledger Nano apps in Rust with a default configuration.

Contains:

- low-level pre-generated bindings to the C SDK version 2.0
- some safe wrappers over common syscalls
- IO abstractions
- signature abstractions

This SDK is incomplete in the sense that wrappers are currently missing, and not all wrappers have a nice Rust abstraction on top, but two apps were made using it:

- [A demo application with a signature UI workflow](https://github.com/LedgerHQ/rust-app)
- [A Password Manager](https://github.com/LedgerHQ/rust-app-password-manager)

You can submit an issue or even a pull request if you wish to contribute, we will check what we can do.

## Supported devices

| Nano S | Nano X | Nano S Plus |
| ------------------ | ------------------ | ------------------ |
| :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |

## Usage

Building requires adding `rust-src` to your Rust installation, and both Clang and arm-none-eabi-gcc.
On Ubuntu, `gcc-multilib` might also be required.

Using rustc nightly builds is mandatory as some unstable features are required.

- `rustup default nightly`
- `rustup component add rust-src`
- install [Clang](http://releases.llvm.org/download.html).
- install an [ARM gcc toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads)

If you wish to install the ARM gcc toolchain using your distribution's packages, these commands should work:

```bash
# On Debian and Ubuntu
sudo apt install clang gcc-arm-none-eabi gcc-multilib

# On Fedora or Red Hat Entreprise Linux
sudo dnf install clang arm-none-eabi-gcc arm-none-eabi-newlib

# On ArchLinux
sudo pacman -S clang arm-none-eabi-gcc arm-none-eabi-newlib
```

This SDK provides three [custom target](https://doc.rust-lang.org/rustc/targets/custom.html) files for Nano S, Nano X and Nano S+.

### Building for Nano S

```
cargo build --release -Z build-std=core --target=./nanos.json
```

### Building for Nano X

```
cargo build --release -Z build-std=core --target=./nanox.json
```

### Building for Nano S+

```
cargo build --release -Z build-std=core --target=./nanosplus.json
```


## Building with rustc < 1.54

Building before rustc 1.54 should fail with `error[E0635]: unknown feature const_fn_trait_bound`.

This is solved by activating a specific feature: `cargo build --features pre1_54`

## Contributing

Make sure you've followed the installation steps above. In order for your PR to be accepted, it will have to pass the CI, which performs the following checks:

- Check if the code builds on nightly
- Check that `clippy` does not emit any warnings
- check that your code follows `rustfmt`'s format (using `cargo fmt`)
* [ledger_device_rust_sdk](./ledger_device_rust_sdk): main Rust SDK crate used to build an application that runs on BOLOS OS,
* [ledger_device_ui_sdk](./ledger_device_ui_sdk): UI SDK used by application to get access to UI gadgets,
* [ledger_secure_sdk_sys](./ledger_secure_sdk_sys): bindings to [ledger_secure_sdk](https://github.com/LedgerHQ/ledger-secure-sdk)
* [include_gif](./include_gif): procedural macro used to manage GIF.
11 changes: 11 additions & 0 deletions include_gif/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "include_gif"
version = "1.0.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
syn = { version = "1.0", features = ["full"] }
gif = "0.11.3"
3 changes: 3 additions & 0 deletions include_gif/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sdk_include_gif

This crate provides a macro `include_gif!("path/to/image.gif")` that packs a gif image into a byte representation that can be understood by the [Rust Nano SDK](https://github.com/LedgerHQ/ledger-nanos-sdk) and included at compile time to produce black-and-white icons.
43 changes: 43 additions & 0 deletions include_gif/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use syn;
use std;
use std::fs::File;
use std::io::Write;

#[proc_macro]
pub fn include_gif(input: TokenStream) -> TokenStream {
let filename = syn::parse_macro_input!(input as syn::LitStr);
let mut decoder = gif::DecodeOptions::new();
decoder.set_color_output(gif::ColorOutput::Indexed);
let file = File::open(filename.value()).unwrap();
let mut decoder = decoder.read_info(file).unwrap();

let frame = decoder.read_next_frame().unwrap().unwrap().clone();
let palette = decoder.palette().unwrap();
let dimensions = frame.width * frame.height;
let (size, remainder) = ((dimensions / 8) as usize, (dimensions % 8) as usize);

let mut packed = Vec::new();
for i in 0..size {
let mut byte = 0;
for j in 0..8 {
let color = (palette[frame.buffer[8*i + j] as usize * 3] != 0) as u8;
byte |= color << j;
}
packed.push(byte);
}
let mut byte = 0;
for j in 0..remainder {
let color = (palette[frame.buffer[8*size + j] as usize * 3] != 0) as u8;
byte |= color << j;
}
packed.push(byte);

let mut b = Vec::new();
write!(&mut b, "(&{:?}, {}, {})", packed, frame.width, frame.height).unwrap();
let a = std::str::from_utf8(&b).unwrap();

a.parse().unwrap()
}
15 changes: 15 additions & 0 deletions ledger_device_sdk/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[target.nanos]
runner = "speculos -m nanos --display=headless"

[target.nanox]
runner = "speculos -m nanox -a 5 --display=headless"

[target.nanosplus]
runner = "speculos -m nanosp -a 1 --display=headless"

[unstable]
build-std = ["core"]
build-std-features = ["compiler-builtins-mem"]

[build]
target = "nanosplus"
30 changes: 30 additions & 0 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "ledger_device_sdk"
version = "1.0.0"
authors = ["yhql"]
edition = "2021"
license.workspace = true

[dev-dependencies]
# enable the 'speculos' feature when testing
# https://github.com/rust-lang/cargo/issues/2911#issuecomment-749580481
ledger_device_sdk = { path = ".", features = ["speculos"] }

testmacro = { git = "https://github.com/yhql/testmacro"}

[dependencies]
ledger_secure_sdk_sys = { path = "../ledger_secure_sdk_sys" }
num-traits = { version = "0.2.14", default_features = false }
rand_core = { version = "0.6.3", default_features = false }
zeroize = { version = "1.6.0", default_features = false }

[profile.release]
opt-level = 's'
lto = true

[features]
speculos = []
pre1_54 = []
lib_bagl = []
ccid = []
pending_review_screen = []
82 changes: 82 additions & 0 deletions ledger_device_sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Ledger wallets SDK for Rust Applications

Crate that allows developing Ledger Nano apps in Rust with a default configuration.

Contains:

- some safe wrappers over common syscalls
- IO abstractions
- signature abstractions

This SDK is incomplete in the sense that wrappers are currently missing, and not all wrappers have a nice Rust abstraction on top, but two apps were made using it:

- [A demo application with a signature UI workflow](https://github.com/LedgerHQ/rust-app)
- [A Password Manager](https://github.com/LedgerHQ/rust-app-password-manager)

You can submit an issue or even a pull request if you wish to contribute, we will check what we can do.

## Supported devices

| Nano S | Nano X | Nano S Plus |
| ------------------ | ------------------ | ------------------ |
| :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |

## Usage

Building requires adding `rust-src` to your Rust installation, and both Clang and arm-none-eabi-gcc.
On Ubuntu, `gcc-multilib` might also be required.

Using rustc nightly builds is mandatory as some unstable features are required.

- `rustup default nightly`
- `rustup component add rust-src`
- install [Clang](http://releases.llvm.org/download.html).
- install an [ARM gcc toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads)

If you wish to install the ARM gcc toolchain using your distribution's packages, these commands should work:

```bash
# On Debian and Ubuntu
sudo apt install clang gcc-arm-none-eabi gcc-multilib

# On Fedora or Red Hat Entreprise Linux
sudo dnf install clang arm-none-eabi-gcc arm-none-eabi-newlib

# On ArchLinux
sudo pacman -S clang arm-none-eabi-gcc arm-none-eabi-newlib
```

This SDK provides three [custom target](https://doc.rust-lang.org/rustc/targets/custom.html) files for Nano S, Nano X and Nano S+.

### Building for Nano S

```
cargo build --release -Z build-std=core --target=./nanos.json
```

### Building for Nano X

```
cargo build --release -Z build-std=core --target=./nanox.json
```

### Building for Nano S+

```
cargo build --release -Z build-std=core --target=./nanosplus.json
```


## Building with rustc < 1.54

Building before rustc 1.54 should fail with `error[E0635]: unknown feature const_fn_trait_bound`.

This is solved by activating a specific feature: `cargo build --features pre1_54`

## Contributing

Make sure you've followed the installation steps above. In order for your PR to be accepted, it will have to pass the CI, which performs the following checks:

- Check if the code builds on nightly
- Check that `clippy` does not emit any warnings
- check that your code follows `rustfmt`'s format (using `cargo fmt`)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn panic(_info: &PanicInfo) -> ! {
loop {}
}

use nanos_sdk::ecc::{make_bip32_path, Secp256r1, SeedDerive};
use ledger_device_rust_sdk::ecc::{make_bip32_path, Secp256r1, SeedDerive};

const PATH: [u32; 5] = make_bip32_path(b"m/44'/123'/0'/0/0");

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/ble.rs → ledger_device_sdk/src/ble.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ledger_sdk_sys::{LEDGER_BLE_receive, LEDGER_BLE_send, LEDGER_BLE_set_recv_buffer};
use ledger_secure_sdk_sys::{LEDGER_BLE_receive, LEDGER_BLE_send, LEDGER_BLE_set_recv_buffer};

pub fn receive(apdu_buffer: &mut [u8], spi_buffer: &[u8]) {
unsafe {
Expand Down
File renamed without changes.
Loading
Loading