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
//! Crate containing various primitives for container chain
18
//! token transfers.
19

            
20
#![cfg_attr(not(feature = "std"), no_std)]
21
extern crate alloc;
22

            
23
pub mod sovereign_paid_remote_exporter;
24

            
25
use {
26
    core::marker::PhantomData,
27
    cumulus_primitives_core::{Ethereum, GlobalConsensus, Location},
28
    frame_support::pallet_prelude::Encode,
29
    sp_core::blake2_256,
30
    sp_runtime::app_crypto::sp_core,
31
};
32

            
33
pub use xcm_executor::traits::ConvertLocation;
34

            
35
pub struct ContainerChainEthereumLocationConverter<AccountId>(PhantomData<AccountId>);
36

            
37
impl<AccountId> ConvertLocation<AccountId> for ContainerChainEthereumLocationConverter<AccountId>
38
where
39
    AccountId: From<[u8; 32]> + Clone,
40
{
41
14
    fn convert_location(location: &Location) -> Option<AccountId> {
42
14
        match location.unpack() {
43
            (1, [GlobalConsensus(Ethereum { chain_id })]) => {
44
                Some(Self::from_chain_id(chain_id).into())
45
            }
46
14
            (2, [GlobalConsensus(Ethereum { chain_id })]) => {
47
14
                Some(Self::from_chain_id(chain_id).into())
48
            }
49
            _ => None,
50
        }
51
14
    }
52
}
53

            
54
impl<AccountId> ContainerChainEthereumLocationConverter<AccountId> {
55
14
    pub fn from_chain_id(chain_id: &u64) -> [u8; 32] {
56
14
        (b"ethereum-chain", chain_id).using_encoded(blake2_256)
57
14
    }
58
    pub fn from_chain_id_with_key(chain_id: &u64, key: [u8; 20]) -> [u8; 32] {
59
        (b"ethereum-chain", chain_id, key).using_encoded(blake2_256)
60
    }
61
}