Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
feat: improve display on public key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Feb 8, 2024
1 parent 0348292 commit 4d2a351
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 51 deletions.
16 changes: 0 additions & 16 deletions src/app_ui/sign/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,3 @@ pub fn delegate_error_screen() {

screen_update();
}

pub fn public_key_mismatch() {
clear_screen();

// Add icon and text to match the C SDK equivalent.
if cfg!(target_os = "nanos") {
"Public key".place(Location::Custom(2), Layout::Centered, true);
"field mismatch...".place(Location::Custom(14), Layout::Centered, true);
} else {
WARNING.draw(57, 10);
"Public key field".place(Location::Custom(28), Layout::Centered, true);
"field mismatch...".place(Location::Custom(42), Layout::Centered, true);
}

screen_update();
}
114 changes: 81 additions & 33 deletions src/handlers/common/validate_public_key.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,94 @@
use ledger_device_sdk::io::Event;
use ledger_secure_sdk_sys::buttons::ButtonEvent;
use ledger_device_sdk::ui::{
bitmaps::{CROSSMARK, EYE},
gadgets::{Field, MultiFieldReview},
};

use crate::{
parsing::{HashingStream, SingleTxStream},
sign_ui,
utils::crypto::{self, public_key::NoSecpAllowed, PathBip32, PublicKeyBe},
AppSW, Instruction,
utils::{
crypto::{self, public_key::NoSecpAllowed, PathBip32, PublicKeyBe},
types::fmt_buffer::FmtBuffer,
},
AppSW,
};

pub fn validate(
stream: &mut HashingStream<SingleTxStream<'_>>,
tx_public_key: Result<PublicKeyBe, NoSecpAllowed>,
path: &PathBip32,
) -> Result<(), AppSW> {
match tx_public_key {
Ok(tx_public_key) => {
let matching_private_key = {
let pk = crypto::bip32_derive(&path.0)
.public_key()
.map_err(|_| AppSW::KeyDeriveFail)?;
PublicKeyBe::from_little_endian(pk)
};
if tx_public_key == matching_private_key {
let matching_private_key = {
let pk = crypto::bip32_derive(&path.0)
.public_key()
.map_err(|_| AppSW::KeyDeriveFail)?;
PublicKeyBe::from_little_endian(pk)
};
let info = match tx_public_key {
Ok(transaction_field) => {
if transaction_field == matching_private_key {
return Ok(());
}
KeyMismatchInfo::KeyMismatch {
transaction_field,
matching_private_key,
}
}
Err(_err) => KeyMismatchInfo::NoSecpAllowed {
matching_private_key,
},
};
let _confirm = ui_display(&info)?;

Err(AppSW::PublicKeyMismatch)
}

enum KeyMismatchInfo {
NoSecpAllowed {
matching_private_key: PublicKeyBe,
},
KeyMismatch {
transaction_field: PublicKeyBe,
matching_private_key: PublicKeyBe,
},
}

fn ui_display(info: &KeyMismatchInfo) -> Result<bool, AppSW> {
let mut key_buf1 = FmtBuffer::<60>::new();
let mut key_buf2 = FmtBuffer::<60>::new();
match info {
KeyMismatchInfo::NoSecpAllowed {
matching_private_key,
} => {
key_buf1.write_str("SECP256K1 curve was used");
matching_private_key.display_str_base58(&mut key_buf2)?;
}
KeyMismatchInfo::KeyMismatch {
transaction_field,
matching_private_key,
} => {
transaction_field.display_str_base58(&mut key_buf1)?;
matching_private_key.display_str_base58(&mut key_buf2)?;
}
Err(_err) => {}
}
stream.reader.comm.reply(AppSW::PublicKeyMismatch);
sign_ui::widgets::public_key_mismatch();

loop {
match stream.reader.comm.next_event::<Instruction>() {
Event::Button(button) => match button {
ButtonEvent::BothButtonsRelease => {
return Err(AppSW::PublicKeyMismatch);
}
_ => {
// ignore all other button presses
}
},
_ => (),
};
}

let my_fields = [
Field {
name: "Transaction Field",
value: key_buf1.as_str(),
},
Field {
name: "Requested BIP32",
value: key_buf2.as_str(),
},
];

let my_review = MultiFieldReview::new(
&my_fields,
&["Pub Key Mismatch"],
Some(&EYE),
"Error!",
Some(&CROSSMARK),
"Error!",
Some(&CROSSMARK),
);

Ok(my_review.show())
}
2 changes: 1 addition & 1 deletion src/handlers/sign_nep366_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn handler(mut stream: SingleTxStream<'_>) -> Result<Signature, AppSW> {
.map_err(|_err| AppSW::TxParsingFail)?;

let delegate_ac_pub_key_prevalidation = handle_delegate_action(&mut stream)?;
validate_public_key::validate(&mut stream, delegate_ac_pub_key_prevalidation, &path)?;
validate_public_key::validate(delegate_ac_pub_key_prevalidation, &path)?;

finalize_sign::end(&mut stream, &path)
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn handler(mut stream: SingleTxStream<'_>) -> Result<Signature, AppSW> {
number_of_actions,
tx_public_key_prevalidation,
} = handle_transaction_prefix(&mut stream)?;
validate_public_key::validate(&mut stream, tx_public_key_prevalidation, &path)?;
validate_public_key::validate(tx_public_key_prevalidation, &path)?;

for i in 0..number_of_actions {
sign_ui::widgets::display_receiving();
Expand Down

0 comments on commit 4d2a351

Please sign in to comment.