bsv_rs/script/
locking_script.rs1use super::address::Address;
7use super::chunk::ScriptChunk;
8use super::script::Script;
9use crate::Result;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct LockingScript(Script);
17
18impl LockingScript {
19 pub fn new() -> Self {
21 Self(Script::new())
22 }
23
24 pub fn from_chunks(chunks: Vec<ScriptChunk>) -> Self {
26 Self(Script::from_chunks(chunks))
27 }
28
29 pub fn from_asm(asm: &str) -> Result<Self> {
31 Ok(Self(Script::from_asm(asm)?))
32 }
33
34 pub fn from_hex(hex: &str) -> Result<Self> {
36 Ok(Self(Script::from_hex(hex)?))
37 }
38
39 pub fn from_binary(bin: &[u8]) -> Result<Self> {
41 Ok(Self(Script::from_binary(bin)?))
42 }
43
44 pub fn from_script(script: Script) -> Self {
46 Self(script)
47 }
48
49 pub fn as_script(&self) -> &Script {
51 &self.0
52 }
53
54 pub fn into_script(self) -> Script {
56 self.0
57 }
58
59 pub fn to_asm(&self) -> String {
61 self.0.to_asm()
62 }
63
64 pub fn to_hex(&self) -> String {
66 self.0.to_hex()
67 }
68
69 pub fn to_binary(&self) -> Vec<u8> {
71 self.0.to_binary()
72 }
73
74 pub fn chunks(&self) -> Vec<ScriptChunk> {
76 self.0.chunks()
77 }
78
79 pub fn len(&self) -> usize {
81 self.0.len()
82 }
83
84 pub fn is_empty(&self) -> bool {
86 self.0.is_empty()
87 }
88
89 pub fn is_push_only(&self) -> bool {
91 self.0.is_push_only()
92 }
93
94 pub fn is_locking_script(&self) -> bool {
96 true
97 }
98
99 pub fn is_unlocking_script(&self) -> bool {
101 false
102 }
103
104 pub fn to_address(&self) -> Option<Address> {
120 let hash = self.0.extract_pubkey_hash()?;
121 Address::new_from_public_key_hash(&hash, true).ok()
122 }
123}
124
125impl Default for LockingScript {
126 fn default() -> Self {
127 Self::new()
128 }
129}
130
131impl From<Script> for LockingScript {
132 fn from(script: Script) -> Self {
133 Self(script)
134 }
135}
136
137impl From<LockingScript> for Script {
138 fn from(locking: LockingScript) -> Self {
139 locking.0
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146
147 #[test]
148 fn test_is_locking_script() {
149 let script = LockingScript::new();
150 assert!(script.is_locking_script());
151 assert!(!script.is_unlocking_script());
152 }
153
154 #[test]
155 fn test_from_hex() {
156 let hex = "76a914000000000000000000000000000000000000000088ac";
157 let script = LockingScript::from_hex(hex).unwrap();
158 assert_eq!(script.to_hex(), hex);
159 }
160
161 #[test]
162 fn test_from_asm() {
163 let asm = "OP_DUP OP_HASH160";
164 let script = LockingScript::from_asm(asm).unwrap();
165 assert_eq!(script.to_hex(), "76a9");
166 }
167
168 #[test]
169 fn test_conversion() {
170 let script = Script::from_hex("76a9").unwrap();
171 let locking = LockingScript::from_script(script.clone());
172 let back: Script = locking.into();
173 assert_eq!(back.to_hex(), script.to_hex());
174 }
175}