diff --git a/cfd_protocol/src/lib.rs b/cfd_protocol/src/lib.rs index 3f68a9c..2901743 100644 --- a/cfd_protocol/src/lib.rs +++ b/cfd_protocol/src/lib.rs @@ -112,8 +112,7 @@ pub fn build_cfd_transactions( taker.lock_amount, ); - let sighash = - secp256k1_zkp::Message::from_slice(&tx.sighash()).expect("sighash is valid message"); + let sighash = tx.sighash().to_message(); let sig = SECP256K1.sign(&sighash, &identity_sk); (tx.inner, sig) @@ -200,7 +199,7 @@ pub fn spending_tx_sighash( spent_amount.as_sat(), SigHashType::All, ); - secp256k1_zkp::Message::from_slice(&sighash).expect("sighash is valid message") + sighash.to_message() } pub fn finalize_spend_transaction( @@ -293,7 +292,7 @@ pub fn punish_transaction( tx }; - let digest = SigHashCache::new(&punish_tx).signature_hash( + let sighash = SigHashCache::new(&punish_tx).signature_hash( 0, &commit_descriptor.script_code(), commit_amount, @@ -309,13 +308,10 @@ pub fn punish_transaction( key: pk, }; let pk_hash = pk.pubkey_hash(); - let sig_sk = SECP256K1.sign(&secp256k1_zkp::Message::from_slice(&digest)?, &sk); + let sig_sk = SECP256K1.sign(&sighash.to_message(), &sk); let publish_them_pk_hash = publish_them_pk.pubkey_hash(); - let sig_publish_other = SECP256K1.sign( - &secp256k1_zkp::Message::from_slice(&digest)?, - &publish_them_sk, - ); + let sig_publish_other = SECP256K1.sign(&sighash.to_message(), &publish_them_sk); let revocation_them_pk = PublicKey::from_private_key( SECP256K1, @@ -326,10 +322,7 @@ pub fn punish_transaction( }, ); let revocation_them_pk_hash = revocation_them_pk.pubkey_hash(); - let sig_revocation_other = SECP256K1.sign( - &secp256k1_zkp::Message::from_slice(&digest)?, - &revocation_them_sk, - ); + let sig_revocation_other = SECP256K1.sign(&sighash.to_message(), &revocation_them_sk); satisfier.insert(pk_hash.as_hash(), (pk, (sig_sk, SigHashType::All))); @@ -589,7 +582,7 @@ impl ContractExecutionTransaction { Ok(EcdsaAdaptorSignature::encrypt( SECP256K1, - &secp256k1_zkp::Message::from_slice(&self.sighash).expect("sighash is valid message"), + &self.sighash.to_message(), &sk, &signature_point, )) @@ -736,7 +729,7 @@ impl CommitTransaction { fn encsign(&self, sk: SecretKey, publish_them_pk: &PublicKey) -> EcdsaAdaptorSignature { EcdsaAdaptorSignature::encrypt( SECP256K1, - &secp256k1_zkp::Message::from_slice(&self.sighash).expect("sighash is valid message"), + &self.sighash.to_message(), &sk, &publish_them_pk.key, ) @@ -875,6 +868,19 @@ impl TransactionExt for bitcoin::Transaction { } } +trait SigHashExt { + fn to_message(self) -> secp256k1_zkp::Message; +} + +impl SigHashExt for SigHash { + fn to_message(self) -> secp256k1_zkp::Message { + use secp256k1_zkp::bitcoin_hashes::Hash; + let hash = secp256k1_zkp::bitcoin_hashes::sha256d::Hash::from_inner(*self.as_inner()); + + hash.into() + } +} + #[cfg(test)] mod tests { use super::*;