Skip to main content

heika_core/
hash.rs

1//! ハッシュ。
2
3use core::fmt;
4use core::str::FromStr;
5
6use crate::hex::{self, ParseHexError};
7
8/// 32バイトのハッシュ。catapult の `Hash256`。トランザクションやブロックのハッシュに使う。
9/// 文字列にするときは、Symbol の慣習に合わせて大文字の16進にする。
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Hash256(pub [u8; 32]);
12
13impl fmt::Display for Hash256 {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        f.write_str(&hex::encode(&self.0))
16    }
17}
18
19impl fmt::Debug for Hash256 {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        write!(f, "Hash256({self})")
22    }
23}
24
25impl FromStr for Hash256 {
26    type Err = ParseHexError;
27
28    fn from_str(text: &str) -> Result<Self, Self::Err> {
29        hex::decode(text).map(Self)
30    }
31}