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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! module to provide some handy interfaces atop the hashes so we have
//! the common interfaces for the project to work with.

use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::{error, fmt, result};

use cryptoxide::blake2b::Blake2b;
use cryptoxide::digest::Digest as _;
use hex::FromHexError;

use typed_bytes::ByteSlice;

use crate::bech32::{self, Bech32};
use crate::hash::Blake2b256;

#[derive(Debug, PartialEq, Clone)]
pub enum Error {
    InvalidDigestSize { got: usize, expected: usize },
    InvalidHexEncoding(FromHexError),
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::InvalidDigestSize { got: sz, expected } => write!(
                f,
                "invalid digest size, expected {} but received {} bytes.",
                expected, sz
            ),
            Error::InvalidHexEncoding(_) => write!(f, "invalid hex encoding for digest value"),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::InvalidDigestSize {
                got: _,
                expected: _,
            } => None,
            Error::InvalidHexEncoding(err) => Some(err),
        }
    }
}

impl From<FromHexError> for Error {
    fn from(err: FromHexError) -> Self {
        Error::InvalidHexEncoding(err)
    }
}

#[derive(Debug, Copy, Clone)]
pub struct TryFromSliceError(());

impl fmt::Display for TryFromSliceError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt("could not convert slice to digest", f)
    }
}

pub trait DigestAlg {
    const HASH_SIZE: usize;
    type DigestData: Clone + PartialEq + Hash + Send + AsRef<[u8]>;
    type DigestContext: Clone;

    fn try_from_slice(slice: &[u8]) -> Result<Self::DigestData, Error>;
    fn new() -> Self::DigestContext;
    fn append_data(ctx: &mut Self::DigestContext, data: &[u8]);
    fn finalize(ctx: Self::DigestContext) -> Self::DigestData;
}

impl DigestAlg for Blake2b256 {
    const HASH_SIZE: usize = 32;
    type DigestData = [u8; Self::HASH_SIZE];
    type DigestContext = Blake2b;

    fn try_from_slice(slice: &[u8]) -> Result<Self::DigestData, Error> {
        if slice.len() == Self::HASH_SIZE {
            let mut out = [0u8; Self::HASH_SIZE];
            out.copy_from_slice(slice);
            Ok(out)
        } else {
            Err(Error::InvalidDigestSize {
                expected: Self::HASH_SIZE,
                got: slice.len(),
            })
        }
    }

    fn new() -> Self::DigestContext {
        Blake2b::new(Self::HASH_SIZE)
    }

    fn append_data(ctx: &mut Self::DigestContext, data: &[u8]) {
        ctx.input(data)
    }

    fn finalize(mut ctx: Self::DigestContext) -> Self::DigestData {
        let mut out: Self::DigestData = [0; Self::HASH_SIZE];
        ctx.result(&mut out);
        out
    }
}

/// A Digest Context for the H digest algorithm
pub struct Context<H: DigestAlg>(H::DigestContext);

impl<H: DigestAlg> Clone for Context<H> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<H: DigestAlg> Default for Context<H> {
    fn default() -> Self {
        Self::new()
    }
}

impl<H: DigestAlg> Context<H> {
    /// Create a new digest context
    pub fn new() -> Self {
        Self(H::new())
    }

    /// Append data in the context
    pub fn append_data(&mut self, data: &[u8]) {
        H::append_data(&mut self.0, data)
    }

    /// Finalize a context and create a digest
    pub fn finalize(self) -> Digest<H> {
        Digest(H::finalize(self.0))
    }
}

#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct Digest<H: DigestAlg>(H::DigestData);

impl<H: DigestAlg> Clone for Digest<H> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

macro_rules! define_from_instances {
    ($hash_ty:ty, $hash_size:expr, $bech32_hrp:expr) => {
        impl From<Digest<$hash_ty>> for [u8; $hash_size] {
            fn from(digest: Digest<$hash_ty>) -> Self {
                digest.0
            }
        }

        impl<'a> From<&'a Digest<$hash_ty>> for &'a [u8; $hash_size] {
            fn from(digest: &'a Digest<$hash_ty>) -> Self {
                &digest.0
            }
        }

        impl From<[u8; $hash_size]> for Digest<$hash_ty> {
            fn from(bytes: [u8; $hash_size]) -> Self {
                Digest(bytes)
            }
        }
        impl From<$hash_ty> for Digest<$hash_ty> {
            fn from(bytes: $hash_ty) -> Self {
                let out: [u8; $hash_size] = bytes.into();
                out.into()
            }
        }
        impl Bech32 for Digest<$hash_ty> {
            const BECH32_HRP: &'static str = $bech32_hrp;
            const BYTES_LEN: usize = $hash_size;

            fn try_from_bech32_str(bech32_str: &str) -> bech32::Result<Self> {
                let bytes = bech32::try_from_bech32_to_bytes::<Self>(bech32_str)?;
                Digest::try_from(&bytes[..]).map_err(bech32::Error::data_invalid)
            }

            fn to_bech32_str(&self) -> String {
                bech32::to_bech32_from_bytes::<Self>(self.as_ref())
            }
        }
    };
}

define_from_instances!(Blake2b256, 32, "blake2b");

impl<H: DigestAlg> PartialEq for Digest<H> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<H: DigestAlg> Eq for Digest<H> {}

impl<H: DigestAlg> PartialOrd for Digest<H> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<H: DigestAlg> Ord for Digest<H> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_ref().cmp(other.as_ref())
    }
}

impl<H: DigestAlg> AsRef<[u8]> for Digest<H> {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl<H: DigestAlg> Hash for Digest<H> {
    fn hash<HA: Hasher>(&self, state: &mut HA) {
        self.0.hash(state)
    }
}

impl<H: DigestAlg> TryFrom<&[u8]> for Digest<H> {
    type Error = Error;
    fn try_from(slice: &[u8]) -> Result<Digest<H>, Self::Error> {
        <H as DigestAlg>::try_from_slice(slice).map(Digest)
    }
}

impl<H: DigestAlg> FromStr for Digest<H> {
    type Err = Error;
    fn from_str(s: &str) -> result::Result<Digest<H>, Self::Err> {
        let bytes = hex::decode(s)?;
        Digest::try_from(&bytes[..])
    }
}

impl<H: DigestAlg> fmt::Display for Digest<H> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", hex::encode(self.as_ref()))
    }
}
impl<H: DigestAlg> fmt::Debug for Digest<H> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(concat!(stringify!($hash_ty), "(0x"))?;
        write!(f, "{}", hex::encode(self.as_ref()))?;
        f.write_str(")")
    }
}

impl<H: DigestAlg> Digest<H> {
    /// Get the digest of a slice of data.
    // We name the constructor this way to highlight that a potentially
    // costly digest computation takes place.
    #[allow(clippy::self_named_constructors)]
    pub fn digest(slice: &[u8]) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(slice);
        ctx.finalize()
    }
}

use std::marker::PhantomData;

/// A typed version of Digest
#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct DigestOf<H: DigestAlg, T> {
    inner: Digest<H>,
    marker: PhantomData<T>,
}

impl<H: DigestAlg, T> Clone for DigestOf<H, T> {
    fn clone(&self) -> Self {
        DigestOf {
            inner: self.inner.clone(),
            marker: self.marker,
        }
    }
}

impl<H: DigestAlg, T> From<DigestOf<H, T>> for Digest<H> {
    fn from(d: DigestOf<H, T>) -> Self {
        d.inner
    }
}

impl<H: DigestAlg, T> From<Digest<H>> for DigestOf<H, T> {
    fn from(d: Digest<H>) -> Self {
        DigestOf {
            inner: d,
            marker: PhantomData,
        }
    }
}

impl<H: DigestAlg, T> PartialEq for DigestOf<H, T> {
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

impl<H: DigestAlg, T> Eq for DigestOf<H, T> {}

impl<H: DigestAlg, T> PartialOrd for DigestOf<H, T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> Ord for DigestOf<H, T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl<H: DigestAlg, T> AsRef<[u8]> for DigestOf<H, T> {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_ref()
    }
}

impl<H: DigestAlg, T> Hash for DigestOf<H, T> {
    fn hash<HA: Hasher>(&self, state: &mut HA) {
        self.inner.hash(state)
    }
}

impl<H: DigestAlg, T> TryFrom<&[u8]> for DigestOf<H, T> {
    type Error = Error;
    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        Digest::<H>::try_from(slice).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> FromStr for DigestOf<H, T> {
    type Err = Error;
    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        Digest::<H>::from_str(s).map(|d| d.into())
    }
}

impl<H: DigestAlg, T> fmt::Display for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}
impl<H: DigestAlg, T> fmt::Debug for DigestOf<H, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl<H: DigestAlg, T> DigestOf<H, T> {
    /// Coerce a digest of T, to a digest of U
    pub fn coerce<U>(&self) -> DigestOf<H, U> {
        DigestOf {
            inner: self.inner.clone(),
            marker: PhantomData,
        }
    }

    pub fn digest_byteslice(byteslice: &ByteSlice<T>) -> Self {
        let mut ctx = Context::new();
        ctx.append_data(byteslice.as_slice());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its AsRef<[u8]> implementation
    pub fn digest(obj: &T) -> Self
    where
        T: AsRef<[u8]>,
    {
        let mut ctx = Context::new();
        ctx.append_data(obj.as_ref());
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }

    /// Get the digest of object T, given its serialization function in closure
    pub fn digest_with<F>(obj: &T, f: F) -> Self
    where
        F: FnOnce(&T) -> &[u8],
    {
        let mut ctx = Context::new();
        ctx.append_data(f(obj));
        DigestOf {
            inner: ctx.finalize(),
            marker: PhantomData,
        }
    }
}

macro_rules! typed_define_from_instances {
    ($hash_ty:ty, $hash_size:expr, $bech32_hrp:expr) => {
        impl<T> From<DigestOf<$hash_ty, T>> for [u8; $hash_size] {
            fn from(digest: DigestOf<$hash_ty, T>) -> Self {
                digest.inner.into()
            }
        }
        impl<'a, T> From<&'a DigestOf<$hash_ty, T>> for &'a [u8; $hash_size] {
            fn from(digest: &'a DigestOf<$hash_ty, T>) -> Self {
                (&digest.inner).into()
            }
        }

        impl<T> From<[u8; $hash_size]> for DigestOf<$hash_ty, T> {
            fn from(bytes: [u8; $hash_size]) -> Self {
                Digest::from(bytes).into()
            }
        }

        impl<T> From<$hash_ty> for DigestOf<$hash_ty, T> {
            fn from(bytes: $hash_ty) -> Self {
                let out: [u8; $hash_size] = bytes.into();
                out.into()
            }
        }
        impl<T> Bech32 for DigestOf<$hash_ty, T> {
            const BECH32_HRP: &'static str = $bech32_hrp;
            const BYTES_LEN: usize = $hash_size;

            fn try_from_bech32_str(bech32_str: &str) -> bech32::Result<Self> {
                let bytes = bech32::try_from_bech32_to_bytes::<Self>(bech32_str)?;
                Digest::try_from(&bytes[..])
                    .map_err(bech32::Error::data_invalid)
                    .map(|d| d.into())
            }

            fn to_bech32_str(&self) -> String {
                bech32::to_bech32_from_bytes::<Self>(self.inner.as_ref())
            }
        }
    };
}

typed_define_from_instances!(Blake2b256, 32, "blake2b");