Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove dependancy on ledger for common types #6412

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion applications/minotari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 54 additions & 0 deletions applications/minotari_ledger_wallet/comms/src/ledger_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -84,3 +85,56 @@ impl<D: Deref<Target = [u8]>> Command<D> {
.map_err(|e| LedgerDeviceError::NativeTransport(e.to_string()))
}
}

pub trait LedgerCommands {
fn build_command(&self, instruction: Instruction, data: Vec<u8>) -> Command<Vec<u8>>;
fn chunk_command(&self, instruction: Instruction, data: Vec<Vec<u8>>) -> Vec<Command<Vec<u8>>>;
}

const WALLET_CLA: u8 = 0x80;

impl LedgerCommands for LedgerWallet {
fn build_command(&self, instruction: Instruction, data: Vec<u8>) -> Command<Vec<u8>> {
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<u8>>) -> Vec<Command<Vec<u8>>> {
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
}
}
4 changes: 1 addition & 3 deletions base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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
56 changes: 1 addition & 55 deletions base_layer/common_types/src/wallet_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PublicKey>, view_key: Option<PrivateKey>) -> Self {
Self {
Expand All @@ -101,50 +93,4 @@ impl LedgerWallet {
pub fn account_bytes(&self) -> Vec<u8> {
self.account.to_le_bytes().to_vec()
}

#[cfg(feature = "ledger")]
pub fn build_command(&self, instruction: Instruction, data: Vec<u8>) -> Command<Vec<u8>> {
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<u8>>) -> Vec<Command<Vec<u8>>> {
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
}
}
2 changes: 1 addition & 1 deletion base_layer/core/src/transactions/key_manager/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading