Browse Source

Convert sighashes to messages non-fallibly

bdk-0.11
Lucas Soriano del Pino 4 years ago
parent
commit
862cf66e66
No known key found for this signature in database GPG Key ID: EE611E973A1530E7
  1. 36
      cfd_protocol/src/lib.rs

36
cfd_protocol/src/lib.rs

@ -112,8 +112,7 @@ pub fn build_cfd_transactions(
taker.lock_amount, taker.lock_amount,
); );
let sighash = let sighash = tx.sighash().to_message();
secp256k1_zkp::Message::from_slice(&tx.sighash()).expect("sighash is valid message");
let sig = SECP256K1.sign(&sighash, &identity_sk); let sig = SECP256K1.sign(&sighash, &identity_sk);
(tx.inner, sig) (tx.inner, sig)
@ -200,7 +199,7 @@ pub fn spending_tx_sighash(
spent_amount.as_sat(), spent_amount.as_sat(),
SigHashType::All, SigHashType::All,
); );
secp256k1_zkp::Message::from_slice(&sighash).expect("sighash is valid message") sighash.to_message()
} }
pub fn finalize_spend_transaction( pub fn finalize_spend_transaction(
@ -293,7 +292,7 @@ pub fn punish_transaction(
tx tx
}; };
let digest = SigHashCache::new(&punish_tx).signature_hash( let sighash = SigHashCache::new(&punish_tx).signature_hash(
0, 0,
&commit_descriptor.script_code(), &commit_descriptor.script_code(),
commit_amount, commit_amount,
@ -309,13 +308,10 @@ pub fn punish_transaction(
key: pk, key: pk,
}; };
let pk_hash = pk.pubkey_hash(); 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 publish_them_pk_hash = publish_them_pk.pubkey_hash();
let sig_publish_other = SECP256K1.sign( let sig_publish_other = SECP256K1.sign(&sighash.to_message(), &publish_them_sk);
&secp256k1_zkp::Message::from_slice(&digest)?,
&publish_them_sk,
);
let revocation_them_pk = PublicKey::from_private_key( let revocation_them_pk = PublicKey::from_private_key(
SECP256K1, SECP256K1,
@ -326,10 +322,7 @@ pub fn punish_transaction(
}, },
); );
let revocation_them_pk_hash = revocation_them_pk.pubkey_hash(); let revocation_them_pk_hash = revocation_them_pk.pubkey_hash();
let sig_revocation_other = SECP256K1.sign( let sig_revocation_other = SECP256K1.sign(&sighash.to_message(), &revocation_them_sk);
&secp256k1_zkp::Message::from_slice(&digest)?,
&revocation_them_sk,
);
satisfier.insert(pk_hash.as_hash(), (pk, (sig_sk, SigHashType::All))); satisfier.insert(pk_hash.as_hash(), (pk, (sig_sk, SigHashType::All)));
@ -589,7 +582,7 @@ impl ContractExecutionTransaction {
Ok(EcdsaAdaptorSignature::encrypt( Ok(EcdsaAdaptorSignature::encrypt(
SECP256K1, SECP256K1,
&secp256k1_zkp::Message::from_slice(&self.sighash).expect("sighash is valid message"), &self.sighash.to_message(),
&sk, &sk,
&signature_point, &signature_point,
)) ))
@ -736,7 +729,7 @@ impl CommitTransaction {
fn encsign(&self, sk: SecretKey, publish_them_pk: &PublicKey) -> EcdsaAdaptorSignature { fn encsign(&self, sk: SecretKey, publish_them_pk: &PublicKey) -> EcdsaAdaptorSignature {
EcdsaAdaptorSignature::encrypt( EcdsaAdaptorSignature::encrypt(
SECP256K1, SECP256K1,
&secp256k1_zkp::Message::from_slice(&self.sighash).expect("sighash is valid message"), &self.sighash.to_message(),
&sk, &sk,
&publish_them_pk.key, &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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

Loading…
Cancel
Save