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 22, 2024
1 parent 1bd2fde commit 86a80d3
Show file tree
Hide file tree
Showing 46 changed files with 1,498 additions and 432 deletions.
166 changes: 111 additions & 55 deletions Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ pub async fn command_runner(

let mut script_signature = Signature::default();
match key_manager_service
.sign_with_nonce_and_message(
.sign_with_challenge_and_message(
&party_info.wallet_spend_key_id,
&party_info.script_nonce_key_id,
&challenge,
Expand Down Expand Up @@ -1007,7 +1007,7 @@ pub async fn command_runner(

let mut metadata_signature = Signature::default();
match key_manager_service
.sign_with_nonce_and_message(
.sign_with_challenge_and_message(
&party_info.sender_offset_key_id,
&party_info.sender_offset_nonce_key_id,
&challenge,
Expand Down
10 changes: 3 additions & 7 deletions applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use std::{fs, io, path::PathBuf, str::FromStr, sync::Arc, time::Instant};
use ledger_transport_hid::{hidapi::HidApi, TransportNativeHID};
use log::*;
use minotari_app_utilities::{consts, identity_management::setup_node_identity};
#[cfg(feature = "ledger")]
use minotari_ledger_wallet_comms::ledger_wallet::LedgerCommands;
use minotari_ledger_wallet_comms::ledger_wallet::Command;
#[cfg(feature = "ledger")]
use minotari_ledger_wallet_comms::{
error::LedgerDeviceError,
Expand Down Expand Up @@ -837,9 +836,7 @@ pub fn prompt_wallet_type(
Ok(hid) => {
println!("Device found.");
let account = prompt_ledger_account(boot_mode).expect("An account value");
let ledger = LedgerWallet::new(account, wallet_config.network, None, None);
match ledger
.build_command(Instruction::GetPublicAlpha, vec![])
match Command::<Vec<u8>>::build_command(account, Instruction::GetPublicAlpha, vec![])
.execute_with_transport(&hid)
{
Ok(result) => {
Expand All @@ -858,8 +855,7 @@ pub fn prompt_wallet_type(
Err(e) => panic!("{}", e),
};

match ledger
.build_command(Instruction::GetViewKey, vec![])
match Command::<Vec<u8>>::build_command(account, Instruction::GetViewKey, vec![])
.execute_with_transport(&hid)
{
Ok(result) => {
Expand Down
11 changes: 9 additions & 2 deletions applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ edition = "2021"

[dependencies]
tari_crypto = { version = "0.20.2", default-features = false }
tari_common_types = { path = "../../../base_layer/common_types", version = "1.0.0-pre.16" }
tari_utilities = { version = "0.7" }
tari_common = { path = "../../../common" }
tari_common_types = { path = "../../../base_layer/common_types" }
tari_script = { path = "../../../infrastructure/tari_script" }

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"
once_cell = "1.19.0"
256 changes: 256 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,256 @@
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

//! # Multi-party Ledger - command line example
use minotari_ledger_wallet_comms::{
accessor_methods::{
ledger_get_app_name,
ledger_get_dh_shared_secret,
ledger_get_public_alpha,
ledger_get_public_key,
ledger_get_raw_schnorr_signature,
ledger_get_script_offset,
ledger_get_script_schnorr_signature,
ledger_get_script_signature,
ledger_get_version,
ledger_get_view_key,
verify_ledger_application,
},
ledger_wallet::get_transport,
};
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::{
key_manager::TransactionKeyManagerBranch,
types::{Commitment, PrivateKey, PublicKey},
};
use tari_crypto::{
keys::{PublicKey as PK, SecretKey},
ristretto::RistrettoSecretKey,
};
use tari_utilities::{hex::Hex, ByteArray};

#[allow(clippy::too_many_lines)]
fn main() {
println!();

// Repeated access to the transport is efficient
for _i in 0..10 {
let instant = std::time::Instant::now();
match get_transport() {
Ok(_) => {},
Err(e) => {
println!("\nError: {}\n", e);
return;
},
};
println!("Transport created in {:?}", instant.elapsed());
}

println!();

// Repeated ledger app verification is efficient
for _i in 0..10 {
let instant = std::time::Instant::now();
match verify_ledger_application() {
Ok(_) => {},
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}
println!("Application verified in {:?}", instant.elapsed());
}

println!();

// GetAppName
println!("\ntest: GetAppName");
match ledger_get_app_name() {
Ok(name) => println!("app name: {}", name),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

// GetVersion
println!("\ntest: GetVersion");
match ledger_get_version() {
Ok(name) => println!("version: {}", name),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

// GetPublicAlpha
println!("\ntest: GetPublicAlpha");
let account = OsRng.next_u64();
match ledger_get_public_alpha(account) {
Ok(public_alpha) => println!("public_alpha: {}", public_alpha.to_hex()),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

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

match ledger_get_public_key(account, index, branch) {
Ok(public_key) => println!("public_key: {}", public_key.to_hex()),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

// GetScriptSignature
println!("\ntest: GetScriptSignature");
let network = Network::LocalNet;
let version = 0u8;
let branch_key = get_random_nonce();
let value = PrivateKey::from(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 ledger_get_script_signature(
account,
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!("\nError: {}\n", e);
return;
},
}

// 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 ledger_get_script_offset(account, &derived_key_commitments, &sender_offset_indexes) {
Ok(script_offset) => println!("script_offset: {}", script_offset.to_hex()),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

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

match ledger_get_view_key(account) {
Ok(view_key) => println!("view_key: {}", view_key.to_hex()),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

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

match ledger_get_dh_shared_secret(account, index, branch, &public_key) {
Ok(shared_secret) => println!("shared_secret: {}", shared_secret.as_bytes().to_vec().to_hex()),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

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

match ledger_get_raw_schnorr_signature(
account,
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!("\nError: {}\n", e);
return;
},
}

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

match ledger_get_script_schnorr_signature(account, private_key_index, private_key_branch, &nonce) {
Ok(signature) => println!(
"signature: ({},{})",
signature.get_signature().to_hex(),
signature.get_public_nonce().to_hex()
),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}

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 86a80d3

Please sign in to comment.