diff --git a/src/silentpayments.rs b/src/silentpayments.rs index c7e7ba3..f98ef00 100644 --- a/src/silentpayments.rs +++ b/src/silentpayments.rs @@ -1,14 +1,16 @@ use std::collections::HashMap; +use std::io::Write; use std::str::FromStr; use anyhow::{Error, Result}; -use rand::{thread_rng, Rng, RngCore}; +use rand::{thread_rng, Rng}; +use sp_client::bitcoin::hashes::{sha256, Hash}; use sp_client::bitcoin::hex::FromHex; use sp_client::bitcoin::psbt::raw; -use sp_client::bitcoin::{Psbt, Transaction}; +use sp_client::bitcoin::{Psbt, Transaction, Txid}; use sp_client::bitcoin::{Amount, OutPoint}; -use sp_client::bitcoin::consensus::{deserialize, serialize}; +use sp_client::bitcoin::consensus::deserialize; use sp_client::silentpayments::sending::SilentPaymentAddress; use sp_client::spclient::{OwnedOutput, Recipient, SpClient, SpWallet}; use sp_client::constants; @@ -74,6 +76,7 @@ pub fn create_transaction_spend_outpoint( outpoint: &OutPoint, sp_wallet: &SpWallet, mut recipient: Recipient, + commited_in_txid: &Txid, fee_rate: Amount ) -> Result { let available_outpoints = sp_wallet.get_outputs().to_spendable_list(); @@ -99,18 +102,25 @@ pub fn create_transaction_spend_outpoint( // update the amount for the recipient recipient.amount = total_available; - // create a dummy commitment + // 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]; - thread_rng().fill_bytes(&mut buf); + 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 new_psbt = sp_wallet.get_client().create_new_psbt( inputs, vec![recipient], - Some(&buf), + Some(hash.as_byte_array()), )?; - let change_addr = sp_wallet.get_client().sp_receiver.get_change_address(); - SpClient::set_fees(&mut new_psbt, fee_rate, change_addr)?; + SpClient::set_fees(&mut new_psbt, fee_rate, address)?; let partial_secret = sp_wallet .get_client()