Add a new `zcash_serialize_bytes` utility function (#2150)

This commit is contained in:
teor 2021-05-14 12:23:02 +10:00 committed by GitHub
parent adcd13bdd3
commit c0326677a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 4 deletions

View File

@ -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> {
@ -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)
}
}