Skip to content

Commit

Permalink
fixed more builds
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed May 22, 2024
1 parent 86f15f1 commit 0729082
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 96 deletions.
1 change: 1 addition & 0 deletions 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
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
}
}
3 changes: 2 additions & 1 deletion base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["minotari_ledger_wallet_comms"]
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
92 changes: 87 additions & 5 deletions base_layer/common_types/src/wallet_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@
// 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;
#[cfg(feature = "ledger")]
use minotari_ledger_wallet_comms::ledger_wallet::LedgerWallet;
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};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WalletType {
Software(PrivateKey, PublicKey),
#[cfg(feature = "ledger")]
Ledger(LedgerWallet),
}

impl Display for WalletType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
WalletType::Software(_k, pk) => write!(f, "Software({:?})", pk),
#[cfg(feature = "ledger")]
WalletType::Ledger(account) => write!(f, "Ledger({account})"),
}
}
Expand All @@ -56,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
}
}

0 comments on commit 0729082

Please sign in to comment.