Skip to content

Commit

Permalink
Add a new zcash_serialize_bytes utility function (#2150)
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 authored May 14, 2021
1 parent adcd13b commit c032667
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions zebra-chain/src/serialization/zcash_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ impl<T: ZcashSerialize> ZcashSerialize for Vec<T> {
}
}

/// Serialize a byte vector as a compactsize number of items, then the items.
///
/// # Correctness
///
/// Most Zcash types have specific rules about serialization of `Vec<u8>`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<W: io::Write>(vec: &Vec<u8>, 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<T: ZcashSerialize> ZcashSerialize for AtLeastOne<T> {
Expand Down Expand Up @@ -101,10 +117,9 @@ pub fn zcash_serialize_bytes_external_count<W: io::Write>(

/// Write a Bitcoin-encoded UTF-8 `&str`.
impl ZcashSerialize for &str {
fn zcash_serialize<W: io::Write>(&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<W: io::Write>(&self, writer: W) -> Result<(), io::Error> {
let str_bytes = self.as_bytes().to_vec();
zcash_serialize_bytes(&str_bytes, writer)
}
}

Expand Down

0 comments on commit c032667

Please sign in to comment.