1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// SPDX-License-Identifier: CC0-1.0
#[cfg(all(feature = "hex", feature = "alloc"))]
use alloc::string::String;
use core::marker::PhantomData;
use core::ops::{
Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use encoding::{BytesEncoder, CompactSizeEncoder, Encodable, Encoder2};
use super::{InstructionIndices, Instructions, ScriptBuf};
use crate::opcodes::all::{
OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY, OP_CHECKSIG, OP_CHECKSIGVERIFY,
};
use crate::prelude::{Box, ToOwned, Vec};
internals::transparent_newtype! {
/// Tidecoin script slice.
///
/// *[See also the `script` module](super).*
///
/// `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`](std::path::Path) vs [`OsStr`](std::ffi::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!
///
/// # Hexadecimal strings
///
/// Scripts are consensus encoded with a length prefix and as a result of this in some places in
/// the ecosystem one will encounter hex strings that include the prefix while in other places
/// the prefix is excluded. To support parsing and formatting scripts as hex we provide a bunch
/// of different APIs and trait implementations. Please see [`examples/script.rs`] for a
/// thorough example of all the APIs.
///
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct Script<T>(PhantomData<T>, [u8]);
impl<T> Script<T> {
/// Treat byte slice as `Script`
pub const fn from_bytes(bytes: &_) -> &Self;
/// Treat mutable byte slice as `Script`
pub fn from_bytes_mut(bytes: &mut _) -> &mut Self;
pub(crate) fn from_boxed_bytes(bytes: Box<_>) -> Box<Self>;
pub(crate) fn from_rc_bytes(bytes: Rc<_>) -> Rc<Self>;
pub(crate) fn from_arc_bytes(bytes: Arc<_>) -> Arc<Self>;
}
}
impl<T: 'static> Default for &Script<T> {
#[inline]
fn default() -> Self {
Script::new()
}
}
impl<T> ToOwned for Script<T> {
type Owned = ScriptBuf<T>;
#[inline]
fn to_owned(&self) -> Self::Owned {
ScriptBuf::from_bytes(self.to_vec())
}
}
impl<T> Script<T> {
/// Constructs a new empty script.
#[inline]
pub const fn new() -> &'static Self {
Self::from_bytes(&[])
}
/// Returns the script data as a byte slice.
///
/// This is just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub const fn as_bytes(&self) -> &[u8] {
&self.1
}
/// Returns the script data as a mutable byte slice.
///
/// This is just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
&mut self.1
}
/// Returns a copy of the script data.
///
/// This is just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub fn to_vec(&self) -> Vec<u8> {
self.as_bytes().to_owned()
}
/// 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`.
#[cfg(all(feature = "hex", feature = "alloc"))]
pub fn to_hex_string_prefixed(&self) -> String {
use internals::hex::{BytesToHexIter, Case};
let iter = encoding::EncodableByteIter::new(self);
BytesToHexIter::new(iter, Case::Lower).collect()
}
/// Encodes the script as lower-case hex.
///
/// This is **not** consensus encoding. The returned hex string will not include the length
/// prefix. See `to_hex_string_prefixed`.
#[cfg(all(feature = "hex", feature = "alloc"))]
pub fn to_hex_string_no_length_prefix(&self) -> String {
use internals::hex::DisplayHex as _;
self.as_bytes().to_lower_hex_string()
}
/// Returns the length in bytes of the script.
#[inline]
pub const fn len(&self) -> usize {
self.as_bytes().len()
}
/// Returns whether the script is the empty script.
#[inline]
pub const fn is_empty(&self) -> bool {
self.as_bytes().is_empty()
}
/// Converts a [`Box<Script>`](Box) into a [`ScriptBuf`] without copying or allocating.
#[must_use]
#[inline]
pub fn into_script_buf(self: Box<Self>) -> ScriptBuf<T> {
let rw = Box::into_raw(self) as *mut [u8];
// SAFETY: copied from `std`
// The pointer was just created from a box without deallocating
// Casting a transparent struct wrapping a slice to the slice pointer is sound (same
// layout).
let inner = unsafe { Box::from_raw(rw) };
ScriptBuf::from_bytes(Vec::from(inner))
}
/// Iterates over decoded instructions.
#[inline]
pub fn instructions(&self) -> Instructions<'_> {
Instructions::new(self, false)
}
/// Iterates over decoded instructions while enforcing minimal pushes.
#[inline]
pub fn instructions_minimal(&self) -> Instructions<'_> {
Instructions::new(self, true)
}
/// Counts signature-check operations using accurate multisig counting.
///
/// This is the counting mode used by the node for redeem scripts and
/// witness scripts. `OP_CHECKSIGADD` is not counted by Tidecoin consensus.
pub fn count_sigops(&self) -> usize {
self.count_sigops_internal(true)
}
/// Counts signature-check operations using legacy multisig counting.
///
/// This is the counting mode used by the node for scriptSigs and
/// scriptPubKeys in context-free block sanity checks.
pub fn count_sigops_legacy(&self) -> usize {
self.count_sigops_internal(false)
}
fn count_sigops_internal(&self, accurate: bool) -> usize {
let mut count = 0;
let mut pushnum_cache = None;
for inst in self.instructions() {
match inst {
Ok(super::Instruction::Op(opcode)) => match opcode.to_u8() {
x if x == OP_CHECKSIG.to_u8() || x == OP_CHECKSIGVERIFY.to_u8() => {
count += 1;
}
x if x == OP_CHECKMULTISIG.to_u8() || x == OP_CHECKMULTISIGVERIFY.to_u8() => {
if accurate {
count += pushnum_cache.map_or(20, usize::from);
} else {
count += 20;
}
}
_ => {
pushnum_cache = opcode.decode_pushnum();
}
},
Ok(super::Instruction::PushBytes(_)) => {
pushnum_cache = None;
}
Err(_) => break,
}
}
count
}
/// Iterates over decoded instructions together with their byte indices.
#[inline]
pub fn instruction_indices(&self) -> InstructionIndices<'_> {
InstructionIndices::new(self, false)
}
/// Iterates over decoded instructions and indices while enforcing minimal pushes.
#[inline]
pub fn instruction_indices_minimal(&self) -> InstructionIndices<'_> {
InstructionIndices::new(self, true)
}
/// Returns the last opcode if the final instruction is an opcode.
pub fn last_opcode(&self) -> Option<crate::opcodes::Opcode> {
match self.instructions().last() {
Some(Ok(super::Instruction::Op(op))) => Some(op),
_ => None,
}
}
/// Returns the last pushed byte slice if the final instruction is a data push.
pub fn last_pushdata(&self) -> Option<&super::PushBytes> {
match self.instructions().last() {
Some(Ok(super::Instruction::PushBytes(bytes))) => Some(bytes),
_ => None,
}
}
}
encoding::encoder_newtype_exact! {
/// The encoder for the [`Script<T>`] type.
pub struct ScriptEncoder<'e>(Encoder2<CompactSizeEncoder, BytesEncoder<'e>>);
}
impl<T> Encodable for Script<T> {
type Encoder<'e>
= ScriptEncoder<'e>
where
Self: 'e;
fn encoder(&self) -> Self::Encoder<'_> {
ScriptEncoder::new(Encoder2::new(
CompactSizeEncoder::new(self.as_bytes().len()),
BytesEncoder::without_length_prefix(self.as_bytes()),
))
}
}
#[cfg(feature = "arbitrary")]
impl<'a, T> Arbitrary<'a> for &'a Script<T> {
#[inline]
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let v = <&'a [u8]>::arbitrary(u)?;
Ok(Script::from_bytes(v))
}
}
macro_rules! delegate_index {
($($type:ty),* $(,)?) => {
$(
/// Script subslicing operation - read [slicing safety](#slicing-safety)!
impl<T> Index<$type> for Script<T> {
type Output = Self;
#[inline]
fn index(&self, index: $type) -> &Self::Output {
Self::from_bytes(&self.as_bytes()[index])
}
}
)*
}
}
delegate_index!(
Range<usize>,
RangeFrom<usize>,
RangeTo<usize>,
RangeFull,
RangeInclusive<usize>,
RangeToInclusive<usize>,
(Bound<usize>, Bound<usize>)
);