1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use bitcoin_hashes::{sha256, sha256t};
use commit_verify::{commit_encode, CommitVerify, ConsensusCommit, PrehashedProtocol, TaggedHash};
use stens::AsciiString;
use strict_encoding::StrictEncode;
use crate::ChunkId;
static MIDSTATE_CONTAINER_ID: [u8; 32] = [
12, 61, 136, 60, 191, 129, 135, 229, 141, 35, 41, 161, 203, 125, 0, 101, 109, 136, 50, 236, 7,
101, 59, 39, 148, 207, 63, 236, 255, 48, 24, 171,
];
pub struct ContainerIdTag;
impl sha256t::Tag for ContainerIdTag {
#[inline]
fn engine() -> sha256::HashEngine {
let midstate = sha256::Midstate::from_inner(MIDSTATE_CONTAINER_ID);
sha256::HashEngine::from_midstate(midstate, 64)
}
}
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
#[derive(Wrapper, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, From)]
#[derive(StrictEncode, StrictDecode)]
#[wrapper(Debug, Display)]
pub struct ContainerId(sha256t::Hash<ContainerIdTag>);
impl<Msg> CommitVerify<Msg, PrehashedProtocol> for ContainerId
where Msg: AsRef<[u8]>
{
#[inline]
fn commit(msg: &Msg) -> ContainerId { ContainerId::hash(msg) }
}
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash, AsAny)]
#[derive(StrictEncode, StrictDecode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
pub struct Container {
pub mime: AsciiString,
pub size: u64,
pub chuhnks: Vec<ChunkId>,
}
impl commit_encode::Strategy for Container {
type Strategy = commit_encode::strategies::UsingStrict;
}
impl ConsensusCommit for Container {
type Commitment = ContainerId;
}
impl Container {
pub fn container_id(&self) -> ContainerId { self.consensus_commit() }
}
#[cfg(test)]
mod test {
use amplify::Wrapper;
use commit_verify::tagged_hash;
use super::*;
#[test]
fn test_container_id_midstate() {
let midstate = tagged_hash::Midstate::with(b"storm:container");
assert_eq!(midstate.into_inner().into_inner(), MIDSTATE_CONTAINER_ID);
}
}