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
use super::derivers::{ParachainPalletGeneralIndexOriginDeriver, TinkernetMultisigSignedDeriver};
use core::marker::PhantomData;
use frame_support::traits::OriginTrait;
use xcm::v2::{Junction, Junctions, MultiLocation, OriginKind};
use xcm_executor::traits::ConvertOrigin;

pub struct DeriveOriginFromPalletInstanceGeneralIndex<Origin, Deriver>(
    PhantomData<(Origin, Deriver)>,
);
impl<Origin: OriginTrait, Deriver: ParachainPalletGeneralIndexOriginDeriver<Origin>>
    ConvertOrigin<Origin> for DeriveOriginFromPalletInstanceGeneralIndex<Origin, Deriver>
where
    Origin::AccountId: From<[u8; 32]>,
{
    fn convert_origin(
        origin: impl Into<MultiLocation>,
        kind: OriginKind,
    ) -> Result<Origin, MultiLocation> {
        let origin = origin.into();
        log::error!(
              target: "xcm::origin_conversion",
            "DeriveOriginFromPlurality origin: {:?}, kind: {:?}",
              origin, kind,
        );
        match (kind, origin.clone()) {
            (
                OriginKind::Native,
                MultiLocation {
                    parents: _,
                    interior:
                        Junctions::X3(
                            Junction::Parachain(para_id),
                            Junction::PalletInstance(pallet_index),
                            Junction::GeneralIndex(id),
                        ),
                },
            ) => Deriver::derive_account(para_id, pallet_index, id).ok_or(origin),
            (_, origin) => Err(origin),
        }
    }
}

pub type DeriveOriginFromTinkernetMultisig<Origin> =
    DeriveOriginFromPalletInstanceGeneralIndex<Origin, TinkernetMultisigSignedDeriver<Origin>>;