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
use codec::{Decode, Encode};
use scale_info::{
    prelude::{fmt::Debug, vec::Vec},
    TypeInfo,
};

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Clone, Encode, Decode, Eq, Hash, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum HasherAlgo {
    Blake2,
    Keccak256,
}

#[derive(PartialEq, Clone, Encode, Decode, Eq, Hash, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum CryptoAlgo {
    Ed25519,
    Sr25519,
    Ecdsa,
}

#[derive(PartialEq, Clone, Encode, Decode, Eq, Hash, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
/// Describe ABI configuration for a gateway so that it's possible to cast types
/// of inbound and outbound messages to that gateway
pub struct GatewayABISpecs {
    /// block number type in bytes
    pub block_number_size: u8,
    /// hash size in bytes
    pub hash_size: u8,
    /// hashing algorithm
    pub hasher: u8,
    /// cryptography algorithm
    pub crypto: u8,
    /// address length in bytes
    pub address_size: u8,
    /// value length in bytes
    pub value_type_size: u8,
}

#[derive(PartialEq, Clone, Encode, Decode, Eq, Hash, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
/// Describe ABI configuration for a gateway so that it's possible to cast types
/// of inbound and outbound messages to that gateway
pub struct GatewayABIConfig {
    /// block number type in bytes
    pub block_number_type_size: u16,
    /// hash size in bytes
    pub hash_size: u16,
    /// hashing algorithm
    pub hasher: HasherAlgo,
    /// cryptography algorithm
    pub crypto: CryptoAlgo,
    /// address length in bytes
    pub address_length: u16,
    /// value length in bytes
    pub value_type_size: u16,
}

impl Default for GatewayABIConfig {
    fn default() -> GatewayABIConfig {
        GatewayABIConfig {
            block_number_type_size: 32,
            hash_size: 32,
            hasher: HasherAlgo::Blake2,
            crypto: CryptoAlgo::Sr25519,
            address_length: 32,  // 32 bytes : 32 * 8 = 256 bits
            value_type_size: 16, // u128 = 16 bytes = 128 bits.
        }
    }
}

#[derive(PartialEq, Clone, Encode, Decode, Eq, Hash, Debug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct ContractActionDesc<Hash, TargetId, AccountId> {
    pub action_id: Hash,
    pub target_id: Option<TargetId>,
    pub to: Option<AccountId>,
}

pub fn decode_buf2val<D: Decode>(buf: Vec<u8>) -> Result<D, &'static str> {
    D::decode(&mut &buf[..]).map_err(|_| "Decoding error: decode_buf2val")
}