Run cargo fmt
Run the formatter and commit only the changes to `owned`, no other changes.
This commit is contained in:
parent
3fdc574851
commit
ae0a5bd64a
|
@ -91,137 +91,137 @@ impl ScriptBuf {
|
||||||
|
|
||||||
mod tmp_pub {
|
mod tmp_pub {
|
||||||
use super::*;
|
use super::*;
|
||||||
impl ScriptBuf {
|
impl ScriptBuf {
|
||||||
/// Creates a new script builder
|
/// Creates a new script builder
|
||||||
pub fn builder() -> Builder { Builder::new() }
|
pub fn builder() -> Builder { Builder::new() }
|
||||||
|
|
||||||
/// Generates OP_RETURN-type of scriptPubkey for the given data.
|
/// Generates OP_RETURN-type of scriptPubkey for the given data.
|
||||||
pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
|
pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
|
||||||
Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()
|
Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a [`ScriptBuf`] from a hex string.
|
|
||||||
pub fn from_hex(s: &str) -> Result<Self, hex::HexToBytesError> {
|
|
||||||
let v = Vec::from_hex(s)?;
|
|
||||||
Ok(ScriptBuf::from_bytes(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds a single opcode to the script.
|
|
||||||
pub fn push_opcode(&mut self, data: Opcode) { self.as_byte_vec().push(data.to_u8()); }
|
|
||||||
|
|
||||||
/// Adds instructions to push some arbitrary data onto the stack.
|
|
||||||
pub fn push_slice<T: AsRef<PushBytes>>(&mut self, data: T) {
|
|
||||||
let data = data.as_ref();
|
|
||||||
self.reserve(Self::reserved_len_for_slice(data.len()));
|
|
||||||
self.push_slice_no_opt(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add a single instruction to the script.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// The method panics if the instruction is a data push with length greater or equal to
|
|
||||||
/// 0x100000000.
|
|
||||||
pub fn push_instruction(&mut self, instruction: Instruction<'_>) {
|
|
||||||
match instruction {
|
|
||||||
Instruction::Op(opcode) => self.push_opcode(opcode),
|
|
||||||
Instruction::PushBytes(bytes) => self.push_slice(bytes),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Like push_instruction, but avoids calling `reserve` to not re-check the length.
|
/// Creates a [`ScriptBuf`] from a hex string.
|
||||||
pub fn push_instruction_no_opt(&mut self, instruction: Instruction<'_>) {
|
pub fn from_hex(s: &str) -> Result<Self, hex::HexToBytesError> {
|
||||||
match instruction {
|
let v = Vec::from_hex(s)?;
|
||||||
Instruction::Op(opcode) => self.push_opcode(opcode),
|
Ok(ScriptBuf::from_bytes(v))
|
||||||
Instruction::PushBytes(bytes) => self.push_slice_no_opt(bytes),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds an `OP_VERIFY` to the script or replaces the last opcode with VERIFY form.
|
/// Adds a single opcode to the script.
|
||||||
///
|
pub fn push_opcode(&mut self, data: Opcode) { self.as_byte_vec().push(data.to_u8()); }
|
||||||
/// Some opcodes such as `OP_CHECKSIG` have a verify variant that works as if `VERIFY` was
|
|
||||||
/// in the script right after. To save space this function appends `VERIFY` only if
|
/// Adds instructions to push some arbitrary data onto the stack.
|
||||||
/// the most-recently-added opcode *does not* have an alternate `VERIFY` form. If it does
|
pub fn push_slice<T: AsRef<PushBytes>>(&mut self, data: T) {
|
||||||
/// the last opcode is replaced. E.g., `OP_CHECKSIG` will become `OP_CHECKSIGVERIFY`.
|
let data = data.as_ref();
|
||||||
///
|
self.reserve(Self::reserved_len_for_slice(data.len()));
|
||||||
/// Note that existing `OP_*VERIFY` opcodes do not lead to the instruction being ignored
|
self.push_slice_no_opt(data);
|
||||||
/// because `OP_VERIFY` consumes an item from the stack so ignoring them would change the
|
}
|
||||||
/// semantics.
|
|
||||||
///
|
/// Add a single instruction to the script.
|
||||||
/// This function needs to iterate over the script to find the last instruction. Prefer
|
///
|
||||||
/// `Builder` if you're creating the script from scratch or if you want to push `OP_VERIFY`
|
/// # Panics
|
||||||
/// multiple times.
|
///
|
||||||
pub fn scan_and_push_verify(&mut self) { self.push_verify(self.last_opcode()); }
|
/// The method panics if the instruction is a data push with length greater or equal to
|
||||||
}
|
/// 0x100000000.
|
||||||
|
pub fn push_instruction(&mut self, instruction: Instruction<'_>) {
|
||||||
|
match instruction {
|
||||||
|
Instruction::Op(opcode) => self.push_opcode(opcode),
|
||||||
|
Instruction::PushBytes(bytes) => self.push_slice(bytes),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Like push_instruction, but avoids calling `reserve` to not re-check the length.
|
||||||
|
pub fn push_instruction_no_opt(&mut self, instruction: Instruction<'_>) {
|
||||||
|
match instruction {
|
||||||
|
Instruction::Op(opcode) => self.push_opcode(opcode),
|
||||||
|
Instruction::PushBytes(bytes) => self.push_slice_no_opt(bytes),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds an `OP_VERIFY` to the script or replaces the last opcode with VERIFY form.
|
||||||
|
///
|
||||||
|
/// Some opcodes such as `OP_CHECKSIG` have a verify variant that works as if `VERIFY` was
|
||||||
|
/// in the script right after. To save space this function appends `VERIFY` only if
|
||||||
|
/// the most-recently-added opcode *does not* have an alternate `VERIFY` form. If it does
|
||||||
|
/// the last opcode is replaced. E.g., `OP_CHECKSIG` will become `OP_CHECKSIGVERIFY`.
|
||||||
|
///
|
||||||
|
/// Note that existing `OP_*VERIFY` opcodes do not lead to the instruction being ignored
|
||||||
|
/// because `OP_VERIFY` consumes an item from the stack so ignoring them would change the
|
||||||
|
/// semantics.
|
||||||
|
///
|
||||||
|
/// This function needs to iterate over the script to find the last instruction. Prefer
|
||||||
|
/// `Builder` if you're creating the script from scratch or if you want to push `OP_VERIFY`
|
||||||
|
/// multiple times.
|
||||||
|
pub fn scan_and_push_verify(&mut self) { self.push_verify(self.last_opcode()); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod tmp_priv {
|
mod tmp_priv {
|
||||||
use super::*;
|
use super::*;
|
||||||
impl ScriptBuf {
|
impl ScriptBuf {
|
||||||
/// Pretends to convert `&mut ScriptBuf` to `&mut Vec<u8>` so that it can be modified.
|
/// Pretends to convert `&mut ScriptBuf` to `&mut Vec<u8>` so that it can be modified.
|
||||||
///
|
///
|
||||||
/// Note: if the returned value leaks the original `ScriptBuf` will become empty.
|
/// Note: if the returned value leaks the original `ScriptBuf` will become empty.
|
||||||
pub(crate) fn as_byte_vec(&mut self) -> ScriptBufAsVec<'_> {
|
pub(crate) fn as_byte_vec(&mut self) -> ScriptBufAsVec<'_> {
|
||||||
let vec = core::mem::take(self).into_bytes();
|
let vec = core::mem::take(self).into_bytes();
|
||||||
ScriptBufAsVec(self, vec)
|
ScriptBufAsVec(self, vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pushes the slice without reserving
|
/// Pushes the slice without reserving
|
||||||
pub(crate) fn push_slice_no_opt(&mut self, data: &PushBytes) {
|
pub(crate) fn push_slice_no_opt(&mut self, data: &PushBytes) {
|
||||||
let mut this = self.as_byte_vec();
|
let mut this = self.as_byte_vec();
|
||||||
// Start with a PUSH opcode
|
// Start with a PUSH opcode
|
||||||
match data.len().to_u64() {
|
match data.len().to_u64() {
|
||||||
n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => {
|
n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => {
|
||||||
this.push(n as u8);
|
this.push(n as u8);
|
||||||
|
}
|
||||||
|
n if n < 0x100 => {
|
||||||
|
this.push(opcodes::Ordinary::OP_PUSHDATA1.to_u8());
|
||||||
|
this.push(n as u8);
|
||||||
|
}
|
||||||
|
n if n < 0x10000 => {
|
||||||
|
this.push(opcodes::Ordinary::OP_PUSHDATA2.to_u8());
|
||||||
|
this.push((n % 0x100) as u8);
|
||||||
|
this.push((n / 0x100) as u8);
|
||||||
|
}
|
||||||
|
// `PushBytes` enforces len < 0x100000000
|
||||||
|
n => {
|
||||||
|
this.push(opcodes::Ordinary::OP_PUSHDATA4.to_u8());
|
||||||
|
this.push((n % 0x100) as u8);
|
||||||
|
this.push(((n / 0x100) % 0x100) as u8);
|
||||||
|
this.push(((n / 0x10000) % 0x100) as u8);
|
||||||
|
this.push((n / 0x1000000) as u8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
n if n < 0x100 => {
|
// Then push the raw bytes
|
||||||
this.push(opcodes::Ordinary::OP_PUSHDATA1.to_u8());
|
this.extend_from_slice(data.as_bytes());
|
||||||
this.push(n as u8);
|
}
|
||||||
}
|
|
||||||
n if n < 0x10000 => {
|
/// Computes the sum of `len` and the length of an appropriate push opcode.
|
||||||
this.push(opcodes::Ordinary::OP_PUSHDATA2.to_u8());
|
pub(crate) fn reserved_len_for_slice(len: usize) -> usize {
|
||||||
this.push((n % 0x100) as u8);
|
len + match len {
|
||||||
this.push((n / 0x100) as u8);
|
0..=0x4b => 1,
|
||||||
}
|
0x4c..=0xff => 2,
|
||||||
// `PushBytes` enforces len < 0x100000000
|
0x100..=0xffff => 3,
|
||||||
n => {
|
// we don't care about oversized, the other fn will panic anyway
|
||||||
this.push(opcodes::Ordinary::OP_PUSHDATA4.to_u8());
|
_ => 5,
|
||||||
this.push((n % 0x100) as u8);
|
|
||||||
this.push(((n / 0x100) % 0x100) as u8);
|
|
||||||
this.push(((n / 0x10000) % 0x100) as u8);
|
|
||||||
this.push((n / 0x1000000) as u8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Then push the raw bytes
|
|
||||||
this.extend_from_slice(data.as_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Computes the sum of `len` and the length of an appropriate push opcode.
|
/// Adds an `OP_VERIFY` to the script or changes the most-recently-added opcode to `VERIFY`
|
||||||
pub(crate) fn reserved_len_for_slice(len: usize) -> usize {
|
/// alternative.
|
||||||
len + match len {
|
///
|
||||||
0..=0x4b => 1,
|
/// See the public fn [`Self::scan_and_push_verify`] to learn more.
|
||||||
0x4c..=0xff => 2,
|
pub(crate) fn push_verify(&mut self, last_opcode: Option<Opcode>) {
|
||||||
0x100..=0xffff => 3,
|
match opcode_to_verify(last_opcode) {
|
||||||
// we don't care about oversized, the other fn will panic anyway
|
Some(opcode) => {
|
||||||
_ => 5,
|
self.as_byte_vec().pop();
|
||||||
}
|
self.push_opcode(opcode);
|
||||||
}
|
}
|
||||||
|
None => self.push_opcode(OP_VERIFY),
|
||||||
/// Adds an `OP_VERIFY` to the script or changes the most-recently-added opcode to `VERIFY`
|
|
||||||
/// alternative.
|
|
||||||
///
|
|
||||||
/// See the public fn [`Self::scan_and_push_verify`] to learn more.
|
|
||||||
pub(crate) fn push_verify(&mut self, last_opcode: Option<Opcode>) {
|
|
||||||
match opcode_to_verify(last_opcode) {
|
|
||||||
Some(opcode) => {
|
|
||||||
self.as_byte_vec().pop();
|
|
||||||
self.push_opcode(opcode);
|
|
||||||
}
|
}
|
||||||
None => self.push_opcode(OP_VERIFY),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> core::iter::FromIterator<Instruction<'a>> for ScriptBuf {
|
impl<'a> core::iter::FromIterator<Instruction<'a>> for ScriptBuf {
|
||||||
fn from_iter<T>(iter: T) -> Self
|
fn from_iter<T>(iter: T) -> Self
|
||||||
|
@ -280,15 +280,11 @@ pub(crate) struct ScriptBufAsVec<'a>(&'a mut ScriptBuf, Vec<u8>);
|
||||||
impl<'a> core::ops::Deref for ScriptBufAsVec<'a> {
|
impl<'a> core::ops::Deref for ScriptBufAsVec<'a> {
|
||||||
type Target = Vec<u8>;
|
type Target = Vec<u8>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target { &self.1 }
|
||||||
&self.1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> core::ops::DerefMut for ScriptBufAsVec<'a> {
|
impl<'a> core::ops::DerefMut for ScriptBufAsVec<'a> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.1 }
|
||||||
&mut self.1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Drop for ScriptBufAsVec<'a> {
|
impl<'a> Drop for ScriptBufAsVec<'a> {
|
||||||
|
|
Loading…
Reference in New Issue