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
// Copyright 2021 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Duniter-v2S is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use frame_support::pallet_prelude::*;
use frame_support::weights::Weight;

/// A trait defining operations for checking if identity-related calls are allowed.
pub trait CheckIdtyCallAllowed<T: Config> {
    /// Checks if creating an identity is allowed.
    fn check_create_identity(creator: T::IdtyIndex) -> Result<(), DispatchError>;
}

impl<T: Config> CheckIdtyCallAllowed<T> for () {
    fn check_create_identity(_creator: T::IdtyIndex) -> Result<(), DispatchError> {
        Ok(())
    }
}

/// A trait defining operations for validating identity names.
pub trait IdtyNameValidator {
    /// Validates an identity name.
    fn validate(idty_name: &IdtyName) -> bool;
}

/// A trait defining behavior for handling new identities creation.
pub trait OnNewIdty<T: Config> {
    /// Called when a new identity is created.
    fn on_created(idty_index: &T::IdtyIndex, creator: &T::IdtyIndex);
}

/// A trait defining behavior for handling removed identities.
/// As the weight accounting can be complicated it should be done
/// at the handler level.
pub trait OnRemoveIdty<T: Config> {
    /// Called when an identity is removed.
    fn on_removed(idty_index: &T::IdtyIndex) -> Weight;
    /// Called when an identity is revoked.
    fn on_revoked(idty_index: &T::IdtyIndex) -> Weight;
}

impl<T: Config> OnNewIdty<T> for () {
    fn on_created(_idty_index: &T::IdtyIndex, _creator: &T::IdtyIndex) {}
}

impl<T: Config> OnRemoveIdty<T> for () {
    fn on_removed(_idty_index: &T::IdtyIndex) -> Weight {
        Weight::zero()
    }

    fn on_revoked(_idty_index: &T::IdtyIndex) -> Weight {
        Weight::zero()
    }
}

/// A trait defining operations for linking identities to accounts.
pub trait LinkIdty<AccountId, IdtyIndex> {
    /// Links an identity to an account.
    fn link_identity(account_id: &AccountId, idty_index: IdtyIndex) -> Result<(), DispatchError>;
}

impl<AccountId, IdtyIndex> LinkIdty<AccountId, IdtyIndex> for () {
    fn link_identity(_: &AccountId, _: IdtyIndex) -> Result<(), DispatchError> {
        Ok(())
    }
}