From 7502fd6538eb96bf566527c90d7bec8a158dee1f Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Mon, 8 Aug 2022 18:56:02 +0200 Subject: [PATCH] fix: recover all known scripts (#4397) Description --- This allows the wallet to recover all known scripts. Motivation and Context --- The wallet stores a list of all known scripts and inputs required to claim them. This PR allows the wallet to use this lest when searching for outputs to recover. Fixes: https://github.com/tari-project/tari/issues/4231 --- .../recovery/standard_outputs_recoverer.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/base_layer/wallet/src/output_manager_service/recovery/standard_outputs_recoverer.rs b/base_layer/wallet/src/output_manager_service/recovery/standard_outputs_recoverer.rs index 13e578fddf..c2aef69431 100644 --- a/base_layer/wallet/src/output_manager_service/recovery/standard_outputs_recoverer.rs +++ b/base_layer/wallet/src/output_manager_service/recovery/standard_outputs_recoverer.rs @@ -89,10 +89,13 @@ where let start = Instant::now(); let outputs_length = outputs.len(); + let known_scripts = self.db.get_all_known_one_sided_payment_scripts()?; + let mut rewound_outputs: Vec<(UnblindedOutput, BulletRangeProof)> = Vec::new(); for output in outputs { // TODO: Only outputs with scripts `== script!(Nop)` is recover-able - can this be improved? - if output.script != script!(Nop) { + let known_script_index = known_scripts.iter().position(|s| s.script == output.script); + if output.script != script!(Nop) && known_script_index.is_none() { continue; } let committed_value = EncryptedValue::decrypt_value( @@ -104,14 +107,22 @@ where let blinding_factor = output.recover_mask(&self.factories.range_proof, &self.rewind_data.rewind_blinding_key)?; if output.verify_mask(&self.factories.range_proof, &blinding_factor, committed_value.into())? { - let script_key = PrivateKey::random(&mut OsRng); + let (input_data, script_key) = if let Some(index) = known_script_index { + ( + known_scripts[index].input.clone(), + known_scripts[index].private_key.clone(), + ) + } else { + let key = PrivateKey::random(&mut OsRng); + (inputs!(PublicKey::from_secret_key(&key)), key) + }; let uo = UnblindedOutput::new( output.version, committed_value, blinding_factor, output.features, output.script, - inputs!(PublicKey::from_secret_key(&script_key)), + input_data, script_key, output.sender_offset_public_key, output.metadata_signature,