1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
mod eth_token_local_processor;
18
mod inbound_token_transfer_validator;
19
mod native_container_tokens_processor;
20
mod native_token_transfer_data;
21
mod native_token_transfer_processor;
22

            
23
use core::marker::PhantomData;
24
pub use eth_token_local_processor::EthTokensLocalProcessor;
25
pub use inbound_token_transfer_validator::InboundTokenTransferValidator;
26
pub use native_container_tokens_processor::NativeContainerTokensProcessor;
27
pub use native_token_transfer_data::NativeTokenTransferData;
28
pub use native_token_transfer_processor::NativeTokenTransferMessageProcessor;
29
use snowbridge_core::Channel;
30
use snowbridge_inbound_queue_primitives::v1::Envelope;
31
use sp_core::Get;
32

            
33
/// Validates the gateway and channel of an inbound envelope
34
pub struct GatewayAndChannelValidator<T>(PhantomData<T>);
35
impl<T> GatewayAndChannelValidator<T>
36
where
37
    T: snowbridge_pallet_inbound_queue::Config + pallet_ethereum_token_transfers::Config,
38
{
39
104
    pub fn validate_gateway_and_channel(channel: &Channel, envelope: &Envelope) -> bool {
40
        // Ensure that the message is intended for the current channel, para_id and agent_id
41
104
        if let Some(channel_info) = pallet_ethereum_token_transfers::CurrentChannelInfo::<T>::get()
42
        {
43
100
            if envelope.channel_id != channel_info.channel_id
44
98
                || channel.para_id != channel_info.para_id
45
96
                || channel.agent_id != channel_info.agent_id
46
            {
47
6
                log::debug!(
48
                    "Unexpected channel id: {:?} != {:?}",
49
                    (envelope.channel_id, channel.para_id, channel.agent_id),
50
                    (
51
                        channel_info.channel_id,
52
                        channel_info.para_id,
53
                        channel_info.agent_id
54
                    )
55
                );
56
6
                return false;
57
94
            }
58
        } else {
59
4
            log::warn!("CurrentChannelInfo not set in storage");
60
4
            return false;
61
        }
62

            
63
        // Check it is from the right gateway
64
94
        if envelope.gateway != T::GatewayAddress::get() {
65
4
            log::warn!("Wrong gateway address: {:?}", envelope.gateway);
66
4
            return false;
67
90
        }
68
90
        true
69
104
    }
70
}