Replace upper-lower serde parsing with from_unprefixed_hex
What changed, and why it matters
This commit is a small internal cleanup in the Rust Bitcoin library. It replaces a hand-written string-splitting routine used when reading a 256-bit number from hexadecimal text with a call to an existing library function that does the same thing. There is no indication this fixes a bug or security issue; it is a code-quality refactor.
No security action required. Treat as a normal code-quality refactor. If auditing, verify that from_unprefixed_hex enforces the same length and error behavior as the removed manual parsing.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The U256 serde::Deserialize implementation previously manually sliced a 64-character hex string into two 32-character halves, parsed each half as a u128 via parse_int::hex_u128_unprefixed, and constructed U256(upper, lower). The patch replaces that manual split with U256::from_unprefixed_hex(s), which is the canonical internal helper for the same operation. The change is -6/+2 lines and preserves the same error mapping. No functional or security change is evident from the diff.
Changed components
units/src/pow.rsU256 serde deserializationInspect captured patch +2 / −6
diff --git a/units/src/pow.rs b/units/src/pow.rs
index 3c58b8ed..2442046e 100644
--- a/units/src/pow.rs
+++ b/units/src/pow.rs
@@ -519,12 +519,8 @@ impl<'de> serde::Deserialize<'de> for U256 {
return Err(de::Error::invalid_length(s.len(), &self));
}
- let upper = parse_int::hex_u128_unprefixed(&s[..32])
- .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(s), &self))?;
- let lower = parse_int::hex_u128_unprefixed(&s[32..])
- .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(s), &self))?;
-
- Ok(U256(upper, lower))
+ U256::from_unprefixed_hex(s)
+ .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(s), &self))
}
}
d.deserialize_str(HexVisitor)
Why this scored 18/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.