refactor(signer): Remove trait ComputeSighash

This commit is contained in:
valued mammal 2024-05-01 14:47:20 -04:00
parent 6dab68d35b
commit f2a2dae84c
No known key found for this signature in database

View File

@ -99,7 +99,7 @@ use miniscript::descriptor::{
Descriptor, DescriptorMultiXKey, DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey, Descriptor, DescriptorMultiXKey, DescriptorPublicKey, DescriptorSecretKey, DescriptorXKey,
InnerXKey, KeyMap, SinglePriv, SinglePubKey, InnerXKey, KeyMap, SinglePriv, SinglePubKey,
}; };
use miniscript::{Legacy, Segwitv0, SigType, Tap, ToPublicKey}; use miniscript::{SigType, ToPublicKey};
use super::utils::SecpCtx; use super::utils::SecpCtx;
use crate::descriptor::{DescriptorMeta, XKeyUtils}; use crate::descriptor::{DescriptorMeta, XKeyUtils};
@ -481,7 +481,7 @@ impl InputSigner for SignerWrapper<PrivateKey> {
&& sign_options.sign_with_tap_internal_key && sign_options.sign_with_tap_internal_key
&& x_only_pubkey == psbt_internal_key && x_only_pubkey == psbt_internal_key
{ {
let (hash, hash_ty) = Tap::sighash(psbt, input_index, None)?; let (hash, hash_ty) = compute_tap_sighash(psbt, input_index, None)?;
sign_psbt_schnorr( sign_psbt_schnorr(
&self.inner, &self.inner,
x_only_pubkey, x_only_pubkey,
@ -516,7 +516,7 @@ impl InputSigner for SignerWrapper<PrivateKey> {
.cloned() .cloned()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for lh in leaf_hashes { for lh in leaf_hashes {
let (hash, hash_ty) = Tap::sighash(psbt, input_index, Some(lh))?; let (hash, hash_ty) = compute_tap_sighash(psbt, input_index, Some(lh))?;
sign_psbt_schnorr( sign_psbt_schnorr(
&self.inner, &self.inner,
x_only_pubkey, x_only_pubkey,
@ -538,12 +538,12 @@ impl InputSigner for SignerWrapper<PrivateKey> {
let (hash, hash_ty) = match self.ctx { let (hash, hash_ty) = match self.ctx {
SignerContext::Segwitv0 => { SignerContext::Segwitv0 => {
let (h, t) = Segwitv0::sighash(psbt, input_index, ())?; let (h, t) = compute_segwitv0_sighash(psbt, input_index)?;
let h = h.to_raw_hash(); let h = h.to_raw_hash();
(h, t) (h, t)
} }
SignerContext::Legacy => { SignerContext::Legacy => {
let (h, t) = Legacy::sighash(psbt, input_index, ())?; let (h, t) = compute_legacy_sighash(psbt, input_index)?;
let h = h.to_raw_hash(); let h = h.to_raw_hash();
(h, t) (h, t)
} }
@ -853,28 +853,11 @@ impl Default for SignOptions {
} }
} }
pub(crate) trait ComputeSighash { /// Computes the legacy sighash.
type Extra; fn compute_legacy_sighash(
type Sighash;
type SighashType;
fn sighash(
psbt: &Psbt, psbt: &Psbt,
input_index: usize, input_index: usize,
extra: Self::Extra, ) -> Result<(sighash::LegacySighash, EcdsaSighashType), SignerError> {
) -> Result<(Self::Sighash, Self::SighashType), SignerError>;
}
impl ComputeSighash for Legacy {
type Extra = ();
type Sighash = sighash::LegacySighash;
type SighashType = EcdsaSighashType;
fn sighash(
psbt: &Psbt,
input_index: usize,
_extra: (),
) -> Result<(Self::Sighash, Self::SighashType), SignerError> {
if input_index >= psbt.inputs.len() || input_index >= psbt.unsigned_tx.input.len() { if input_index >= psbt.inputs.len() || input_index >= psbt.unsigned_tx.input.len() {
return Err(SignerError::InputIndexOutOfRange); return Err(SignerError::InputIndexOutOfRange);
} }
@ -882,11 +865,12 @@ impl ComputeSighash for Legacy {
let psbt_input = &psbt.inputs[input_index]; let psbt_input = &psbt.inputs[input_index];
let tx_input = &psbt.unsigned_tx.input[input_index]; let tx_input = &psbt.unsigned_tx.input[input_index];
let sighash = psbt_input let sighash_type = psbt_input
.sighash_type .sighash_type
.unwrap_or_else(|| EcdsaSighashType::All.into()) .unwrap_or_else(|| EcdsaSighashType::All.into())
.ecdsa_hash_ty() .ecdsa_hash_ty()
.map_err(|_| SignerError::InvalidSighash)?; .map_err(|_| SignerError::InvalidSighash)?;
let script = match psbt_input.redeem_script { let script = match psbt_input.redeem_script {
Some(ref redeem_script) => redeem_script.clone(), Some(ref redeem_script) => redeem_script.clone(),
None => { None => {
@ -907,23 +891,17 @@ impl ComputeSighash for Legacy {
sighash::SighashCache::new(&psbt.unsigned_tx).legacy_signature_hash( sighash::SighashCache::new(&psbt.unsigned_tx).legacy_signature_hash(
input_index, input_index,
&script, &script,
sighash.to_u32(), sighash_type.to_u32(),
)?, )?,
sighash, sighash_type,
)) ))
}
} }
impl ComputeSighash for Segwitv0 { /// Computes the segwitv0 sighash.
type Extra = (); fn compute_segwitv0_sighash(
type Sighash = sighash::SegwitV0Sighash;
type SighashType = EcdsaSighashType;
fn sighash(
psbt: &Psbt, psbt: &Psbt,
input_index: usize, input_index: usize,
_extra: (), ) -> Result<(sighash::SegwitV0Sighash, EcdsaSighashType), SignerError> {
) -> Result<(Self::Sighash, Self::SighashType), SignerError> {
if input_index >= psbt.inputs.len() || input_index >= psbt.unsigned_tx.input.len() { if input_index >= psbt.inputs.len() || input_index >= psbt.unsigned_tx.input.len() {
return Err(SignerError::InputIndexOutOfRange); return Err(SignerError::InputIndexOutOfRange);
} }
@ -980,31 +958,21 @@ impl ComputeSighash for Segwitv0 {
.unwrap_or(false) .unwrap_or(false)
{ {
let script_pubkey = psbt_input.redeem_script.as_ref().unwrap(); let script_pubkey = psbt_input.redeem_script.as_ref().unwrap();
sighasher.p2wpkh_signature_hash( sighasher.p2wpkh_signature_hash(input_index, script_pubkey, value, sighash_type)?
input_index,
script_pubkey,
value,
sighash_type,
)?
} else { } else {
return Err(SignerError::MissingWitnessScript); return Err(SignerError::MissingWitnessScript);
} }
} }
}; };
Ok((sighash, sighash_type)) Ok((sighash, sighash_type))
}
} }
impl ComputeSighash for Tap { /// Computes the taproot sighash.
type Extra = Option<taproot::TapLeafHash>; fn compute_tap_sighash(
type Sighash = TapSighash;
type SighashType = TapSighashType;
fn sighash(
psbt: &Psbt, psbt: &Psbt,
input_index: usize, input_index: usize,
extra: Self::Extra, extra: Option<taproot::TapLeafHash>,
) -> Result<(Self::Sighash, TapSighashType), SignerError> { ) -> Result<(sighash::TapSighash, TapSighashType), SignerError> {
if input_index >= psbt.inputs.len() || input_index >= psbt.unsigned_tx.input.len() { if input_index >= psbt.inputs.len() || input_index >= psbt.unsigned_tx.input.len() {
return Err(SignerError::InputIndexOutOfRange); return Err(SignerError::InputIndexOutOfRange);
} }
@ -1044,7 +1012,6 @@ impl ComputeSighash for Tap {
cache.taproot_signature_hash(input_index, &prevouts, None, extra, sighash_type)?, cache.taproot_signature_hash(input_index, &prevouts, None, extra, sighash_type)?,
sighash_type, sighash_type,
)) ))
}
} }
impl PartialOrd for SignersContainerKey { impl PartialOrd for SignersContainerKey {