From 2dabcac0da12a23b20204eb2762b044191dc44c5 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Wed, 17 Mar 2021 23:33:42 -0600 Subject: [PATCH] remote-wallet: Expose Ledger app settings --- remote-wallet/src/ledger.rs | 69 +++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/remote-wallet/src/ledger.rs b/remote-wallet/src/ledger.rs index 27201ee6b..131877bd3 100644 --- a/remote-wallet/src/ledger.rs +++ b/remote-wallet/src/ledger.rs @@ -67,6 +67,23 @@ mod commands { pub const SIGN_MESSAGE: u8 = 0x06; } +enum ConfigurationVersion { + Deprecated(Vec), + Current(Vec), +} + +#[derive(Debug)] +pub enum PubkeyDisplayMode { + Short, + Long, +} + +#[derive(Debug)] +pub struct LedgerSettings { + pub enable_blind_signing: bool, + pub pubkey_display: PubkeyDisplayMode, +} + /// Ledger Wallet device pub struct LedgerWallet { pub device: hidapi::HidDevice, @@ -275,26 +292,50 @@ impl LedgerWallet { } fn get_firmware_version(&self) -> Result { - if let Ok(version) = self._send_apdu(commands::GET_APP_CONFIGURATION, 0, 0, &[], false) { - if version.len() != 5 { + self.get_configuration_vector().map(|config| match config { + ConfigurationVersion::Current(config) => { + FirmwareVersion::new(config[2].into(), config[3].into(), config[4].into()) + } + ConfigurationVersion::Deprecated(config) => { + FirmwareVersion::new(config[1].into(), config[2].into(), config[3].into()) + } + }) + } + + pub fn get_settings(&self) -> Result { + self.get_configuration_vector().map(|config| match config { + ConfigurationVersion::Current(config) => { + let enable_blind_signing = config[0] != 0; + let pubkey_display = if config[1] == 0 { + PubkeyDisplayMode::Long + } else { + PubkeyDisplayMode::Short + }; + LedgerSettings { + enable_blind_signing, + pubkey_display, + } + } + ConfigurationVersion::Deprecated(_) => LedgerSettings { + enable_blind_signing: false, + pubkey_display: PubkeyDisplayMode::Short, + }, + }) + } + + fn get_configuration_vector(&self) -> Result { + if let Ok(config) = self._send_apdu(commands::GET_APP_CONFIGURATION, 0, 0, &[], false) { + if config.len() != 5 { return Err(RemoteWalletError::Protocol("Version packet size mismatch")); } - Ok(FirmwareVersion::new( - version[2].into(), - version[3].into(), - version[4].into(), - )) + Ok(ConfigurationVersion::Current(config)) } else { - let version = + let config = self._send_apdu(commands::DEPRECATED_GET_APP_CONFIGURATION, 0, 0, &[], true)?; - if version.len() != 4 { + if config.len() != 4 { return Err(RemoteWalletError::Protocol("Version packet size mismatch")); } - Ok(FirmwareVersion::new( - version[1].into(), - version[2].into(), - version[3].into(), - )) + Ok(ConfigurationVersion::Deprecated(config)) } }