Skip to content

Commit

Permalink
id: add StrictHash type
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Feb 10, 2024
1 parent a98ed20 commit 9f90575
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion commit_verify/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -125,3 +126,26 @@ impl<T: CommitEncode> CommitId for T {

fn commit_id(&self) -> Self::CommitmentId { self.commit().finish().into() }
}

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]

Check warning on line 130 in commit_verify/src/id.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/id.rs#L130

Added line #L130 was not covered by tests
#[wrapper(Deref, BorrowSlice, Display, FromStr, Hex, Index, RangeOps)]
#[derive(StrictDumb, strict_encoding::StrictType, StrictEncode, StrictDecode)]

Check warning on line 132 in commit_verify/src/id.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/id.rs#L132

Added line #L132 was not covered by tests
#[strict_type(lib = LIB_NAME_COMMIT_VERIFY)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 136 in commit_verify/src/id.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/id.rs#L136

Added line #L136 was not covered by tests
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<Sha256> for StrictHash {
fn from(hash: Sha256) -> Self { hash.finish().into() }

Check warning on line 150 in commit_verify/src/id.rs

View check run for this annotation

Codecov / codecov/patch

commit_verify/src/id.rs#L150

Added line #L150 was not covered by tests
}
2 changes: 1 addition & 1 deletion commit_verify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 9f90575

Please sign in to comment.