Skip to content

Commit

Permalink
Use buffer to store data to be sent back through comm object
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Oct 7, 2024
1 parent 5387c6f commit 979099e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion starknet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starknet"
version = "2.1.0"
version = "2.1.1"
edition = "2021"
authors = ["Ledger"]

Expand Down
2 changes: 1 addition & 1 deletion starknet/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub fn show_status(flag: bool, ctx: &mut Ctx) {
#[cfg(any(target_os = "stax", target_os = "flex"))]
{
let status = NbglReviewStatus::new();
status.show_and_return(flag);
status.show(flag);
ctx.home.show_and_return();
}
}
Expand Down
69 changes: 40 additions & 29 deletions starknet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ extern "C" fn sample_main() {
// or an APDU command
if let io::Event::Command(ins) = display::main_ui(&mut comm) {
match handle_apdu(&mut comm, &ins, &mut ctx) {
Ok(()) => comm.reply_ok(),
Ok(data) => {
comm.append(data.as_slice());
comm.reply_ok()
}
Err(sw) => comm.reply(sw),
}
}
Expand All @@ -54,7 +57,10 @@ extern "C" fn sample_main() {
// Wait for an APDU command
let ins: Ins = comm.next_command();
match handle_apdu(&mut comm, &ins, &mut ctx) {
Ok(()) => comm.reply_ok(),
Ok(data) => {
comm.append(data.as_slice());
comm.reply_ok()
}
Err(sw) => comm.reply(sw),
}
}
Expand Down Expand Up @@ -108,12 +114,16 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,

let apdu_header = comm.get_apdu_metadata();

let mut rdata: Vec<u8> = Vec::new();

match ins {
Ins::GetVersion => {
let version_major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
let version_minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
let version_patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap();
comm.append([version_major, version_minor, version_patch].as_slice());

rdata.extend_from_slice([version_major, version_minor, version_patch].as_slice());
//comm.append([version_major, version_minor, version_patch].as_slice());
}
Ins::GetPubkey { display } => {
ctx.reset();
Expand All @@ -138,7 +148,8 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
true => display::pkey_ui(key.as_ref(), ctx),
};
if ret {
comm.append(key.as_ref());
//comm.append(key.as_ref());
rdata.extend_from_slice(key.as_ref());
} else {
return Err(io::StatusWords::UserCancelled.into());
}
Expand All @@ -164,10 +175,10 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
match display::show_hash(ctx, false) {
true => {
crypto::sign_hash(ctx).unwrap();
comm.append([0x41].as_slice());
comm.append(ctx.hash.r.as_ref());
comm.append(ctx.hash.s.as_ref());
comm.append([ctx.hash.v].as_slice());
rdata.extend_from_slice([0x41].as_slice());
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
}
false => {
return Err(io::StatusWords::UserCancelled.into());
Expand Down Expand Up @@ -213,12 +224,12 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
true => {
display::show_pending();
ctx.hash.m_hash = crypto::tx_hash(&ctx.tx);
comm.append(ctx.hash.m_hash.value.as_ref());
rdata.extend_from_slice(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
comm.append([SIG_LENGTH].as_slice());
comm.append(ctx.hash.r.as_ref());
comm.append(ctx.hash.s.as_ref());
comm.append([ctx.hash.v].as_slice());
rdata.extend_from_slice([SIG_LENGTH].as_slice());
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
display::show_status(true, ctx);
}
false => {
Expand All @@ -231,12 +242,12 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
ctx.hash.m_hash = crypto::tx_hash(&ctx.tx);
match display::show_hash(ctx, true) {
true => {
comm.append(ctx.hash.m_hash.value.as_ref());
rdata.extend_from_slice(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
comm.append([SIG_LENGTH].as_slice());
comm.append(ctx.hash.r.as_ref());
comm.append(ctx.hash.s.as_ref());
comm.append([ctx.hash.v].as_slice());
rdata.extend_from_slice([SIG_LENGTH].as_slice());
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
display::show_status(true, ctx);
}
false => {
Expand Down Expand Up @@ -284,12 +295,12 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
true => {
display::show_pending();
ctx.hash.m_hash = crypto::tx_hash(&ctx.tx);
comm.append(ctx.hash.m_hash.value.as_ref());
rdata.extend_from_slice(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
comm.append([0x41].as_slice());
comm.append(ctx.hash.r.as_ref());
comm.append(ctx.hash.s.as_ref());
comm.append([ctx.hash.v].as_slice());
rdata.extend_from_slice([0x41].as_slice());
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
display::show_status(true, ctx);
}
false => {
Expand All @@ -302,12 +313,12 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
ctx.hash.m_hash = crypto::tx_hash(&ctx.tx);
match display::show_hash(ctx, true) {
true => {
comm.append(ctx.hash.m_hash.value.as_ref());
rdata.extend_from_slice(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
comm.append([0x41].as_slice());
comm.append(ctx.hash.r.as_ref());
comm.append(ctx.hash.s.as_ref());
comm.append([ctx.hash.v].as_slice());
rdata.extend_from_slice([0x41].as_slice());
rdata.extend_from_slice(ctx.hash.r.as_ref());
rdata.extend_from_slice(ctx.hash.s.as_ref());
rdata.extend_from_slice([ctx.hash.v].as_slice());
display::show_status(true, ctx);
}
false => {
Expand Down Expand Up @@ -375,5 +386,5 @@ fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<Vec<u8>,
}
}
}
Ok(())
Ok(rdata)
}
Binary file modified tests/snapshots/flex/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanosp/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanox/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/stax/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 979099e

Please sign in to comment.