From c8009c17c288a38e5b03051545f9dcf97dafff2f Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Wed, 19 Jun 2024 16:59:26 +0200 Subject: [PATCH] Apply m_of_n_feature branch - PR #4751 --- .../src/automation/commands.rs | 14 ++++++++++++++ applications/minotari_console_wallet/src/cli.rs | 7 +++++++ .../minotari_console_wallet/src/wallet_modes.rs | 6 ++++++ .../core/src/transactions/key_manager/inner.rs | 11 +++++++++++ .../core/src/transactions/key_manager/interface.rs | 6 ++++++ .../core/src/transactions/key_manager/wrapper.rs | 12 ++++++++++++ 6 files changed, 56 insertions(+) diff --git a/applications/minotari_console_wallet/src/automation/commands.rs b/applications/minotari_console_wallet/src/automation/commands.rs index aa459859d5..6479447d17 100644 --- a/applications/minotari_console_wallet/src/automation/commands.rs +++ b/applications/minotari_console_wallet/src/automation/commands.rs @@ -63,6 +63,7 @@ use tari_comms::{ }; use tari_comms_dht::{envelope::NodeDestination, DhtDiscoveryRequester}; use tari_core::transactions::{ + key_manager::TransactionKeyManagerInterface, tari_amount::{uT, MicroMinotari, Minotari}, transaction_components::{ encrypted_data::PaymentId, @@ -617,6 +618,7 @@ pub async fn command_runner( let mut output_service = wallet.output_manager_service.clone(); let dht_service = wallet.dht_service.discovery_service_requester().clone(); let connectivity_requester = wallet.comms.connectivity(); + let key_manager_service = wallet.key_manager_service.clone(); let mut online = false; let mut tx_ids = Vec::new(); @@ -676,6 +678,18 @@ pub async fn command_runner( Err(e) => eprintln!("BurnMinotari error! {}", e), } }, + CreateKeyPair(args) => match key_manager_service.create_key_pair(args.key_branch).await { + Ok((sk, pk)) => { + println!( + "New key pair: + 1. secret key: {}, + 2. public key: {}", + sk.to_hex(), + pk.to_hex() + ) + }, + Err(e) => eprintln!("CreateKeyPair error! {}", e), + }, SendMinotari(args) => { match send_tari( transaction_service.clone(), diff --git a/applications/minotari_console_wallet/src/cli.rs b/applications/minotari_console_wallet/src/cli.rs index 3a4ce8c5d7..054012a879 100644 --- a/applications/minotari_console_wallet/src/cli.rs +++ b/applications/minotari_console_wallet/src/cli.rs @@ -116,6 +116,8 @@ pub enum CliCommands { GetBalance, SendMinotari(SendMinotariArgs), BurnMinotari(BurnMinotariArgs), + CreateKeyPair(CreateKeyPairArgs), + SendOneSided(SendMinotariArgs), SendOneSidedToStealthAddress(SendMinotariArgs), MakeItRain(MakeItRainArgs), CoinSplit(CoinSplitArgs), @@ -157,6 +159,11 @@ pub struct BurnMinotariArgs { pub message: String, } +#[derive(Debug, Args, Clone)] +pub struct CreateKeyPairArgs { + #[clap(short, long, default_value = "Burn funds")] + pub key_branch: String, +} #[derive(Debug, Args, Clone)] pub struct MakeItRainArgs { pub destination: TariAddress, diff --git a/applications/minotari_console_wallet/src/wallet_modes.rs b/applications/minotari_console_wallet/src/wallet_modes.rs index 391796800d..a050a4667a 100644 --- a/applications/minotari_console_wallet/src/wallet_modes.rs +++ b/applications/minotari_console_wallet/src/wallet_modes.rs @@ -495,6 +495,8 @@ mod test { burn-minotari --message Ups_these_funds_will_be_burned! 100T + create-key-pair --key-branch pie + coin-split --message Make_many_dust_UTXOs! --fee-per-gram 2 0.001T 499 make-it-rain --duration 100 --transactions-per-second 10 --start-amount 0.009200T --increase-amount 0T \ @@ -514,6 +516,7 @@ mod test { let mut get_balance = false; let mut send_tari = false; let mut burn_tari = false; + let mut create_key_pair = false; let mut make_it_rain = false; let mut coin_split = false; let mut discover_peer = false; @@ -525,6 +528,8 @@ mod test { CliCommands::GetBalance => get_balance = true, CliCommands::SendMinotari(_) => send_tari = true, CliCommands::BurnMinotari(_) => burn_tari = true, + CliCommands::CreateKeyPair(_) => create_key_pair = true, + CliCommands::SendOneSided(_) => {}, CliCommands::SendOneSidedToStealthAddress(_) => {}, CliCommands::MakeItRain(_) => make_it_rain = true, CliCommands::CoinSplit(_) => coin_split = true, @@ -558,6 +563,7 @@ mod test { get_balance && send_tari && burn_tari && + create_key_pair && make_it_rain && coin_split && discover_peer && diff --git a/base_layer/core/src/transactions/key_manager/inner.rs b/base_layer/core/src/transactions/key_manager/inner.rs index 21a037051c..952dea9013 100644 --- a/base_layer/core/src/transactions/key_manager/inner.rs +++ b/base_layer/core/src/transactions/key_manager/inner.rs @@ -63,6 +63,7 @@ use tari_key_manager::{ }; use tari_utilities::{hex::Hex, ByteArray}; use tokio::sync::RwLock; +use zeroize::Zeroizing; const LOG_TARGET: &str = "c::bn::key_manager::key_manager_service"; const TRANSACTION_KEY_MANAGER_MAX_SEARCH_DEPTH: u64 = 1_000_000; @@ -179,6 +180,16 @@ where TBackend: KeyManagerBackend + 'static Ok((key_id, key)) } + pub async fn create_key_pair( + &mut self, + branch: &str, + ) -> Result<(Zeroizing, PublicKey), KeyManagerServiceError> { + self.add_key_manager_branch(branch)?; + let (key_id, public_key) = self.get_next_key(branch).await?; + let private_key = Zeroizing::new(self.get_private_key(&key_id).await?); + Ok((private_key, public_key)) + } + pub async fn get_static_key(&self, branch: &str) -> Result { match self.key_managers.get(branch) { None => Err(KeyManagerServiceError::UnknownKeyBranch), diff --git a/base_layer/core/src/transactions/key_manager/interface.rs b/base_layer/core/src/transactions/key_manager/interface.rs index 9697afdc5b..45a9a9e434 100644 --- a/base_layer/core/src/transactions/key_manager/interface.rs +++ b/base_layer/core/src/transactions/key_manager/interface.rs @@ -29,6 +29,7 @@ use tari_common_types::types::{ComAndPubSignature, Commitment, PrivateKey, Publi use tari_comms::types::CommsDHKE; use tari_crypto::{hashing::DomainSeparatedHash, ristretto::RistrettoComSig}; use tari_key_manager::key_manager_service::{KeyId, KeyManagerInterface, KeyManagerServiceError}; +use zeroize::Zeroizing; use crate::transactions::{ tari_amount::MicroMinotari, @@ -274,6 +275,11 @@ pub trait TransactionKeyManagerInterface: KeyManagerInterface { amount: &PrivateKey, claim_public_key: &PublicKey, ) -> Result; + + async fn create_key_pair + Send>( + &self, + branch: T, + ) -> Result<(Zeroizing, PublicKey), KeyManagerServiceError>; } #[async_trait::async_trait] diff --git a/base_layer/core/src/transactions/key_manager/wrapper.rs b/base_layer/core/src/transactions/key_manager/wrapper.rs index dfd6167837..a9801bfb1f 100644 --- a/base_layer/core/src/transactions/key_manager/wrapper.rs +++ b/base_layer/core/src/transactions/key_manager/wrapper.rs @@ -40,6 +40,7 @@ use tari_key_manager::{ }, }; use tokio::sync::RwLock; +use zeroize::Zeroizing; use crate::transactions::{ key_manager::{ @@ -480,6 +481,17 @@ where TBackend: KeyManagerBackend + 'static .generate_burn_proof(spending_key, amount, claim_public_key) .await } + + async fn create_key_pair + Send>( + &self, + branch: T, + ) -> Result<(Zeroizing, PublicKey), KeyManagerServiceError> { + self.transaction_key_manager_inner + .write() + .await + .create_key_pair(&branch.into()) + .await + } } #[async_trait::async_trait]