-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update app instruction handling and UI.
- Loading branch information
1 parent
720b20d
commit 8a9f430
Showing
6 changed files
with
228 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/***************************************************************************** | ||
* Ledger App Boilerplate Rust. | ||
* (c) 2023 Ledger SAS. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*****************************************************************************/ | ||
|
||
use crate::utils; | ||
use core::str::from_utf8; | ||
use nanos_sdk::io; | ||
use nanos_ui::bitmaps::{CROSSMARK, EYE, VALIDATE_14}; | ||
use nanos_ui::ui::{Field, MultiFieldReview}; | ||
|
||
pub fn ui_display_pk(pk: &[u8]) -> Result<bool, io::Reply> { | ||
// Todo add error handling | ||
// ====================== | ||
let hex = utils::to_hex(pk).unwrap(); | ||
let m = from_utf8(&hex).unwrap(); | ||
// ====================== | ||
|
||
let my_field = [Field { | ||
name: "Public Key", | ||
value: m[..pk.len() * 2].as_ref(), | ||
}]; | ||
|
||
let my_review = MultiFieldReview::new( | ||
&my_field, | ||
&["Confirm Address"], | ||
Some(&EYE), | ||
"Approve", | ||
Some(&VALIDATE_14), | ||
"Reject", | ||
Some(&CROSSMARK), | ||
); | ||
|
||
Ok(my_review.show()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/***************************************************************************** | ||
* Ledger App Boilerplate Rust. | ||
* (c) 2023 Ledger SAS. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*****************************************************************************/ | ||
|
||
use crate::app_ui::address::ui_display_pk; | ||
use crate::SW_DENY; | ||
use nanos_sdk::ecc::{Secp256k1, SeedDerive}; | ||
use nanos_sdk::{io, testing}; | ||
|
||
const MAX_ALLOWED_PATH_LEN: usize = 10; | ||
|
||
// const SW_DENY: u16 = 0x6985; | ||
|
||
pub fn handler_get_public_key(comm: &mut io::Comm, display: bool) -> Result<(), io::Reply> { | ||
let mut path = [0u32; MAX_ALLOWED_PATH_LEN]; | ||
let data = comm.get_data()?; | ||
|
||
let path_len = read_bip32_path(data, &mut path)?; | ||
|
||
let pk = Secp256k1::derive_from_path(&path[..path_len]) | ||
.public_key() | ||
.map_err(|x| io::Reply(0x6eu16 | (x as u16 & 0xff)))?; | ||
|
||
// Display public key on device if requested | ||
if display { | ||
testing::debug_print("showing public key\n"); | ||
if !ui_display_pk(&pk.pubkey)? { | ||
testing::debug_print("denied\n"); | ||
return Err(io::Reply(SW_DENY)); | ||
} | ||
} | ||
|
||
comm.append(&[pk.pubkey.len() as u8]); | ||
comm.append(&pk.pubkey); | ||
// Rust SDK key derivation API does not return chaincode yet | ||
// so we just append a dummy chaincode. | ||
const CHAINCODE_LEN: usize = 32; | ||
comm.append(&[CHAINCODE_LEN as u8]); // Dummy chaincode length | ||
comm.append(&[0u8; CHAINCODE_LEN]); // Dummy chaincode | ||
|
||
Ok(()) | ||
} | ||
|
||
fn read_bip32_path(data: &[u8], path: &mut [u32]) -> Result<usize, io::Reply> { | ||
// Check input length and path buffer capacity | ||
if data.len() < 1 || path.len() < data.len() / 4 { | ||
return Err(io::StatusWords::BadLen.into()); | ||
} | ||
|
||
let path_len = data[0] as usize; // First byte is the length of the path | ||
let path_data = &data[1..]; | ||
|
||
// Check path data length and alignment | ||
if path_data.len() != path_len * 4 | ||
|| path_data.len() > MAX_ALLOWED_PATH_LEN * 4 | ||
|| path_data.len() % 4 != 0 | ||
{ | ||
return Err(io::StatusWords::BadLen.into()); | ||
} | ||
|
||
let mut idx = 0; | ||
for (i, chunk) in path_data.chunks(4).enumerate() { | ||
path[idx] = u32::from_be_bytes(chunk.try_into().unwrap()); | ||
idx = i + 1; | ||
} | ||
|
||
Ok(idx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/***************************************************************************** | ||
* Ledger App Boilerplate Rust. | ||
* (c) 2023 Ledger SAS. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*****************************************************************************/ | ||
|
||
use core::str::FromStr; | ||
use nanos_sdk::io; | ||
|
||
pub fn handler_get_version(comm: &mut io::Comm) -> Result<(), io::Reply> { | ||
if let Some((major, minor, patch)) = parse_version_string(env!("CARGO_PKG_VERSION")) { | ||
comm.append(&[major, minor, patch]); | ||
Ok(()) | ||
} else { | ||
Err(io::StatusWords::Unknown.into()) | ||
} | ||
} | ||
|
||
fn parse_version_string(input: &str) -> Option<(u8, u8, u8)> { | ||
// Split the input string by '.'. | ||
// Input should be of the form "major.minor.patch", | ||
// where "major", "minor", and "patch" are integers. | ||
let mut parts = input.split('.'); | ||
let major = u8::from_str(parts.next()?).ok()?; | ||
let minor = u8::from_str(parts.next()?).ok()?; | ||
let patch = u8::from_str(parts.next()?).ok()?; | ||
Some((major, minor, patch)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters