This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve display on public key validation
- Loading branch information
dj8yf0μl
committed
Feb 8, 2024
1 parent
0348292
commit 4d2a351
Showing
4 changed files
with
83 additions
and
51 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
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 |
---|---|---|
@@ -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()) | ||
} |
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