Flatten trivial errors.

The errors `SegwitV0Error` and `LegacyScripthashError` contained only
one variant - out of range. There will not be a new one in the future so
this change flattens it to simplify.
This commit is contained in:
Martin Habovstiak 2024-01-22 10:30:03 +01:00
parent a4d01d0b6c
commit 3c4f6850f4
2 changed files with 15 additions and 84 deletions

View File

@ -764,7 +764,7 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
script_code: &Script, script_code: &Script,
value: Amount, value: Amount,
sighash_type: EcdsaSighashType, sighash_type: EcdsaSighashType,
) -> Result<(), SigningDataError<SegwitV0Error>> { ) -> Result<(), SigningDataError<transaction::InputsIndexError>> {
let zero_hash = sha256d::Hash::all_zeros(); let zero_hash = sha256d::Hash::all_zeros();
let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag(); let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag();
@ -842,7 +842,7 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
witness_script: &Script, witness_script: &Script,
value: Amount, value: Amount,
sighash_type: EcdsaSighashType, sighash_type: EcdsaSighashType,
) -> Result<SegwitV0Sighash, SegwitV0Error> { ) -> Result<SegwitV0Sighash, transaction::InputsIndexError> {
let mut enc = SegwitV0Sighash::engine(); let mut enc = SegwitV0Sighash::engine();
self.segwit_v0_encode_signing_data_to( self.segwit_v0_encode_signing_data_to(
&mut enc, &mut enc,
@ -882,10 +882,10 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
input_index: usize, input_index: usize,
script_pubkey: &Script, script_pubkey: &Script,
sighash_type: U, sighash_type: U,
) -> EncodeSigningDataResult<SigningDataError<LegacyError>> { ) -> EncodeSigningDataResult<SigningDataError<transaction::InputsIndexError>> {
// Validate input_index. // Validate input_index.
if let Err(e) = self.tx.borrow().tx_in(input_index) { if let Err(e) = self.tx.borrow().tx_in(input_index) {
return EncodeSigningDataResult::WriteResult(Err(SigningDataError::Sighash(e.into()))); return EncodeSigningDataResult::WriteResult(Err(SigningDataError::Sighash(e)));
} }
let sighash_type: u32 = sighash_type.into(); let sighash_type: u32 = sighash_type.into();
@ -1013,7 +1013,7 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
{ {
Ok(true) => Ok(LegacySighash::from_byte_array(UINT256_ONE)), Ok(true) => Ok(LegacySighash::from_byte_array(UINT256_ONE)),
Ok(false) => Ok(LegacySighash::from_engine(engine)), Ok(false) => Ok(LegacySighash::from_engine(engine)),
Err(e) => Err(match e.unwrap_sighash() { LegacyError::InputsIndex(error) => error }), Err(e) => Err(e.unwrap_sighash()),
} }
} }
@ -1206,11 +1206,17 @@ impl From<PrevoutsIndexError> for TaprootError {
#[non_exhaustive] #[non_exhaustive]
pub enum P2wpkhError { pub enum P2wpkhError {
/// Error computing the sighash. /// Error computing the sighash.
Sighash(SegwitV0Error), Sighash(transaction::InputsIndexError),
/// Script is not a witness program for a p2wpkh output. /// Script is not a witness program for a p2wpkh output.
NotP2wpkhScript, NotP2wpkhScript,
} }
impl From<transaction::InputsIndexError> for P2wpkhError {
fn from(value: transaction::InputsIndexError) -> Self {
P2wpkhError::Sighash(value)
}
}
impl fmt::Display for P2wpkhError { impl fmt::Display for P2wpkhError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use P2wpkhError::*; use P2wpkhError::*;
@ -1234,44 +1240,6 @@ impl std::error::Error for P2wpkhError {
} }
} }
impl From<SegwitV0Error> for P2wpkhError {
fn from(e: SegwitV0Error) -> Self { Self::Sighash(e) }
}
/// Error computing the segwit sighash.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SegwitV0Error {
/// Index out of bounds when accessing transaction input vector.
InputsIndex(transaction::InputsIndexError),
}
impl fmt::Display for SegwitV0Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SegwitV0Error::*;
match *self {
InputsIndex(ref e) => write_err!(f, "inputs index"; e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for SegwitV0Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use SegwitV0Error::*;
match *self {
InputsIndex(ref e) => Some(e),
}
}
}
impl From<transaction::InputsIndexError> for SegwitV0Error {
fn from(e: transaction::InputsIndexError) -> Self { Self::InputsIndex(e) }
}
/// Using `SIGHASH_SINGLE` requires an output at the same index as the input. /// Using `SIGHASH_SINGLE` requires an output at the same index as the input.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive] #[non_exhaustive]
@ -1331,39 +1299,6 @@ impl std::error::Error for AnnexError {
} }
} }
/// Error computing the legacy sighash.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LegacyError {
/// Index out of bounds when accessing transaction input vector.
InputsIndex(transaction::InputsIndexError),
}
impl fmt::Display for LegacyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use LegacyError::*;
match *self {
InputsIndex(ref e) => write_err!(f, "inputs index"; e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for LegacyError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use LegacyError::*;
match *self {
InputsIndex(ref e) => Some(e),
}
}
}
impl From<transaction::InputsIndexError> for LegacyError {
fn from(e: transaction::InputsIndexError) -> Self { Self::InputsIndex(e) }
}
fn is_invalid_use_of_sighash_single(sighash: u32, input_index: usize, outputs_len: usize) -> bool { fn is_invalid_use_of_sighash_single(sighash: u32, input_index: usize, outputs_len: usize) -> bool {
let ty = EcdsaSighashType::from_consensus(sighash); let ty = EcdsaSighashType::from_consensus(sighash);
ty == EcdsaSighashType::Single && input_index >= outputs_len ty == EcdsaSighashType::Single && input_index >= outputs_len

View File

@ -23,7 +23,7 @@ use internals::write_err;
use secp256k1::{Message, Secp256k1, Signing}; use secp256k1::{Message, Secp256k1, Signing};
use crate::bip32::{self, KeySource, Xpriv, Xpub}; use crate::bip32::{self, KeySource, Xpriv, Xpub};
use crate::blockdata::transaction::{Transaction, TxOut}; use crate::blockdata::transaction::{self, Transaction, TxOut};
use crate::crypto::ecdsa; use crate::crypto::ecdsa;
use crate::crypto::key::{PrivateKey, PublicKey}; use crate::crypto::key::{PrivateKey, PublicKey};
use crate::prelude::*; use crate::prelude::*;
@ -436,7 +436,7 @@ impl Psbt {
let witness_script = let witness_script =
input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?; input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?;
let sighash = let sighash =
cache.p2wsh_signature_hash(input_index, witness_script, utxo.value, hash_ty)?; cache.p2wsh_signature_hash(input_index, witness_script, utxo.value, hash_ty).map_err(SignError::SegwitV0Sighash)?;
Ok((Message::from_digest(sighash.to_byte_array()), hash_ty)) Ok((Message::from_digest(sighash.to_byte_array()), hash_ty))
} }
Tr => { Tr => {
@ -771,7 +771,7 @@ pub enum SignError {
/// The `scriptPubkey` is not a P2WPKH script. /// The `scriptPubkey` is not a P2WPKH script.
NotWpkh, NotWpkh,
/// Sighash computation error (segwit v0 input). /// Sighash computation error (segwit v0 input).
SegwitV0Sighash(sighash::SegwitV0Error), SegwitV0Sighash(transaction::InputsIndexError),
/// Sighash computation error (p2wpkh input). /// Sighash computation error (p2wpkh input).
P2wpkhSighash(sighash::P2wpkhError), P2wpkhSighash(sighash::P2wpkhError),
/// Unable to determine the output type. /// Unable to determine the output type.
@ -834,10 +834,6 @@ impl std::error::Error for SignError {
} }
} }
impl From<sighash::SegwitV0Error> for SignError {
fn from(e: sighash::SegwitV0Error) -> Self { Self::SegwitV0Sighash(e) }
}
impl From<sighash::P2wpkhError> for SignError { impl From<sighash::P2wpkhError> for SignError {
fn from(e: sighash::P2wpkhError) -> Self { Self::P2wpkhSighash(e) } fn from(e: sighash::P2wpkhError) -> Self { Self::P2wpkhSighash(e) }
} }