pub struct Script(/* private fields */);
alloc
only.Expand description
Bitcoin script slice.
See also the bitcoin::script
module.
Script
is a script slice, the most primitive script type. It’s usually seen in its borrowed
form &Script
. It is always encoded as a series of bytes representing the opcodes and data
pushes.
§Validity
Script
does not have any validity invariants - it’s essentially just a marked slice of
bytes. This is similar to Path
vs OsStr
where they
are trivially cast-able to each-other and Path
doesn’t guarantee being a usable FS path but
having a newtype still has value because of added methods, readability and basic type checking.
Although at least data pushes could be checked not to overflow the script, bad scripts are allowed to be in a transaction (outputs just become unspendable) and there even are such transactions in the chain. Thus we must allow such scripts to be placed in the transaction.
§Slicing safety
Slicing is similar to how str
works: some ranges may be incorrect and indexing by
usize
is not supported. However, as opposed to std
, we have no way of checking
correctness without causing linear complexity so there are no panics on invalid
ranges! If you supply an invalid range, you’ll get a garbled script.
The range is considered valid if it’s at a boundary of instruction. Care must be taken especially with push operations because you could get a reference to arbitrary attacker-supplied bytes that look like a valid script.
It is recommended to use .instructions()
method to get an iterator over script
instructions and work with that instead.
§Memory safety
The type is #[repr(transparent)]
for internal purposes only!
No consumer crate may rely on the representation of the struct!
§References
§Bitcoin Core References
Implementations§
Source§impl Script
impl Script
Sourcepub fn from_bytes(bytes: &[u8]) -> &Script
pub fn from_bytes(bytes: &[u8]) -> &Script
Treat byte slice as Script
Sourcepub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Script
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Script
Treat mutable byte slice as Script
Sourcepub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
Returns the script data as a mutable byte slice.
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
👎Deprecating in a future version: use to_vec instead
pub fn to_bytes(&self) -> Vec<u8> ⓘ
Returns a copy of the script data.
Sourcepub fn into_script_buf(self: Box<Self>) -> ScriptBuf
pub fn into_script_buf(self: Box<Self>) -> ScriptBuf
Converts a Box<Script>
into a ScriptBuf
without copying or allocating.
Trait Implementations§
Source§impl BorrowMut<Script> for ScriptBuf
impl BorrowMut<Script> for ScriptBuf
Source§fn borrow_mut(&mut self) -> &mut Script
fn borrow_mut(&mut self) -> &mut Script
Source§impl<'de> Deserialize<'de> for &'de Script
Available on crate feature serde
only.Can only deserialize borrowed bytes.
impl<'de> Deserialize<'de> for &'de Script
serde
only.Can only deserialize borrowed bytes.
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'a> From<&'a Script> for Arc<Script>
Available on target_has_atomic="ptr"
only.Note: This will fail to compile on old Rust for targets that don’t support atomics
impl<'a> From<&'a Script> for Arc<Script>
target_has_atomic="ptr"
only.Note: This will fail to compile on old Rust for targets that don’t support atomics
Source§impl Index<(Bound<usize>, Bound<usize>)> for Script
Script subslicing operation - read slicing safety!
impl Index<(Bound<usize>, Bound<usize>)> for Script
Script subslicing operation - read slicing safety!
Source§impl Index<RangeInclusive<usize>> for Script
Script subslicing operation - read slicing safety!
impl Index<RangeInclusive<usize>> for Script
Script subslicing operation - read slicing safety!
Source§impl Index<RangeToInclusive<usize>> for Script
Script subslicing operation - read slicing safety!
impl Index<RangeToInclusive<usize>> for Script
Script subslicing operation - read slicing safety!