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: fix build by make ledger type optional #6350

Merged
merged 3 commits into from
May 23, 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
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use log::*;
use minotari_app_utilities::{consts, identity_management::setup_node_identity};
use minotari_ledger_wallet_comms::{
error::LedgerDeviceError,
ledger_wallet::{get_transport, Instruction, LedgerWallet},
ledger_wallet::{get_transport, Instruction},
};
use minotari_wallet::{
error::{WalletError, WalletStorageError},
Expand All @@ -56,7 +56,7 @@ use tari_common::{
};
use tari_common_types::{
types::{PrivateKey, PublicKey},
wallet_types::WalletType,
wallet_types::{LedgerWallet, WalletType},
};
use tari_comms::{
multiaddr::Multiaddr,
Expand Down
1 change: 0 additions & 1 deletion applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
tari_crypto = { version = "0.20.0", default-features = false }
tari_common = { path = "../../../common", version = "1.0.0-pre.12" }

ledger-transport = { git = "https://github.com/Zondax/ledger-rs", rev = "20e2a20" }
ledger-transport-hid = { git = "https://github.com/Zondax/ledger-rs", rev = "20e2a20" }
Expand Down
93 changes: 5 additions & 88 deletions applications/minotari_ledger_wallet/comms/src/ledger_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,15 @@
// 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.

use std::{
fmt,
fmt::{Display, Formatter},
ops::Deref,
};
use std::ops::Deref;

use ledger_transport::{APDUAnswer, APDUCommand};
use ledger_transport_hid::{hidapi::HidApi, TransportNativeHID};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use serde::{Deserialize, Serialize};
use tari_common::configuration::Network;
use tari_crypto::ristretto::RistrettoPublicKey;

use crate::error::LedgerDeviceError;

const WALLET_CLA: u8 = 0x80;
// const LOG_TARGET: &str = "wallet::ledger_wallet::comms";

#[repr(u8)]
#[derive(FromPrimitive, Debug, Copy, Clone, PartialEq)]
pub enum Instruction {
Expand Down Expand Up @@ -72,6 +62,10 @@ pub struct Command<D> {
}

impl<D: Deref<Target = [u8]>> Command<D> {
pub fn new(inner: APDUCommand<D>) -> Command<D> {
Self { inner }
}

pub fn execute(&self) -> Result<APDUAnswer<Vec<u8>>, LedgerDeviceError> {
get_transport()?
.exchange(&self.inner)
Expand All @@ -87,80 +81,3 @@ impl<D: Deref<Target = [u8]>> Command<D> {
.map_err(|e| LedgerDeviceError::NativeTransport(e.to_string()))
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LedgerWallet {
account: u64,
pub pubkey: Option<RistrettoPublicKey>,
pub network: Network,
}

impl Display for LedgerWallet {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "account {}", self.account)?;
write!(f, "pubkey {}", self.pubkey.is_some())?;
Ok(())
}
}

impl LedgerWallet {
pub fn new(account: u64, network: Network, pubkey: Option<RistrettoPublicKey>) -> Self {
Self {
account,
pubkey,
network,
}
}

pub fn account_bytes(&self) -> Vec<u8> {
self.account.to_le_bytes().to_vec()
}

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 {
inner: APDUCommand {
cla: WALLET_CLA,
ins: instruction.as_byte(),
p1: 0x00,
p2: 0x00,
data: base_data,
},
}
}

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 {
inner: APDUCommand {
cla: WALLET_CLA,
ins: instruction.as_byte(),
p1: u8::try_from(i).unwrap_or(0),
p2: more,
data: base_data,
},
});
}

commands
}
}
5 changes: 3 additions & 2 deletions base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "1.0.0-pre.13"
edition = "2018"

[dependencies]
minotari_ledger_wallet_comms = { path = "../../applications/minotari_ledger_wallet/comms", version = "1.0.0-pre.13" }
minotari_ledger_wallet_comms = { path = "../../applications/minotari_ledger_wallet/comms", version = "1.0.0-pre.13", optional = true }
tari_crypto = { version = "0.20.1" }
tari_utilities = { version = "0.7" }
tari_common = { path = "../../common", version = "1.0.0-pre.13" }
Expand All @@ -25,9 +25,10 @@ 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 = []
ledger = ["minotari_ledger_wallet_comms", "ledger-transport"]

[package.metadata.cargo-machete]
ignored = ["strum", "strum_macros"] # this is so we can run cargo machete without getting false positive about macro dependancies
91 changes: 88 additions & 3 deletions base_layer/common_types/src/wallet_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@
// 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.

use std::{
convert::TryFrom,
fmt,
fmt::{Display, Formatter},
};

use chacha20poly1305::aead::OsRng;
use minotari_ledger_wallet_comms::ledger_wallet::LedgerWallet;
#[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_crypto::keys::{PublicKey as PublicKeyTrait, SecretKey};
use tari_common::configuration::Network;
use tari_crypto::{
keys::{PublicKey as PublicKeyTrait, SecretKey},
ristretto::RistrettoPublicKey,
};

use crate::types::{PrivateKey, PublicKey};

Expand All @@ -53,3 +60,81 @@ impl Default for WalletType {
WalletType::Software(k.clone(), PublicKey::from_secret_key(&k))
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LedgerWallet {
account: u64,
pub pubkey: Option<RistrettoPublicKey>,
pub network: Network,
}

impl Display for LedgerWallet {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "account {}", self.account)?;
write!(f, "pubkey {}", self.pubkey.is_some())?;
Ok(())
}
}

#[cfg(feature = "ledger")]
const WALLET_CLA: u8 = 0x80;

impl LedgerWallet {
pub fn new(account: u64, network: Network, pubkey: Option<RistrettoPublicKey>) -> Self {
Self {
account,
pubkey,
network,
}
}

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
}
}
Loading