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 19, 2024
1 parent 446a118 commit 63b5981
Show file tree
Hide file tree
Showing 20 changed files with 1,929 additions and 334 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"
207 changes: 207 additions & 0 deletions applications/minotari_ledger_wallet/comms/examples/ledger_demo/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

//! # Multi-party Ledger - command line example
use minotari_ledger_wallet_comms::{
accessor_methods::{
get_app_name,
get_dh_shared_secret,
get_public_alpha,
get_public_key,
get_raw_schnorr_signature,
get_script_offset,
get_script_schnorr_signature,
get_script_signature,
get_version,
get_view_key,
},
ledger_wallet::{get_transport, LedgerWallet},
};
use rand::rngs::OsRng;
/// This example demonstrates how to use the Ledger Nano S/X for the Tari wallet. In order to run the example, you
/// need to have the `MinoTari Wallet` application installed on your Ledger device. For that, please follow the
/// instructions in the [README](../../wallet/README.md) file.
/// With this example, you can:
/// - Detect the hardware wallet
/// - Verify that the Ledger application is installed and the version is correct
/// - TBD
///
/// -----------------------------------------------------------------------------------------------
/// Example use:
/// `cargo run --release --example ledger_demo`
/// -----------------------------------------------------------------------------------------------
use rand::RngCore;
use tari_common::configuration::Network;
use tari_common_types::types::{Commitment, PrivateKey, PublicKey};
use tari_core::transactions::{
key_manager::TransactionKeyManagerBranch,
tari_amount::MicroMinotari,
transaction_components::TransactionInputVersion,
};
use tari_crypto::{
keys::{PublicKey as PK, SecretKey},
ristretto::RistrettoSecretKey,
};
use tari_utilities::{hex::Hex, ByteArray};

fn main() {
let account = OsRng.next_u64();
let ledger = LedgerWallet::new(account, Network::LocalNet, None, None);
let transport = match get_transport() {
Ok(transport) => transport,
Err(e) => {
println!("\nError: {}\n", e);
return;
},
};

println!();

// GetAppName
println!("\ntest: GetAppName");
match get_app_name(&transport, &ledger) {
Ok(name) => println!("app name: {}", name),
Err(e) => println!("\n{}\n", e),
}

// GetVersion
println!("\ntest: GetVersion");
match get_version(&transport, &ledger) {
Ok(name) => println!("version: {}", name),
Err(e) => println!("\n{}\n", e),
}

// GetPublicAlpha
println!("\ntest: GetPublicAlpha");
match get_public_alpha(&transport, &ledger) {
Ok(public_alpha) => println!("public_alpha: {}", public_alpha.to_hex()),
Err(e) => println!("\n{}\n", e),
}

// GetPublicKey
println!("\ntest: GetPublicKey");
let index = OsRng.next_u64();
let branch = TransactionKeyManagerBranch::CommitmentMask;

match get_public_key(&transport, &ledger, index, branch) {
Ok(public_key) => println!("public_key: {}", public_key.to_hex()),
Err(e) => println!("\n{}\n", e),
}

// GetScriptSignature
println!("\ntest: GetScriptSignature");
let network = Network::LocalNet;
let version = TransactionInputVersion::get_current_version();
let branch_key = get_random_nonce();
let value = MicroMinotari(123456);
let spend_private_key = get_random_nonce();
let commitment = Commitment::from_public_key(&PublicKey::from_secret_key(&get_random_nonce()));
let mut script_message = [0u8; 32];
script_message.copy_from_slice(&get_random_nonce().to_vec());

match get_script_signature(
&transport,
&ledger,
network,
version,
&branch_key,
value,
&spend_private_key,
&commitment,
script_message,
) {
Ok(signature) => println!(
"script_sig: ({},{},{},{},{})",
signature.ephemeral_commitment().to_hex(),
signature.ephemeral_pubkey().to_hex(),
signature.u_x().to_hex(),
signature.u_a().to_hex(),
signature.u_y().to_hex()
),
Err(e) => println!("\n{}\n", e),
}

// GetScriptOffset
println!("\ntest: GetScriptOffset");
let mut derived_key_commitments = Vec::new();
let mut sender_offset_indexes = Vec::new();
for _i in 0..5 {
derived_key_commitments.push(get_random_nonce());
sender_offset_indexes.push(OsRng.next_u64());
}

match get_script_offset(&transport, &ledger, &derived_key_commitments, &sender_offset_indexes) {
Ok(script_offset) => println!("script_offset: {}", script_offset.to_hex()),
Err(e) => println!("\n{}\n", e),
}

// GetViewKey
println!("\ntest: GetViewKey");

match get_view_key(&transport, &ledger) {
Ok(view_key) => println!("view_key: {}", view_key.to_hex()),
Err(e) => println!("\n{}\n", e),
}

// GetDHSharedSecret
println!("\ntest: GetDHSharedSecret");
let index = OsRng.next_u64();
let branch = TransactionKeyManagerBranch::SenderOffset;
let public_key = PublicKey::from_secret_key(&get_random_nonce());

match get_dh_shared_secret(&transport, &ledger, index, branch, &public_key) {
Ok(shared_secret) => println!("shared_secret: {}", shared_secret.as_bytes().to_vec().to_hex()),
Err(e) => println!("\n{}\n", e),
}

// GetRawSchnorrSignature
println!("\ntest: GetRawSchnorrSignature");
let private_key_index = OsRng.next_u64();
let private_key_branch = TransactionKeyManagerBranch::SenderOffset;
let nonce_index = OsRng.next_u64();
let nonce_branch = TransactionKeyManagerBranch::SenderOffset;
let mut challenge = [0u8; 64];
OsRng.fill_bytes(&mut challenge);

match get_raw_schnorr_signature(
&transport,
&ledger,
private_key_index,
private_key_branch,
nonce_index,
nonce_branch,
challenge,
) {
Ok(signature) => println!(
"signature: ({},{})",
signature.get_signature().to_hex(),
signature.get_public_nonce().to_hex()
),
Err(e) => println!("\n{}\n", e),
}

// GetScriptSchnorrSignature
println!("\ntest: GetScriptSchnorrSignature");
let private_key_index = OsRng.next_u64();
let private_key_branch = TransactionKeyManagerBranch::SenderOffset;
let mut challenge = [0u8; 64];
OsRng.fill_bytes(&mut challenge);

match get_script_schnorr_signature(&transport, &ledger, private_key_index, private_key_branch, challenge) {
Ok(signature) => println!(
"signature: ({},{})",
signature.get_signature().to_hex(),
signature.get_public_nonce().to_hex()
),
Err(e) => println!("\n{}\n", e),
}

println!("\nTest completed successfully\n");
}

pub fn get_random_nonce() -> PrivateKey {
let mut raw_bytes = [0u8; 64];
OsRng.fill_bytes(&mut raw_bytes);
RistrettoSecretKey::from_uniform_bytes(&raw_bytes).expect("will not fail")
}
Loading

0 comments on commit 63b5981

Please sign in to comment.