From a19a77e5b2cfcaf1e6d32b801633e9c2636a21fc Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Thu, 13 Jul 2023 16:20:14 +0200 Subject: [PATCH] Upgrade ledger SDK --- Cargo.toml | 1 + app/Cargo.lock | 14 ++++++++++++-- app/Cargo.toml | 2 +- app/src/app_utils.rs | 2 +- app/src/main.rs | 28 +++++++++++++++++----------- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44345ba2..065403d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "utils", + # "app" ] exclude = [ "app" ] diff --git a/app/Cargo.lock b/app/Cargo.lock index 32748548..a15c379d 100644 --- a/app/Cargo.lock +++ b/app/Cargo.lock @@ -6,7 +6,7 @@ version = 3 name = "app" version = "0.2.0" dependencies = [ - "nanos_sdk", + "nanos_sdk 0.2.1", "nanos_ui", "utils", ] @@ -58,13 +58,23 @@ dependencies = [ "rand_core", ] +[[package]] +name = "nanos_sdk" +version = "0.2.1" +source = "git+https://github.com/LedgerHQ/ledger-nanos-sdk.git?rev=4d9bfc6183d94cee6edb239c39286be3825cc179#4d9bfc6183d94cee6edb239c39286be3825cc179" +dependencies = [ + "cc", + "num-traits", + "rand_core", +] + [[package]] name = "nanos_ui" version = "0.2.0" source = "git+https://github.com/LedgerHQ/ledger-nanos-ui.git?rev=c7fe3dff2417f9a118ad176d578c5c1ee07e83cd#c7fe3dff2417f9a118ad176d578c5c1ee07e83cd" dependencies = [ "include_gif", - "nanos_sdk", + "nanos_sdk 0.2.0", ] [[package]] diff --git a/app/Cargo.toml b/app/Cargo.toml index 13df1aee..feaa490e 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -5,7 +5,7 @@ authors = ["alephium devs"] edition = "2021" [dependencies] -nanos_sdk = { git = "https://github.com/LedgerHQ/ledger-nanos-sdk.git" } +nanos_sdk = { git = "https://github.com/LedgerHQ/ledger-nanos-sdk.git", rev = "4d9bfc6183d94cee6edb239c39286be3825cc179" } nanos_ui = { git = "https://github.com/LedgerHQ/ledger-nanos-ui.git", rev = "c7fe3dff2417f9a118ad176d578c5c1ee07e83cd" } utils= { path = "../utils" } diff --git a/app/src/app_utils.rs b/app/src/app_utils.rs index f7674d74..2dce24cb 100644 --- a/app/src/app_utils.rs +++ b/app/src/app_utils.rs @@ -4,7 +4,7 @@ use core::ptr::null; #[cfg(feature = "debug")] pub mod print { - use nanos_sdk::debug_print; + use nanos_sdk::testing::debug_print; pub fn println(s: &str) { debug_print(s); diff --git a/app/src/main.rs b/app/src/main.rs index ab14493b..c490f027 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -2,6 +2,7 @@ #![no_main] use nanos_sdk::buttons::ButtonEvent; +use nanos_sdk::ecc::SeedDerive; use nanos_ui::layout; use nanos_ui::layout::Draw; use nanos_ui::layout::StringPlace; @@ -34,7 +35,7 @@ fn sign_ui(path: &[u32], message: &[u8]) -> Result, Sysc } if ui::Validator::new("Sign ?").ask() { - let signature = Secp256k1::from_bip32(path) + let signature = Secp256k1::derive_from_path(path) .deterministic_sign(message) .map_err(|_| SyscallError::Unspecified)?; ui::SingleMessage::new("Signing...").show(); @@ -150,13 +151,14 @@ enum Ins { SignHash, } -impl From for Ins { - fn from(ins: u8) -> Ins { - match ins { - 0 => Ins::GetVersion, - 1 => Ins::GetPubKey, - 2 => Ins::SignHash, - _ => panic!(), +impl TryFrom for Ins { + type Error = (); + fn try_from(header: io::ApduHeader) -> Result { + match header.ins { + 0 => Ok(Ins::GetVersion), + 1 => Ok(Ins::GetPubKey), + 2 => Ok(Ins::SignHash), + _ => Err(()), } } } @@ -169,6 +171,10 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins) -> Result { } let mut path: [u32; 5] = [0; 5]; + let apdu_header = comm.get_apdu_metadata(); + if apdu_header.cla != 0x80 { + return Err(io::StatusWords::BadCla.into()); + } match ins { Ins::GetVersion => { @@ -186,8 +192,8 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins) -> Result { } println_slice::<40>(raw_path); - let p1 = comm.get_p1(); - let p2 = comm.get_p2(); + let p1 = apdu_header.p1; + let p2 = apdu_header.p2; let (pk, hd_index) = if p1 == 0 { (derive_pub_key(& mut path)?, path[path.len() - 1]) @@ -226,7 +232,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins) -> Result { } fn derive_pub_key(path: &[u32]) -> Result, Reply> { - let pk = Secp256k1::from_bip32(path) + let pk = Secp256k1::derive_from_path(path) .public_key() .map_err(|x| Reply(0x6eu16 | (x as u16 & 0xff)))?; return Ok(pk);