catbuffer_rust/lock_hash_algorithm_dto.rs
1/*
2 * // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
3 * // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
4 * // All rights reserved.
5 * //
6 * // This file is part of Catapult.
7 * //
8 * // Catapult is free software: you can redistribute it and/or modify
9 * // it under the terms of the GNU Lesser General Public License as published by
10 * // the Free Software Foundation, either version 3 of the License, or
11 * // (at your option) any later version.
12 * //
13 * // Catapult is distributed in the hope that it will be useful,
14 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * // GNU Lesser General Public License for more details.
17 * //
18 * // You should have received a copy of the GNU Lesser General Public License
19 * // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22use num_derive::{FromPrimitive, ToPrimitive};
23use num_traits::{FromPrimitive, ToPrimitive};
24use strum_macros::EnumIter;
25
26use super::generator_utils::*;
27
28/// Enumeration of lock hash algorithms.
29#[allow(non_camel_case_types)]
30#[repr(u8)]
31#[derive(Debug, Clone, Copy, PartialEq, FromPrimitive, ToPrimitive, EnumIter)]
32pub enum LockHashAlgorithmDto {
33 /// Input is hashed using sha-3 256.
34 SHA3_256 = 0,
35
36 /// Input is hashed twice: first with sha-256 and then with ripemd-160 (bitcoin's OP_HASH160).
37 HASH_160 = 1,
38
39 /// Input is hashed twice with sha-256 (bitcoin's OP_HASH256).
40 HASH_256 = 2,
41
42}
43
44impl LockHashAlgorithmDto {
45 pub const LENGTH: usize = std::mem::size_of::<Self>();
46
47 /// Gets the size of the type.
48 ///
49 /// # Returns
50 ///
51 /// A usize.
52 pub fn get_size(&self) -> usize {
53 Self::LENGTH
54 }
55
56 /// Gets the value of the enum.
57 ///
58 /// # Returns
59 ///
60 /// A u8
61 pub fn get_value(&self) -> u8 {
62 self.to_u8().unwrap()
63 }
64
65
66 /// Creates an `LockHashAlgorithmDto` from a slice.
67 ///
68 /// # Returns
69 ///
70 /// A `LockHashAlgorithmDto`.
71 pub fn from_binary(src: &[u8]) -> Self {
72 // assert_eq!(src.len(), Self::LENGTH);
73 let buf = fixed_bytes::<{ Self::LENGTH }>(src);
74 Self::from_u8(u8::from_le_bytes(buf)).unwrap()
75 }
76
77 /// Serializes an type to bytes.
78 ///
79 /// # Returns
80 ///
81 /// A Serialized bytes.
82 pub fn serializer(&self) -> Vec<u8> {
83 self.get_value().to_le_bytes().to_vec()
84 }
85}