Last active
May 31, 2017 01:52
-
-
Save Latrasis/31aaf57186f67eacbe6e1a8188ffeafc to your computer and use it in GitHub Desktop.
Grin Encoding RFC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Current Traits: | |
pub trait Writeable { | |
/// Write the data held by this Writeable to the provided writer | |
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error>; | |
} | |
pub trait Readable where Self: Sized { | |
/// Reads the data necessary to this Readable from the provided reader | |
fn read(reader: &mut Reader) -> Result<Self, Error>; | |
} | |
pub trait Writer { | |
/// The mode this serializer is writing in | |
fn serialization_mode(&self) -> SerializationMode; | |
/// Write a T: Sized + AsRef<[u8]> | |
fn write_fixed_bytes<T: AsFixedBytes>(&mut self, fixed: &T) -> Result<(), Error>; | |
} | |
pub trait Reader { | |
// ... | |
} | |
// Alternative | |
pub trait Format { | |
Full, | |
Hash, | |
SigHash | |
} | |
pub trait Decode: Sized { | |
type DecodingError: From<Error>; | |
fn decode<B: bytes::Buf>(&self, src: &mut B) -> Result<Option<Self>, Self::DecodingError>; | |
} | |
pub trait Encode: Sized { | |
type EncodingError: From<Error>; | |
fn encode<B: bytes::BufMut>(&self, dst: &mut B, f: Format) -> Result<(), Self::EncodingError>; | |
} | |
pub trait IntoHash: Sized { | |
fn as_hash(&self) -> grin_core::hash::Hash; | |
} | |
impl IntoHash for Encode { | |
fn as_hash(&self) -> grin_core::hash::Hash { | |
let mut hasher = Keccak::new_sha3_256(); | |
let mut bytes = BytesMut::with_capacity(0); | |
let mut ret = [0; 32]; | |
self.encode(&mut bytes, Format::Hash); | |
hasher.update(bytes.as_ref()); | |
hasher.finalize(&mut ret); | |
Hash(ret) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment