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
446a118
commit 63b5981
Showing
20 changed files
with
1,929 additions
and
334 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
207 changes: 207 additions & 0 deletions
207
applications/minotari_ledger_wallet/comms/examples/ledger_demo/main.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,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") | ||
} |
Oops, something went wrong.