From 9f9057555d5d8907f3537069044f7503b81c3ee7 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 10 Feb 2024 16:52:16 +0100 Subject: [PATCH] id: add StrictHash type --- commit_verify/src/id.rs | 26 +++++++++++++++++++++++++- commit_verify/src/lib.rs | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/commit_verify/src/id.rs b/commit_verify/src/id.rs index 47024207..dde6bcfa 100644 --- a/commit_verify/src/id.rs +++ b/commit_verify/src/id.rs @@ -19,11 +19,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +use amplify::Bytes32; use sha2::Sha256; use strict_encoding::{StrictEncode, StrictType}; use strict_types::typesys::TypeFqn; -use crate::DigestExt; +use crate::{DigestExt, LIB_NAME_COMMIT_VERIFY}; pub struct CommitEngine { finished: bool, @@ -125,3 +126,26 @@ impl CommitId for T { fn commit_id(&self) -> Self::CommitmentId { self.commit().finish().into() } } + +#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] +#[wrapper(Deref, BorrowSlice, Display, FromStr, Hex, Index, RangeOps)] +#[derive(StrictDumb, strict_encoding::StrictType, StrictEncode, StrictDecode)] +#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)] +#[cfg_attr( + feature = "serde", + derive(Serialize, Deserialize), + serde(crate = "serde_crate", transparent) +)] +pub struct StrictHash( + #[from] + #[from([u8; 32])] + Bytes32, +); + +impl CommitmentId for StrictHash { + const TAG: &'static str = "urn:ubideco:strict-types:value-hash#2024-02-10"; +} + +impl From for StrictHash { + fn from(hash: Sha256) -> Self { hash.finish().into() } +} diff --git a/commit_verify/src/lib.rs b/commit_verify/src/lib.rs index 51eedafe..00ea5ada 100644 --- a/commit_verify/src/lib.rs +++ b/commit_verify/src/lib.rs @@ -62,7 +62,7 @@ pub use conceal::Conceal; pub use convolve::{ConvolveCommit, ConvolveCommitProof, ConvolveVerifyError}; pub use digest::{Digest, DigestExt, Ripemd160, Sha256}; pub use embed::{EmbedCommitProof, EmbedCommitVerify, EmbedVerifyError, VerifyEq}; -pub use id::{CommitEncode, CommitEngine, CommitId, CommitmentId, CommitmentLayout}; +pub use id::{CommitEncode, CommitEngine, CommitId, CommitmentId, CommitmentLayout, StrictHash}; pub use merkle::{MerkleBuoy, MerkleHash, MerkleLeaves, MerkleNode, NodeBranching}; pub const LIB_NAME_COMMIT_VERIFY: &str = "CommitVerify";