Skip to content

Commit

Permalink
Feature/add in app settings (#42)
Browse files Browse the repository at this point in the history
* Add in-app settings
  • Loading branch information
siy authored Sep 13, 2023
1 parent ee15b20 commit 1619e8d
Show file tree
Hide file tree
Showing 38 changed files with 465 additions and 931 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "babylon-ledger-app"
version = "0.5.9-rcnet-v3.1"
version = "0.7.0"
authors = ["siy"]
edition = "2021"
description = "Radix Babylon"
Expand Down
162 changes: 39 additions & 123 deletions doc/api.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/app_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum AppError {
BadTxSignType = 0x6e35,
BadTxSignDigestState = 0x6e36,
BadTxSignRequestedState = 0x6e37,
BadTxSignHashSignState = 0x6e38, // Hash signing is disabled

BadTxSignDecoderErrorInvalidInput = 0x6e41,
BadTxSignDecoderErrorInvalidLen = 0x6e42,
Expand Down
12 changes: 3 additions & 9 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ use nanos_sdk::io::ApduHeader;

#[repr(u8)]
pub enum Command {
GetAppVersion,
GetDeviceModel,
GetDeviceId,
GetAppVersion,
GetAppSettings,
GetPubKeyEd25519,
GetPrivKeyEd25519,
GetPubKeySecp256k1,
GetPrivKeySecp256k1,
SignTxEd25519,
SignTxEd25519Summary,
SignTxSecp256k1,
SignTxSecp256k1Summary,
SignAuthEd25519,
SignAuthSecp256k1,
VerifyAddressEd25519,
Expand All @@ -29,14 +26,11 @@ impl TryFrom<ApduHeader> for Command {
0x10 => Ok(Command::GetAppVersion),
0x11 => Ok(Command::GetDeviceModel),
0x12 => Ok(Command::GetDeviceId),
0x22 => Ok(Command::GetAppSettings),
0x21 => Ok(Command::GetPubKeyEd25519),
0x22 => Ok(Command::GetPrivKeyEd25519),
0x31 => Ok(Command::GetPubKeySecp256k1),
0x32 => Ok(Command::GetPrivKeySecp256k1),
0x41 => Ok(Command::SignTxEd25519),
0x42 => Ok(Command::SignTxEd25519Summary),
0x51 => Ok(Command::SignTxSecp256k1),
0x52 => Ok(Command::SignTxSecp256k1Summary),
0x61 => Ok(Command::SignAuthEd25519),
0x71 => Ok(Command::SignAuthSecp256k1),
0x81 => Ok(Command::VerifyAddressEd25519),
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/address_verifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::sign::tx_state::info_message;
use crate::ui::multipage_validator::MultipageValidator;
use crate::ui::utils::info_message;
use nanos_sdk::io::Comm;
use sbor::bech32::address::Address;
use sbor::bech32::encoder::Bech32;
Expand Down
5 changes: 1 addition & 4 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
pub mod dispatcher;
mod get_app_settings;
mod get_app_version;
mod get_device_id;
mod get_device_model;
mod get_private_key_ed25519;
mod get_private_key_secp256k1;
mod get_public_key_ed25519;
mod get_public_key_secp256k1;
mod params_zero;
mod process_sign_outcome;
mod sign_auth_ed25519;
mod sign_auth_secp256k1;
mod sign_tx_ed25519;
mod sign_tx_ed25519_summary;
mod sign_tx_secp256k1;
mod sign_tx_secp256k1_summary;
mod verify_address_ed25519;
mod verify_address_secp256k1;
5 changes: 1 addition & 4 deletions src/handler/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ pub fn dispatcher<T: Copy>(

match ins {
Command::GetAppVersion => get_app_version::handle(comm),
Command::GetAppSettings => get_app_settings::handle(comm, state),
Command::GetDeviceModel => get_device_model::handle(comm),
Command::GetDeviceId => get_device_id::handle(comm),
Command::GetPubKeyEd25519 => get_public_key_ed25519::handle(comm),
Command::GetPrivKeyEd25519 => get_private_key_ed25519::handle(comm),
Command::GetPubKeySecp256k1 => get_public_key_secp256k1::handle(comm),
Command::GetPrivKeySecp256k1 => get_private_key_secp256k1::handle(comm),
Command::SignTxEd25519 => sign_tx_ed25519::handle(comm, class, state),
Command::SignTxEd25519Summary => sign_tx_ed25519_summary::handle(comm, class, state),
Command::SignTxSecp256k1 => sign_tx_secp256k1::handle(comm, class, state),
Command::SignTxSecp256k1Summary => sign_tx_secp256k1_summary::handle(comm, class, state),
Command::SignAuthEd25519 => sign_auth_ed25519::handle(comm, class, state),
Command::SignAuthSecp256k1 => sign_auth_secp256k1::handle(comm, class, state),
Command::VerifyAddressEd25519 => verify_address_ed25519::handle(comm),
Expand Down
7 changes: 7 additions & 0 deletions src/handler/get_app_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::app_error::AppError;
use crate::sign::tx_state::TxState;
use nanos_sdk::io::Comm;

pub fn handle<T: Copy>(comm: &mut Comm, state: &mut TxState<T>) -> Result<(), AppError> {
state.send_settings(comm)
}
4 changes: 2 additions & 2 deletions src/handler/sign_auth_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use nanos_sdk::io::Comm;

use crate::app_error::AppError;
use crate::command_class::CommandClass;
use crate::crypto::curves::Curve;
use crate::handler::process_sign_outcome::process_sign_outcome;
use crate::sign::sign_type::SignType;
use crate::sign::tx_state::TxState;

pub fn handle<T: Copy>(
Expand All @@ -12,6 +12,6 @@ pub fn handle<T: Copy>(
state: &mut TxState<T>,
) -> Result<(), AppError> {
state
.process_sign(comm, class, SignType::AuthEd25519)
.sign_auth(comm, class, Curve::Ed25519)
.and_then(|outcome| process_sign_outcome(outcome))
}
4 changes: 2 additions & 2 deletions src/handler/sign_auth_secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use nanos_sdk::io::Comm;

use crate::app_error::AppError;
use crate::command_class::CommandClass;
use crate::crypto::curves::Curve;
use crate::handler::process_sign_outcome::process_sign_outcome;
use crate::sign::sign_type::SignType;
use crate::sign::tx_state::TxState;

pub fn handle<T: Copy>(
Expand All @@ -12,6 +12,6 @@ pub fn handle<T: Copy>(
state: &mut TxState<T>,
) -> Result<(), AppError> {
state
.process_sign(comm, class, SignType::AuthSecp256k1)
.sign_auth(comm, class, Curve::Secp256k1)
.and_then(|outcome| process_sign_outcome(outcome))
}
4 changes: 2 additions & 2 deletions src/handler/sign_tx_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use nanos_sdk::io::Comm;

use crate::app_error::AppError;
use crate::command_class::CommandClass;
use crate::crypto::curves::Curve;
use crate::handler::process_sign_outcome::process_sign_outcome;
use crate::sign::sign_type::SignType;
use crate::sign::tx_state::TxState;

pub fn handle<T: Copy>(
Expand All @@ -12,6 +12,6 @@ pub fn handle<T: Copy>(
state: &mut TxState<T>,
) -> Result<(), AppError> {
state
.process_sign(comm, class, SignType::Ed25519)
.sign_tx(comm, class, Curve::Ed25519)
.and_then(|outcome| process_sign_outcome(outcome))
}
22 changes: 0 additions & 22 deletions src/handler/sign_tx_ed25519_summary.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/handler/sign_tx_secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use nanos_sdk::io::Comm;

use crate::app_error::AppError;
use crate::command_class::CommandClass;
use crate::crypto::curves::Curve;
use crate::handler::process_sign_outcome::process_sign_outcome;
use crate::sign::sign_type::SignType;
use crate::sign::tx_state::TxState;

pub fn handle<T: Copy>(
Expand All @@ -12,6 +12,6 @@ pub fn handle<T: Copy>(
state: &mut TxState<T>,
) -> Result<(), AppError> {
state
.process_sign(comm, class, SignType::Secp256k1)
.sign_tx(comm, class, Curve::Secp256k1)
.and_then(|outcome| process_sign_outcome(outcome))
}
22 changes: 0 additions & 22 deletions src/handler/sign_tx_secp256k1_summary.rs

This file was deleted.

124 changes: 112 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@
#![feature(const_mut_refs)]

use nanos_sdk::io::{Comm, Event};
use nanos_ui::bagls::{CERTIFICATE_ICON, DASHBOARD_X_ICON, PROCESSING_ICON};
use nanos_ui::bagls::{CERTIFICATE_ICON, COGGLE_ICON, DASHBOARD_X_ICON, PROCESSING_ICON};
use nanos_ui::ui::clear_screen;

use handler::dispatcher;

use crate::app_error::AppError;
use crate::ledger_display_io::LedgerTTY;
use crate::settings::Settings;
use crate::sign::tx_state::TxState;
use crate::ui::menu::{Menu, MenuItem};
use crate::ui::menu::{Menu, MenuFeature, MenuItem};
use crate::ui::multipage_validator::MultipageValidator;
use crate::ui::single_message::SingleMessage;
use crate::ui::utils::RADIX_LOGO_ICON;
use crate::ui::utils::{BACK_ICON, RADIX_LOGO_ICON};

mod app_error;
mod command;
mod command_class;
mod crypto;
mod handler;
mod ledger_display_io;
mod settings;
mod sign;
mod ui;
mod utilities;
Expand All @@ -38,30 +41,127 @@ const APPLICATION_ABOUT: &str = concat!(
);
const APPLICATION_VERSION: &str = concat!("\n", env!("CARGO_PKG_VERSION"), "\n",);

fn app_menu_action() {}
fn app_menu_action() -> bool {
false
}

fn version_menu_action() {
fn version_menu_action() -> bool {
clear_screen();
SingleMessage::new(APPLICATION_VERSION).show_and_wait();
false
}

fn get_verbose_mode_state() -> bool {
Settings::get().verbose_mode
}

fn get_blind_signing_state() -> bool {
Settings::get().blind_signing
}

fn settings_menu_action() -> bool {
clear_screen();

let menu = [
MenuItem::new(
MenuFeature::OnOffState(get_verbose_mode_state),
"\nVerbose Mode",
verbose_mode_setting_action,
),
MenuItem::new(
MenuFeature::OnOffState(get_blind_signing_state),
"\nBlind Signing",
blind_signing_setting_action,
),
MenuItem::new(
MenuFeature::Icon(&BACK_ICON),
"\nBack",
back_from_setting_action,
),
];

Menu::new(&menu).event_loop();

false
}

fn verbose_mode_setting_action() -> bool {
clear_screen();

Settings {
verbose_mode: MultipageValidator::new(
&[&"Set Verbose", &"Mode"],
&[&"Enable"],
&[&"Disable"],
)
.ask(),
blind_signing: get_blind_signing_state(),
}
.update();

false
}

fn blind_signing_setting_action() -> bool {
clear_screen();

Settings {
verbose_mode: get_verbose_mode_state(),
blind_signing: MultipageValidator::new(
&[&"Set Blind", &"Signing"],
&[&"Enable"],
&[&"Disable"],
)
.ask(),
}
.update();

false
}

fn back_from_setting_action() -> bool {
true
}

fn about_menu_action() {
fn about_menu_action() -> bool {
clear_screen();
SingleMessage::new(APPLICATION_ABOUT).show_and_wait();
false
}

fn quit_menu_action() {
fn quit_menu_action() -> bool {
clear_screen();
nanos_sdk::exit_app(0);
}

#[no_mangle]
extern "C" fn sample_main() {
let menu = [
MenuItem::new(&RADIX_LOGO_ICON, "\nRadix Babylon", app_menu_action),
MenuItem::new(&PROCESSING_ICON, "\nVersion", version_menu_action),
MenuItem::new(&CERTIFICATE_ICON, "\nAbout", about_menu_action),
MenuItem::new(&DASHBOARD_X_ICON, "\nQuit", quit_menu_action),
MenuItem::new(
MenuFeature::Icon(&RADIX_LOGO_ICON),
"\nRadix Babylon",
app_menu_action,
),
MenuItem::new(
MenuFeature::Icon(&PROCESSING_ICON),
"\nVersion",
version_menu_action,
),
MenuItem::new(
MenuFeature::Icon(&COGGLE_ICON),
"\nSettings",
settings_menu_action,
),
MenuItem::new(
MenuFeature::Icon(&CERTIFICATE_ICON),
"\nAbout",
about_menu_action,
),
MenuItem::new(
MenuFeature::Icon(&DASHBOARD_X_ICON),
"\nQuit",
quit_menu_action,
),
];
let mut comm = Comm::new();
let mut state = TxState::new(LedgerTTY::new_tty());
Expand All @@ -74,7 +174,7 @@ extern "C" fn sample_main() {
let event = comm.next_event();

match event {
Event::Button(button_event) => main_menu.handle(button_event),
Event::Button(button_event) => _ = main_menu.handle(button_event),
Event::Command(ins) => {
match dispatcher::dispatcher(&mut comm, ins, &mut state) {
Ok(()) => comm.reply_ok(),
Expand Down
Loading

0 comments on commit 1619e8d

Please sign in to comment.