Deprecate the to_hex implementation on Work and Target
What changed, and why it matters
This commit is a routine API cleanup, not a security fix. It marks the `to_hex()` methods on two Bitcoin proof-of-work types (`Work` and `Target`) as deprecated, telling developers to use Rust's standard `format!("{var:x}")` instead. No vulnerability is present or fixed.
No security action required. Developers using these APIs should plan to migrate to `format!("{value:x}")` before the deprecated methods are removed in a future release.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds #[deprecated(...)] attributes to to_hex() on the WorkExt and Target extension traits in bitcoin/src/pow.rs, and moves the implementations to use format!("{self:x}"). It also adds an alloc::string::String import needed by the new code. The underlying impl_to_hex_from_lower_hex! macro remains in place. This is purely a deprecation/API-consistency change; it does not alter behavior, fix a bug, or address a security issue.
Changed components
bitcoin/src/pow.rsWorkExt::to_hexTarget::to_hexInspect captured patch +11 / −0
diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs
index 94839a85..8236c7d4 100644
--- a/bitcoin/src/pow.rs
+++ b/bitcoin/src/pow.rs
@@ -5,6 +5,7 @@
//! Provides the [`Work`] and [`Target`] types that are used in proof-of-work calculations. The
//! functions here are designed to be fast, by that we mean it is safe to use them to check headers.
+use alloc::string::String;
use core::ops::{Add, Div, Mul, Not, Rem, Shl, Shr, Sub};
use core::{cmp, fmt};
@@ -151,10 +152,16 @@ pub trait WorkExt {
/// `log2_work` output in its logs.
#[cfg(feature = "std")]
fn log2(self) -> f64;
+
+ /// Gets the hex representation of the [`Work`] value as a [`String`].
+ #[deprecated(since = "0.33.0", note = "use `format!(\"{var:x}\")` instead")]
+ fn to_hex(&self) -> String;
}
impl WorkExt for Work {
#[cfg(feature = "std")]
fn log2(self) -> f64 { self.0.to_f64().log2() }
+
+ fn to_hex(&self) -> String { format!("{self:x}") }
}
impl_to_hex_from_lower_hex!(Work, |_| 64);
@@ -418,6 +425,10 @@ internal_macros::define_extension_trait! {
/// The return value should be checked against [`Params::max_attainable_target`] or use one of
/// the `Target::MAX_ATTAINABLE_FOO` constants.
fn max_transition_threshold_unchecked(&self) -> Self { Self(self.0 << 2) }
+
+ /// Gets the hex representation of the [`Target`] value as a [`String`].
+ #[deprecated(since = "0.33.0", note = "use `format!(\"{var:x}\")` instead")]
+ fn to_hex(&self) -> String { format!("{self:x}") }
}
}
impl_to_hex_from_lower_hex!(Target, |_| 64);
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.