Skip to content

Commit

Permalink
Add use tx output as provided
Browse files Browse the repository at this point in the history
Added an option to `fn encumber_aggregate_utxo` to use the transaction output from the
blockchain or as provided from file.
  • Loading branch information
hansieodendaal committed Oct 2, 2024
1 parent 7e07972 commit 2ed4203
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use minotari_wallet::{
connectivity_service::WalletConnectivityInterface,
output_manager_service::{
handle::{OutputManagerEvent, OutputManagerHandle},
service::UseOutput,
UtxoSelectionCriteria,
},
transaction_service::{
Expand Down Expand Up @@ -207,6 +208,7 @@ async fn encumber_aggregate_utxo(
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey), CommandError> {
wallet_transaction_service
.encumber_aggregate_utxo(
Expand All @@ -220,6 +222,7 @@ async fn encumber_aggregate_utxo(
dh_shared_secret_shares,
recipient_address,
original_maturity,
use_output,
)
.await
.map_err(CommandError::TransactionServiceError)
Expand Down Expand Up @@ -1261,6 +1264,11 @@ pub async fn command_runner(
dh_shared_secret_shares,
current_recipient_address,
original_maturity,
if pre_mine_from_file.is_some() {
UseOutput::AsProvided(embedded_output)
} else {
UseOutput::FromBlockchain
},
)
.await
{
Expand Down
5 changes: 4 additions & 1 deletion base_layer/wallet/src/output_manager_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use tower::Service;

use crate::output_manager_service::{
error::OutputManagerError,
service::{Balance, OutputInfoByTxId},
service::{Balance, OutputInfoByTxId, UseOutput},
storage::models::{DbWalletOutput, KnownOneSidedPaymentScript, SpendingPriority},
UtxoSelectionCriteria,
};
Expand All @@ -73,6 +73,7 @@ pub enum OutputManagerRequest {
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
},
SpendBackupPreMineUtxo {
tx_id: TxId,
Expand Down Expand Up @@ -817,6 +818,7 @@ impl OutputManagerHandle {
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<
(
Transaction,
Expand All @@ -842,6 +844,7 @@ impl OutputManagerHandle {
dh_shared_secret_shares,
recipient_address,
original_maturity,
use_output,
})
.await??
{
Expand Down
38 changes: 27 additions & 11 deletions base_layer/wallet/src/output_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ where
dh_shared_secret_shares,
recipient_address,
original_maturity,
use_output,
} => self
.encumber_aggregate_utxo(
tx_id,
Expand All @@ -274,6 +275,7 @@ where
original_maturity,
RangeProofType::BulletProofPlus,
0.into(),
use_output,
)
.await
.map(OutputManagerResponse::EncumberAggregateUtxo),
Expand Down Expand Up @@ -1255,6 +1257,7 @@ where
original_maturity: u64,
range_proof_type: RangeProofType,
minimum_value_promise: MicroMinotari,
use_output: UseOutput,
) -> Result<
(
Transaction,
Expand All @@ -1267,17 +1270,20 @@ where
OutputManagerError,
> {
trace!(target: LOG_TARGET, "encumber_aggregate_utxo: start");
// Fetch the output from the blockchain
let output = self
.fetch_unspent_outputs_from_node(vec![output_hash])
.await?
.pop()
.ok_or_else(|| {
OutputManagerError::ServiceError(format!(
"Output with hash {} not found in blockchain (TxId: {})",
output_hash, tx_id
))
})?;
// Fetch the output from the blockchain or use provided
let output = match use_output {
UseOutput::FromBlockchain => self
.fetch_unspent_outputs_from_node(vec![output_hash])
.await?
.pop()
.ok_or_else(|| {
OutputManagerError::ServiceError(format!(
"Output with hash {} not found in blockchain (TxId: {})",
output_hash, tx_id
))
})?,
UseOutput::AsProvided(val) => val,
};
if output.commitment != expected_commitment {
return Err(OutputManagerError::ServiceError(format!(
"Output commitment does not match expected commitment (TxId: {})",
Expand Down Expand Up @@ -3299,6 +3305,16 @@ where
}
}

/// Use the provided output when encumbering an aggregate UTXO or not, for use with
/// `fn encumber_aggregate_utxo`
#[derive(Clone)]
pub enum UseOutput {
/// The transaction output will be fetched from the blockchain
FromBlockchain,
/// The transaction output must be provided
AsProvided(TransactionOutput),
}

fn get_multi_sig_script_components(
script: &TariScript,
tx_id: TxId,
Expand Down
5 changes: 4 additions & 1 deletion base_layer/wallet/src/transaction_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use tokio::sync::broadcast;
use tower::Service;

use crate::{
output_manager_service::UtxoSelectionCriteria,
output_manager_service::{service::UseOutput, UtxoSelectionCriteria},
transaction_service::{
error::TransactionServiceError,
storage::models::{
Expand Down Expand Up @@ -113,6 +113,7 @@ pub enum TransactionServiceRequest {
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
},
SpendBackupPreMineUtxo {
fee_per_gram: MicroMinotari,
Expand Down Expand Up @@ -756,6 +757,7 @@ impl TransactionServiceHandle {
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey), TransactionServiceError> {
match self
.handle
Expand All @@ -770,6 +772,7 @@ impl TransactionServiceHandle {
dh_shared_secret_shares,
recipient_address,
original_maturity,
use_output,
})
.await??
{
Expand Down
5 changes: 5 additions & 0 deletions base_layer/wallet/src/transaction_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ use crate::{
connectivity_service::WalletConnectivityInterface,
output_manager_service::{
handle::{OutputManagerEvent, OutputManagerHandle},
service::UseOutput,
storage::models::SpendingPriority,
UtxoSelectionCriteria,
},
Expand Down Expand Up @@ -711,6 +712,7 @@ where
dh_shared_secret_shares,
recipient_address,
original_maturity,
use_output,
} => self
.encumber_aggregate_tx(
fee_per_gram,
Expand All @@ -723,6 +725,7 @@ where
dh_shared_secret_shares,
recipient_address,
original_maturity,
use_output,
)
.await
.map(
Expand Down Expand Up @@ -1208,6 +1211,7 @@ where
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey), TransactionServiceError> {
let tx_id = TxId::new_random();

Expand All @@ -1226,6 +1230,7 @@ where
dh_shared_secret_shares,
recipient_address.clone(),
original_maturity,
use_output,
)
.await
{
Expand Down

0 comments on commit 2ed4203

Please sign in to comment.