Skip to content

Commit 437d1e2

Browse files
committed
Add more utility functions to script
1 parent d6851ad commit 437d1e2

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

src/script.rs

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use std::{fmt, io, ops};
3030
#[cfg(feature = "serde")] use serde;
3131

3232
use encode::{self, Decodable, Encodable};
33-
use {opcodes, ScriptHash, WScriptHash};
33+
use bitcoin::hashes::Hash;
34+
use {opcodes, ScriptHash, WScriptHash, PubkeyHash, WPubkeyHash};
3435

3536
use bitcoin::PublicKey;
3637

@@ -208,6 +209,75 @@ impl Script {
208209
/// Creates a new empty script
209210
pub fn new() -> Script { Script(vec![].into_boxed_slice()) }
210211

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+
211281
/// The length in bytes of the script
212282
pub fn len(&self) -> usize { self.0.len() }
213283

@@ -225,7 +295,6 @@ impl Script {
225295

226296
/// Compute the P2SH output corresponding to this redeem script
227297
pub fn to_p2sh(&self) -> Script {
228-
use bitcoin::hashes::Hash;
229298
Builder::new().push_opcode(opcodes::all::OP_HASH160)
230299
.push_slice(&ScriptHash::hash(&self.0)[..])
231300
.push_opcode(opcodes::all::OP_EQUAL)
@@ -235,7 +304,6 @@ impl Script {
235304
/// Compute the P2WSH output corresponding to this witnessScript (aka the "witness redeem
236305
/// script")
237306
pub fn to_v0_p2wsh(&self) -> Script {
238-
use bitcoin::hashes::Hash;
239307
Builder::new().push_int(0)
240308
.push_slice(&WScriptHash::hash(&self.0)[..])
241309
.into_script()

0 commit comments

Comments
 (0)