Move and deprecate script_find_and_remove
Done as part of flattening the `util` module. We have a function in `util::misc` that operates on scripts, it is an implementation of `FindAndDelete` from Bitcoin Core and is primarily useful for supporting `CODESEPARATOR`, which we do not support. Move the public `script_find_and_remove` function out of `util/misc.rs` and into `util/mod.rs`, delete the testing and deprecate the function.
This commit is contained in:
parent
aeacbe763d
commit
041d6a8097
|
@ -86,6 +86,7 @@ pub mod consensus;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod hash_types;
|
pub mod hash_types;
|
||||||
pub mod policy;
|
pub mod policy;
|
||||||
|
pub mod sign_message;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
||||||
// SPDX-License-Identifier: CC0-1.0
|
// SPDX-License-Identifier: CC0-1.0
|
||||||
|
|
||||||
//! Miscellaneous functions.
|
//! Signature
|
||||||
//!
|
//!
|
||||||
//! This module provides various utility functions including secp256k1 signature
|
//! This module provides signature related functions including secp256k1 signature recovery when
|
||||||
//! recovery when library is used with the `secp-recovery` feature.
|
//! library is used with the `secp-recovery` feature.
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use crate::prelude::*;
|
|
||||||
|
|
||||||
use crate::hashes::{sha256d, Hash, HashEngine};
|
use crate::hashes::{sha256d, Hash, HashEngine};
|
||||||
|
|
||||||
use crate::blockdata::opcodes;
|
|
||||||
use crate::consensus::{encode, Encodable};
|
use crate::consensus::{encode, Encodable};
|
||||||
|
|
||||||
#[cfg(feature = "secp-recovery")]
|
#[cfg(feature = "secp-recovery")]
|
||||||
|
@ -205,47 +202,6 @@ mod message_signing {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search for `needle` in the vector `haystack` and remove every
|
|
||||||
/// instance of it, returning the number of instances removed.
|
|
||||||
/// Loops through the vector opcode by opcode, skipping pushed data.
|
|
||||||
pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
|
|
||||||
if needle.len() > haystack.len() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if needle.is_empty() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut top = haystack.len() - needle.len();
|
|
||||||
let mut n_deleted = 0;
|
|
||||||
|
|
||||||
let mut i = 0;
|
|
||||||
while i <= top {
|
|
||||||
if &haystack[i..(i + needle.len())] == needle {
|
|
||||||
for j in i..top {
|
|
||||||
haystack.swap(j + needle.len(), j);
|
|
||||||
}
|
|
||||||
n_deleted += 1;
|
|
||||||
// This is ugly but prevents infinite loop in case of overflow
|
|
||||||
let overflow = top < needle.len();
|
|
||||||
top = top.wrapping_sub(needle.len());
|
|
||||||
if overflow {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
i += match opcodes::All::from((*haystack)[i]).classify(opcodes::ClassifyContext::Legacy) {
|
|
||||||
opcodes::Class::PushBytes(n) => n as usize + 1,
|
|
||||||
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA1) => 2,
|
|
||||||
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA2) => 3,
|
|
||||||
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA4) => 5,
|
|
||||||
_ => 1
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
haystack.truncate(top.wrapping_add(needle.len()));
|
|
||||||
n_deleted
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Hash message for signature using Bitcoin's message signing format.
|
/// Hash message for signature using Bitcoin's message signing format.
|
||||||
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
|
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
|
||||||
let mut engine = sha256d::Hash::engine();
|
let mut engine = sha256d::Hash::engine();
|
||||||
|
@ -260,47 +216,6 @@ pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::hashes::hex::ToHex;
|
use crate::hashes::hex::ToHex;
|
||||||
use super::script_find_and_remove;
|
|
||||||
use super::signed_msg_hash;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_script_find_and_remove() {
|
|
||||||
let mut v = vec![101u8, 102, 103, 104, 102, 103, 104, 102, 103, 104, 105, 106, 107, 108, 109];
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[]), 0);
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 105]), 0);
|
|
||||||
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103, 104, 105, 106, 107, 108, 109]);
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[105, 106, 107]), 1);
|
|
||||||
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103, 104, 108, 109]);
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[104, 108, 109]), 1);
|
|
||||||
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103]);
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[101]), 1);
|
|
||||||
assert_eq!(v, vec![102, 103, 104, 102, 103, 104, 102, 103]);
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[102]), 3);
|
|
||||||
assert_eq!(v, vec![103, 104, 103, 104, 103]);
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[103, 104]), 2);
|
|
||||||
assert_eq!(v, vec![103]);
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0);
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[105]), 0);
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[103]), 1);
|
|
||||||
assert_eq!(v, Vec::<u8>::new());
|
|
||||||
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0);
|
|
||||||
assert_eq!(script_find_and_remove(&mut v, &[105]), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_script_codesep_remove() {
|
|
||||||
let mut s = vec![33u8, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 171, 33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 171, 81];
|
|
||||||
assert_eq!(script_find_and_remove(&mut s, &[171]), 2);
|
|
||||||
assert_eq!(s, vec![33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 81]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_signed_msg_hash() {
|
fn test_signed_msg_hash() {
|
|
@ -15,7 +15,6 @@ pub mod bip32;
|
||||||
pub mod bip152;
|
pub mod bip152;
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
pub mod merkleblock;
|
pub mod merkleblock;
|
||||||
pub mod misc;
|
|
||||||
pub mod psbt;
|
pub mod psbt;
|
||||||
pub mod taproot;
|
pub mod taproot;
|
||||||
pub mod uint;
|
pub mod uint;
|
||||||
|
@ -119,3 +118,53 @@ pub mod address {
|
||||||
|
|
||||||
#[deprecated(since = "0.30.0", note = "Please use crate::bip158")]
|
#[deprecated(since = "0.30.0", note = "Please use crate::bip158")]
|
||||||
pub use crate::bip158;
|
pub use crate::bip158;
|
||||||
|
|
||||||
|
/// The `misc` module was moved and re-named to `sign_message`.
|
||||||
|
pub mod misc {
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
/// Search for `needle` in the vector `haystack` and remove every
|
||||||
|
/// instance of it, returning the number of instances removed.
|
||||||
|
/// Loops through the vector opcode by opcode, skipping pushed data.
|
||||||
|
// For why we deprecated see: https://github.com/rust-bitcoin/rust-bitcoin/pull/1259#discussion_r968613736
|
||||||
|
#[deprecated(since = "0.30.0", note = "No longer supported")]
|
||||||
|
pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
|
||||||
|
use crate::blockdata::opcodes;
|
||||||
|
|
||||||
|
if needle.len() > haystack.len() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if needle.is_empty() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut top = haystack.len() - needle.len();
|
||||||
|
let mut n_deleted = 0;
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
|
while i <= top {
|
||||||
|
if &haystack[i..(i + needle.len())] == needle {
|
||||||
|
for j in i..top {
|
||||||
|
haystack.swap(j + needle.len(), j);
|
||||||
|
}
|
||||||
|
n_deleted += 1;
|
||||||
|
// This is ugly but prevents infinite loop in case of overflow
|
||||||
|
let overflow = top < needle.len();
|
||||||
|
top = top.wrapping_sub(needle.len());
|
||||||
|
if overflow {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
i += match opcodes::All::from((*haystack)[i]).classify(opcodes::ClassifyContext::Legacy) {
|
||||||
|
opcodes::Class::PushBytes(n) => n as usize + 1,
|
||||||
|
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA1) => 2,
|
||||||
|
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA2) => 3,
|
||||||
|
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA4) => 5,
|
||||||
|
_ => 1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
haystack.truncate(top.wrapping_add(needle.len()));
|
||||||
|
n_deleted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue