From 3c4f6850f4d985747105eb988969999b3163f0d2 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Mon, 22 Jan 2024 10:30:03 +0100 Subject: [PATCH] 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. --- bitcoin/src/crypto/sighash.rs | 89 +++++------------------------------ bitcoin/src/psbt/mod.rs | 10 ++-- 2 files changed, 15 insertions(+), 84 deletions(-) diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs index 05da7ef5..de6170ff 100644 --- a/bitcoin/src/crypto/sighash.rs +++ b/bitcoin/src/crypto/sighash.rs @@ -764,7 +764,7 @@ impl> SighashCache { script_code: &Script, value: Amount, sighash_type: EcdsaSighashType, - ) -> Result<(), SigningDataError> { + ) -> Result<(), SigningDataError> { let zero_hash = sha256d::Hash::all_zeros(); let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag(); @@ -842,7 +842,7 @@ impl> SighashCache { witness_script: &Script, value: Amount, sighash_type: EcdsaSighashType, - ) -> Result { + ) -> Result { let mut enc = SegwitV0Sighash::engine(); self.segwit_v0_encode_signing_data_to( &mut enc, @@ -882,10 +882,10 @@ impl> SighashCache { input_index: usize, script_pubkey: &Script, sighash_type: U, - ) -> EncodeSigningDataResult> { + ) -> EncodeSigningDataResult> { // Validate 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(); @@ -1013,7 +1013,7 @@ impl> SighashCache { { Ok(true) => Ok(LegacySighash::from_byte_array(UINT256_ONE)), 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 for TaprootError { #[non_exhaustive] pub enum P2wpkhError { /// Error computing the sighash. - Sighash(SegwitV0Error), + Sighash(transaction::InputsIndexError), /// Script is not a witness program for a p2wpkh output. NotP2wpkhScript, } +impl From for P2wpkhError { + fn from(value: transaction::InputsIndexError) -> Self { + P2wpkhError::Sighash(value) + } +} + impl fmt::Display for P2wpkhError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use P2wpkhError::*; @@ -1234,44 +1240,6 @@ impl std::error::Error for P2wpkhError { } } -impl From 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 for SegwitV0Error { - fn from(e: transaction::InputsIndexError) -> Self { Self::InputsIndex(e) } -} - /// Using `SIGHASH_SINGLE` requires an output at the same index as the input. #[derive(Debug, Clone, PartialEq, Eq)] #[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 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 { let ty = EcdsaSighashType::from_consensus(sighash); ty == EcdsaSighashType::Single && input_index >= outputs_len diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 26ed4993..25a428df 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -23,7 +23,7 @@ use internals::write_err; use secp256k1::{Message, Secp256k1, Signing}; 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::key::{PrivateKey, PublicKey}; use crate::prelude::*; @@ -436,7 +436,7 @@ impl Psbt { let witness_script = input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?; 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)) } Tr => { @@ -771,7 +771,7 @@ pub enum SignError { /// The `scriptPubkey` is not a P2WPKH script. NotWpkh, /// Sighash computation error (segwit v0 input). - SegwitV0Sighash(sighash::SegwitV0Error), + SegwitV0Sighash(transaction::InputsIndexError), /// Sighash computation error (p2wpkh input). P2wpkhSighash(sighash::P2wpkhError), /// Unable to determine the output type. @@ -834,10 +834,6 @@ impl std::error::Error for SignError { } } -impl From for SignError { - fn from(e: sighash::SegwitV0Error) -> Self { Self::SegwitV0Sighash(e) } -} - impl From for SignError { fn from(e: sighash::P2wpkhError) -> Self { Self::P2wpkhSighash(e) } }