Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make pre mine spend tx stealth #6596

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions applications/minotari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ use tari_comms_dht::{envelope::NodeDestination, DhtDiscoveryRequester};
use tari_core::{
blocks::pre_mine::get_pre_mine_items,
covenants::Covenant,
one_sided::shared_secret_to_output_encryption_key,
transactions::{
key_manager::TransactionKeyManagerInterface,
tari_amount::{uT, MicroMinotari, Minotari},
transaction_components::{
encrypted_data::PaymentId,
EncryptedData,
OutputFeatures,
Transaction,
TransactionInput,
Expand All @@ -93,13 +95,16 @@ use tari_core::{
},
},
};
use tari_crypto::ristretto::{pedersen::PedersenCommitment, RistrettoSecretKey};
use tari_crypto::{
dhke::DiffieHellmanSharedSecret,
ristretto::{pedersen::PedersenCommitment, RistrettoSecretKey},
};
use tari_key_manager::{
key_manager_service::{KeyId, KeyManagerInterface},
SeedWords,
};
use tari_p2p::{auto_update::AutoUpdateConfig, peer_seeds::SeedPeer, PeerSeedsConfig};
use tari_script::{script, CheckSigSchnorrSignature};
use tari_script::{push_pubkey_script, CheckSigSchnorrSignature};
use tari_shutdown::Shutdown;
use tari_utilities::{hex::Hex, ByteArray, SafePassword};
use tokio::{
Expand Down Expand Up @@ -208,7 +213,7 @@ async fn encumber_aggregate_utxo(
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey), CommandError> {
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey, PublicKey), CommandError> {
wallet_transaction_service
.encumber_aggregate_utxo(
fee_per_gram,
Expand Down Expand Up @@ -1275,6 +1280,7 @@ pub async fn command_runner(
script_pubkey,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
)) => {
outputs_for_parties.push(Step3OutputsForParties {
output_index: current_index,
Expand All @@ -1295,6 +1301,7 @@ pub async fn command_runner(
metadata_signature_ephemeral_pubkey: total_metadata_ephemeral_public_key,
encrypted_data: transaction.body.outputs()[0].clone().encrypted_data,
output_features: transaction.body.outputs()[0].clone().features,
shared_secret,
});
outputs_for_self.push(Step3OutputsForSelf {
output_index: current_index,
Expand Down Expand Up @@ -1426,6 +1433,55 @@ pub async fn command_runner(
},
};

// lets verify the script
let shared_secret = match DiffieHellmanSharedSecret::<PublicKey>::from_canonical_bytes(
leader_info.shared_secret.as_bytes(),
) {
Ok(v) => v,
Err(e) => {
eprintln!("\nError: Could not create shared secret from canonical bytes! {}\n", e);
break;
},
};

let encryption_key = shared_secret_to_output_encryption_key(&shared_secret)?;
let (committed_value, commitment_mask_private_key, _payment_id) = match EncryptedData::decrypt_data(
&encryption_key,
&leader_info.output_commitment,
&leader_info.encrypted_data,
) {
Ok((value, mask, id)) => (value, mask, id),
Err(e) => {
eprintln!("\nError: Could not decrypt data! {}\n", e);
break;
},
};
let commitment_mask_key_id = &key_manager_service
.import_key(commitment_mask_private_key.clone())
.await?;
match key_manager_service
.verify_mask(
&leader_info.output_commitment,
commitment_mask_key_id,
committed_value.as_u64(),
)
.await
{
Ok(_) => {},
Err(e) => {
eprintln!("\nError: Could not verify mask! {}\n", e);
break;
},
}
// now lets calculate the script with stealth key
let script_spending_key = key_manager_service
.stealth_address_script_spending_key(
commitment_mask_key_id,
party_info.recipient_address.public_spend_key(),
)
.await?;
let script = push_pubkey_script(&script_spending_key);

// Metadata signature
let script_offset = key_manager_service
.get_script_offset(&vec![party_info.pre_mine_script_key_id.clone()], &vec![party_info
Expand All @@ -1434,9 +1490,7 @@ pub async fn command_runner(
.await?;
let challenge = TransactionOutput::build_metadata_signature_challenge(
&TransactionOutputVersion::get_current_version(),
&script!(PushPubKey(Box::new(
party_info.recipient_address.public_spend_key().clone()
)))?,
&script,
&leader_info.output_features,
&leader_info.sender_offset_pubkey,
&leader_info.metadata_signature_ephemeral_commitment,
Expand Down
1 change: 1 addition & 0 deletions applications/minotari_console_wallet/src/automation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct Step3OutputsForParties {
metadata_signature_ephemeral_pubkey: PublicKey,
encrypted_data: EncryptedData,
output_features: OutputFeatures,
shared_secret: PublicKey,
}

// Step 4 outputs for leader with `PreMineSpendInputOutputSigs`
Expand Down
4 changes: 4 additions & 0 deletions base_layer/wallet/src/output_manager_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub enum OutputManagerResponse {
PublicKey,
PublicKey,
PublicKey,
PublicKey,
),
),
SpendBackupPreMineUtxo((Transaction, MicroMinotari, MicroMinotari)),
Expand Down Expand Up @@ -831,6 +832,7 @@ impl OutputManagerHandle {
PublicKey,
PublicKey,
PublicKey,
PublicKey,
),
OutputManagerError,
> {
Expand Down Expand Up @@ -858,13 +860,15 @@ impl OutputManagerHandle {
total_script_key,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
)) => Ok((
transaction,
amount,
fee,
total_script_key,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
)),
_ => Err(OutputManagerError::UnexpectedApiResponse),
}
Expand Down
19 changes: 17 additions & 2 deletions base_layer/wallet/src/output_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ where
PublicKey,
PublicKey,
PublicKey,
PublicKey,
),
OutputManagerError,
> {
Expand Down Expand Up @@ -1373,14 +1374,16 @@ where
range_proof_type,
..Default::default()
};
let script = script!(PushPubKey(Box::new(recipient_address.public_spend_key().clone())))?;
// we assign a temp script to calculate all the sizes for now, we override this with the stealth one later if
// needed
let temp_script = script!(PushPubKey(Box::new(recipient_address.public_spend_key().clone())))?;
let metadata_byte_size = self
.resources
.consensus_constants
.transaction_weight_params()
.round_up_features_and_scripts_size(
output_features.get_serialized_size()? +
script.get_serialized_size()? +
temp_script.get_serialized_size()? +
Covenant::default().get_serialized_size()?,
);
let fee = self.get_fee_calc();
Expand Down Expand Up @@ -1496,6 +1499,13 @@ where
.fold(PublicKey::default(), |acc, x| acc + x);
trace!(target: LOG_TARGET, "encumber_aggregate_utxo: prepared inputs for partial metadata signature");

let script_spending_key = self
.resources
.key_manager
.stealth_address_script_spending_key(&spending_key_id, recipient_address.public_spend_key())
.await?;
let script = push_pubkey_script(&script_spending_key);

// Create the output with a partially signed metadata signature
let output = WalletOutputBuilder::new(amount, spending_key_id)
.with_features(
Expand Down Expand Up @@ -1573,13 +1583,18 @@ where

let fee = stp.get_fee_amount()?;

// shared secret does not support debug so we manually convert this to a public key
let shared_secret_bytes = shared_secret.as_bytes();
let shared_secret_public_key = PublicKey::from_canonical_bytes(shared_secret_bytes)?;

Ok((
tx,
amount,
fee,
total_script_public_key,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret_public_key,
))
}

Expand Down
13 changes: 11 additions & 2 deletions base_layer/wallet/src/transaction_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,14 @@ impl fmt::Display for TransactionServiceRequest {
pub enum TransactionServiceResponse {
TransactionSent(TxId),
TransactionSentWithOutputHash(TxId, FixedHash),
EncumberAggregateUtxo(TxId, Box<Transaction>, Box<PublicKey>, Box<PublicKey>, Box<PublicKey>),
EncumberAggregateUtxo(
TxId,
Box<Transaction>,
Box<PublicKey>,
Box<PublicKey>,
Box<PublicKey>,
Box<PublicKey>,
),
UnspentOutputs(Vec<TransactionOutput>),
TransactionImported(TxId),
BurntTransactionSent {
Expand Down Expand Up @@ -762,7 +769,7 @@ impl TransactionServiceHandle {
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey), TransactionServiceError> {
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey, PublicKey), TransactionServiceError> {
match self
.handle
.call(TransactionServiceRequest::EncumberAggregateUtxo {
Expand All @@ -785,12 +792,14 @@ impl TransactionServiceHandle {
total_script_key,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
) => Ok((
tx_id,
*transaction,
*total_script_key,
*total_metadata_ephemeral_public_key,
*total_script_nonce,
*shared_secret,
)),
_ => Err(TransactionServiceError::UnexpectedApiResponse),
}
Expand Down
14 changes: 12 additions & 2 deletions base_layer/wallet/src/transaction_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,13 +727,21 @@ where
)
.await
.map(
|(tx_id, tx, total_script_pubkey, total_metadata_ephemeral_public_key, total_script_nonce)| {
|(
tx_id,
tx,
total_script_pubkey,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
)| {
TransactionServiceResponse::EncumberAggregateUtxo(
tx_id,
Box::new(tx),
Box::new(total_script_pubkey),
Box::new(total_metadata_ephemeral_public_key),
Box::new(total_script_nonce),
Box::new(shared_secret),
)
},
),
Expand Down Expand Up @@ -1209,7 +1217,7 @@ where
recipient_address: TariAddress,
original_maturity: u64,
use_output: UseOutput,
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey), TransactionServiceError> {
) -> Result<(TxId, Transaction, PublicKey, PublicKey, PublicKey, PublicKey), TransactionServiceError> {
let tx_id = TxId::new_random();

match self
Expand Down Expand Up @@ -1237,6 +1245,7 @@ where
total_script_key,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
)) => {
let completed_tx = CompletedTransaction::new(
tx_id,
Expand All @@ -1261,6 +1270,7 @@ where
total_script_key,
total_metadata_ephemeral_public_key,
total_script_nonce,
shared_secret,
))
},
Err(e) => Err(e.into()),
Expand Down
Loading