forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ledger methods to support faucet spending.
- Loading branch information
1 parent
cc74845
commit 6c84725
Showing
12 changed files
with
1,607 additions
and
304 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
applications/minotari_ledger_wallet/comms/examples/ledger_demo/ledger_wallet.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.