diff --git a/src/silentpayments.rs b/src/silentpayments.rs index b1088de..09d5dca 100644 --- a/src/silentpayments.rs +++ b/src/silentpayments.rs @@ -5,10 +5,8 @@ use anyhow::{Error, Result}; use rand::{thread_rng, Rng}; use sp_client::bitcoin::consensus::deserialize; -use sp_client::bitcoin::key::{Keypair, Secp256k1, TapTweak}; -use sp_client::bitcoin::psbt::{raw, Output}; -use sp_client::bitcoin::{Address, Psbt, ScriptBuf, Transaction, Txid}; -use sp_client::bitcoin::{Amount, OutPoint, TxOut}; +use sp_client::bitcoin::psbt::raw; +use sp_client::bitcoin::{Psbt, Amount, OutPoint}; use sp_client::constants::{ self, DUST_THRESHOLD, PSBT_SP_ADDRESS_KEY, PSBT_SP_PREFIX, PSBT_SP_SUBTYPE, }; @@ -52,7 +50,7 @@ pub fn create_transaction( for outpoint in mandatory_inputs { let (must_outpoint, must_output) = available_outpoints .remove_entry(&outpoint) - .ok_or_else(|| Error::msg("Mandatory outpoint unknown"))?; + .ok_or_else(|| Error::msg(format!("Mandatory outpoint unknown: {}", outpoint)))?; total_available += must_output.amount; inputs.insert(must_outpoint, must_output); } @@ -174,13 +172,13 @@ pub fn map_outputs_to_sp_address(psbt_str: &str) -> Result PublicKey { let prevout = tx.input.get(0).unwrap().to_owned(); @@ -209,11 +207,11 @@ mod tests { tweak_data } - fn helper_create_commitment(payload_to_hash: String) -> String { + fn helper_create_commitment(payload_to_hash: String) -> sha256::Hash { let mut engine = sha256::HashEngine::default(); - engine.write_all(&payload_to_hash.as_bytes()); + engine.write_all(&payload_to_hash.as_bytes()).unwrap(); let hash = sha256::Hash::from_engine(engine); - hash.to_byte_array().to_lower_hex_string() + hash } #[test] @@ -225,17 +223,19 @@ mod tests { }; let mut alice_wallet: SpWallet = serde_json::from_str(ALICE_WALLET).unwrap(); let mut bob_wallet: SpWallet = serde_json::from_str(BOB_WALLET).unwrap(); - let message: CipherMessage = CipherMessage::new(ALICE_ADDRESS.to_owned(), "TEST".to_owned()); - let commitment = helper_create_commitment(serde_json::to_string(&message).unwrap()); - - assert!(commitment == "d12f3c5b37240bc3abf2976f41fdf9a594f0680aafd2781ac448f80440fbeb99"); + let pcd = Pcd::new("TEST".to_owned()); + let pcd_hash = helper_create_commitment(pcd.to_string()); + let mut key = [0u8; 32]; + key.copy_from_slice(&Vec::from_hex(KEY).unwrap()); + let prd = Prd::new(ALICE_ADDRESS.try_into().unwrap(), key, pcd_hash); + let commitment = helper_create_commitment(serde_json::to_string(&prd).unwrap()); let psbt = create_transaction( &vec![], &HashSet::new(), &alice_wallet, vec![recipient], - Some(Vec::from_hex(COMMITMENT).unwrap()), + Some(commitment.as_byte_array().to_vec()), FEE_RATE, None, )