diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 4a24691e..21c18a1b 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -289,11 +289,14 @@ impl Script { /// Whether the script is the empty script pub fn is_empty(&self) -> bool { self.0.is_empty() } - /// Convert the script into a byte vector - pub fn into_vec(self) -> Vec { self.0.into_vec() } + /// Returns the script data + pub fn as_bytes(&self) -> &[u8] { &*self.0 } - /// returns a copy of the script data - pub fn data (&self) -> Vec { self.0.clone().into_vec() } + /// Returns a copy of the script data + pub fn to_bytes(&self) -> Vec { self.0.clone().into_vec() } + + /// Convert the script into a byte vector + pub fn into_bytes(self) -> Vec { self.0.into_vec() } /// Compute the P2SH output corresponding to this redeem script pub fn to_p2sh(&self) -> Script { diff --git a/src/internal_macros.rs b/src/internal_macros.rs index a4c5e00e..5f756c20 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -79,8 +79,16 @@ macro_rules! impl_array_newtype { pub fn is_empty(&self) -> bool { false } #[inline] - /// Returns the underlying data. - pub fn data(&self) -> [$ty; $len] { self.0.clone() } + /// Returns the underlying bytes. + pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 } + + #[inline] + /// Returns the underlying bytes. + pub fn to_bytes(&self) -> [$ty; $len] { self.0.clone() } + + #[inline] + /// Returns the underlying bytes. + pub fn into_bytes(self) -> [$ty; $len] { self.0 } } impl<'a> From<&'a [$ty]> for $thing { diff --git a/src/util/address.rs b/src/util/address.rs index 1d2cee45..593f8970 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -116,7 +116,7 @@ impl Address { Address { network: network, payload: Payload::ScriptHash( - Hash160::from_data(builder.into_script().into_vec().as_slice()) + Hash160::from_data(builder.into_script().as_bytes()) ) } } @@ -127,7 +127,7 @@ impl Address { use crypto::digest::Digest; let mut digest = Sha256::new(); - digest.input(script.clone().into_vec().as_slice()); + digest.input(script.as_bytes()); let mut d = [0u8; 32]; digest.result(&mut d); @@ -151,14 +151,14 @@ impl Address { use crypto::digest::Digest; let mut digest = Sha256::new(); - digest.input(script.clone().into_vec().as_slice()); + digest.input(script.as_bytes()); let mut d = [0u8; 32]; digest.result(&mut d); let ws = script::Builder::new().push_int(0).push_slice(&d).into_script(); Address { network: network, - payload: Payload::ScriptHash(Hash160::from_data(ws.into_vec().as_slice())) + payload: Payload::ScriptHash(Hash160::from_data(ws.as_bytes())) } } diff --git a/src/util/hash.rs b/src/util/hash.rs index 0d96df9c..135b873c 100644 --- a/src/util/hash.rs +++ b/src/util/hash.rs @@ -510,8 +510,8 @@ mod tests { #[test] fn test_sha256d_data() { assert_eq!( - Sha256dHash::from_data(&[]).data(), - [ + Sha256dHash::from_data(&[]).as_bytes(), + &[ 0x5d, 0xf6, 0xe0, 0xe2, 0x76, 0x13, 0x59, 0xd3, 0x0a, 0x82, 0x75, 0x05, 0x8e, 0x29, 0x9f, 0xcc, 0x03, 0x81, 0x53, 0x45, 0x45, 0xf5, 0x5c, 0xf4, 0x3e, 0x41, 0x98, 0x3f, 0x5d, 0x4c, 0x94, 0x56,