Skip to main content

bsv/script/
unlocking_script.rs

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