bitcoin: Move sign_message module errors to error submodule
What changed, and why it matters
This commit is a simple internal code cleanup. It moves an error type (MessageSignatureError) from one place in the sign_message module into a new error submodule and re-exports it so users can still access it the same way. No behavior, logic, or security properties changed.
No security action needed. Treat as a normal refactoring/reorganization change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors MessageSignatureError from the message_signing submodule into a new error submodule under sign_message. It updates the re-export path and changes one doc attribute from #[doc(inline)] to #[doc(no_inline)]. The enum variants, trait implementations, and public API surface remain identical. No functional code changes.
Changed components
bitcoin/src/sign_message.rsInspect captured patch +60 / −47
diff --git a/bitcoin/src/sign_message.rs b/bitcoin/src/sign_message.rs
index 7ee75311..da1717f7 100644
--- a/bitcoin/src/sign_message.rs
+++ b/bitcoin/src/sign_message.rs
@@ -14,67 +14,25 @@ use crate::PrivateKey;
#[rustfmt::skip]
#[doc(inline)]
#[cfg(feature = "secp-recovery")]
-pub use self::message_signing::{MessageSignature, MessageSignatureError};
+pub use self::message_signing::MessageSignature;
+#[doc(no_inline)]
+#[cfg(feature = "secp-recovery")]
+pub use self::error::MessageSignatureError;
/// The prefix for signed messages using Bitcoin's message signing protocol.
pub const BITCOIN_SIGNED_MSG_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
#[cfg(feature = "secp-recovery")]
mod message_signing {
- use core::convert::Infallible;
use core::fmt;
use hashes::sha256d;
- use internals::write_err;
use secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
+ use super::error::MessageSignatureError;
use crate::address::{Address, AddressType};
use crate::crypto::key::LegacyPublicKey;
- /// An error used for dealing with Bitcoin Signed Messages.
- #[derive(Debug, Clone, PartialEq, Eq)]
- #[non_exhaustive]
- pub enum MessageSignatureError {
- /// Signature is expected to be 65 bytes.
- InvalidLength,
- /// The signature is invalidly constructed.
- InvalidEncoding(secp256k1::Error),
- /// Invalid base64 encoding.
- InvalidBase64,
- /// Unsupported Address Type
- UnsupportedAddressType(AddressType),
- }
-
- impl From<Infallible> for MessageSignatureError {
- fn from(never: Infallible) -> Self { match never {} }
- }
-
- impl fmt::Display for MessageSignatureError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::InvalidLength => write!(f, "length not 65 bytes"),
- Self::InvalidEncoding(ref e) => write_err!(f, "invalid encoding"; e),
- Self::InvalidBase64 => write!(f, "invalid base64"),
- Self::UnsupportedAddressType(ref address_type) =>
- write!(f, "unsupported address type: {}", address_type),
- }
- }
- }
-
- #[cfg(feature = "std")]
- impl std::error::Error for MessageSignatureError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::InvalidEncoding(ref e) => Some(e),
- Self::InvalidLength | Self::InvalidBase64 | Self::UnsupportedAddressType(_) => None,
- }
- }
- }
-
- impl From<secp256k1::Error> for MessageSignatureError {
- fn from(e: secp256k1::Error) -> Self { Self::InvalidEncoding(e) }
- }
-
/// A signature on a Bitcoin Signed Message.
///
/// In order to use the `to_base64` and `from_base64` methods, as well as the
@@ -216,6 +174,61 @@ pub fn sign(msg: impl AsRef<[u8]>, privkey: &PrivateKey) -> MessageSignature {
privkey.raw_ecdsa_sign_recoverable(msg_to_sign)
}
+/// Error types for message signing.
+#[cfg(feature = "secp-recovery")]
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
+
+ use internals::write_err;
+
+ use crate::address::AddressType;
+
+ /// An error used for dealing with Bitcoin Signed Messages.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum MessageSignatureError {
+ /// Signature is expected to be 65 bytes.
+ InvalidLength,
+ /// The signature is invalidly constructed.
+ InvalidEncoding(secp256k1::Error),
+ /// Invalid base64 encoding.
+ InvalidBase64,
+ /// Unsupported Address Type
+ UnsupportedAddressType(AddressType),
+ }
+
+ impl From<Infallible> for MessageSignatureError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for MessageSignatureError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::InvalidLength => write!(f, "length not 65 bytes"),
+ Self::InvalidEncoding(ref e) => write_err!(f, "invalid encoding"; e),
+ Self::InvalidBase64 => write!(f, "invalid base64"),
+ Self::UnsupportedAddressType(ref address_type) =>
+ write!(f, "unsupported address type: {}", address_type),
+ }
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for MessageSignatureError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::InvalidEncoding(ref e) => Some(e),
+ Self::InvalidLength | Self::InvalidBase64 | Self::UnsupportedAddressType(_) => None,
+ }
+ }
+ }
+
+ impl From<secp256k1::Error> for MessageSignatureError {
+ fn from(e: secp256k1::Error) -> Self { Self::InvalidEncoding(e) }
+ }
+}
+
#[cfg(test)]
mod tests {
use alloc::string::ToString;
Why this scored 15/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.