diff --git a/Cargo.lock b/Cargo.lock index ae9c6dcf2f..e02a0966f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3361,6 +3361,7 @@ dependencies = [ "num-derive", "num-traits", "serde", + "tari_common_types", "tari_crypto", "thiserror", ] @@ -6018,8 +6019,6 @@ dependencies = [ "bs58 0.5.1", "chacha20poly1305", "digest 0.10.7", - "ledger-transport 0.10.0 (git+https://github.com/Zondax/ledger-rs?rev=20e2a20)", - "minotari_ledger_wallet_comms", "newtype-ops", "once_cell", "primitive-types", diff --git a/applications/minotari_console_wallet/Cargo.toml b/applications/minotari_console_wallet/Cargo.toml index 57b4798e4e..d9c0a2a50a 100644 --- a/applications/minotari_console_wallet/Cargo.toml +++ b/applications/minotari_console_wallet/Cargo.toml @@ -89,7 +89,7 @@ tari_features = { path = "../../common/tari_features", version = "1.0.0-pre.16" [features] default = ["libtor", "ledger"] grpc = [] -ledger = ["ledger-transport-hid", "minotari_ledger_wallet_comms", "tari_common_types/ledger"] +ledger = ["ledger-transport-hid", "minotari_ledger_wallet_comms"] libtor = ["tari_libtor"] [package.metadata.cargo-machete] diff --git a/applications/minotari_console_wallet/src/init/mod.rs b/applications/minotari_console_wallet/src/init/mod.rs index 2aa678b49d..09dcc86651 100644 --- a/applications/minotari_console_wallet/src/init/mod.rs +++ b/applications/minotari_console_wallet/src/init/mod.rs @@ -29,6 +29,8 @@ use ledger_transport_hid::{hidapi::HidApi, TransportNativeHID}; use log::*; use minotari_app_utilities::{consts, identity_management::setup_node_identity}; #[cfg(feature = "ledger")] +use minotari_ledger_wallet_comms::ledger_wallet::LedgerCommands; +#[cfg(feature = "ledger")] use minotari_ledger_wallet_comms::{ error::LedgerDeviceError, ledger_wallet::{get_transport, Instruction}, diff --git a/applications/minotari_ledger_wallet/comms/Cargo.toml b/applications/minotari_ledger_wallet/comms/Cargo.toml index 585f1fe631..a22ea86e09 100644 --- a/applications/minotari_ledger_wallet/comms/Cargo.toml +++ b/applications/minotari_ledger_wallet/comms/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] tari_crypto = { version = "0.20.2", default-features = false } - +tari_common_types = { path = "../../../base_layer/common_types", version = "1.0.0-pre.16" } ledger-transport = { git = "https://github.com/Zondax/ledger-rs", rev = "20e2a20" } ledger-transport-hid = { git = "https://github.com/Zondax/ledger-rs", rev = "20e2a20" } num-derive = "0.4.2" diff --git a/applications/minotari_ledger_wallet/comms/src/ledger_wallet.rs b/applications/minotari_ledger_wallet/comms/src/ledger_wallet.rs index 44f897bfb8..f85dfc4fb5 100644 --- a/applications/minotari_ledger_wallet/comms/src/ledger_wallet.rs +++ b/applications/minotari_ledger_wallet/comms/src/ledger_wallet.rs @@ -26,6 +26,7 @@ use ledger_transport::{APDUAnswer, APDUCommand}; use ledger_transport_hid::{hidapi::HidApi, TransportNativeHID}; use num_derive::FromPrimitive; use num_traits::FromPrimitive; +use tari_common_types::wallet_types::LedgerWallet; use crate::error::LedgerDeviceError; @@ -84,3 +85,56 @@ impl> Command { .map_err(|e| LedgerDeviceError::NativeTransport(e.to_string())) } } + +pub trait LedgerCommands { + fn build_command(&self, instruction: Instruction, data: Vec) -> Command>; + fn chunk_command(&self, instruction: Instruction, data: Vec>) -> Vec>>; +} + +const WALLET_CLA: u8 = 0x80; + +impl LedgerCommands for LedgerWallet { + fn build_command(&self, instruction: Instruction, data: Vec) -> Command> { + let mut base_data = self.account_bytes(); + base_data.extend_from_slice(&data); + + Command::new(APDUCommand { + cla: WALLET_CLA, + ins: instruction.as_byte(), + p1: 0x00, + p2: 0x00, + data: base_data, + }) + } + + fn chunk_command(&self, instruction: Instruction, data: Vec>) -> Vec>> { + let num_chunks = data.len(); + let mut more; + let mut commands = vec![]; + + for (i, chunk) in data.iter().enumerate() { + if i + 1 == num_chunks { + more = 0; + } else { + more = 1; + } + + // Prepend the account on the first payload + let mut base_data = vec![]; + if i == 0 { + base_data.extend_from_slice(&self.account_bytes()); + } + base_data.extend_from_slice(chunk); + + commands.push(Command::new(APDUCommand { + cla: WALLET_CLA, + ins: instruction.as_byte(), + p1: u8::try_from(i).unwrap_or(0), + p2: more, + data: base_data, + })); + } + + commands + } +} diff --git a/base_layer/common_types/Cargo.toml b/base_layer/common_types/Cargo.toml index ed7583721d..dffa78ebd4 100644 --- a/base_layer/common_types/Cargo.toml +++ b/base_layer/common_types/Cargo.toml @@ -7,7 +7,6 @@ version = "1.0.0-pre.16" edition = "2018" [dependencies] -minotari_ledger_wallet_comms = { path = "../../applications/minotari_ledger_wallet/comms", version = "1.0.0-pre.16", optional = true } tari_crypto = { version = "0.20.3" } tari_utilities = { version = "0.7" } tari_common = { path = "../../common", version = "1.0.0-pre.16" } @@ -26,10 +25,9 @@ thiserror = "1.0.29" base64 = "0.21.0" blake2 = "0.10" primitive-types = { version = "0.12", features = ["serde"] } -ledger-transport = { git = "https://github.com/Zondax/ledger-rs", rev = "20e2a20", optional = true } [features] -ledger = ["minotari_ledger_wallet_comms", "ledger-transport"] +default = [] [package.metadata.cargo-machete] ignored = ["strum", "strum_macros"] # this is so we can run cargo machete without getting false positive about macro dependancies diff --git a/base_layer/common_types/src/wallet_types.rs b/base_layer/common_types/src/wallet_types.rs index af69f9eb9f..165f343933 100644 --- a/base_layer/common_types/src/wallet_types.rs +++ b/base_layer/common_types/src/wallet_types.rs @@ -19,17 +19,12 @@ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#[cfg(feature = "ledger")] -use std::convert::TryFrom; + use std::{ fmt, fmt::{Display, Formatter}, }; -#[cfg(feature = "ledger")] -use ledger_transport::APDUCommand; -#[cfg(feature = "ledger")] -use minotari_ledger_wallet_comms::ledger_wallet::{Command, Instruction}; use serde::{Deserialize, Serialize}; use tari_common::configuration::Network; use tari_crypto::keys::PublicKey as PublicKeyTrait; @@ -85,9 +80,6 @@ impl Display for LedgerWallet { } } -#[cfg(feature = "ledger")] -const WALLET_CLA: u8 = 0x80; - impl LedgerWallet { pub fn new(account: u64, network: Network, public_alpha: Option, view_key: Option) -> Self { Self { @@ -101,50 +93,4 @@ impl LedgerWallet { pub fn account_bytes(&self) -> Vec { self.account.to_le_bytes().to_vec() } - - #[cfg(feature = "ledger")] - pub fn build_command(&self, instruction: Instruction, data: Vec) -> Command> { - let mut base_data = self.account_bytes(); - base_data.extend_from_slice(&data); - - Command::new(APDUCommand { - cla: WALLET_CLA, - ins: instruction.as_byte(), - p1: 0x00, - p2: 0x00, - data: base_data, - }) - } - - #[cfg(feature = "ledger")] - pub fn chunk_command(&self, instruction: Instruction, data: Vec>) -> Vec>> { - let num_chunks = data.len(); - let mut more; - let mut commands = vec![]; - - for (i, chunk) in data.iter().enumerate() { - if i + 1 == num_chunks { - more = 0; - } else { - more = 1; - } - - // Prepend the account on the first payload - let mut base_data = vec![]; - if i == 0 { - base_data.extend_from_slice(&self.account_bytes()); - } - base_data.extend_from_slice(chunk); - - commands.push(Command::new(APDUCommand { - cla: WALLET_CLA, - ins: instruction.as_byte(), - p1: u8::try_from(i).unwrap_or(0), - p2: more, - data: base_data, - })); - } - - commands - } } diff --git a/base_layer/core/src/transactions/key_manager/inner.rs b/base_layer/core/src/transactions/key_manager/inner.rs index bbfc55efb3..c5914902f5 100644 --- a/base_layer/core/src/transactions/key_manager/inner.rs +++ b/base_layer/core/src/transactions/key_manager/inner.rs @@ -27,7 +27,7 @@ use log::*; #[cfg(feature = "ledger")] use minotari_ledger_wallet_comms::{ error::LedgerDeviceError, - ledger_wallet::{get_transport, Instruction}, + ledger_wallet::{get_transport, Instruction, LedgerCommands}, }; use rand::rngs::OsRng; use strum::IntoEnumIterator;