@@ -91,137 +91,137 @@ impl ScriptBuf {
9191
9292mod tmp_pub {
9393 use super :: * ;
94- impl ScriptBuf {
95- /// Creates a new script builder
96- pub fn builder ( ) -> Builder { Builder :: new ( ) }
94+ impl ScriptBuf {
95+ /// Creates a new script builder
96+ pub fn builder ( ) -> Builder { Builder :: new ( ) }
9797
98- /// Generates OP_RETURN-type of scriptPubkey for the given data.
99- pub fn new_op_return < T : AsRef < PushBytes > > ( data : T ) -> Self {
100- Builder :: new ( ) . push_opcode ( OP_RETURN ) . push_slice ( data) . into_script ( )
101- }
98+ /// Generates OP_RETURN-type of scriptPubkey for the given data.
99+ pub fn new_op_return < T : AsRef < PushBytes > > ( data : T ) -> Self {
100+ Builder :: new ( ) . push_opcode ( OP_RETURN ) . push_slice ( data) . into_script ( )
101+ }
102102
103- /// Creates a [`ScriptBuf`] from a hex string.
104- pub fn from_hex ( s : & str ) -> Result < Self , hex:: HexToBytesError > {
105- let v = Vec :: from_hex ( s) ?;
106- Ok ( ScriptBuf :: from_bytes ( v) )
107- }
103+ /// Creates a [`ScriptBuf`] from a hex string.
104+ pub fn from_hex ( s : & str ) -> Result < Self , hex:: HexToBytesError > {
105+ let v = Vec :: from_hex ( s) ?;
106+ Ok ( ScriptBuf :: from_bytes ( v) )
107+ }
108108
109- /// Adds a single opcode to the script.
110- pub fn push_opcode ( & mut self , data : Opcode ) { self . as_byte_vec ( ) . push ( data. to_u8 ( ) ) ; }
109+ /// Adds a single opcode to the script.
110+ pub fn push_opcode ( & mut self , data : Opcode ) { self . as_byte_vec ( ) . push ( data. to_u8 ( ) ) ; }
111111
112- /// Adds instructions to push some arbitrary data onto the stack.
113- pub fn push_slice < T : AsRef < PushBytes > > ( & mut self , data : T ) {
114- let data = data. as_ref ( ) ;
115- self . reserve ( Self :: reserved_len_for_slice ( data. len ( ) ) ) ;
116- self . push_slice_no_opt ( data) ;
117- }
112+ /// Adds instructions to push some arbitrary data onto the stack.
113+ pub fn push_slice < T : AsRef < PushBytes > > ( & mut self , data : T ) {
114+ let data = data. as_ref ( ) ;
115+ self . reserve ( Self :: reserved_len_for_slice ( data. len ( ) ) ) ;
116+ self . push_slice_no_opt ( data) ;
117+ }
118118
119- /// Add a single instruction to the script.
120- ///
121- /// # Panics
122- ///
123- /// The method panics if the instruction is a data push with length greater or equal to
124- /// 0x100000000.
125- pub fn push_instruction ( & mut self , instruction : Instruction < ' _ > ) {
126- match instruction {
127- Instruction :: Op ( opcode) => self . push_opcode ( opcode) ,
128- Instruction :: PushBytes ( bytes) => self . push_slice ( bytes) ,
119+ /// Add a single instruction to the script.
120+ ///
121+ /// # Panics
122+ ///
123+ /// The method panics if the instruction is a data push with length greater or equal to
124+ /// 0x100000000.
125+ pub fn push_instruction ( & mut self , instruction : Instruction < ' _ > ) {
126+ match instruction {
127+ Instruction :: Op ( opcode) => self . push_opcode ( opcode) ,
128+ Instruction :: PushBytes ( bytes) => self . push_slice ( bytes) ,
129+ }
129130 }
130- }
131131
132- /// Like push_instruction, but avoids calling `reserve` to not re-check the length.
133- pub fn push_instruction_no_opt ( & mut self , instruction : Instruction < ' _ > ) {
134- match instruction {
135- Instruction :: Op ( opcode) => self . push_opcode ( opcode) ,
136- Instruction :: PushBytes ( bytes) => self . push_slice_no_opt ( bytes) ,
132+ /// Like push_instruction, but avoids calling `reserve` to not re-check the length.
133+ pub fn push_instruction_no_opt ( & mut self , instruction : Instruction < ' _ > ) {
134+ match instruction {
135+ Instruction :: Op ( opcode) => self . push_opcode ( opcode) ,
136+ Instruction :: PushBytes ( bytes) => self . push_slice_no_opt ( bytes) ,
137+ }
137138 }
138- }
139139
140- /// Adds an `OP_VERIFY` to the script or replaces the last opcode with VERIFY form.
141- ///
142- /// Some opcodes such as `OP_CHECKSIG` have a verify variant that works as if `VERIFY` was
143- /// in the script right after. To save space this function appends `VERIFY` only if
144- /// the most-recently-added opcode *does not* have an alternate `VERIFY` form. If it does
145- /// the last opcode is replaced. E.g., `OP_CHECKSIG` will become `OP_CHECKSIGVERIFY`.
146- ///
147- /// Note that existing `OP_*VERIFY` opcodes do not lead to the instruction being ignored
148- /// because `OP_VERIFY` consumes an item from the stack so ignoring them would change the
149- /// semantics.
150- ///
151- /// This function needs to iterate over the script to find the last instruction. Prefer
152- /// `Builder` if you're creating the script from scratch or if you want to push `OP_VERIFY`
153- /// multiple times.
154- pub fn scan_and_push_verify ( & mut self ) { self . push_verify ( self . last_opcode ( ) ) ; }
155- }
140+ /// Adds an `OP_VERIFY` to the script or replaces the last opcode with VERIFY form.
141+ ///
142+ /// Some opcodes such as `OP_CHECKSIG` have a verify variant that works as if `VERIFY` was
143+ /// in the script right after. To save space this function appends `VERIFY` only if
144+ /// the most-recently-added opcode *does not* have an alternate `VERIFY` form. If it does
145+ /// the last opcode is replaced. E.g., `OP_CHECKSIG` will become `OP_CHECKSIGVERIFY`.
146+ ///
147+ /// Note that existing `OP_*VERIFY` opcodes do not lead to the instruction being ignored
148+ /// because `OP_VERIFY` consumes an item from the stack so ignoring them would change the
149+ /// semantics.
150+ ///
151+ /// This function needs to iterate over the script to find the last instruction. Prefer
152+ /// `Builder` if you're creating the script from scratch or if you want to push `OP_VERIFY`
153+ /// multiple times.
154+ pub fn scan_and_push_verify ( & mut self ) { self . push_verify ( self . last_opcode ( ) ) ; }
155+ }
156156}
157157
158158mod tmp_priv {
159159 use super :: * ;
160- impl ScriptBuf {
161- /// Pretends to convert `&mut ScriptBuf` to `&mut Vec<u8>` so that it can be modified.
162- ///
163- /// Note: if the returned value leaks the original `ScriptBuf` will become empty.
164- pub ( crate ) fn as_byte_vec ( & mut self ) -> ScriptBufAsVec < ' _ > {
165- let vec = core:: mem:: take ( self ) . into_bytes ( ) ;
166- ScriptBufAsVec ( self , vec)
167- }
160+ impl ScriptBuf {
161+ /// Pretends to convert `&mut ScriptBuf` to `&mut Vec<u8>` so that it can be modified.
162+ ///
163+ /// Note: if the returned value leaks the original `ScriptBuf` will become empty.
164+ pub ( crate ) fn as_byte_vec ( & mut self ) -> ScriptBufAsVec < ' _ > {
165+ let vec = core:: mem:: take ( self ) . into_bytes ( ) ;
166+ ScriptBufAsVec ( self , vec)
167+ }
168168
169- /// Pushes the slice without reserving
170- pub ( crate ) fn push_slice_no_opt ( & mut self , data : & PushBytes ) {
171- let mut this = self . as_byte_vec ( ) ;
172- // Start with a PUSH opcode
173- match data. len ( ) . to_u64 ( ) {
174- n if n < opcodes:: Ordinary :: OP_PUSHDATA1 as u64 => {
175- this. push ( n as u8 ) ;
176- }
177- n if n < 0x100 => {
178- this. push ( opcodes:: Ordinary :: OP_PUSHDATA1 . to_u8 ( ) ) ;
179- this. push ( n as u8 ) ;
180- }
181- n if n < 0x10000 => {
182- this. push ( opcodes:: Ordinary :: OP_PUSHDATA2 . to_u8 ( ) ) ;
183- this. push ( ( n % 0x100 ) as u8 ) ;
184- this. push ( ( n / 0x100 ) as u8 ) ;
185- }
186- // `PushBytes` enforces len < 0x100000000
187- n => {
188- this. push ( opcodes:: Ordinary :: OP_PUSHDATA4 . to_u8 ( ) ) ;
189- this. push ( ( n % 0x100 ) as u8 ) ;
190- this. push ( ( ( n / 0x100 ) % 0x100 ) as u8 ) ;
191- this. push ( ( ( n / 0x10000 ) % 0x100 ) as u8 ) ;
192- this. push ( ( n / 0x1000000 ) as u8 ) ;
169+ /// Pushes the slice without reserving
170+ pub ( crate ) fn push_slice_no_opt ( & mut self , data : & PushBytes ) {
171+ let mut this = self . as_byte_vec ( ) ;
172+ // Start with a PUSH opcode
173+ match data. len ( ) . to_u64 ( ) {
174+ n if n < opcodes:: Ordinary :: OP_PUSHDATA1 as u64 => {
175+ this. push ( n as u8 ) ;
176+ }
177+ n if n < 0x100 => {
178+ this. push ( opcodes:: Ordinary :: OP_PUSHDATA1 . to_u8 ( ) ) ;
179+ this. push ( n as u8 ) ;
180+ }
181+ n if n < 0x10000 => {
182+ this. push ( opcodes:: Ordinary :: OP_PUSHDATA2 . to_u8 ( ) ) ;
183+ this. push ( ( n % 0x100 ) as u8 ) ;
184+ this. push ( ( n / 0x100 ) as u8 ) ;
185+ }
186+ // `PushBytes` enforces len < 0x100000000
187+ n => {
188+ this. push ( opcodes:: Ordinary :: OP_PUSHDATA4 . to_u8 ( ) ) ;
189+ this. push ( ( n % 0x100 ) as u8 ) ;
190+ this. push ( ( ( n / 0x100 ) % 0x100 ) as u8 ) ;
191+ this. push ( ( ( n / 0x10000 ) % 0x100 ) as u8 ) ;
192+ this. push ( ( n / 0x1000000 ) as u8 ) ;
193+ }
193194 }
195+ // Then push the raw bytes
196+ this. extend_from_slice ( data. as_bytes ( ) ) ;
194197 }
195- // Then push the raw bytes
196- this. extend_from_slice ( data. as_bytes ( ) ) ;
197- }
198198
199- /// Computes the sum of `len` and the length of an appropriate push opcode.
200- pub ( crate ) fn reserved_len_for_slice ( len : usize ) -> usize {
201- len + match len {
202- 0 ..=0x4b => 1 ,
203- 0x4c ..=0xff => 2 ,
204- 0x100 ..=0xffff => 3 ,
205- // we don't care about oversized, the other fn will panic anyway
206- _ => 5 ,
199+ /// Computes the sum of `len` and the length of an appropriate push opcode.
200+ pub ( crate ) fn reserved_len_for_slice ( len : usize ) -> usize {
201+ len + match len {
202+ 0 ..=0x4b => 1 ,
203+ 0x4c ..=0xff => 2 ,
204+ 0x100 ..=0xffff => 3 ,
205+ // we don't care about oversized, the other fn will panic anyway
206+ _ => 5 ,
207+ }
207208 }
208- }
209209
210- /// Adds an `OP_VERIFY` to the script or changes the most-recently-added opcode to `VERIFY`
211- /// alternative.
212- ///
213- /// See the public fn [`Self::scan_and_push_verify`] to learn more.
214- pub ( crate ) fn push_verify ( & mut self , last_opcode : Option < Opcode > ) {
215- match opcode_to_verify ( last_opcode) {
216- Some ( opcode) => {
217- self . as_byte_vec ( ) . pop ( ) ;
218- self . push_opcode ( opcode) ;
210+ /// Adds an `OP_VERIFY` to the script or changes the most-recently-added opcode to `VERIFY`
211+ /// alternative.
212+ ///
213+ /// See the public fn [`Self::scan_and_push_verify`] to learn more.
214+ pub ( crate ) fn push_verify ( & mut self , last_opcode : Option < Opcode > ) {
215+ match opcode_to_verify ( last_opcode) {
216+ Some ( opcode) => {
217+ self . as_byte_vec ( ) . pop ( ) ;
218+ self . push_opcode ( opcode) ;
219+ }
220+ None => self . push_opcode ( OP_VERIFY ) ,
219221 }
220- None => self . push_opcode ( OP_VERIFY ) ,
221222 }
222223 }
223224}
224- }
225225
226226impl < ' a > core:: iter:: FromIterator < Instruction < ' a > > for ScriptBuf {
227227 fn from_iter < T > ( iter : T ) -> Self
@@ -280,15 +280,11 @@ pub(crate) struct ScriptBufAsVec<'a>(&'a mut ScriptBuf, Vec<u8>);
280280impl < ' a > core:: ops:: Deref for ScriptBufAsVec < ' a > {
281281 type Target = Vec < u8 > ;
282282
283- fn deref ( & self ) -> & Self :: Target {
284- & self . 1
285- }
283+ fn deref ( & self ) -> & Self :: Target { & self . 1 }
286284}
287285
288286impl < ' a > core:: ops:: DerefMut for ScriptBufAsVec < ' a > {
289- fn deref_mut ( & mut self ) -> & mut Self :: Target {
290- & mut self . 1
291- }
287+ fn deref_mut ( & mut self ) -> & mut Self :: Target { & mut self . 1 }
292288}
293289
294290impl < ' a > Drop for ScriptBufAsVec < ' a > {
0 commit comments