From c0326677a406f5ee9ece7c858b7d786ea9c83dd6 Mon Sep 17 00:00:00 2001 From: teor Date: Fri, 14 May 2021 12:23:02 +1000 Subject: [PATCH] Add a new `zcash_serialize_bytes` utility function (#2150) --- .../src/serialization/zcash_serialize.rs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/zebra-chain/src/serialization/zcash_serialize.rs b/zebra-chain/src/serialization/zcash_serialize.rs index 5c5b06969..8ebaac32c 100644 --- a/zebra-chain/src/serialization/zcash_serialize.rs +++ b/zebra-chain/src/serialization/zcash_serialize.rs @@ -40,6 +40,22 @@ impl ZcashSerialize for Vec { } } +/// Serialize a byte vector as a compactsize number of items, then the items. +/// +/// # Correctness +/// +/// Most Zcash types have specific rules about serialization of `Vec`s. +/// Check the spec and consensus rules before using this function. +/// +/// See `zcash_serialize_bytes_external_count` for more details, and usage information. +// +// we specifically want to serialize `Vec`s here, rather than generic slices +#[allow(clippy::ptr_arg)] +pub fn zcash_serialize_bytes(vec: &Vec, mut writer: W) -> Result<(), io::Error> { + writer.write_compactsize(vec.len() as u64)?; + zcash_serialize_bytes_external_count(vec, writer) +} + /// Serialize an `AtLeastOne` vector as a compactsize number of items, then the /// items. This is the most common format in Zcash. impl ZcashSerialize for AtLeastOne { @@ -101,10 +117,9 @@ pub fn zcash_serialize_bytes_external_count( /// Write a Bitcoin-encoded UTF-8 `&str`. impl ZcashSerialize for &str { - fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - let str_bytes = self.as_bytes(); - writer.write_compactsize(str_bytes.len() as u64)?; - writer.write_all(str_bytes) + fn zcash_serialize(&self, writer: W) -> Result<(), io::Error> { + let str_bytes = self.as_bytes().to_vec(); + zcash_serialize_bytes(&str_bytes, writer) } }