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

upgrade + deprecate zemu transport #110

Merged
merged 4 commits into from
May 9, 2024
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
7 changes: 3 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ jobs:
- name: show versions
run: |
rustup show

- name: Install rustfmt
run: rustup component add rustfmt --toolchain nightly
- name: rustfmt
run: |
cargo fmt --version
cargo fmt -- --check

cargo +nightly fmt -- --check
- name: rust cache
uses: Swatinem/rust-cache@v1
with:
Expand Down
9 changes: 0 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ members = [
"ledger-apdu",
"ledger-transport",
"ledger-transport-hid",
"ledger-transport-zemu",
"ledger-zondax-generic",
]

exclude = []

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
overflow-checks = true

[patch.crates-io]
ledger-apdu = { path = "ledger-apdu" }
ledger-transport = { path = "ledger-transport" }
ledger-transport-hid = { path = "ledger-transport-hid" }
ledger-transport-zemu = { path = "ledger-transport-zemu" }
ledger-zondax-generic = { path = "ledger-zondax-generic" }
8 changes: 4 additions & 4 deletions ledger-apdu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ledger-apdu"
description = "Ledger Hardware Wallet - Common APDU Protocol Types"
version = "0.10.0"
version = "0.11.0"
license = "Apache-2.0"
authors = ["Zondax AG <[email protected]>"]
homepage = "https://github.com/zondax/ledger-rs"
Expand All @@ -16,6 +16,6 @@ std = ["snafu/std", "no-std-compat/std"]
default = ["std"]

[dependencies]
arrayref = "0.3.6"
no-std-compat = "0.4.1"
snafu = { version = "0.7", features = ["rust_1_46"], default-features = false }
arrayref = "0.3"
no-std-compat = "0.4"
snafu = { version = "0.8", default-features = false }
15 changes: 7 additions & 8 deletions ledger-apdu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,21 @@ pub enum APDUAnswerError {

impl<B> APDUAnswer<B>
where
B: std::ops::Deref<Target = [u8]>,
B: Deref<Target = [u8]>,
{
/// Attempt to interpret the given slice as an APDU answer
pub fn from_answer(answer: B) -> Result<Self, APDUAnswerError> {
ensure!(answer.len() >= 2, TooShortSnafu);
let retcode = arrayref::array_ref!(answer, answer.len() - 2, 2);
let retcode = u16::from_be_bytes(*retcode);

Ok(APDUAnswer {
data: answer,
retcode,
})
Ok(APDUAnswer { data: answer, retcode })
}

/// Will return the answer's payload
#[inline(always)]
pub fn apdu_data(&self) -> &[u8] {
&self.data[..self.data.len() - 2]
&self.data[.. self.data.len() - 2]
}

/// Will return the answer's payload
Expand All @@ -103,7 +100,9 @@ where
/// Will attempt to interpret the error code as an [APDUErrorCode],
/// returning the code as is otherwise
pub fn error_code(&self) -> Result<APDUErrorCode, u16> {
self.retcode.try_into().map_err(|_| self.retcode)
self.retcode
.try_into()
.map_err(|_| self.retcode)
}

/// Returns the raw return code
Expand Down Expand Up @@ -149,7 +148,7 @@ pub enum APDUErrorCode {

#[cfg(feature = "std")]
impl APDUErrorCode {
/// Quickhand to retrieve the error code's description / display
/// Quick-hand to retrieve the error code's description / display
pub fn description(&self) -> std::string::String {
std::format!("{}", self)
}
Expand Down
42 changes: 19 additions & 23 deletions ledger-apdu/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,65 +23,61 @@ const APDU_RESPONSE: &[u8] = &[0xDE, 0xAD, 0xBE, 0xEF, 0x90, 0x00];
fn apdu_command_vec() {
let data = std::vec![SERIALIZED_APDU[5]; 3];

let command = APDUCommand {
cla: 0xFF,
ins: 0x00,
p1: 0,
p2: 0,
data,
};
let command = APDUCommand { cla: 0xFF, ins: 0x00, p1: 0, p2: 0, data };

assert_eq!(SERIALIZED_APDU, &command.serialize()[..])
}

#[test]
fn apdu_command_slice() {
let data = &SERIALIZED_APDU[5..];

let _ = APDUCommand {
cla: 0xFF,
ins: 0x00,
p1: 0,
p2: 0,
data,
};
let data = &SERIALIZED_APDU[5 ..];

let _ = APDUCommand { cla: 0xFF, ins: 0x00, p1: 0, p2: 0, data };
}

#[test]
fn apdu_answer_success() {
let answer = APDUAnswer::from_answer(APDU_RESPONSE).expect("valid answer length >= 2");

let code = answer.error_code().expect("valid error code");
let code = answer
.error_code()
.expect("valid error code");
assert_eq!(code, APDUErrorCode::NoError);

assert_eq!(answer.apdu_data(), &APDU_RESPONSE[..4]);
assert_eq!(answer.apdu_data(), &APDU_RESPONSE[.. 4]);
}

#[test]
fn apdu_answer_vec() {
let answer = APDUAnswer::from_answer(APDU_RESPONSE.to_vec()).expect("valid answer length >= 2");

let code = answer.error_code().expect("valid error code");
let code = answer
.error_code()
.expect("valid error code");
assert_eq!(code, APDUErrorCode::NoError);

assert_eq!(answer.apdu_data(), &APDU_RESPONSE[..4]);
assert_eq!(answer.apdu_data(), &APDU_RESPONSE[.. 4]);
}

#[test]
fn apdu_answer_error() {
let answer = APDUAnswer::from_answer(&[0x64, 0x00][..]).expect("valid answer length >= 2");

let code = answer.error_code().expect("valid error code");
let code = answer
.error_code()
.expect("valid error code");
assert_eq!(code, APDUErrorCode::ExecutionError);

assert_eq!(answer.apdu_data(), &[]);
}

#[test]
fn apdu_answer_unknown() {
let answer = APDUAnswer::from_answer(&APDU_RESPONSE[..4]).expect("valid answer length >= 2");
let answer = APDUAnswer::from_answer(&APDU_RESPONSE[.. 4]).expect("valid answer length >= 2");

let code = answer.error_code().expect_err("invalid error code");
let code = answer
.error_code()
.expect_err("invalid error code");
assert_eq!(code, 0xBEEF);

assert_eq!(answer.apdu_data(), &[0xDE, 0xAD]);
Expand Down
19 changes: 9 additions & 10 deletions ledger-transport-hid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ledger-transport-hid"
description = "Ledger Hardware Wallet - HID Transport"
version = "0.10.0"
version = "0.11.0"
license = "Apache-2.0"
authors = ["Zondax AG <[email protected]>"]
homepage = "https://github.com/zondax/ledger-rs"
Expand All @@ -13,19 +13,18 @@ edition = "2021"

[dependencies]
libc = "0.2"
byteorder = "1.4"
cfg-if = "1.0.0"
thiserror = "1.0"
byteorder = "1.5"
cfg-if = "1"
thiserror = "1"
hex = "0.4"
log = "0.4"

ledger-transport = "0.10"

hidapi = { version = "1.4.1", features = ["linux-static-hidraw"], default-features = false }
ledger-transport = { path = "../ledger-transport" }
hidapi = { version = "2.6.1", features = ["linux-static-hidraw"], default-features = false }

[dev-dependencies]
once_cell = "1"
ledger-zondax-generic = "0.10"
serial_test = "0.7.0"
env_logger = "0.9"
ledger-zondax-generic = { path = "../ledger-zondax-generic" }
serial_test = "3"
env_logger = "0.11"
futures = "0.3"
Loading
Loading