-
Notifications
You must be signed in to change notification settings - Fork 197
Encoding library and standardizing usage #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
54c338c
Created cbor and json traits
austinabell 92adf5c
abstract variable hashing
austinabell fd6be66
abstract blake 256 hashing
austinabell aafd59e
test variable lengths
austinabell 48e0e11
remove irrelevant comments
austinabell 91985bc
fix error usage
austinabell 029b105
remove todo
austinabell 4d55e03
Clean error usage and remove JSON trait until needed
austinabell 0979885
switch blake2 hashing to more efficient lib
austinabell cb93889
Change trait function signature and changed format of error
austinabell 482a074
remove JSON encoding type
austinabell 1ccb4f1
change unmarshalling from requiring mutable ref
austinabell b239c4d
fix typo
austinabell c8a7114
change fixed hash to return a vector
austinabell 525242d
changed back to fixed array
austinabell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,5 +4,6 @@ members = [ | |
| "vm", | ||
| "vm/address", | ||
| "node", | ||
| "crypto" | ||
| "crypto", | ||
| "encoding" | ||
| ] | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "encoding" | ||
| version = "0.1.0" | ||
| authors = ["ChainSafe Systems <info@chainsafe.io>"] | ||
| edition = "2018" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| blake2b_simd = "0.5.9" | ||
| serde_cbor = "0.10.2" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| use super::errors::Error; | ||
|
|
||
| pub trait Cbor { | ||
| fn unmarshal_cbor(bz: &[u8]) -> Result<Self, Error> | ||
| where | ||
| Self: Sized; | ||
| fn marshal_cbor(&self) -> Result<Vec<u8>, Error>; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| use std::fmt; | ||
|
|
||
| /// Error type for encoding and decoding data through any Ferret supported protocol | ||
| /// | ||
| /// This error will provide any details about the data which was attempted to be | ||
| /// encoded or decoded. The | ||
| /// | ||
| /// Usage: | ||
| /// ```no_run | ||
| /// use encoding::{Error, CodecProtocol}; | ||
| /// | ||
| /// Error::Marshalling { | ||
| /// description: format!("{:?}", vec![0]), | ||
| /// protocol: CodecProtocol::Cbor, | ||
| /// }; | ||
| /// Error::Unmarshalling { | ||
| /// description: format!("{:?}", vec![0]), | ||
| /// protocol: CodecProtocol::Cbor, | ||
| /// }; | ||
| /// ``` | ||
| #[derive(Debug, PartialEq)] | ||
| pub enum Error { | ||
| Unmarshalling { | ||
| description: String, | ||
| protocol: CodecProtocol, | ||
| }, | ||
| Marshalling { | ||
| description: String, | ||
| protocol: CodecProtocol, | ||
| }, | ||
| } | ||
|
|
||
| impl fmt::Display for Error { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Error::Unmarshalling { | ||
| description, | ||
| protocol, | ||
| } => write!( | ||
| f, | ||
| "Could not decode in format {}: {}", | ||
| protocol, description | ||
| ), | ||
| Error::Marshalling { | ||
| description, | ||
| protocol, | ||
| } => write!( | ||
| f, | ||
| "Could not encode in format {}: {}", | ||
| protocol, description | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<serde_cbor::error::Error> for Error { | ||
| fn from(err: serde_cbor::error::Error) -> Error { | ||
| Error::Marshalling { | ||
| description: err.to_string(), | ||
| protocol: CodecProtocol::Cbor, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// CodecProtocol defines the protocol in which the data is encoded or decoded | ||
| /// | ||
| /// This is used with the encoding errors, to detail the encoding protocol or any other | ||
| /// information about how the data was encoded or decoded | ||
| #[derive(Debug, PartialEq)] | ||
| pub enum CodecProtocol { | ||
| Cbor, | ||
| } | ||
|
|
||
| impl fmt::Display for CodecProtocol { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match *self { | ||
| CodecProtocol::Cbor => write!(f, "Cbor"), | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use blake2b_simd::Params; | ||
|
|
||
| /// generates blake2b hash with provided size | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// use encoding::blake2b_variable; | ||
| /// | ||
| /// let ingest: Vec<u8> = vec![]; | ||
| /// let hash = blake2b_variable(ingest, 20); | ||
| /// assert_eq!(hash.len(), 20); | ||
| /// ``` | ||
| pub fn blake2b_variable(ingest: Vec<u8>, size: usize) -> Vec<u8> { | ||
| let hash = Params::new() | ||
| .hash_length(size) | ||
| .to_state() | ||
| .update(&ingest) | ||
| .finalize(); | ||
|
|
||
| hash.as_bytes().to_vec() | ||
| } | ||
|
|
||
| /// generates blake2b hash of fixed 32 bytes size | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// use encoding::blake2b_256; | ||
| /// | ||
| /// let ingest: Vec<u8> = vec![]; | ||
| /// let hash = blake2b_256(ingest); | ||
| /// assert_eq!(hash.len(), 32); | ||
| /// ``` | ||
| pub fn blake2b_256(ingest: Vec<u8>) -> [u8; 32] { | ||
| let digest = Params::new() | ||
| .hash_length(32) | ||
| .to_state() | ||
| .update(&ingest) | ||
| .finalize(); | ||
|
|
||
| let mut ret = [0u8; 32]; | ||
| ret.clone_from_slice(digest.as_bytes()); | ||
| ret | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn hash_length() { | ||
| let ingest = vec![1, 4, 2, 3]; | ||
| let hash = blake2b_variable(ingest.clone(), 8); | ||
| assert_eq!(hash.len(), 8); | ||
| let hash = blake2b_variable(ingest.clone(), 20); | ||
| assert_eq!(hash.len(), 20); | ||
| let hash = blake2b_variable(ingest.clone(), 32); | ||
| assert_eq!(hash.len(), 32); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| mod cbor; | ||
| mod errors; | ||
| mod hash; | ||
|
|
||
| pub use cbor::*; | ||
| pub use errors::*; | ||
| pub use hash::*; |
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this actually used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to create the 20 byte hash used as the address for secp256k1 and actor protocols