Merge rust-bitcoin/rust-bitcoin#4503: Fix up script to/from hex

2c0f388108 Fix up script to/from hex (Tobin C. Harding)

Pull request description:

  We (I) have recently done to PRs patching the way we handle converting scripts to and from hex.

  In doing these I made a mess of the deprecation because after both PRs were in I had managed to change the return type and the behaviour of the deprecated function.

  On top of that the docs were either outright wrong or not that clear.

  Props to Kix for doing post merge review and finding my mistakes.

  Close #4498

ACKs for top commit:
  apoelstra:
    ACK 2c0f3881085ba540d517de121d4ba005f9e73a8c; successfully ran local tests

Tree-SHA512: 8bd8590c07efdbfcf113bfcb4b96dc01992c4f215a11e4a1b1f907c8ae9fa47d83c29298bd9b2afc2628b12eb51afd023a48f241a456a0e02a37854b41f6654b
This commit is contained in:
merge-script 2025-05-16 19:14:34 +00:00
commit e619364f5c
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 13 additions and 15 deletions

View File

@ -376,19 +376,19 @@ crate::internal_macros::define_extension_trait! {
fn to_asm_string(&self) -> String { self.to_string() }
/// Consensus encodes the script as lower-case hex.
#[deprecated(since = "TBD", note = "use `to_hex_string_prefixed()` instead")]
fn to_hex_string(&self) -> String { self.to_hex_string_prefixed() }
#[deprecated(since = "TBD", note = "use `to_hex_string_no_length_prefix` instead")]
fn to_hex_string(&self) -> String { self.to_hex_string_no_length_prefix() }
/// Consensus encodes the script as lower-case hex.
///
/// Consensus encoding includes a length prefix. To hex encode without the length prefix use
/// `to_hex_string_no_length_prefix`.
fn to_hex_string_prefixed(&self) -> String { consensus::encode::serialize_hex(self) }
/// Consensus encodes the script as lower-case hex.
/// Encodes the script as lower-case hex.
///
/// This is **not** consensus encoding, you likely want to use `to_hex_string_prefixed`.
///
/// # Returns
///
/// The returned hex string will not include the length prefix.
/// This is **not** consensus encoding. The returned hex string will not include the length
/// prefix. See `to_hex_string_prefixed`.
fn to_hex_string_no_length_prefix(&self) -> String {
self.as_bytes().to_lower_hex_string()
}

View File

@ -35,17 +35,15 @@ crate::internal_macros::define_extension_trait! {
}
/// Constructs a new [`ScriptBuf`] from a hex string.
///
/// The input string is expected to be consensus encoded i.e., includes the length prefix.
#[deprecated(since = "TBD", note = "use `from_hex_string_prefixed()` instead")]
fn from_hex(s: &str) -> Result<ScriptBuf, consensus::FromHexError> {
Self::from_hex_prefixed(s)
#[deprecated(since = "TBD", note = "use `from_hex_string_no_length_prefix()` instead")]
fn from_hex(s: &str) -> Result<ScriptBuf, hex::HexToBytesError> {
Self::from_hex_no_length_prefix(s)
}
/// Constructs a new [`ScriptBuf`] from a hex string.
///
/// This is **not** consensus encoding. If your hex string is a consensus encode script then
/// use `ScriptBuf::from_hex`.
/// This is **not** consensus encoding. If your hex string is a consensus encoded script
/// then use `ScriptBuf::from_hex_prefixed`.
fn from_hex_no_length_prefix(s: &str) -> Result<ScriptBuf, hex::HexToBytesError> {
let v = Vec::from_hex(s)?;
Ok(ScriptBuf::from_bytes(v))