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
use crate::{
    abi::{TypeAbi, TypeName},
    codec,
    codec::derive::{NestedDecode, NestedEncode, TopDecode, TopEncode},
};

/// Message hash type for the `verifyCustomSecp256k1` CryptoApi function
#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug)]
pub enum MessageHashType {
    ECDSAPlainMsg,
    ECDSASha256,
    ECDSADoubleSha256,
    ECDSAKeccak256,
    ECDSARipemd160,
}

impl MessageHashType {
    pub fn as_u8(&self) -> u8 {
        match self {
            Self::ECDSAPlainMsg => 0,
            Self::ECDSASha256 => 1,
            Self::ECDSADoubleSha256 => 2,
            Self::ECDSAKeccak256 => 3,
            Self::ECDSARipemd160 => 4,
        }
    }
}

impl From<u8> for MessageHashType {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::ECDSASha256,
            2 => Self::ECDSADoubleSha256,
            3 => Self::ECDSAKeccak256,
            4 => Self::ECDSARipemd160,
            _ => Self::ECDSAPlainMsg,
        }
    }
}

impl TypeAbi for MessageHashType {
    fn type_name() -> TypeName {
        "MessageHashType".into()
    }
}