Skip to content

Commit

Permalink
Add ledger methods
Browse files Browse the repository at this point in the history
Added ledger methods to support faucet spending.
  • Loading branch information
hansieodendaal committed Jul 17, 2024
1 parent 446a118 commit 010ac1d
Show file tree
Hide file tree
Showing 12 changed files with 1,558 additions and 308 deletions.
871 changes: 660 additions & 211 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ edition = "2021"

[dependencies]
tari_crypto = { version = "0.20.2", default-features = false }
tari_utilities = { version = "0.7" }
tari_common = { git = "https://github.com/tari-project/tari", tag = "v1.0.0-pre.16" }
tari_common_types = { git = "https://github.com/tari-project/tari", tag = "v1.0.0-pre.16", features = ["ledger"] }
tari_core = { git = "https://github.com/tari-project/tari", tag = "v1.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"
num-traits = "0.2.15"
serde = { version = "1.0.106", features = ["derive"] }
thiserror = "1.0.26"
thiserror = "1.0.26"

rand = "0.9.0-alpha.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2023 The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// 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::{
fmt,
fmt::{Display, Formatter},
};

use ledger_transport::APDUCommand;
use minotari_ledger_wallet_comms::ledger_wallet::{Command, Instruction};
use serde::{Deserialize, Serialize};
use tari_common::configuration::Network;
use tari_common_types::types::{PrivateKey, PublicKey};
use tari_utilities::ByteArray;

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

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

const WALLET_CLA: u8 = 0x80;

impl LedgerWallet {
pub fn new(account: u64, network: Network, public_alpha: Option<PublicKey>, view_key: Option<PrivateKey>) -> Self {
Self {
account,
public_alpha,
network,
view_key,
}
}

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

commands
}
}
Loading

0 comments on commit 010ac1d

Please sign in to comment.