From 066410ec6355a70410b90d9f423cc2fd4f31ab58 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Thu, 27 Jun 2024 17:19:04 +0200 Subject: [PATCH] Add optional payload to create_transaction_spend_outpoint --- src/silentpayments.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/silentpayments.rs b/src/silentpayments.rs index c2b58e5..ff9301f 100644 --- a/src/silentpayments.rs +++ b/src/silentpayments.rs @@ -77,6 +77,7 @@ pub fn create_transaction_spend_outpoint( sp_wallet: &SpWallet, mut recipient: Recipient, commited_in_txid: &Txid, + payload: Option>, fee_rate: Amount ) -> Result { let available_outpoints = sp_wallet.get_outputs().to_spendable_list(); @@ -105,19 +106,30 @@ pub fn create_transaction_spend_outpoint( // Take the recipient address let address = recipient.address.clone(); - // create a dummy commitment that is H(b_scan | commited_in txid) - let mut buf = [0u8;64]; - buf[..32].copy_from_slice(commited_in_txid.as_raw_hash().as_byte_array()); - buf[32..].copy_from_slice(&sp_wallet.get_client().get_scan_key().secret_bytes()); - - let mut engine = sha256::HashEngine::default(); - engine.write_all(&buf)?; - let hash = sha256::Hash::from_engine(engine); + let mut commitment = [0u8;32]; + if let Some(p) = payload { + let mut engine = sha256::HashEngine::default(); + engine.write_all(&p)?; + let hash = sha256::Hash::from_engine(engine); + + commitment.copy_from_slice(hash.as_byte_array()); + } else { + // create a dummy commitment that is H(b_scan | commited_in txid) + let mut buf = [0u8;64]; + buf[..32].copy_from_slice(commited_in_txid.as_raw_hash().as_byte_array()); + buf[32..].copy_from_slice(&sp_wallet.get_client().get_scan_key().secret_bytes()); + + let mut engine = sha256::HashEngine::default(); + engine.write_all(&buf)?; + let hash = sha256::Hash::from_engine(engine); + + commitment.copy_from_slice(hash.as_byte_array()); + } let mut new_psbt = sp_wallet.get_client().create_new_psbt( inputs, vec![recipient], - Some(hash.as_byte_array()), + Some(&commitment), )?; SpClient::set_fees(&mut new_psbt, fee_rate, address)?;