@@ -148,47 +148,6 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
148
148
149
149
pub struct Builder ( pub BitcoinBuilder ) ;
150
150
151
- pub fn parse_instruction(
152
- instruction: Option <Result <Instruction <' _>, :: bitcoin:: script:: Error >>,
153
- ) -> Option <Opcode > {
154
- match instruction {
155
- Some ( Ok ( bitcoin:: script:: Instruction :: PushBytes ( push_bytes) ) ) => {
156
- // Handle OP_0 (Stored in rust-bitcoin as PushBytes(PushBytes([])))
157
- if push_bytes. len( ) == 0 {
158
- Some ( :: bitcoin:: opcodes:: all:: OP_PUSHBYTES_0 )
159
- } else {
160
- None
161
- }
162
- }
163
- Some ( Ok ( bitcoin:: script:: Instruction :: Op ( op) ) ) => Some ( op) ,
164
- _ => None ,
165
- }
166
- }
167
-
168
- pub fn check_optimality(
169
- opcode: Option <Opcode >,
170
- next_opcode: Option <Opcode >,
171
- ) -> ( bool , Option <Opcode >) {
172
- if opcode == None || next_opcode == None {
173
- ( true , None )
174
- } else {
175
- let opcode = opcode. unwrap( ) ;
176
- let next_opcode = next_opcode. unwrap( ) ;
177
- match ( opcode, next_opcode) {
178
- ( OP_PUSHNUM_1 , OP_ADD ) => ( false , Some ( OP_1ADD ) ) ,
179
- ( OP_PUSHNUM_1 , OP_SUB ) => ( false , Some ( OP_1SUB ) ) ,
180
- ( OP_DROP , OP_DROP ) => ( false , Some ( OP_2DROP ) ) ,
181
- ( OP_PUSHBYTES_0 , OP_ROLL ) => ( false , None ) ,
182
- ( OP_PUSHNUM_1 , OP_ROLL ) => ( false , Some ( OP_SWAP ) ) ,
183
- ( OP_PUSHNUM_2 , OP_ROLL ) => ( false , Some ( OP_ROT ) ) ,
184
- ( OP_PUSHBYTES_0 , OP_PICK ) => ( false , Some ( OP_DUP ) ) ,
185
- ( OP_PUSHBYTES_1 , OP_PICK ) => ( false , Some ( OP_OVER ) ) ,
186
- ( OP_IF , OP_ELSE ) => ( false , Some ( OP_NOTIF ) ) ,
187
- ( _, _) => ( true , None ) ,
188
- }
189
- }
190
- }
191
-
192
151
impl Builder {
193
152
pub fn new( ) -> Self {
194
153
let builder = BitcoinBuilder :: new( ) ;
@@ -203,38 +162,8 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
203
162
self . 0 . as_script( )
204
163
}
205
164
206
- pub fn last_opcode( & self ) -> Option <Opcode > {
207
- match self . 0 . as_script( ) . instructions( ) . last( ) {
208
- Some ( Ok ( instr) ) => match instr {
209
- bitcoin:: script:: Instruction :: PushBytes ( push_bytes) => {
210
- // Handle OP_0 (Stored in rust-bitcoin as PushBytes(PushBytes([])))
211
- if push_bytes. len( ) == 0 {
212
- Some ( :: bitcoin:: opcodes:: all:: OP_PUSHBYTES_0 )
213
- } else {
214
- None
215
- }
216
- }
217
- bitcoin:: script:: Instruction :: Op ( op) => Some ( op) ,
218
- } ,
219
- _ => None ,
220
- }
221
- }
222
-
223
165
pub fn push_opcode( mut self , opcode: Opcode ) -> Builder {
224
- let ( optimal, replacement_opcode) =
225
- check_optimality( self . last_opcode( ) , Some ( opcode) ) ;
226
- if optimal {
227
- self . 0 = self . 0 . push_opcode( opcode) ;
228
- } else {
229
- // NOTE: This is only valid because here we know that the last element of
230
- // the script is either 0 or an Opcode and not some arbitrary data push.
231
- let mut script_bytes = self . 0 . into_bytes( ) ;
232
- script_bytes. truncate( script_bytes. len( ) - 1 ) ;
233
- self . 0 = BitcoinBuilder :: from( script_bytes) ;
234
- if let Some ( replacement_opcode) = replacement_opcode {
235
- self . 0 = self . 0 . push_opcode( replacement_opcode) ;
236
- }
237
- }
166
+ self . 0 = self . 0 . push_opcode( opcode) ;
238
167
self
239
168
}
240
169
@@ -305,24 +234,9 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
305
234
}
306
235
impl NotU8Pushable for :: bitcoin:: ScriptBuf {
307
236
fn bitcoin_script_push( self , builder: Builder ) -> Builder {
308
- let ( optimal, replacement_opcode) =
309
- check_optimality( builder. last_opcode( ) , self . first_opcode( ) ) ;
310
237
let mut script_vec = Vec :: with_capacity( builder. 0 . as_bytes( ) . len( ) + self . as_bytes( ) . len( ) ) ;
311
- if optimal {
312
- script_vec. extend_from_slice( builder. as_bytes( ) ) ;
313
- script_vec. extend_from_slice( self . as_bytes( ) ) ;
314
- } else {
315
- let first_script_bytes = builder. 0 . as_bytes( ) ;
316
- script_vec
317
- . extend_from_slice( & first_script_bytes[ ..first_script_bytes. len( ) - 1 ] ) ;
318
- match replacement_opcode {
319
- Some ( opcode) => script_vec. push( opcode. to_u8( ) ) ,
320
- None => ( ) ,
321
- }
322
- let second_script_bytes = self . as_bytes( ) ;
323
- script_vec
324
- . extend_from_slice( & second_script_bytes[ 1 ..second_script_bytes. len( ) ] ) ;
325
- }
238
+ script_vec. extend_from_slice( builder. as_bytes( ) ) ;
239
+ script_vec. extend_from_slice( self . as_bytes( ) ) ;
326
240
Builder :: from( script_vec)
327
241
}
328
242
}
0 commit comments