From 4c67010247c6215c226cbf876d2f9bc69d5b650d Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Wed, 31 Jul 2024 15:19:29 +0200 Subject: [PATCH] merge --- .../src/automation/commands.rs | 36 ++++++---- .../minotari_console_wallet/src/cli.rs | 4 +- .../minotari_console_wallet/src/init/mod.rs | 8 +-- .../src/wallet_modes.rs | 3 +- .../comms/examples/ledger_demo/main.rs | 2 +- base_layer/common_types/src/key_branches.rs | 12 ++-- .../src/transactions/key_manager/inner.rs | 65 ++++++++++--------- .../core/src/transactions/key_manager/mod.rs | 1 + .../src/output_manager_service/service.rs | 5 +- .../wallet/src/transaction_service/service.rs | 5 +- 10 files changed, 72 insertions(+), 69 deletions(-) diff --git a/applications/minotari_console_wallet/src/automation/commands.rs b/applications/minotari_console_wallet/src/automation/commands.rs index 3ca689fed10..5827e4a2a7d 100644 --- a/applications/minotari_console_wallet/src/automation/commands.rs +++ b/applications/minotari_console_wallet/src/automation/commands.rs @@ -1264,22 +1264,28 @@ pub async fn command_runner( println!(); }, PreMineSpendBackupUtxo(args) => { - let commitment = if let Ok(val) = Commitment::from_hex(&args.commitment) { - val - } else { - eprintln!("\nError: Invalid 'commitment' provided!\n"); - continue; - }; - let hash = if let Ok(val) = FixedHash::from_hex(&args.output_hash) { - val - } else { - eprintln!("\nError: Invalid 'output_hash' provided!\n"); - continue; + match key_manager_service.get_wallet_type().await { + WalletType::Ledger(_) => {}, + _ => { + eprintln!("\nError: Wallet type must be 'Ledger' to spend pre-mine outputs!\n"); + break; + }, + } + + let embedded_output = match get_embedded_pre_mine_outputs(vec![args.output_index]) { + Ok(outputs) => outputs[0].clone(), + Err(e) => { + eprintln!("\nError: {}\n", e); + break; + }, }; + let commitment = embedded_output.commitment.clone(); + let output_hash = embedded_output.hash(); + match spend_backup_pre_mine_utxo( transaction_service.clone(), args.fee_per_gram, - hash, + output_hash, commitment.clone(), args.recipient_address, ) @@ -1290,10 +1296,12 @@ pub async fn command_runner( println!("Spend utxo: {} with tx_id: {}", commitment.to_hex(), tx_id); println!(); }, - Err(e) => eprintln!("\nError:Spent pre-mine transaction error! {}\n", e), + Err(e) => { + eprintln!("\nError: Spent pre-mine transaction error! {}\n", e); + break; + }, } }, - PreMineCreatePartyDetails(args) => { PreMineSpendPartyDetails(args) => { match key_manager_service.get_wallet_type().await { WalletType::Ledger(_) => {}, diff --git a/applications/minotari_console_wallet/src/cli.rs b/applications/minotari_console_wallet/src/cli.rs index 4432e4ea130..22db41468c9 100644 --- a/applications/minotari_console_wallet/src/cli.rs +++ b/applications/minotari_console_wallet/src/cli.rs @@ -241,9 +241,7 @@ pub struct PreMineSpendBackupUtxoArgs { #[clap(long)] pub fee_per_gram: MicroMinotari, #[clap(long)] - pub commitment: String, - #[clap(long)] - pub output_hash: String, + pub output_index: usize, #[clap(long)] pub recipient_address: TariAddress, } diff --git a/applications/minotari_console_wallet/src/init/mod.rs b/applications/minotari_console_wallet/src/init/mod.rs index b548e41d35f..2fe925622e2 100644 --- a/applications/minotari_console_wallet/src/init/mod.rs +++ b/applications/minotari_console_wallet/src/init/mod.rs @@ -64,7 +64,7 @@ use tari_comms::{ use tari_core::{ consensus::ConsensusManager, transactions::{ - key_manager::{TariKeyId, TransactionKeyManagerInterface}, + key_manager::{TariKeyId, TransactionKeyManagerInterface, LEDGER_NOT_SUPPORTED}, transaction_components::TransactionError, CryptoFactories, }, @@ -576,9 +576,7 @@ pub async fn start_wallet( { return Err(ExitError::new( ExitCode::WalletError, - "Ledger is not supported in this build, please enable the \"ledger\" feature for console wallet and \ - core" - .to_string(), + format!("{}", LEDGER_NOT_SUPPORTED), )); } @@ -591,7 +589,7 @@ pub async fn start_wallet( match wallet.key_manager_service.get_public_key_at_key_id(&key_id).await { Ok(public_key) => {}, Err(e) => { - if e.to_string().contains("Ledger is not supported in this build") { + if e.to_string().contains(LEDGER_NOT_SUPPORTED) { return Err(ExitError::new(ExitCode::WalletError, format!(" {}", e))); } }, diff --git a/applications/minotari_console_wallet/src/wallet_modes.rs b/applications/minotari_console_wallet/src/wallet_modes.rs index 2f93972a680..a4fb0277ef5 100644 --- a/applications/minotari_console_wallet/src/wallet_modes.rs +++ b/applications/minotari_console_wallet/src/wallet_modes.rs @@ -177,7 +177,8 @@ fn force_exit_for_pre_mine_commands(command: &CliCommands) -> bool { CliCommands::PreMineSpendEncumberAggregateUtxo(_) | CliCommands::PreMineSpendAggregateTransaction(_) | CliCommands::PreMineSpendPartyDetails(_) | - CliCommands::PreMineSpendInputOutputSigs(_) + CliCommands::PreMineSpendInputOutputSigs(_) | + CliCommands::PreMineSpendBackupUtxo(_) ) } diff --git a/applications/minotari_ledger_wallet/comms/examples/ledger_demo/main.rs b/applications/minotari_ledger_wallet/comms/examples/ledger_demo/main.rs index 6197f52c40d..b1b4db696e4 100644 --- a/applications/minotari_ledger_wallet/comms/examples/ledger_demo/main.rs +++ b/applications/minotari_ledger_wallet/comms/examples/ledger_demo/main.rs @@ -116,7 +116,7 @@ fn main() { let index = OsRng.next_u64(); for branch in &[ - TransactionKeyManagerBranch::SenderOffsetLedger, + TransactionKeyManagerBranch::OneSidedSenderOffset, TransactionKeyManagerBranch::Spend, TransactionKeyManagerBranch::RandomKey, TransactionKeyManagerBranch::PreMine, diff --git a/base_layer/common_types/src/key_branches.rs b/base_layer/common_types/src/key_branches.rs index c90b684731e..9391bea5d23 100644 --- a/base_layer/common_types/src/key_branches.rs +++ b/base_layer/common_types/src/key_branches.rs @@ -98,7 +98,7 @@ impl TransactionKeyManagerBranch { Some(Branch::Nonce) => Some(TransactionKeyManagerBranch::Nonce), Some(Branch::KernelNonce) => Some(TransactionKeyManagerBranch::KernelNonce), Some(Branch::SenderOffset) => Some(TransactionKeyManagerBranch::SenderOffset), - Some(Branch::SenderOffsetLedger) => Some(TransactionKeyManagerBranch::SenderOffsetLedger), + Some(Branch::OneSidedSenderOffset) => Some(TransactionKeyManagerBranch::OneSidedSenderOffset), Some(Branch::Spend) => Some(TransactionKeyManagerBranch::Spend), Some(Branch::RandomKey) => Some(TransactionKeyManagerBranch::RandomKey), Some(Branch::PreMine) => Some(TransactionKeyManagerBranch::PreMine), @@ -122,7 +122,7 @@ mod test { PRE_MINE, RANDOM_KEY, SENDER_OFFSET, - SENDER_OFFSET_LEDGER, + ONE_SIDED_SENDER_OFFSET, }, WALLET_COMMS_AND_SPEND_KEY_BRANCH, }; @@ -158,9 +158,9 @@ mod test { SENDER_OFFSET, ), ( - Branch::SenderOffsetLedger as u8, - TransactionKeyManagerBranch::SenderOffsetLedger, - SENDER_OFFSET_LEDGER, + Branch::OneSidedSenderOffset as u8, + TransactionKeyManagerBranch::OneSidedSenderOffset, + ONE_SIDED_SENDER_OFFSET, ), ( Branch::Spend as u8, @@ -213,7 +213,7 @@ mod test { assert_eq!(&branch.get_branch_key(), *key); assert_eq!(TransactionKeyManagerBranch::from_key(key), *branch); }, - TransactionKeyManagerBranch::SenderOffsetLedger => { + TransactionKeyManagerBranch::OneSidedSenderOffset => { assert_eq!(branch.as_byte(), *expected_byte); assert_eq!(TransactionKeyManagerBranch::from_byte(*expected_byte), Some(*branch)); assert_eq!(&branch.get_branch_key(), *key); diff --git a/base_layer/core/src/transactions/key_manager/inner.rs b/base_layer/core/src/transactions/key_manager/inner.rs index 08eb5c50492..93515bec3aa 100644 --- a/base_layer/core/src/transactions/key_manager/inner.rs +++ b/base_layer/core/src/transactions/key_manager/inner.rs @@ -76,6 +76,8 @@ const LOG_TARGET: &str = "c::bn::key_manager::key_manager_service"; const TRANSACTION_KEY_MANAGER_MAX_SEARCH_DEPTH: u64 = 1_000_000; const HASHER_LABEL_STEALTH_KEY: &str = "script key"; +pub const LEDGER_NOT_SUPPORTED: &str = "Ledger is not supported in this build, please enable the \"ledger\" feature."; + use crate::{ common::ConfidentialOutputHasher, one_sided::diffie_hellman_stealth_domain_hasher, @@ -188,12 +190,12 @@ where TBackend: KeyManagerBackend + 'static pub async fn get_random_key(&self) -> Result, KeyManagerServiceError> { match &self.wallet_type { WalletType::Ledger(ledger) => { - debug!(target: LOG_TARGET, "get_random_key: wallet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "get_random_key: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { Err(KeyManagerServiceError::LedgerError(format!( - "Ledger {} is not supported in this build, please enable the \"ledger\" feature for core", - ledger + "{} 'get_random_key' was called. ({})", + LEDGER_NOT_SUPPORTED, ledger ))) } #[cfg(feature = "ledger")] @@ -240,13 +242,13 @@ where TBackend: KeyManagerBackend + 'static if let WalletType::Ledger(ledger) = &self.wallet_type { match TransactionKeyManagerBranch::from_key(branch) { TransactionKeyManagerBranch::OneSidedSenderOffset | TransactionKeyManagerBranch::RandomKey => { + debug!(target: LOG_TARGET, "get_public_key_at_key_id: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { - Err(KeyManagerServiceError::LedgerError( - "Ledger is not supported in this build, please enable the \"ledger\" feature for \ - core" - .to_string(), - )) + Err(KeyManagerServiceError::LedgerError(format!( + "{} 'get_public_key_at_key_id' was called.", + LEDGER_NOT_SUPPORTED + ))) } #[cfg(feature = "ledger")] @@ -667,13 +669,12 @@ where TBackend: KeyManagerBackend + 'static if let KeyId::Managed { branch, index } = secret_key_id { match TransactionKeyManagerBranch::from_key(branch) { TransactionKeyManagerBranch::OneSidedSenderOffset | TransactionKeyManagerBranch::RandomKey => { - debug!(target: LOG_TARGET, "get_diffie_hellman_shared_secret: wallet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "get_diffie_hellman_shared_secret: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { return Err(TransactionError::LedgerNotSupported(format!( - "Ledger {} (has index {}) is not supported in this build, please enable the \ - \"ledger\" feature for core", - ledger, index + "{} 'get_diffie_hellman_shared_secret' was called. ({} (has index {}))", + LEDGER_NOT_SUPPORTED, ledger, index ))); } @@ -707,13 +708,12 @@ where TBackend: KeyManagerBackend + 'static WalletType::Ledger(ledger) => match secret_key_id { KeyId::Managed { branch, index } => match TransactionKeyManagerBranch::from_key(branch) { TransactionKeyManagerBranch::OneSidedSenderOffset => { - debug!(target: LOG_TARGET, "get_diffie_hellman_stealth_domain_hasher: allet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "get_diffie_hellman_stealth_domain_hasher: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { Err(TransactionError::LedgerNotSupported(format!( - "Ledger {} (has index {}) is not supported in this build, please enable the \ - \"ledger\" feature for core", - ledger, index + "{} 'get_diffie_hellman_stealth_domain_hasher' was called. ({} (has index {}))", + LEDGER_NOT_SUPPORTED, ledger, index ))) } @@ -792,13 +792,12 @@ where TBackend: KeyManagerBackend + 'static match (&self.wallet_type, script_key_id) { (WalletType::Ledger(ledger), KeyId::Derived { key }) => { - debug!(target: LOG_TARGET, "get_script_signature: wallet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "get_script_signature: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { Err(TransactionError::LedgerNotSupported(format!( - "Ledger {} (has script_key_id {}, branch {}, index {}) is not supported in this build, please \ - enable the \"ledger\" feature for core", - ledger, script_key_id, branch, index + "{} 'get_script_signature' was called. ({} (has key {}))", + LEDGER_NOT_SUPPORTED, ledger, key ))) } @@ -993,12 +992,12 @@ where TBackend: KeyManagerBackend + 'static Ok(script_offset) }, WalletType::Ledger(ledger) => { - debug!(target: LOG_TARGET, "get_script_offset: wallet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "get_script_offset: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { Err(TransactionError::LedgerNotSupported(format!( - "Ledger {} is not supported in this build, please enable the \"ledger\" feature for core", - ledger + "{} 'get_script_offset' was called. ({})", + LEDGER_NOT_SUPPORTED, ledger ))) } @@ -1082,12 +1081,12 @@ where TBackend: KeyManagerBackend + 'static ) -> Result { match &self.wallet_type { WalletType::Ledger(ledger) => { - debug!(target: LOG_TARGET, "sign_script_message: wallet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "sign_script_message: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { Err(TransactionError::LedgerNotSupported(format!( - "Ledger {} is not supported in this build, please enable the \"ledger\" feature for core", - ledger + "{} 'sign_script_message' was called. ({})", + LEDGER_NOT_SUPPORTED, ledger ))) } @@ -1127,12 +1126,12 @@ where TBackend: KeyManagerBackend + 'static ) -> Result { match &self.wallet_type { WalletType::Ledger(ledger) => { - debug!(target: LOG_TARGET, "sign_with_nonce_and_challenge: wallet type {}", self.wallet_type); + debug!(target: LOG_TARGET, "sign_with_nonce_and_challenge: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { Err(TransactionError::LedgerNotSupported(format!( - "Ledger {} is not supported in this build, please enable the \"ledger\" feature for core", - ledger + "{} 'sign_with_nonce_and_challenge' was called. ({})", + LEDGER_NOT_SUPPORTED, ledger ))) } @@ -1245,11 +1244,13 @@ where TBackend: KeyManagerBackend + 'static .await }, WalletType::Ledger(ledger) => { + debug!(target: LOG_TARGET, "get_one_sided_metadata_signature: {}", self.wallet_type); #[cfg(not(feature = "ledger"))] { - Err(TransactionError::LedgerNotSupported( - "One sided metadata signature was called for ledger, but ledger is not supported.".to_string(), - )) + Err(TransactionError::LedgerNotSupported(format!( + "{} 'get_one_sided_metadata_signature' was called. ({})", + LEDGER_NOT_SUPPORTED, ledger + ))) } #[cfg(feature = "ledger")] diff --git a/base_layer/core/src/transactions/key_manager/mod.rs b/base_layer/core/src/transactions/key_manager/mod.rs index b1c356c574f..37fe079630a 100644 --- a/base_layer/core/src/transactions/key_manager/mod.rs +++ b/base_layer/core/src/transactions/key_manager/mod.rs @@ -36,6 +36,7 @@ mod initializer; pub use initializer::TransactionKeyManagerInitializer; mod inner; +pub use inner::LEDGER_NOT_SUPPORTED; /// This is a memory database implementation of the `TransactionKeyManager` trait. mod memory_db_key_manager; pub use inner::TransactionKeyManagerInner; diff --git a/base_layer/wallet/src/output_manager_service/service.rs b/base_layer/wallet/src/output_manager_service/service.rs index cb20287102b..8495bc79822 100644 --- a/base_layer/wallet/src/output_manager_service/service.rs +++ b/base_layer/wallet/src/output_manager_service/service.rs @@ -67,8 +67,7 @@ use tari_core::{ }, }; use tari_crypto::ristretto::pedersen::PedersenCommitment; -use tari_key_manager::key_manager_service::SerializedKeyString; -use tari_key_manager::key_manager_service::{KeyAndId, KeyId}; +use tari_key_manager::key_manager_service::{KeyAndId, KeyId, SerializedKeyString}; use tari_script::{ inputs, push_pubkey_script, @@ -1395,7 +1394,7 @@ where stp.change_recipient_sender_offset_private_key( self.resources .key_manager - .get_next_key(TransactionKeyManagerBranch::SenderOffsetLedger.get_branch_key()) + .get_next_key(TransactionKeyManagerBranch::OneSidedSenderOffset.get_branch_key()) .await? .key_id, )?; diff --git a/base_layer/wallet/src/transaction_service/service.rs b/base_layer/wallet/src/transaction_service/service.rs index 04364dd10a3..3203d91c978 100644 --- a/base_layer/wallet/src/transaction_service/service.rs +++ b/base_layer/wallet/src/transaction_service/service.rs @@ -47,10 +47,7 @@ use tari_core::{ consensus::ConsensusManager, covenants::Covenant, mempool::FeePerGramStat, - one_sided::{ - shared_secret_to_output_encryption_key, - shared_secret_to_output_spending_key, - }, + one_sided::{shared_secret_to_output_encryption_key, shared_secret_to_output_spending_key}, proto::{base_node as base_node_proto, base_node::FetchMatchingUtxos}, transactions::{ key_manager::TransactionKeyManagerInterface,