From de133cc0a3ec71e281709fa3a0908d162793cd1a Mon Sep 17 00:00:00 2001 From: Sosthene Date: Mon, 27 May 2024 15:59:29 +0200 Subject: [PATCH] [experimental] identify transaction we sent according to commitment value --- crates/sp_client/src/api.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/sp_client/src/api.rs b/crates/sp_client/src/api.rs index c2ea56e..9a5d7c1 100644 --- a/crates/sp_client/src/api.rs +++ b/crates/sp_client/src/api.rs @@ -508,9 +508,33 @@ fn handle_recover_transaction( } else { // We are sender of a transaction // We only need to return the message + // eiter this is notification, a challenge, or response to a challenge + // if notification, commitment is the same than in the message + // if challenge or response, commitment is H(commitment | b_scan), b_scan being different depending on who we are if let Some(message) = messages .iter() - .find(|m| m.commitment.as_ref() == Some(&commitment_str)) + .find(|m| { + if commitment.is_empty() || m.commitment.is_none() { return false } + match m.status { + CachedMessageStatus::SentWaitingConfirmation => { + // commitment we're looking for is simply what's in the message + m.commitment.as_ref().map(|c| Vec::from_hex(&c).unwrap()).unwrap() == commitment + }, + CachedMessageStatus::MustSpendConfirmation | CachedMessageStatus::ReceivedMustConfirm => { + // we compute the potential commitment + let m_commitment = m.commitment.as_ref().map(|c| Vec::from_hex(&c).unwrap()).unwrap(); + let mut buf = [0u8;64]; + buf[..32].copy_from_slice(&m_commitment); + buf[32..].copy_from_slice(&sp_wallet.get_client().get_scan_key().secret_bytes()); + + let mut engine = sha256::HashEngine::default(); + engine.write_all(&buf).unwrap(); + let hash = sha256::Hash::from_engine(engine); + hash.to_byte_array().to_vec() == commitment + }, + _ => return false + } + }) { return Ok(message.clone()); } else {