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
#![allow(clippy::exhaustive_enums)]
//! The Proposal Target System.

//! Target system format for Substrate
//! ┌─────────────────────┬───────┬───────┬─────────────┐
//! │                     │       │       │             │
//! │           Zeros     │ Pallet│ Call  │   Tree ID   │
//! │           (20B)     │  Idx  │  Idx  │    (4B)     │
//! │                     │       │       │             │
//! └─────────────────────┴───────┴───────┴─────────────┘
//!                       ▲       ▲       ▲             ▲
//!                       │   20  │   21  │ 22 23 24 25 │
//!                       │       │       │             │
//!
//! Target system format for Evm
//! ┌────────────────┬──────────────────────────────────┐
//! │                │                                  │
//! │      Zeros     │     Contract Address             │
//! │      (6B)      │           (20B)                  │
//! │                │                                  │
//! └────────────────┘──────────────────────────────────┘
//!                  ▲                                  ▲
//!                  │ 6                             25 │
//!                  │                                  │

#[cfg(all(not(feature = "std"), feature = "substrate"))]
use alloc::vec::Vec;

/// `TargetSystem` (26 Bytes)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "scale",
    derive(
        scale_info::TypeInfo,
        scale_codec::Encode,
        scale_codec::Decode,
        scale_codec::MaxEncodedLen
    )
)]
#[non_exhaustive]
pub enum TargetSystem {
    /// Ethereum Contract address (20 bytes).
    ContractAddress([u8; 20]),
    /// Webb Protocol-Substrate 6 bytes (pallet_index, call_index, tree_id ).
    #[cfg(any(feature = "substrate", feature = "ink"))]
    Substrate(SubstrateTargetSystem),
}

/// Substrate Target System
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, typed_builder::TypedBuilder,
)]
#[cfg_attr(
    feature = "scale",
    derive(
        scale_info::TypeInfo,
        scale_codec::Encode,
        scale_codec::Decode,
        scale_codec::MaxEncodedLen
    )
)]
#[cfg(any(feature = "substrate", feature = "ink"))]
#[allow(clippy::module_name_repetitions)]
pub struct SubstrateTargetSystem {
    /// Pallet index of proposal handler pallet
    pub pallet_index: u8,
    /// Webb Protocol Merkle `TreeId` (4 bytes).
    pub tree_id: u32,
}

impl TargetSystem {
    /// Length of the `TargetSystem` (26 bytes).
    pub const LENGTH: usize = 26;
    /// Create a new `TargetSystem` as a `ContractAddress`.
    #[must_use]
    pub fn new_contract_address<T: Into<[u8; 20]>>(address: T) -> Self {
        let bytes = address.into();
        Self::ContractAddress(bytes)
    }

    /// Get the underlying bytes of the `TargetSystem`.
    #[must_use]
    pub fn to_bytes(&self) -> [u8; Self::LENGTH] {
        match self {
            TargetSystem::ContractAddress(address) => {
                let mut bytes = [0u8; TargetSystem::LENGTH];
                let f = 6usize;
                let t = f + 20;
                bytes[f..t].copy_from_slice(address);
                bytes
            }
            #[cfg(any(feature = "substrate", feature = "ink"))]
            TargetSystem::Substrate(target_system) => {
                let mut bytes = [0u8; TargetSystem::LENGTH];
                let f = 22usize;
                let t = f + core::mem::size_of::<u32>();
                bytes[f - 1] = target_system.pallet_index;
                bytes[f..t]
                    .copy_from_slice(&target_system.tree_id.to_be_bytes());
                bytes
            }
        }
    }

    /// Get the underlying bytes of the `TargetSystem` without copying.
    #[must_use]
    pub fn into_bytes(self) -> [u8; Self::LENGTH] {
        self.to_bytes()
    }

    /// Get substrate `TargetSystem` details
    #[cfg(any(feature = "substrate", feature = "ink"))]
    #[must_use]
    pub fn get_substrate_target_system(self) -> Option<SubstrateTargetSystem> {
        match self {
            TargetSystem::Substrate(target_system) => Some(target_system),
            _ => None,
        }
    }

    /// Get smart contract address from the target system.
    /// Returns `[0; 20]` if the target system is not a contract address.
    #[must_use]
    pub fn into_contract_address_or_default(self) -> [u8; 20] {
        match self {
            TargetSystem::ContractAddress(address) => address,
            #[cfg(any(feature = "substrate", feature = "ink"))]
            _ => [0; 20],
        }
    }
}

impl From<[u8; TargetSystem::LENGTH]> for TargetSystem {
    fn from(bytes: [u8; TargetSystem::LENGTH]) -> Self {
        // check the first 20 bytes are zeros.
        // if so, it is a substrate target system.
        let substrate_based_system =
            cfg!(feature = "substrate") || cfg!(feature = "ink");
        if bytes[0..20] == [0u8; 20] && substrate_based_system {
            #[cfg(any(feature = "substrate", feature = "ink"))]
            {
                let mut tree_id_bytes = [0u8; 4];
                let f = 22usize;
                let t = f + core::mem::size_of::<u32>();
                tree_id_bytes.copy_from_slice(&bytes[f..t]);
                let tree_id = u32::from_be_bytes(tree_id_bytes);
                let target = SubstrateTargetSystem::builder()
                    .pallet_index(bytes[f - 1])
                    .tree_id(tree_id)
                    .build();
                TargetSystem::Substrate(target)
            }
            #[cfg(not(any(feature = "substrate", feature = "ink")))]
            {
                unreachable!("This should not happen, since substrate_based_system is false at compile time");
            }
        } else {
            let mut address = [0u8; 20];
            let f = 6usize;
            let t = f + 20;
            address.copy_from_slice(&bytes[f..t]);
            TargetSystem::ContractAddress(address)
        }
    }
}

impl From<TargetSystem> for [u8; TargetSystem::LENGTH] {
    fn from(target_system: TargetSystem) -> Self {
        target_system.into_bytes()
    }
}

#[cfg(feature = "substrate")]
impl Default for TargetSystem {
    fn default() -> Self {
        let target = SubstrateTargetSystem::builder()
            .pallet_index(0)
            .tree_id(0)
            .build();
        TargetSystem::Substrate(target)
    }
}