From 42b7f328fbc99829b5ddb4df164a01ae5a4798d5 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 31 Jul 2019 16:17:08 +0100 Subject: [PATCH] legacy::Script::address This is the counterpart to legacy::TransparentAddress::script. --- zcash_primitives/src/legacy.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/zcash_primitives/src/legacy.rs b/zcash_primitives/src/legacy.rs index d1d7c1a62..18d038881 100644 --- a/zcash_primitives/src/legacy.rs +++ b/zcash_primitives/src/legacy.rs @@ -38,6 +38,31 @@ impl Script { pub fn write(&self, mut writer: W) -> io::Result<()> { Vector::write(&mut writer, &self.0, |w, e| w.write_u8(*e)) } + + /// Returns the address that this Script contains, if any. + pub fn address(&self) -> Option { + if self.0.len() == 25 + && self.0[0] == OpCode::Dup as u8 + && self.0[1] == OpCode::Hash160 as u8 + && self.0[2] == 0x14 + && self.0[23] == OpCode::EqualVerify as u8 + && self.0[24] == OpCode::CheckSig as u8 + { + let mut hash = [0; 20]; + hash.copy_from_slice(&self.0[3..23]); + Some(TransparentAddress::PublicKey(hash)) + } else if self.0.len() == 23 + && self.0[0] == OpCode::Hash160 as u8 + && self.0[1] == 0x14 + && self.0[22] == OpCode::Equal as u8 + { + let mut hash = [0; 20]; + hash.copy_from_slice(&self.0[2..22]); + Some(TransparentAddress::Script(hash)) + } else { + None + } + } } impl Shl for Script { @@ -151,7 +176,8 @@ mod tests { 0x76, 0xa9, 0x14, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x88, 0xac, ] - ) + ); + assert_eq!(addr.script().address(), Some(addr)); } #[test] @@ -163,6 +189,7 @@ mod tests { 0xa9, 0x14, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x87, ] - ) + ); + assert_eq!(addr.script().address(), Some(addr)); } }