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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// RGB Core Library: a reference implementation of RGB smart contract standards.
// Written in 2019-2022 by
//     Dr. Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the MIT License along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use bitcoin::hashes::{sha256, sha256t};
use commit_verify::{
    commit_encode, CommitConceal, CommitVerify, ConsensusCommit, PrehashedProtocol, TaggedHash,
};
use stens::AsciiString;
use strict_encoding::{strict_serialize, StrictEncode};

use crate::{ConfidentialState, RevealedState};

// "rgb:container:id"
static MIDSTATE_ATTACHMENT_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,
];
// "rgb:container:confidential"
static MIDSTATE_CONFIDENTIAL_ATTACHMENT: [u8; 32] = [
    91, 91, 44, 25, 205, 155, 231, 106, 244, 163, 175, 204, 49, 17, 129, 52, 227, 151, 9, 5, 246,
    42, 1, 226, 126, 43, 141, 177, 84, 100, 61, 108,
];

/// Tag used for [`AttachmentId`] hash type
pub struct AttachmentIdTag;

impl sha256t::Tag for AttachmentIdTag {
    #[inline]
    fn engine() -> sha256::HashEngine {
        let midstate = sha256::Midstate::from_inner(MIDSTATE_ATTACHMENT_ID);
        sha256::HashEngine::from_midstate(midstate, 64)
    }
}

/// Unique data attachment identifier
#[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 AttachmentId(sha256t::Hash<AttachmentIdTag>);

impl<Msg> CommitVerify<Msg, PrehashedProtocol> for AttachmentId
where Msg: AsRef<[u8]>
{
    #[inline]
    fn commit(msg: &Msg) -> AttachmentId { AttachmentId::hash(msg) }
}

/// Tag used for [`Confidential`] hash type
pub struct ConfidentialAttachmentTag;

impl sha256t::Tag for ConfidentialAttachmentTag {
    #[inline]
    fn engine() -> sha256::HashEngine {
        let midstate = sha256::Midstate::from_inner(MIDSTATE_CONFIDENTIAL_ATTACHMENT);
        sha256::HashEngine::from_midstate(midstate, 64)
    }
}

/// Confidential representation of data attachment information
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", transparent)
)]
#[derive(Wrapper, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, From, AsAny)]
#[derive(StrictEncode, StrictDecode)]
#[wrapper(Debug, Display)]
pub struct Confidential(sha256t::Hash<ConfidentialAttachmentTag>);

impl commit_encode::Strategy for Confidential {
    type Strategy = commit_encode::strategies::UsingStrict;
}

impl ConsensusCommit for Confidential {
    type Commitment = AttachmentId;
}

impl ConfidentialState for Confidential {}

impl Confidential {
    pub fn attachment_id(&self) -> AttachmentId { self.consensus_commit() }
}

#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash, AsAny, Display)]
#[derive(StrictEncode, StrictDecode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
#[display("{id}~{mime}")]
pub struct Revealed {
    pub id: AttachmentId,
    pub mime: AsciiString,
    pub salt: u64,
}

impl CommitConceal for Revealed {
    type ConcealedCommitment = Confidential;

    fn commit_conceal(&self) -> Self::ConcealedCommitment {
        Confidential::hash(
            &strict_serialize(self).expect("Encoding of predefined data types must not fail"),
        )
    }
}
impl commit_encode::Strategy for Revealed {
    type Strategy = commit_encode::strategies::UsingConceal;
}

impl RevealedState for Revealed {}

#[cfg(test)]
mod test {
    use amplify::Wrapper;
    use commit_verify::tagged_hash;

    use super::*;

    #[test]
    fn test_attachment_id_midstate() {
        let midstate = tagged_hash::Midstate::with(b"rgb:container:id");
        assert_eq!(midstate.into_inner().into_inner(), MIDSTATE_ATTACHMENT_ID);
    }

    #[test]
    fn test_confidential_midstate() {
        let midstate = tagged_hash::Midstate::with(b"rgb:container:confidential");
        assert_eq!(
            midstate.into_inner().into_inner(),
            MIDSTATE_CONFIDENTIAL_ATTACHMENT
        );
    }
}