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

Y333/starknet nbgl2 #33

Merged
merged 5 commits into from
Sep 23, 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
4 changes: 2 additions & 2 deletions Cargo.lock

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

33 changes: 33 additions & 0 deletions samples/apdu/get_version_multiple.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000
=> 5a00000000



2 changes: 1 addition & 1 deletion starknet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
authors = ["Ledger"]

[dependencies]
ledger_device_sdk = "1.15.5"
ledger_device_sdk = "1.16.0"
ledger_secure_sdk_sys = { version = "1.4.6", features = ["heap"]}
include_gif = "1.2.0"
hex = { version = "0.4", default-features = false, features = ["alloc"]}
Expand Down
7 changes: 7 additions & 0 deletions starknet/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ impl Hash {
}
}

#[cfg(any(target_os = "stax", target_os = "flex"))]
use ledger_device_sdk::nbgl::NbglHomeAndSettings;

pub struct Ctx {
pub req_type: RequestType,
pub tx: Transaction,
pub hash: Hash,
pub bip32_path: [u32; 6],
#[cfg(any(target_os = "stax", target_os = "flex"))]
pub home: NbglHomeAndSettings,
}

impl Ctx {
Expand All @@ -103,6 +108,8 @@ impl Ctx {
hash: Hash::default(),
req_type: RequestType::Unknown,
bip32_path: [0u32; 6],
#[cfg(any(target_os = "stax", target_os = "flex"))]
home: NbglHomeAndSettings::new(),
}
}

Expand Down
41 changes: 22 additions & 19 deletions starknet/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use crate::{
};
use alloc::format;
use include_gif::include_gif;
use ledger_device_sdk::{
io::{Comm, Event},
testing,
};
use ledger_device_sdk::{io::Comm, testing};

use crate::context::{Ctx, Transaction};

Expand All @@ -26,8 +23,6 @@ use ledger_device_sdk::nbgl::{
NbglReviewStatus, NbglStatus, TagValueConfirm, TagValueList, TransactionType, TuneIndex,
};

use crate::Ins;

pub fn show_tx(ctx: &mut Ctx) -> Option<bool> {
match support_clear_sign(&ctx.tx) {
Some(t) => {
Expand Down Expand Up @@ -224,7 +219,9 @@ pub fn show_hash(ctx: &mut Ctx, is_tx_hash: bool) -> bool {
.blind();
}

review.show(&my_field)
let res = review.show(&my_field);
ctx.home.show_and_return();
res
}
}

Expand All @@ -246,7 +243,8 @@ pub fn show_pending() {
}
}

pub fn show_status(flag: bool) {
#[allow(unused_variables)]
pub fn show_status(flag: bool, ctx: &mut Ctx) {
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
{
let content = match flag {
Expand All @@ -261,10 +259,12 @@ pub fn show_status(flag: bool) {
{
let mut status = NbglReviewStatus::new();
status.show(flag);
ctx.home.show_and_return();
}
}

pub fn pkey_ui(key: &[u8]) -> bool {
#[allow(unused_variables)]
pub fn pkey_ui(key: &[u8], ctx: &mut Ctx) -> bool {
let mut pk_hex = [0u8; 64];
hex::encode_to_slice(&key[1..33], &mut pk_hex[0..]).unwrap();
let m = core::str::from_utf8_mut(&mut pk_hex).unwrap();
Expand Down Expand Up @@ -301,20 +301,26 @@ pub fn pkey_ui(key: &[u8]) -> bool {
true => {
let status = NbglStatus::new();
status.text("Public Key Confirmed").show(true);
ctx.home.show_and_return();
true
}
false => {
let status = NbglStatus::new();
status.text("Public Key Rejected").show(false);
ctx.home.show_and_return();
false
}
}
}
}

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use crate::Ins;
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_device_sdk::io::Event;

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
fn about_ui(comm: &mut Comm) -> Event<Ins> {
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
{
let pages = [
&Page::from((["Starknet", "(c) 2024 Ledger"], true)),
Expand Down Expand Up @@ -352,19 +358,16 @@ pub fn main_ui(comm: &mut Comm) -> Event<Ins> {
}

#[cfg(any(target_os = "stax", target_os = "flex"))]
pub fn main_ui(_comm: &mut Comm) -> Event<Ins> {
pub fn main_ui_nbgl(_comm: &mut Comm) -> NbglHomeAndSettings {
// Load glyph from 64x64 4bpp gif file with include_gif macro. Creates an NBGL compatible glyph.
const APP_ICON: NbglGlyph = NbglGlyph::from_include(include_gif!("starknet_64x64.gif", NBGL));

// Display the home screen.
NbglHomeAndSettings::new()
.glyph(&APP_ICON)
.infos(
"Starknet",
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_AUTHORS"),
)
.show()
NbglHomeAndSettings::new().glyph(&APP_ICON).infos(
"Starknet",
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_AUTHORS"),
)
}

fn support_clear_sign(tx: &Transaction) -> Option<usize> {
Expand Down
56 changes: 37 additions & 19 deletions starknet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,43 @@ extern "C" fn sample_main() {
// Init comm and set the expected CLA byte for the application
let mut comm = io::Comm::new().set_expected_cla(0x5A);

// Initialize reference to Comm instance for NBGL
// API calls.
let mut ctx: Ctx = Ctx::new();

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
{
loop {
// Wait for either a specific button push to exit the app
// 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(),
Err(sw) => comm.reply(sw),
}
}
}
}

#[cfg(any(target_os = "stax", target_os = "flex"))]
init_comm(&mut comm);
{
// Initialize reference to Comm instance for NBGL
// API calls.
init_comm(&mut comm);

let mut ctx: Ctx = Ctx::new();
ctx.home = display::main_ui_nbgl(&mut comm);

loop {
// Wait for either a specific button push to exit the app
// or an APDU command
if let io::Event::Command(ins) = display::main_ui(&mut comm) {
match handle_apdu(&mut comm, ins, &mut ctx) {
ctx.home.show_and_return();
loop {
// Wait for an APDU command
let ins: Ins = comm.next_command();
match handle_apdu(&mut comm, &ins, &mut ctx) {
Ok(()) => comm.reply_ok(),
Err(sw) => comm.reply(sw),
}
}
}
}

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
enum Ins {
GetVersion,
Expand Down Expand Up @@ -81,7 +99,7 @@ impl TryFrom<io::ApduHeader> for Ins {

use ledger_device_sdk::io::Reply;

fn handle_apdu(comm: &mut io::Comm, ins: Ins, ctx: &mut Ctx) -> Result<(), Reply> {
fn handle_apdu(comm: &mut io::Comm, ins: &Ins, ctx: &mut Ctx) -> Result<(), Reply> {
if comm.rx == 0 {
return Err(io::StatusWords::NothingReceived.into());
}
Expand Down Expand Up @@ -115,7 +133,7 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins, ctx: &mut Ctx) -> Result<(), Reply
Ok(key) => {
let ret = match display {
false => true,
true => display::pkey_ui(key.as_ref()),
true => display::pkey_ui(key.as_ref(), ctx),
};
if ret {
comm.append(key.as_ref());
Expand Down Expand Up @@ -195,14 +213,14 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins, ctx: &mut Ctx) -> Result<(), Reply
ctx.hash.m_hash = crypto::tx_hash(&ctx.tx);
comm.append(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
display::show_status(true);
display::show_status(true, ctx);
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());
}
false => {
display::show_status(false);
display::show_status(false, ctx);
return Err(io::StatusWords::UserCancelled.into());
}
},
Expand All @@ -213,14 +231,14 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins, ctx: &mut Ctx) -> Result<(), Reply
true => {
comm.append(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
display::show_status(true);
display::show_status(true, ctx);
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());
}
false => {
display::show_status(false);
display::show_status(false, ctx);
return Err(io::StatusWords::UserCancelled.into());
}
}
Expand Down Expand Up @@ -266,14 +284,14 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins, ctx: &mut Ctx) -> Result<(), Reply
ctx.hash.m_hash = crypto::tx_hash(&ctx.tx);
comm.append(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
display::show_status(true);
display::show_status(true, ctx);
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());
}
false => {
display::show_status(false);
display::show_status(false, ctx);
return Err(io::StatusWords::UserCancelled.into());
}
},
Expand All @@ -284,14 +302,14 @@ fn handle_apdu(comm: &mut io::Comm, ins: Ins, ctx: &mut Ctx) -> Result<(), Reply
true => {
comm.append(ctx.hash.m_hash.value.as_ref());
crypto::sign_hash(ctx).unwrap();
display::show_status(true);
display::show_status(true, ctx);
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());
}
false => {
display::show_status(false);
display::show_status(false, ctx);
return Err(io::StatusWords::UserCancelled.into());
}
}
Expand Down
Loading