diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 32e723ffe8..2b0e2bfa77 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -52,7 +52,7 @@ impl AsRef<[u8]> for Script { impl fmt::Debug for Script { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("Script(")?; - self.fmt_asm(f)?; + Script::fmt_asm(self.as_ref(), f)?; f.write_str(")") } } @@ -439,10 +439,10 @@ impl Script { } /// Write the assembly decoding of the script to the formatter. - pub fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result { + pub fn fmt_asm(script: &[u8], f: &mut dyn fmt::Write) -> fmt::Result { let mut index = 0; - while index < self.0.len() { - let opcode = opcodes::All::from(self.0[index]); + while index < script.len() { + let opcode = opcodes::All::from(script[index]); index += 1; let data_len = if let opcodes::Class::PushBytes(n) = opcode.classify() { @@ -450,31 +450,31 @@ impl Script { } else { match opcode { opcodes::all::OP_PUSHDATA1 => { - if self.0.len() < index + 1 { + if script.len() < index + 1 { f.write_str("")?; break; } - match read_uint(&self.0[index..], 1) { + match read_uint(&script[index..], 1) { Ok(n) => { index += 1; n as usize } Err(_) => { f.write_str("")?; break; } } } opcodes::all::OP_PUSHDATA2 => { - if self.0.len() < index + 2 { + if script.len() < index + 2 { f.write_str("")?; break; } - match read_uint(&self.0[index..], 2) { + match read_uint(&script[index..], 2) { Ok(n) => { index += 2; n as usize } Err(_) => { f.write_str("")?; break; } } } opcodes::all::OP_PUSHDATA4 => { - if self.0.len() < index + 4 { + if script.len() < index + 4 { f.write_str("")?; break; } - match read_uint(&self.0[index..], 4) { + match read_uint(&script[index..], 4) { Ok(n) => { index += 4; n as usize } Err(_) => { f.write_str("")?; break; } } @@ -493,8 +493,8 @@ impl Script { // Write any pushdata if data_len > 0 { f.write_str(" ")?; - if index + data_len <= self.0.len() { - for ch in &self.0[index..index + data_len] { + if index + data_len <= script.len() { + for ch in &script[index..index + data_len] { write!(f, "{:02x}", ch)?; } index += data_len; @@ -507,12 +507,17 @@ impl Script { Ok(()) } - /// Get the assembly decoding of the script. - pub fn asm(&self) -> String { + /// Create an assembly decoding of the script in the given byte slice. + pub fn str_asm(script: &[u8]) -> String { let mut buf = String::new(); - self.fmt_asm(&mut buf).unwrap(); + Script::fmt_asm(script, &mut buf).unwrap(); buf } + + /// Get the assembly decoding of the script. + pub fn asm(&self) -> String { + Script::str_asm(self.as_ref()) + } } /// Creates a new script from an existing vector