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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use crate::{value_json_hash, CidRef, CID, JSON_CODEC};

use fluence_blake3 as blake3;
use serde::Serialize;
use thiserror::Error as ThisError;

use std::convert::TryInto;
use std::rc::Rc;

#[derive(ThisError, Debug)]
pub enum CidVerificationError {
    #[error("Value mismatch in the {type_name:?} store for CID {cid_repr:?}")]
    ValueMismatch {
        // nb: type_name is std::any::type_name() result that may be inconsistent between the Rust compiler versions
        type_name: &'static str,
        cid_repr: Rc<CidRef>,
    },

    #[error("JSON error: {0}")]
    InvalidJson(#[from] serde_json::Error),
    #[error(transparent)]
    MalformedCid(#[from] cid::Error),
    #[error("unsupported CID codec: {0}")]
    UnsupportedCidCodec(u64),
    #[error("unsupported multihash code: {0}")]
    UnsupportedHashCode(u64),
}

pub fn verify_value<Val: Serialize>(
    cid: &CID<Val>,
    value: &Val,
) -> Result<(), CidVerificationError> {
    let real_cid: cid::Cid = cid.try_into()?;

    let codec = real_cid.codec();
    match codec {
        JSON_CODEC => verify_json_value(real_cid.hash(), value, cid),
        _ => Err(CidVerificationError::UnsupportedCidCodec(codec)),
    }
}

pub fn verify_raw_value<Val>(
    cid: &CID<Val>,
    raw_value: impl AsRef<[u8]>,
) -> Result<(), CidVerificationError> {
    use digest::Digest;
    use multihash_codetable::Code;

    let real_cid: cid::Cid = cid.try_into()?;

    let codec = real_cid.codec();
    // we insist ATM that raw values should be JSON-encoded, but
    // we do not validate that it is valid JSON data
    if codec != JSON_CODEC {
        return Err(CidVerificationError::UnsupportedCidCodec(codec));
    }

    let mhash = real_cid.hash();
    let raw_code = mhash.code();

    let code: Code = raw_code
        .try_into()
        .map_err(|_| CidVerificationError::UnsupportedHashCode(raw_code))?;

    let expected_hash = match code {
        Code::Sha2_256 => {
            let mut hasher = sha2::Sha256::new();
            hasher.update(raw_value);
            hasher.finalize().to_vec()
        }
        Code::Blake3_256 => {
            let mut hasher = blake3::Hasher::new();
            hasher.update(raw_value.as_ref());
            hasher.finalize().to_vec()
        }
        _ => return Err(CidVerificationError::UnsupportedHashCode(raw_code)),
    };
    // actually, multihash may contain less bytes than the full hash; to avoid abuse, we reject such multihashes
    if expected_hash == mhash.digest() {
        Ok(())
    } else {
        Err(CidVerificationError::ValueMismatch {
            type_name: std::any::type_name::<Val>(),
            cid_repr: cid.get_inner(),
        })
    }
}

fn verify_json_value<Val: Serialize>(
    mhash: &multihash_codetable::Multihash,
    value: &Val,
    cid: &CID<Val>,
) -> Result<(), CidVerificationError> {
    use multihash_codetable::Code;

    let raw_code = mhash.code();
    let code: Code = raw_code
        .try_into()
        .map_err(|_| CidVerificationError::UnsupportedHashCode(raw_code))?;

    let expected_hash = match code {
        Code::Sha2_256 => value_json_hash::<sha2::Sha256, Val>(value)?,
        Code::Blake3_256 => value_json_hash::<blake3::Hasher, Val>(value)?,
        _ => return Err(CidVerificationError::UnsupportedHashCode(raw_code)),
    };
    // actually, multihash may contain less bytes than the full hash; to avoid abuse, we reject such multihashes
    if expected_hash == mhash.digest() {
        Ok(())
    } else {
        Err(CidVerificationError::ValueMismatch {
            type_name: std::any::type_name::<Val>(),
            cid_repr: cid.get_inner(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use multihash::Multihash;
    use serde_json::json;

    #[test]
    fn test_verify_sha2_256() {
        verify_value(
            &CID::new("bagaaierajwlhumardpzj6dv2ahcerm3vyfrjwl7nahg7zq5o3eprwv6v3vpa"),
            &json!("test"),
        )
        .unwrap();
        verify_value(
            &CID::new("bagaaierauyk65lxcdxsrphpaqdpiymcszdnjaejyibv2ohbyyaziix35kt2a"),
            &json!([1, 2, 3]),
        )
        .unwrap();
        verify_value(
            &CID::new("bagaaieranodle477gt6odhllqbhp6wr7k5d23jhkuixr2soadzjn3n4hlnfq"),
            &json!(1),
        )
        .unwrap();
        verify_value(
            &CID::new("bagaaierad7lci6475zdrps4h6fmcpmqyknz5z6bw6p6tmpjkfyueavqw4kaq"),
            &json!({"key": 42}),
        )
        .unwrap();
    }

    #[test]
    fn test_verify_blake3() {
        verify_value(
            &CID::new("z3v8BBKBcZMDh6ANTaiT7PmfrBWbBmoVQvDxojXt1M4eczFDmhF"),
            &json!("test"),
        )
        .unwrap();
        verify_value(
            &CID::new("z3v8BBK9PYQwY7AGn9wb79BFTzSQiLALGAEmyqSYbCV2D9y8RLw"),
            &json!([1, 2, 3]),
        )
        .unwrap();
        verify_value(
            &CID::new("z3v8BBKGqF5gxukC6oU2EsSnTD7hBRorAabGJ8UDpNKneW7UApe"),
            &json!(1),
        )
        .unwrap();
        verify_value(
            &CID::new("z3v8BBK3kqxb39bomB9bJQ22a734aidv5C7QmjdfKiePgVjdQUQ"),
            &json!({"key": 42}),
        )
        .unwrap();
    }

    #[test]
    fn test_incorrect_value() {
        // CID of json!(1)
        let cid_1 = CID::new("bagaaieranodle477gt6odhllqbhp6wr7k5d23jhkuixr2soadzjn3n4hlnfq");
        let err = verify_value(&cid_1, &json!(2));
        assert!(
            matches!(err, Err(CidVerificationError::ValueMismatch { .. })),
            "{:?}",
            err
        );
    }

    #[test]
    fn test_verify_unknown_codec() {
        use std::str::FromStr;

        //  git raw object
        const GIT_RAW_CODEC: u64 = 0x78;
        // CID of json!(1)
        let cid_1 =
            cid::Cid::from_str("bagaaieranodle477gt6odhllqbhp6wr7k5d23jhkuixr2soadzjn3n4hlnfq")
                .unwrap();

        let unknown_format_cid =
            cid::Cid::new(cid::Version::V1, GIT_RAW_CODEC, cid_1.hash().clone()).unwrap();
        let unknown_format_cid = CID::new(unknown_format_cid.to_string());

        let err = verify_value(&unknown_format_cid, &json!(1));
        match err {
            Err(CidVerificationError::UnsupportedCidCodec(codec)) => {
                assert_eq!(codec, GIT_RAW_CODEC);
            }
            _ => panic!("wrong result: {:?}", err),
        }
    }

    #[test]
    fn test_verify_unknown_hasher() {
        use std::str::FromStr;

        const SHAKE_128_CODE: u64 = 0x18;

        let cid_1 =
            cid::Cid::from_str("bagaaieranodle477gt6odhllqbhp6wr7k5d23jhkuixr2soadzjn3n4hlnfq")
                .unwrap();

        let unknown_hasher_multihash =
            Multihash::wrap(SHAKE_128_CODE, cid_1.hash().digest()).unwrap();

        let unknown_hasher_cid =
            cid::Cid::new(cid::Version::V1, JSON_CODEC, unknown_hasher_multihash).unwrap();
        let unknown_hasher_cid = CID::new(unknown_hasher_cid.to_string());

        let err = verify_value(&unknown_hasher_cid, &json!(1));
        match err {
            Err(CidVerificationError::UnsupportedHashCode(code)) => {
                assert_eq!(code, SHAKE_128_CODE);
            }
            _ => panic!("wrong result: {:?}", err),
        }
    }

    #[test]
    fn test_verify_unsupported_hasher() {
        use multihash_codetable::Code;
        use std::str::FromStr;

        // we have no plan to support it, but it may change, and the test should be corrected
        let ripemd160_code: u64 = Code::Ripemd160.into();

        let cid_1 =
            cid::Cid::from_str("bagaaieranodle477gt6odhllqbhp6wr7k5d23jhkuixr2soadzjn3n4hlnfq")
                .unwrap();

        let unknown_hasher_multihash =
            Multihash::wrap(ripemd160_code, cid_1.hash().digest()).unwrap();

        let unknown_hasher_cid =
            cid::Cid::new(cid::Version::V1, JSON_CODEC, unknown_hasher_multihash).unwrap();
        let unknown_hasher_cid = CID::new(unknown_hasher_cid.to_string());

        let err = verify_value(&unknown_hasher_cid, &json!(1));
        match err {
            Err(CidVerificationError::UnsupportedHashCode(code)) => {
                assert_eq!(code, ripemd160_code);
            }
            _ => panic!("wrong result: {:?}", err),
        }
    }

    #[test]
    fn test_verify_garbage() {
        let garbage_cid = CID::new("garbage");
        let err = verify_value(&garbage_cid, &json!(1));
        assert!(
            matches!(
                err,
                Err(CidVerificationError::MalformedCid(cid::Error::ParsingError))
            ),
            "{:?}",
            err
        );
    }
}