bitcoin: Move script module errors to error submodule
What changed, and why it matters
This commit is a routine code cleanup: it moves error type definitions for Bitcoin scripts into a dedicated 'error' submodule and re-exports them so existing code keeps working. There is no change to how the library behaves, no bug fix, and no security-relevant change.
No security action needed. Review as normal refactoring if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors bitcoin/src/blockdata/script/mod.rs by relocating the Error enum and related error re-exports into a new error submodule. It preserves the public API via re-exports (pub use self::error::{Error, PushBytesError, ...}) and does not alter error variants, logic, or behavior. Imports are adjusted accordingly. This is purely an organizational/documentation consistency change.
Changed components
bitcoin/src/blockdata/script/mod.rsInspect captured patch +68 / −51
diff --git a/bitcoin/src/blockdata/script/mod.rs b/bitcoin/src/blockdata/script/mod.rs
index cb798303..c8a931d5 100644
--- a/bitcoin/src/blockdata/script/mod.rs
+++ b/bitcoin/src/blockdata/script/mod.rs
@@ -55,9 +55,6 @@ mod tests;
pub mod witness_program;
pub mod witness_version;
-use core::convert::Infallible;
-use core::fmt;
-
use io::{BufRead, Write};
use self::witness_version::WitnessVersion;
@@ -67,7 +64,6 @@ use crate::key::WPubkeyHash;
use crate::opcodes::all::*;
use crate::opcodes::Opcode;
use crate::prelude::Vec;
-use crate::OutPoint;
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
@@ -76,20 +72,24 @@ pub use self::{
builder::Builder,
instruction::{Instruction, Instructions, InstructionIndices},
owned::{ScriptBufExt, ScriptPubKeyBufExt},
- push_bytes::{PushBytes, PushBytesBuf, PushBytesError, PushBytesErrorReport, ScriptIntError},
+ push_bytes::{PushBytes, PushBytesBuf, PushBytesErrorReport},
};
#[doc(no_inline)]
pub use primitives::script::ScriptBufDecoderError;
#[doc(inline)]
pub use primitives::script::{
- error, RedeemScript, RedeemScriptBuf, RedeemScriptSizeError, RedeemScriptTag, Script,
- ScriptBuf, ScriptBufDecoder, ScriptEncoder, ScriptHash, ScriptHashableTag, ScriptPubKey,
- ScriptPubKeyBuf, ScriptPubKeyTag, ScriptSig, ScriptSigBuf, ScriptSigTag, SignetBlockScript,
- SignetBlockScriptBuf, SignetBlockScriptTag, Tag, TapScript, TapScriptBuf, TapScriptTag,
- WScriptHash, WitnessScript, WitnessScriptBuf, WitnessScriptSizeError, WitnessScriptTag,
+ RedeemScript, RedeemScriptBuf, RedeemScriptTag, Script, ScriptBuf, ScriptBufDecoder,
+ ScriptEncoder, ScriptHash, ScriptHashableTag, ScriptPubKey, ScriptPubKeyBuf, ScriptPubKeyTag,
+ ScriptSig, ScriptSigBuf, ScriptSigTag, SignetBlockScript, SignetBlockScriptBuf,
+ SignetBlockScriptTag, Tag, TapScript, TapScriptBuf, TapScriptTag, WScriptHash, WitnessScript,
+ WitnessScriptBuf, WitnessScriptTag,
};
pub(crate) use self::borrowed::ScriptExtPriv;
+#[doc(no_inline)]
+pub use self::error::{
+ Error, PushBytesError, RedeemScriptSizeError, ScriptIntError, WitnessScriptSizeError,
+};
pub(crate) use self::owned::ScriptBufExtPriv;
impl_asref_push_bytes!(ScriptHash, WScriptHash);
@@ -241,52 +241,69 @@ impl<T> Decodable for ScriptBuf<T> {
}
}
-/// Ways that a script might fail. Not everything is split up as
-/// much as it could be; patches welcome if more detailed errors
-/// would help you.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum Error {
- /// Something did a non-minimal push; for more information see
- /// <https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#push-operators>
- NonMinimalPush,
- /// Some opcode expected a parameter but it was missing or truncated.
- EarlyEndOfScript,
- /// Tried to read an array off the stack as a number when it was more than 4 bytes.
- NumericOverflow,
- /// Cannot find the spent output.
- UnknownSpentOutput(OutPoint),
- /// Cannot serialize the spending transaction.
- Serialization,
-}
+/// Error types for Bitcoin scripts.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
-impl From<Infallible> for Error {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ use crate::OutPoint;
+
+ #[rustfmt::skip] // Keep public re-exports separate.
+ #[doc(inline)]
+ pub use super::push_bytes::{PushBytesError, ScriptIntError};
+ #[doc(no_inline)]
+ pub use primitives::script::error::{
+ FromHexError, RedeemScriptSizeError, ScriptBufDecoderError, WitnessScriptSizeError,
+ };
-impl fmt::Display for Error {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::NonMinimalPush => f.write_str("non-minimal datapush"),
- Self::EarlyEndOfScript => f.write_str("unexpected end of script"),
- Self::NumericOverflow =>
- f.write_str("numeric overflow (number on stack larger than 4 bytes)"),
- Self::UnknownSpentOutput(ref point) => write!(f, "unknown spent output: {}", point),
- Self::Serialization =>
- f.write_str("can not serialize the spending transaction in Transaction::verify()"),
+ /// Ways that a script might fail. Not everything is split up as
+ /// much as it could be; patches welcome if more detailed errors
+ /// would help you.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum Error {
+ /// Something did a non-minimal push; for more information see
+ /// <https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#push-operators>
+ NonMinimalPush,
+ /// Some opcode expected a parameter but it was missing or truncated.
+ EarlyEndOfScript,
+ /// Tried to read an array off the stack as a number when it was more than 4 bytes.
+ NumericOverflow,
+ /// Cannot find the spent output.
+ UnknownSpentOutput(OutPoint),
+ /// Cannot serialize the spending transaction.
+ Serialization,
+ }
+
+ impl From<Infallible> for Error {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::NonMinimalPush => f.write_str("non-minimal datapush"),
+ Self::EarlyEndOfScript => f.write_str("unexpected end of script"),
+ Self::NumericOverflow =>
+ f.write_str("numeric overflow (number on stack larger than 4 bytes)"),
+ Self::UnknownSpentOutput(ref point) => write!(f, "unknown spent output: {}", point),
+ Self::Serialization => f.write_str(
+ "can not serialize the spending transaction in Transaction::verify()",
+ ),
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for Error {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::NonMinimalPush
- | Self::EarlyEndOfScript
- | Self::NumericOverflow
- | Self::UnknownSpentOutput(_)
- | Self::Serialization => None,
+ #[cfg(feature = "std")]
+ impl std::error::Error for Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::NonMinimalPush
+ | Self::EarlyEndOfScript
+ | Self::NumericOverflow
+ | Self::UnknownSpentOutput(_)
+ | Self::Serialization => None,
+ }
}
}
}
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.