Skip to main content

bsv/script/
locking_script.rs

1//! LockingScript: type-safe wrapper around Script for output scripts.
2
3use crate::script::script::Script;
4use std::ops::Deref;
5
6/// A locking script (scriptPubKey) wrapping a generic Script.
7///
8/// Uses `Deref` to Script for transparent access to all Script methods.
9/// Provides type-safety to distinguish output scripts from input scripts.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct LockingScript(pub(crate) Script);
12
13impl Deref for LockingScript {
14    type Target = Script;
15    fn deref(&self) -> &Script {
16        &self.0
17    }
18}
19
20impl LockingScript {
21    /// Create from raw binary bytes.
22    pub fn from_binary(bin: &[u8]) -> Self {
23        LockingScript(Script::from_binary(bin))
24    }
25
26    /// Create from hex string.
27    pub fn from_hex(hex: &str) -> Result<Self, crate::script::error::ScriptError> {
28        Ok(LockingScript(Script::from_hex(hex)?))
29    }
30
31    /// Create from ASM string.
32    pub fn from_asm(asm: &str) -> Self {
33        LockingScript(Script::from_asm(asm))
34    }
35
36    /// Create from an existing Script.
37    pub fn from_script(script: Script) -> Self {
38        LockingScript(script)
39    }
40}