Skip to content

Commit

Permalink
refactor(base_layer/core): remove duplicate setting of recovery byte (#…
Browse files Browse the repository at this point in the history
…4240)

Description
---
Removes commitment calculation and setting of recovery byte in output manager. 

Motivation and Context
---
Noticed that recovery byte was needlessly set in the output manager. Recovery byte is finalized before the transaction is sent. 

How Has This Been Tested?
---
Existing tests
  • Loading branch information
sdbondi authored Jul 4, 2022
1 parent a997934 commit 3e5a4bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use derivative::Derivative;
use tari_common_types::types::{BlindingFactor, ComSignature, PrivateKey, PublicKey};
use tari_common_types::types::{BlindingFactor, ComSignature, Commitment, PrivateKey, PublicKey};
use tari_crypto::commitment::HomomorphicCommitmentFactory;
use tari_script::{ExecutionStack, TariScript};

Expand Down Expand Up @@ -59,6 +59,7 @@ pub struct UnblindedOutputBuilder {
metadata_signed_by_receiver: bool,
metadata_signed_by_sender: bool,
encrypted_value: EncryptedValue,
rewind_data: Option<RewindData>,
}

impl UnblindedOutputBuilder {
Expand All @@ -76,16 +77,14 @@ impl UnblindedOutputBuilder {
metadata_signed_by_receiver: false,
metadata_signed_by_sender: false,
encrypted_value: EncryptedValue::default(),
rewind_data: None,
}
}

pub fn update_recovery_byte_if_required(
&mut self,
factories: &CryptoFactories,
rewind_data: Option<&RewindData>,
) -> Result<(), TransactionError> {
let commitment = factories.commitment.commit(&self.spending_key, &self.value.into());
self.features.update_recovery_byte(&commitment, rewind_data);
pub fn update_recovery_byte(&mut self, factories: &CryptoFactories) -> Result<(), TransactionError> {
let commitment = self.generate_commitment(factories);
self.features
.update_recovery_byte(&commitment, self.rewind_data.as_ref());
Ok(())
}

Expand Down Expand Up @@ -179,6 +178,11 @@ impl UnblindedOutputBuilder {
self
}

pub fn with_rewind_data(mut self, rewind_data: RewindData) -> Self {
self.rewind_data = Some(rewind_data);
self
}

pub fn with_script_private_key(mut self, script_private_key: PrivateKey) -> Self {
self.script_private_key = Some(script_private_key);
self
Expand All @@ -187,6 +191,12 @@ impl UnblindedOutputBuilder {
pub fn covenant(&self) -> &Covenant {
&self.covenant
}

pub fn generate_commitment(&self, factories: &CryptoFactories) -> Commitment {
factories
.commitment
.commit_value(&self.spending_key, self.value.as_u64())
}
}

#[cfg(test)]
Expand Down Expand Up @@ -216,8 +226,6 @@ mod test {
#[test]
fn test_update_recovery_byte_if_required() {
let mut uob = UnblindedOutputBuilder::new(100.into(), RistrettoSecretKey::default());
assert!(uob
.update_recovery_byte_if_required(&CryptoFactories::default(), None)
.is_ok());
assert!(uob.update_recovery_byte(&CryptoFactories::default(),).is_ok());
}
}
17 changes: 3 additions & 14 deletions base_layer/wallet/src/output_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,21 +679,11 @@ where
let input_data = inputs!(PublicKey::from_secret_key(&script_private_key));
let script = script!(Nop);

let commitment = self
.resources
.factories
.commitment
.commit_value(&spending_key, value.as_u64());
let updated_features = OutputFeatures::features_with_updated_recovery_byte(
&commitment,
Some(&self.resources.rewind_data),
&features.clone(),
);

Ok(UnblindedOutputBuilder::new(value, spending_key)
.with_features(updated_features)
.with_features(features)
.with_script(script)
.with_input_data(input_data)
.with_rewind_data(self.resources.rewind_data.clone())
.with_script_private_key(script_private_key))
}

Expand Down Expand Up @@ -1104,8 +1094,7 @@ where
let public_offset_commitment_private_key = PrivateKey::random(&mut OsRng);
let public_offset_commitment_pub_key = PublicKey::from_secret_key(&public_offset_commitment_private_key);

unblinded_output
.update_recovery_byte_if_required(&self.resources.factories, Some(&self.resources.rewind_data))?;
unblinded_output.update_recovery_byte(&self.resources.factories)?;
unblinded_output.sign_as_receiver(sender_offset_public_key, public_offset_commitment_pub_key)?;
unblinded_output.sign_as_sender(&sender_offset_private_key)?;

Expand Down

0 comments on commit 3e5a4bb

Please sign in to comment.