@@ -30,7 +30,8 @@ use std::{fmt, io, ops};
30
30
#[ cfg( feature = "serde" ) ] use serde;
31
31
32
32
use encode:: { self , Decodable , Encodable } ;
33
- use { opcodes, ScriptHash , WScriptHash } ;
33
+ use bitcoin:: hashes:: Hash ;
34
+ use { opcodes, ScriptHash , WScriptHash , PubkeyHash , WPubkeyHash } ;
34
35
35
36
use bitcoin:: PublicKey ;
36
37
@@ -208,6 +209,75 @@ impl Script {
208
209
/// Creates a new empty script
209
210
pub fn new ( ) -> Script { Script ( vec ! [ ] . into_boxed_slice ( ) ) }
210
211
212
+ /// Generates P2PK-type of scriptPubkey
213
+ pub fn new_p2pk ( pubkey : & PublicKey ) -> Script {
214
+ Builder :: new ( )
215
+ . push_key ( pubkey)
216
+ . push_opcode ( opcodes:: all:: OP_CHECKSIG )
217
+ . into_script ( )
218
+ }
219
+
220
+ /// Generates P2PKH-type of scriptPubkey
221
+ pub fn new_p2pkh ( pubkey_hash : & PubkeyHash ) -> Script {
222
+ Builder :: new ( )
223
+ . push_opcode ( opcodes:: all:: OP_DUP )
224
+ . push_opcode ( opcodes:: all:: OP_HASH160 )
225
+ . push_slice ( & pubkey_hash[ ..] )
226
+ . push_opcode ( opcodes:: all:: OP_EQUALVERIFY )
227
+ . push_opcode ( opcodes:: all:: OP_CHECKSIG )
228
+ . into_script ( )
229
+ }
230
+
231
+ /// Generates P2SH-type of scriptPubkey with a given hash of the redeem script
232
+ pub fn new_p2sh ( script_hash : & ScriptHash ) -> Script {
233
+ Builder :: new ( )
234
+ . push_opcode ( opcodes:: all:: OP_HASH160 )
235
+ . push_slice ( & script_hash[ ..] )
236
+ . push_opcode ( opcodes:: all:: OP_EQUAL )
237
+ . into_script ( )
238
+ }
239
+
240
+ /// Generates P2WPKH-type of scriptPubkey
241
+ pub fn new_v0_wpkh ( pubkey_hash : & WPubkeyHash ) -> Script {
242
+ Script :: new_witness_program ( :: bech32:: u5:: try_from_u8 ( 0 ) . unwrap ( ) , & pubkey_hash. to_vec ( ) )
243
+ }
244
+
245
+ /// Generates P2WSH-type of scriptPubkey with a given hash of the redeem script
246
+ pub fn new_v0_wsh ( script_hash : & WScriptHash ) -> Script {
247
+ Script :: new_witness_program ( :: bech32:: u5:: try_from_u8 ( 0 ) . unwrap ( ) , & script_hash. to_vec ( ) )
248
+ }
249
+
250
+ /// Generates P2WSH-type of scriptPubkey with a given hash of the redeem script
251
+ pub fn new_witness_program ( ver : :: bech32:: u5 , program : & [ u8 ] ) -> Script {
252
+ let mut verop = ver. to_u8 ( ) ;
253
+ assert ! ( verop <= 16 , "incorrect witness version provided: {}" , verop) ;
254
+ if verop > 0 {
255
+ verop = 0x50 + verop;
256
+ }
257
+ Builder :: new ( )
258
+ . push_opcode ( verop. into ( ) )
259
+ . push_slice ( & program)
260
+ . into_script ( )
261
+ }
262
+
263
+ /// Generates OP_RETURN-type of scriptPubkey for a given data
264
+ pub fn new_op_return ( data : & [ u8 ] ) -> Script {
265
+ Builder :: new ( )
266
+ . push_opcode ( opcodes:: all:: OP_RETURN )
267
+ . push_slice ( data)
268
+ . into_script ( )
269
+ }
270
+
271
+ /// Returns 160-bit hash of the script
272
+ pub fn script_hash ( & self ) -> ScriptHash {
273
+ ScriptHash :: hash ( & self . as_bytes ( ) )
274
+ }
275
+
276
+ /// Returns 256-bit hash of the script for P2WSH outputs
277
+ pub fn wscript_hash ( & self ) -> WScriptHash {
278
+ WScriptHash :: hash ( & self . as_bytes ( ) )
279
+ }
280
+
211
281
/// The length in bytes of the script
212
282
pub fn len ( & self ) -> usize { self . 0 . len ( ) }
213
283
@@ -225,7 +295,6 @@ impl Script {
225
295
226
296
/// Compute the P2SH output corresponding to this redeem script
227
297
pub fn to_p2sh ( & self ) -> Script {
228
- use bitcoin:: hashes:: Hash ;
229
298
Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_HASH160 )
230
299
. push_slice ( & ScriptHash :: hash ( & self . 0 ) [ ..] )
231
300
. push_opcode ( opcodes:: all:: OP_EQUAL )
@@ -235,7 +304,6 @@ impl Script {
235
304
/// Compute the P2WSH output corresponding to this witnessScript (aka the "witness redeem
236
305
/// script")
237
306
pub fn to_v0_p2wsh ( & self ) -> Script {
238
- use bitcoin:: hashes:: Hash ;
239
307
Builder :: new ( ) . push_int ( 0 )
240
308
. push_slice ( & WScriptHash :: hash ( & self . 0 ) [ ..] )
241
309
. into_script ( )
0 commit comments