Skip to content

Commit

Permalink
validate parsed ErgoTree header,
Browse files Browse the repository at this point in the history
check no bytes left unconsumed after ErgoTree is parsed;
  • Loading branch information
greenhat committed Feb 3, 2023
1 parent 2e8c645 commit af93336
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 221 deletions.
6 changes: 6 additions & 0 deletions ergotree-ir/src/chain/ergo_box/box_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl From<BoxId> for Vec<i8> {
}
}

impl std::fmt::Display for BoxId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl SigmaSerializable for BoxId {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
self.0.scorex_serialize(w)?;
Expand Down
11 changes: 8 additions & 3 deletions ergotree-ir/src/chain/json/ergo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ impl TryFrom<ErgoBoxJson> for ErgoBox {
if ergo_box.box_id() == box_id {
Ok(ergo_box)
} else {
Err(ErgoBoxFromJsonError::InvalidBoxId)
Err(ErgoBoxFromJsonError::InvalidBoxId {
json: box_id,
actual: ergo_box.box_id(),
})
}
}
None => Ok(ergo_box),
Expand Down Expand Up @@ -180,8 +183,10 @@ impl TryFrom<ErgoBoxCandidateJson> for ErgoBoxCandidate {
#[derive(Error, PartialEq, Eq, Debug, Clone)]
pub enum ErgoBoxFromJsonError {
/// Box id parsed from JSON differs from calculated from box serialized bytes
#[error("Box id parsed from JSON differs from calculated from box serialized bytes")]
InvalidBoxId,
#[error(
"Box id parsed from JSON {json} differs from calculated from box serialized bytes {actual}"
)]
InvalidBoxId { json: BoxId, actual: BoxId },
/// Box serialization failed (id calculation)
#[error("Box serialization failed (id calculation): {0}")]
SerializationError(#[from] SigmaSerializationError),
Expand Down

0 comments on commit af93336

Please sign in to comment.