-
Notifications
You must be signed in to change notification settings - Fork 1
feat(rust/signed-doc): Comment and Proposal Document Validation #207
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 38 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
7014694
wip(rust/signed_doc): add comment document type
saibatizoku 25107e6
Merge remote-tracking branch 'origin/main' into feat/cat-sign-doc-val…
saibatizoku c2a8d7b
wip(rust/signed_doc): add comment document validation
saibatizoku 1b0cbb5
Merge remote-tracking branch 'origin/main' into feat/cat-sign-doc-val…
saibatizoku 8b9622b
chore(docs): fix spelling
saibatizoku 5bb6cc4
fix(rust/signed-doc): signed docs implement Clone
saibatizoku b5d70cb
Merge remote-tracking branch 'origin/main' into feat/cat-sign-doc-val…
saibatizoku c6f3a04
Merge branch 'main' into feat/cat-sign-doc-validator
Mr-Leshiy 1ce8a3c
add proposal document validation
Mr-Leshiy b54623a
refactor
Mr-Leshiy d710304
wip
Mr-Leshiy 49b541e
add full template validation
Mr-Leshiy d173bf4
wip
Mr-Leshiy f9ec1fc
replace trait with Fn
Mr-Leshiy 6630c02
wip
Mr-Leshiy 2182445
wip
Mr-Leshiy cd4301c
wip
Mr-Leshiy 40478ae
make signed doc signature validation async
Mr-Leshiy c287e3a
make a separate trait
Mr-Leshiy fc8d5ba
make all validation async
Mr-Leshiy 3c9506b
Merge branch 'main' into feat/cat-sign-doc-validator
Mr-Leshiy 2c2bd38
fix
Mr-Leshiy fb9e256
refactor
Mr-Leshiy 613a958
add more rules
Mr-Leshiy bfd27c0
add section rule
Mr-Leshiy 7efe2e2
cleanup
Mr-Leshiy 09e1b2c
refactor
Mr-Leshiy 6d933fb
added new Section struct
Mr-Leshiy 20929de
wip
Mr-Leshiy d60579c
add utils mod
Mr-Leshiy 38104bb
wip
Mr-Leshiy 249f0b4
fix signature test
Mr-Leshiy 40c82d2
refactor
Mr-Leshiy b7bbe15
finilize validation logic
Mr-Leshiy 8121f33
Merge branch 'main' into feat/cat-sign-doc-validator
Mr-Leshiy 5b42d74
fix spelling
Mr-Leshiy f2273d3
fix clippy lints
Mr-Leshiy 0dece66
Merge branch 'main' into feat/cat-sign-doc-validator
Mr-Leshiy 75e0a65
fix
Mr-Leshiy 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
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 |
|---|---|---|
| @@ -1,93 +1,92 @@ | ||
| //! Catalyst Signed Document Content Payload | ||
|
|
||
| use catalyst_types::problem_report::ProblemReport; | ||
|
|
||
| use crate::metadata::{ContentEncoding, ContentType}; | ||
|
|
||
| /// Decompressed Document Content type bytes. | ||
| #[derive(Debug, Clone, PartialEq)] | ||
| #[derive(Debug, Clone, PartialEq, Default)] | ||
| pub struct Content { | ||
| /// Content data bytes | ||
| data: Vec<u8>, | ||
| /// Content type | ||
| content_type: ContentType, | ||
| /// Content encoding | ||
| content_encoding: Option<ContentEncoding>, | ||
| /// Original Decompressed Document's data bytes | ||
| data: Option<Vec<u8>>, | ||
| } | ||
|
|
||
| impl Content { | ||
| /// Creates a new `Content` value, from the encoded data. | ||
| /// verifies a Document's content, that it is correctly encoded and it corresponds and | ||
| /// parsed to the specified type | ||
| /// | ||
| /// # Errors | ||
| /// Returns an error if content is not correctly encoded | ||
| pub(crate) fn from_encoded( | ||
| mut data: Vec<u8>, content_type: ContentType, content_encoding: Option<ContentEncoding>, | ||
| ) -> anyhow::Result<Self> { | ||
| if let Some(encoding) = content_encoding { | ||
| data = encoding | ||
| .decode(&data) | ||
| .map_err(|e| anyhow::anyhow!("Failed to decode {encoding} content: {e}"))?; | ||
| mut data: Vec<u8>, content_type: Option<ContentType>, | ||
| content_encoding: Option<ContentEncoding>, report: &ProblemReport, | ||
| ) -> Self { | ||
| if let Some(content_encoding) = content_encoding { | ||
| if let Ok(decoded_data) = content_encoding.decode(&data) { | ||
| data = decoded_data; | ||
| } else { | ||
| report.invalid_value( | ||
| "payload", | ||
| &hex::encode(&data), | ||
| &format!("Invalid Document content, should {content_encoding} encodable"), | ||
| "Invalid Document content type.", | ||
| ); | ||
| return Self::default(); | ||
| } | ||
| } | ||
| if let Some(content_type) = content_type { | ||
| if content_type.validate(&data).is_err() { | ||
| report.invalid_value( | ||
| "payload", | ||
| &hex::encode(&data), | ||
| &format!("Invalid Document content type, should {content_type} encodable"), | ||
| "Invalid Document content type.", | ||
| ); | ||
| return Self::default(); | ||
| } | ||
| } | ||
| content_type.validate(&data)?; | ||
|
|
||
| Ok(Self { | ||
| data, | ||
| content_type, | ||
| content_encoding, | ||
| }) | ||
| Self { data: Some(data) } | ||
| } | ||
|
|
||
| /// Creates a new `Content` value, from the decoded (original) data. | ||
| /// verifies that it corresponds and parsed to the specified type. | ||
| /// | ||
| /// # Errors | ||
| /// Returns an error if content is not correctly encoded | ||
| pub(crate) fn from_decoded( | ||
| data: Vec<u8>, content_type: ContentType, content_encoding: Option<ContentEncoding>, | ||
| ) -> anyhow::Result<Self> { | ||
| pub(crate) fn from_decoded(data: Vec<u8>, content_type: ContentType) -> anyhow::Result<Self> { | ||
| content_type.validate(&data)?; | ||
| Ok(Self { | ||
| data, | ||
| content_type, | ||
| content_encoding, | ||
| }) | ||
| } | ||
|
|
||
| /// Return `true` if Document's content type is Json | ||
| #[must_use] | ||
| pub fn is_json(&self) -> bool { | ||
| matches!(self.content_type, ContentType::Json) | ||
| Ok(Self { data: Some(data) }) | ||
| } | ||
|
|
||
| /// Return `true` if Document's content type is Json | ||
| #[must_use] | ||
| pub fn is_cbor(&self) -> bool { | ||
| matches!(self.content_type, ContentType::Cbor) | ||
| } | ||
|
|
||
| /// Return an decoded (original) content bytes, | ||
| /// by the corresponding `content_encoding` provided field. | ||
| #[must_use] | ||
| pub fn decoded_bytes(&self) -> &[u8] { | ||
| &self.data | ||
| /// Return an decoded (original) content bytes. | ||
| /// | ||
| /// # Errors | ||
| /// - Missing Document content | ||
| pub fn decoded_bytes(&self) -> anyhow::Result<&[u8]> { | ||
| self.data | ||
| .as_deref() | ||
| .ok_or(anyhow::anyhow!("Missing Document content")) | ||
| } | ||
|
|
||
| /// Return an encoded content bytes, | ||
| /// by the corresponding `content_encoding` provided field | ||
| pub(crate) fn encoded_bytes(&self) -> anyhow::Result<Vec<u8>> { | ||
| if let Some(encoding) = self.content_encoding { | ||
| let data = encoding | ||
| .encode(&self.data) | ||
| .map_err(|e| anyhow::anyhow!("Failed to encode {encoding} content: {e}"))?; | ||
| Ok(data) | ||
| } else { | ||
| Ok(self.data.clone()) | ||
| } | ||
| /// by the provided `content_encoding` provided field. | ||
| /// | ||
| /// # Errors | ||
| /// - Missing Document content | ||
| /// - Failed to encode content. | ||
| pub(crate) fn encoded_bytes( | ||
| &self, content_encoding: ContentEncoding, | ||
| ) -> anyhow::Result<Vec<u8>> { | ||
| let content = self.decoded_bytes()?; | ||
| let data = content_encoding | ||
| .encode(content) | ||
| .map_err(|e| anyhow::anyhow!("Failed to encode {content_encoding} content: {e}"))?; | ||
| Ok(data) | ||
| } | ||
|
|
||
| /// Return content byte size | ||
| /// Return content byte size. | ||
| /// If content is empty returns `0`. | ||
| #[must_use] | ||
| pub fn size(&self) -> usize { | ||
| self.data.len() | ||
| self.data.as_ref().map(Vec::len).unwrap_or_default() | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.