#![cfg_attr(not(feature = "std"), no_std)]
mod benchmarking;
pub mod legacy;
pub mod migration;
#[cfg(test)]
mod tests;
mod types;
pub mod weights;
extern crate alloc;
use crate::types::{AuthorityProperties, Provider, Suffix, Username, UsernameInformation};
use alloc::{boxed::Box, vec::Vec};
use codec::Encode;
use frame_support::{
ensure,
pallet_prelude::{DispatchError, DispatchResult},
traits::{
BalanceStatus, Currency, Defensive, Get, OnUnbalanced, ReservableCurrency, StorageVersion,
},
BoundedVec,
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use sp_runtime::traits::{
AppendZerosInput, Hash, IdentifyAccount, Saturating, StaticLookup, Verify, Zero,
};
pub use types::{
Data, IdentityInformationProvider, Judgement, RegistrarIndex, RegistrarInfo, Registration,
};
pub use weights::WeightInfo;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type ProviderOf<T> = Provider<BalanceOf<T>>;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper<Public, Signature> {
fn sign_message(message: &[u8]) -> (Public, Signature);
}
#[cfg(feature = "runtime-benchmarks")]
impl BenchmarkHelper<sp_runtime::MultiSigner, sp_runtime::MultiSignature> for () {
fn sign_message(message: &[u8]) -> (sp_runtime::MultiSigner, sp_runtime::MultiSignature) {
let public = sp_io::crypto::sr25519_generate(0.into(), None);
let signature = sp_runtime::MultiSignature::Sr25519(
sp_io::crypto::sr25519_sign(
0.into(),
&public.into_account().try_into().unwrap(),
message,
)
.unwrap(),
);
(public.into(), signature)
}
}
#[pallet::config]
pub trait Config: frame_system::Config {
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: ReservableCurrency<Self::AccountId>;
#[pallet::constant]
type BasicDeposit: Get<BalanceOf<Self>>;
#[pallet::constant]
type ByteDeposit: Get<BalanceOf<Self>>;
#[pallet::constant]
type UsernameDeposit: Get<BalanceOf<Self>>;
#[pallet::constant]
type SubAccountDeposit: Get<BalanceOf<Self>>;
#[pallet::constant]
type MaxSubAccounts: Get<u32>;
type IdentityInformation: IdentityInformationProvider;
#[pallet::constant]
type MaxRegistrars: Get<u32>;
type Slashed: OnUnbalanced<NegativeImbalanceOf<Self>>;
type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type RegistrarOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type OffchainSignature: Verify<Signer = Self::SigningPublicKey> + Parameter;
type SigningPublicKey: IdentifyAccount<AccountId = Self::AccountId>;
type UsernameAuthorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
#[pallet::constant]
type PendingUsernameExpiration: Get<BlockNumberFor<Self>>;
#[pallet::constant]
type UsernameGracePeriod: Get<BlockNumberFor<Self>>;
#[pallet::constant]
type MaxSuffixLength: Get<u32>;
#[pallet::constant]
type MaxUsernameLength: Get<u32>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<Self::SigningPublicKey, Self::OffchainSignature>;
type WeightInfo: WeightInfo;
}
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::storage]
pub type IdentityOf<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
Registration<BalanceOf<T>, T::MaxRegistrars, T::IdentityInformation>,
OptionQuery,
>;
#[pallet::storage]
pub type UsernameOf<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, Username<T>, OptionQuery>;
#[pallet::storage]
pub type SuperOf<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, (T::AccountId, Data), OptionQuery>;
#[pallet::storage]
pub type SubsOf<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
(BalanceOf<T>, BoundedVec<T::AccountId, T::MaxSubAccounts>),
ValueQuery,
>;
#[pallet::storage]
pub type Registrars<T: Config> = StorageValue<
_,
BoundedVec<
Option<
RegistrarInfo<
BalanceOf<T>,
T::AccountId,
<T::IdentityInformation as IdentityInformationProvider>::FieldsIdentifier,
>,
>,
T::MaxRegistrars,
>,
ValueQuery,
>;
#[pallet::storage]
pub type AuthorityOf<T: Config> =
StorageMap<_, Twox64Concat, Suffix<T>, AuthorityProperties<T::AccountId>, OptionQuery>;
#[pallet::storage]
pub type UsernameInfoOf<T: Config> = StorageMap<
_,
Blake2_128Concat,
Username<T>,
UsernameInformation<T::AccountId, BalanceOf<T>>,
OptionQuery,
>;
#[pallet::storage]
pub type PendingUsernames<T: Config> = StorageMap<
_,
Blake2_128Concat,
Username<T>,
(T::AccountId, BlockNumberFor<T>, ProviderOf<T>),
OptionQuery,
>;
#[pallet::storage]
pub type UnbindingUsernames<T: Config> =
StorageMap<_, Blake2_128Concat, Username<T>, BlockNumberFor<T>, OptionQuery>;
#[pallet::error]
pub enum Error<T> {
TooManySubAccounts,
NotFound,
NotNamed,
EmptyIndex,
FeeChanged,
NoIdentity,
StickyJudgement,
JudgementGiven,
InvalidJudgement,
InvalidIndex,
InvalidTarget,
TooManyRegistrars,
AlreadyClaimed,
NotSub,
NotOwned,
JudgementForDifferentIdentity,
JudgementPaymentFailed,
InvalidSuffix,
NotUsernameAuthority,
NoAllocation,
InvalidSignature,
RequiresSignature,
InvalidUsername,
UsernameTaken,
NoUsername,
NotExpired,
TooEarly,
NotUnbinding,
AlreadyUnbinding,
InsufficientPrivileges,
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
IdentitySet { who: T::AccountId },
IdentityCleared { who: T::AccountId, deposit: BalanceOf<T> },
IdentityKilled { who: T::AccountId, deposit: BalanceOf<T> },
JudgementRequested { who: T::AccountId, registrar_index: RegistrarIndex },
JudgementUnrequested { who: T::AccountId, registrar_index: RegistrarIndex },
JudgementGiven { target: T::AccountId, registrar_index: RegistrarIndex },
RegistrarAdded { registrar_index: RegistrarIndex },
SubIdentityAdded { sub: T::AccountId, main: T::AccountId, deposit: BalanceOf<T> },
SubIdentitiesSet { main: T::AccountId, number_of_subs: u32, new_deposit: BalanceOf<T> },
SubIdentityRenamed { sub: T::AccountId, main: T::AccountId },
SubIdentityRemoved { sub: T::AccountId, main: T::AccountId, deposit: BalanceOf<T> },
SubIdentityRevoked { sub: T::AccountId, main: T::AccountId, deposit: BalanceOf<T> },
AuthorityAdded { authority: T::AccountId },
AuthorityRemoved { authority: T::AccountId },
UsernameSet { who: T::AccountId, username: Username<T> },
UsernameQueued { who: T::AccountId, username: Username<T>, expiration: BlockNumberFor<T> },
PreapprovalExpired { whose: T::AccountId },
PrimaryUsernameSet { who: T::AccountId, username: Username<T> },
DanglingUsernameRemoved { who: T::AccountId, username: Username<T> },
UsernameUnbound { username: Username<T> },
UsernameRemoved { username: Username<T> },
UsernameKilled { username: Username<T> },
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::add_registrar(T::MaxRegistrars::get()))]
pub fn add_registrar(
origin: OriginFor<T>,
account: AccountIdLookupOf<T>,
) -> DispatchResultWithPostInfo {
T::RegistrarOrigin::ensure_origin(origin)?;
let account = T::Lookup::lookup(account)?;
let (i, registrar_count) = Registrars::<T>::try_mutate(
|registrars| -> Result<(RegistrarIndex, usize), DispatchError> {
registrars
.try_push(Some(RegistrarInfo {
account,
fee: Zero::zero(),
fields: Default::default(),
}))
.map_err(|_| Error::<T>::TooManyRegistrars)?;
Ok(((registrars.len() - 1) as RegistrarIndex, registrars.len()))
},
)?;
Self::deposit_event(Event::RegistrarAdded { registrar_index: i });
Ok(Some(T::WeightInfo::add_registrar(registrar_count as u32)).into())
}
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::set_identity(T::MaxRegistrars::get()))]
pub fn set_identity(
origin: OriginFor<T>,
info: Box<T::IdentityInformation>,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let mut id = match IdentityOf::<T>::get(&sender) {
Some(mut id) => {
id.judgements.retain(|j| j.1.is_sticky());
id.info = *info;
id
},
None => Registration {
info: *info,
judgements: BoundedVec::default(),
deposit: Zero::zero(),
},
};
let new_deposit = Self::calculate_identity_deposit(&id.info);
let old_deposit = id.deposit;
Self::rejig_deposit(&sender, old_deposit, new_deposit)?;
id.deposit = new_deposit;
let judgements = id.judgements.len();
IdentityOf::<T>::insert(&sender, id);
Self::deposit_event(Event::IdentitySet { who: sender });
Ok(Some(T::WeightInfo::set_identity(judgements as u32)).into())
}
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::set_subs_old(T::MaxSubAccounts::get())
.saturating_add(T::WeightInfo::set_subs_new(subs.len() as u32))
)]
pub fn set_subs(
origin: OriginFor<T>,
subs: Vec<(T::AccountId, Data)>,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
ensure!(IdentityOf::<T>::contains_key(&sender), Error::<T>::NotFound);
ensure!(
subs.len() <= T::MaxSubAccounts::get() as usize,
Error::<T>::TooManySubAccounts
);
let (old_deposit, old_ids) = SubsOf::<T>::get(&sender);
let new_deposit = Self::subs_deposit(subs.len() as u32);
let not_other_sub =
subs.iter().filter_map(|i| SuperOf::<T>::get(&i.0)).all(|i| i.0 == sender);
ensure!(not_other_sub, Error::<T>::AlreadyClaimed);
if old_deposit < new_deposit {
T::Currency::reserve(&sender, new_deposit - old_deposit)?;
} else if old_deposit > new_deposit {
let err_amount = T::Currency::unreserve(&sender, old_deposit - new_deposit);
debug_assert!(err_amount.is_zero());
}
for s in old_ids.iter() {
SuperOf::<T>::remove(s);
}
let mut ids = BoundedVec::<T::AccountId, T::MaxSubAccounts>::default();
for (id, name) in subs {
SuperOf::<T>::insert(&id, (sender.clone(), name));
ids.try_push(id).expect("subs length is less than T::MaxSubAccounts; qed");
}
let new_subs = ids.len();
if ids.is_empty() {
SubsOf::<T>::remove(&sender);
} else {
SubsOf::<T>::insert(&sender, (new_deposit, ids));
}
Self::deposit_event(Event::SubIdentitiesSet {
main: sender,
number_of_subs: new_subs as u32,
new_deposit,
});
Ok(Some(
T::WeightInfo::set_subs_old(old_ids.len() as u32) .saturating_add(T::WeightInfo::set_subs_new(new_subs as u32)),
)
.into())
}
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::clear_identity(
T::MaxRegistrars::get(),
T::MaxSubAccounts::get(),
))]
pub fn clear_identity(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let (subs_deposit, sub_ids) = SubsOf::<T>::take(&sender);
let id = IdentityOf::<T>::take(&sender).ok_or(Error::<T>::NoIdentity)?;
let deposit = id.total_deposit().saturating_add(subs_deposit);
for sub in sub_ids.iter() {
SuperOf::<T>::remove(sub);
}
let err_amount = T::Currency::unreserve(&sender, deposit);
debug_assert!(err_amount.is_zero());
Self::deposit_event(Event::IdentityCleared { who: sender, deposit });
#[allow(deprecated)]
Ok(Some(T::WeightInfo::clear_identity(
id.judgements.len() as u32,
sub_ids.len() as u32,
))
.into())
}
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::request_judgement(T::MaxRegistrars::get(),))]
pub fn request_judgement(
origin: OriginFor<T>,
#[pallet::compact] reg_index: RegistrarIndex,
#[pallet::compact] max_fee: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let registrars = Registrars::<T>::get();
let registrar = registrars
.get(reg_index as usize)
.and_then(Option::as_ref)
.ok_or(Error::<T>::EmptyIndex)?;
ensure!(max_fee >= registrar.fee, Error::<T>::FeeChanged);
let mut id = IdentityOf::<T>::get(&sender).ok_or(Error::<T>::NoIdentity)?;
let item = (reg_index, Judgement::FeePaid(registrar.fee));
match id.judgements.binary_search_by_key(®_index, |x| x.0) {
Ok(i) =>
if id.judgements[i].1.is_sticky() {
return Err(Error::<T>::StickyJudgement.into())
} else {
id.judgements[i] = item
},
Err(i) =>
id.judgements.try_insert(i, item).map_err(|_| Error::<T>::TooManyRegistrars)?,
}
T::Currency::reserve(&sender, registrar.fee)?;
let judgements = id.judgements.len();
IdentityOf::<T>::insert(&sender, id);
Self::deposit_event(Event::JudgementRequested {
who: sender,
registrar_index: reg_index,
});
Ok(Some(T::WeightInfo::request_judgement(judgements as u32)).into())
}
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::cancel_request(T::MaxRegistrars::get()))]
pub fn cancel_request(
origin: OriginFor<T>,
reg_index: RegistrarIndex,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let mut id = IdentityOf::<T>::get(&sender).ok_or(Error::<T>::NoIdentity)?;
let pos = id
.judgements
.binary_search_by_key(®_index, |x| x.0)
.map_err(|_| Error::<T>::NotFound)?;
let fee = if let Judgement::FeePaid(fee) = id.judgements.remove(pos).1 {
fee
} else {
return Err(Error::<T>::JudgementGiven.into())
};
let err_amount = T::Currency::unreserve(&sender, fee);
debug_assert!(err_amount.is_zero());
let judgements = id.judgements.len();
IdentityOf::<T>::insert(&sender, id);
Self::deposit_event(Event::JudgementUnrequested {
who: sender,
registrar_index: reg_index,
});
Ok(Some(T::WeightInfo::cancel_request(judgements as u32)).into())
}
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::set_fee(T::MaxRegistrars::get()))]
pub fn set_fee(
origin: OriginFor<T>,
#[pallet::compact] index: RegistrarIndex,
#[pallet::compact] fee: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let registrars = Registrars::<T>::mutate(|rs| -> Result<usize, DispatchError> {
rs.get_mut(index as usize)
.and_then(|x| x.as_mut())
.and_then(|r| {
if r.account == who {
r.fee = fee;
Some(())
} else {
None
}
})
.ok_or_else(|| DispatchError::from(Error::<T>::InvalidIndex))?;
Ok(rs.len())
})?;
Ok(Some(T::WeightInfo::set_fee(registrars as u32)).into())
}
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::set_account_id(T::MaxRegistrars::get()))]
pub fn set_account_id(
origin: OriginFor<T>,
#[pallet::compact] index: RegistrarIndex,
new: AccountIdLookupOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let new = T::Lookup::lookup(new)?;
let registrars = Registrars::<T>::mutate(|rs| -> Result<usize, DispatchError> {
rs.get_mut(index as usize)
.and_then(|x| x.as_mut())
.and_then(|r| {
if r.account == who {
r.account = new;
Some(())
} else {
None
}
})
.ok_or_else(|| DispatchError::from(Error::<T>::InvalidIndex))?;
Ok(rs.len())
})?;
Ok(Some(T::WeightInfo::set_account_id(registrars as u32)).into())
}
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::set_fields(T::MaxRegistrars::get()))]
pub fn set_fields(
origin: OriginFor<T>,
#[pallet::compact] index: RegistrarIndex,
fields: <T::IdentityInformation as IdentityInformationProvider>::FieldsIdentifier,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let registrars =
Registrars::<T>::mutate(|registrars| -> Result<usize, DispatchError> {
let registrar = registrars
.get_mut(index as usize)
.and_then(|r| r.as_mut())
.filter(|r| r.account == who)
.ok_or_else(|| DispatchError::from(Error::<T>::InvalidIndex))?;
registrar.fields = fields;
Ok(registrars.len())
})?;
Ok(Some(T::WeightInfo::set_fields(registrars as u32)).into())
}
#[pallet::call_index(9)]
#[pallet::weight(T::WeightInfo::provide_judgement(T::MaxRegistrars::get()))]
pub fn provide_judgement(
origin: OriginFor<T>,
#[pallet::compact] reg_index: RegistrarIndex,
target: AccountIdLookupOf<T>,
judgement: Judgement<BalanceOf<T>>,
identity: T::Hash,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
ensure!(!judgement.has_deposit(), Error::<T>::InvalidJudgement);
Registrars::<T>::get()
.get(reg_index as usize)
.and_then(Option::as_ref)
.filter(|r| r.account == sender)
.ok_or(Error::<T>::InvalidIndex)?;
let mut id = IdentityOf::<T>::get(&target).ok_or(Error::<T>::InvalidTarget)?;
if T::Hashing::hash_of(&id.info) != identity {
return Err(Error::<T>::JudgementForDifferentIdentity.into())
}
let item = (reg_index, judgement);
match id.judgements.binary_search_by_key(®_index, |x| x.0) {
Ok(position) => {
if let Judgement::FeePaid(fee) = id.judgements[position].1 {
T::Currency::repatriate_reserved(
&target,
&sender,
fee,
BalanceStatus::Free,
)
.map_err(|_| Error::<T>::JudgementPaymentFailed)?;
}
id.judgements[position] = item
},
Err(position) => id
.judgements
.try_insert(position, item)
.map_err(|_| Error::<T>::TooManyRegistrars)?,
}
let judgements = id.judgements.len();
IdentityOf::<T>::insert(&target, id);
Self::deposit_event(Event::JudgementGiven { target, registrar_index: reg_index });
Ok(Some(T::WeightInfo::provide_judgement(judgements as u32)).into())
}
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::kill_identity(
T::MaxRegistrars::get(),
T::MaxSubAccounts::get(),
))]
pub fn kill_identity(
origin: OriginFor<T>,
target: AccountIdLookupOf<T>,
) -> DispatchResultWithPostInfo {
T::ForceOrigin::ensure_origin(origin)?;
let target = T::Lookup::lookup(target)?;
let (subs_deposit, sub_ids) = SubsOf::<T>::take(&target);
let id = IdentityOf::<T>::take(&target).ok_or(Error::<T>::NoIdentity)?;
let deposit = id.total_deposit().saturating_add(subs_deposit);
for sub in sub_ids.iter() {
SuperOf::<T>::remove(sub);
}
T::Slashed::on_unbalanced(T::Currency::slash_reserved(&target, deposit).0);
Self::deposit_event(Event::IdentityKilled { who: target, deposit });
#[allow(deprecated)]
Ok(Some(T::WeightInfo::kill_identity(id.judgements.len() as u32, sub_ids.len() as u32))
.into())
}
#[pallet::call_index(11)]
#[pallet::weight(T::WeightInfo::add_sub(T::MaxSubAccounts::get()))]
pub fn add_sub(
origin: OriginFor<T>,
sub: AccountIdLookupOf<T>,
data: Data,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let sub = T::Lookup::lookup(sub)?;
ensure!(IdentityOf::<T>::contains_key(&sender), Error::<T>::NoIdentity);
ensure!(!SuperOf::<T>::contains_key(&sub), Error::<T>::AlreadyClaimed);
SubsOf::<T>::try_mutate(&sender, |(ref mut subs_deposit, ref mut sub_ids)| {
ensure!(
sub_ids.len() < T::MaxSubAccounts::get() as usize,
Error::<T>::TooManySubAccounts
);
let deposit = T::SubAccountDeposit::get();
T::Currency::reserve(&sender, deposit)?;
SuperOf::<T>::insert(&sub, (sender.clone(), data));
sub_ids.try_push(sub.clone()).expect("sub ids length checked above; qed");
*subs_deposit = subs_deposit.saturating_add(deposit);
Self::deposit_event(Event::SubIdentityAdded { sub, main: sender.clone(), deposit });
Ok(())
})
}
#[pallet::call_index(12)]
#[pallet::weight(T::WeightInfo::rename_sub(T::MaxSubAccounts::get()))]
pub fn rename_sub(
origin: OriginFor<T>,
sub: AccountIdLookupOf<T>,
data: Data,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let sub = T::Lookup::lookup(sub)?;
ensure!(IdentityOf::<T>::contains_key(&sender), Error::<T>::NoIdentity);
ensure!(SuperOf::<T>::get(&sub).map_or(false, |x| x.0 == sender), Error::<T>::NotOwned);
SuperOf::<T>::insert(&sub, (&sender, data));
Self::deposit_event(Event::SubIdentityRenamed { main: sender, sub });
Ok(())
}
#[pallet::call_index(13)]
#[pallet::weight(T::WeightInfo::remove_sub(T::MaxSubAccounts::get()))]
pub fn remove_sub(origin: OriginFor<T>, sub: AccountIdLookupOf<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(IdentityOf::<T>::contains_key(&sender), Error::<T>::NoIdentity);
let sub = T::Lookup::lookup(sub)?;
let (sup, _) = SuperOf::<T>::get(&sub).ok_or(Error::<T>::NotSub)?;
ensure!(sup == sender, Error::<T>::NotOwned);
SuperOf::<T>::remove(&sub);
SubsOf::<T>::mutate(&sup, |(ref mut subs_deposit, ref mut sub_ids)| {
sub_ids.retain(|x| x != &sub);
let deposit = T::SubAccountDeposit::get().min(*subs_deposit);
*subs_deposit -= deposit;
let err_amount = T::Currency::unreserve(&sender, deposit);
debug_assert!(err_amount.is_zero());
Self::deposit_event(Event::SubIdentityRemoved { sub, main: sender, deposit });
});
Ok(())
}
#[pallet::call_index(14)]
#[pallet::weight(T::WeightInfo::quit_sub(T::MaxSubAccounts::get()))]
pub fn quit_sub(origin: OriginFor<T>) -> DispatchResult {
let sender = ensure_signed(origin)?;
let (sup, _) = SuperOf::<T>::take(&sender).ok_or(Error::<T>::NotSub)?;
SubsOf::<T>::mutate(&sup, |(ref mut subs_deposit, ref mut sub_ids)| {
sub_ids.retain(|x| x != &sender);
let deposit = T::SubAccountDeposit::get().min(*subs_deposit);
*subs_deposit -= deposit;
let _ =
T::Currency::repatriate_reserved(&sup, &sender, deposit, BalanceStatus::Free);
Self::deposit_event(Event::SubIdentityRevoked {
sub: sender,
main: sup.clone(),
deposit,
});
});
Ok(())
}
#[pallet::call_index(15)]
#[pallet::weight(T::WeightInfo::add_username_authority())]
pub fn add_username_authority(
origin: OriginFor<T>,
authority: AccountIdLookupOf<T>,
suffix: Vec<u8>,
allocation: u32,
) -> DispatchResult {
T::UsernameAuthorityOrigin::ensure_origin(origin)?;
let authority = T::Lookup::lookup(authority)?;
Self::validate_suffix(&suffix)?;
let suffix = Suffix::<T>::try_from(suffix).map_err(|_| Error::<T>::InvalidSuffix)?;
AuthorityOf::<T>::insert(
&suffix,
AuthorityProperties::<T::AccountId> { account_id: authority.clone(), allocation },
);
Self::deposit_event(Event::AuthorityAdded { authority });
Ok(())
}
#[pallet::call_index(16)]
#[pallet::weight(T::WeightInfo::remove_username_authority())]
pub fn remove_username_authority(
origin: OriginFor<T>,
suffix: Vec<u8>,
authority: AccountIdLookupOf<T>,
) -> DispatchResult {
T::UsernameAuthorityOrigin::ensure_origin(origin)?;
let suffix = Suffix::<T>::try_from(suffix).map_err(|_| Error::<T>::InvalidSuffix)?;
let authority = T::Lookup::lookup(authority)?;
let properties =
AuthorityOf::<T>::take(&suffix).ok_or(Error::<T>::NotUsernameAuthority)?;
ensure!(properties.account_id == authority, Error::<T>::InvalidSuffix);
Self::deposit_event(Event::AuthorityRemoved { authority });
Ok(())
}
#[pallet::call_index(17)]
#[pallet::weight(T::WeightInfo::set_username_for(if *use_allocation { 1 } else { 0 }))]
pub fn set_username_for(
origin: OriginFor<T>,
who: AccountIdLookupOf<T>,
username: Vec<u8>,
signature: Option<T::OffchainSignature>,
use_allocation: bool,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let suffix = Self::validate_username(&username)?;
let provider = AuthorityOf::<T>::try_mutate(
&suffix,
|maybe_authority| -> Result<ProviderOf<T>, DispatchError> {
let properties =
maybe_authority.as_mut().ok_or(Error::<T>::NotUsernameAuthority)?;
ensure!(properties.account_id == sender, Error::<T>::NotUsernameAuthority);
if use_allocation {
ensure!(properties.allocation > 0, Error::<T>::NoAllocation);
properties.allocation.saturating_dec();
Ok(Provider::new_with_allocation())
} else {
let deposit = T::UsernameDeposit::get();
T::Currency::reserve(&sender, deposit)?;
Ok(Provider::new_with_deposit(deposit))
}
},
)?;
let bounded_username =
Username::<T>::try_from(username).map_err(|_| Error::<T>::InvalidUsername)?;
ensure!(
!UsernameInfoOf::<T>::contains_key(&bounded_username),
Error::<T>::UsernameTaken
);
ensure!(
!PendingUsernames::<T>::contains_key(&bounded_username),
Error::<T>::UsernameTaken
);
let who = T::Lookup::lookup(who)?;
if let Some(s) = signature {
Self::validate_signature(&bounded_username[..], &s, &who)?;
Self::insert_username(&who, bounded_username, provider);
} else {
Self::queue_acceptance(&who, bounded_username, provider);
}
Ok(())
}
#[pallet::call_index(18)]
#[pallet::weight(T::WeightInfo::accept_username())]
pub fn accept_username(
origin: OriginFor<T>,
username: Username<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let (approved_for, _, provider) =
PendingUsernames::<T>::take(&username).ok_or(Error::<T>::NoUsername)?;
ensure!(approved_for == who.clone(), Error::<T>::InvalidUsername);
Self::insert_username(&who, username.clone(), provider);
Self::deposit_event(Event::UsernameSet { who: who.clone(), username });
Ok(Pays::No.into())
}
#[pallet::call_index(19)]
#[pallet::weight(T::WeightInfo::remove_expired_approval(0))]
pub fn remove_expired_approval(
origin: OriginFor<T>,
username: Username<T>,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
if let Some((who, expiration, provider)) = PendingUsernames::<T>::take(&username) {
let now = frame_system::Pallet::<T>::block_number();
ensure!(now > expiration, Error::<T>::NotExpired);
let actual_weight = match provider {
Provider::AuthorityDeposit(deposit) => {
let suffix = Self::suffix_of_username(&username)
.ok_or(Error::<T>::InvalidUsername)?;
let authority_account = AuthorityOf::<T>::get(&suffix)
.map(|auth_info| auth_info.account_id)
.ok_or(Error::<T>::NotUsernameAuthority)?;
let err_amount = T::Currency::unreserve(&authority_account, deposit);
debug_assert!(err_amount.is_zero());
T::WeightInfo::remove_expired_approval(0)
},
Provider::Allocation => {
T::WeightInfo::remove_expired_approval(1)
},
Provider::System => {
return Err(Error::<T>::InvalidTarget.into());
},
};
Self::deposit_event(Event::PreapprovalExpired { whose: who.clone() });
Ok((Some(actual_weight), Pays::No).into())
} else {
Err(Error::<T>::NoUsername.into())
}
}
#[pallet::call_index(20)]
#[pallet::weight(T::WeightInfo::set_primary_username())]
pub fn set_primary_username(origin: OriginFor<T>, username: Username<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let account_of_username =
UsernameInfoOf::<T>::get(&username).ok_or(Error::<T>::NoUsername)?.owner;
ensure!(who == account_of_username, Error::<T>::InvalidUsername);
UsernameOf::<T>::insert(&who, username.clone());
Self::deposit_event(Event::PrimaryUsernameSet { who: who.clone(), username });
Ok(())
}
#[pallet::call_index(21)]
#[pallet::weight(T::WeightInfo::unbind_username())]
pub fn unbind_username(origin: OriginFor<T>, username: Username<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let username_info =
UsernameInfoOf::<T>::get(&username).ok_or(Error::<T>::NoUsername)?;
let suffix = Self::suffix_of_username(&username).ok_or(Error::<T>::InvalidUsername)?;
let authority_account = AuthorityOf::<T>::get(&suffix)
.map(|auth_info| auth_info.account_id)
.ok_or(Error::<T>::NotUsernameAuthority)?;
ensure!(who == authority_account, Error::<T>::NotUsernameAuthority);
match username_info.provider {
Provider::AuthorityDeposit(_) | Provider::Allocation => {
let now = frame_system::Pallet::<T>::block_number();
let grace_period_expiry = now.saturating_add(T::UsernameGracePeriod::get());
UnbindingUsernames::<T>::try_mutate(&username, |maybe_init| {
if maybe_init.is_some() {
return Err(Error::<T>::AlreadyUnbinding);
}
*maybe_init = Some(grace_period_expiry);
Ok(())
})?;
},
Provider::System => return Err(Error::<T>::InsufficientPrivileges.into()),
}
Self::deposit_event(Event::UsernameUnbound { username });
Ok(())
}
#[pallet::call_index(22)]
#[pallet::weight(T::WeightInfo::remove_username())]
pub fn remove_username(
origin: OriginFor<T>,
username: Username<T>,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
let grace_period_expiry =
UnbindingUsernames::<T>::take(&username).ok_or(Error::<T>::NotUnbinding)?;
let now = frame_system::Pallet::<T>::block_number();
ensure!(now >= grace_period_expiry, Error::<T>::TooEarly);
let username_info = UsernameInfoOf::<T>::take(&username)
.defensive_proof("an unbinding username must exist")
.ok_or(Error::<T>::NoUsername)?;
UsernameOf::<T>::mutate(&username_info.owner, |maybe_primary| {
if maybe_primary.as_ref().map_or(false, |primary| *primary == username) {
*maybe_primary = None;
}
});
match username_info.provider {
Provider::AuthorityDeposit(username_deposit) => {
let suffix = Self::suffix_of_username(&username)
.defensive_proof("registered username must be valid")
.ok_or(Error::<T>::InvalidUsername)?;
if let Some(authority_account) =
AuthorityOf::<T>::get(&suffix).map(|auth_info| auth_info.account_id)
{
let err_amount =
T::Currency::unreserve(&authority_account, username_deposit);
debug_assert!(err_amount.is_zero());
}
},
Provider::Allocation => {
},
Provider::System => return Err(Error::<T>::InsufficientPrivileges.into()),
}
Self::deposit_event(Event::UsernameRemoved { username });
Ok(Pays::No.into())
}
#[pallet::call_index(23)]
#[pallet::weight(T::WeightInfo::kill_username(0))]
pub fn kill_username(
origin: OriginFor<T>,
username: Username<T>,
) -> DispatchResultWithPostInfo {
T::ForceOrigin::ensure_origin(origin)?;
let username_info =
UsernameInfoOf::<T>::take(&username).ok_or(Error::<T>::NoUsername)?;
UsernameOf::<T>::mutate(&username_info.owner, |maybe_primary| {
if match maybe_primary {
Some(primary) if *primary == username => true,
_ => false,
} {
*maybe_primary = None;
}
});
let _ = UnbindingUsernames::<T>::take(&username);
let actual_weight = match username_info.provider {
Provider::AuthorityDeposit(username_deposit) => {
let suffix =
Self::suffix_of_username(&username).ok_or(Error::<T>::InvalidUsername)?;
if let Some(authority_account) =
AuthorityOf::<T>::get(&suffix).map(|auth_info| auth_info.account_id)
{
T::Slashed::on_unbalanced(
T::Currency::slash_reserved(&authority_account, username_deposit).0,
);
}
T::WeightInfo::kill_username(0)
},
Provider::Allocation => {
T::WeightInfo::kill_username(1)
},
Provider::System => {
T::WeightInfo::kill_username(1)
},
};
Self::deposit_event(Event::UsernameKilled { username });
Ok((Some(actual_weight), Pays::No).into())
}
}
}
impl<T: Config> Pallet<T> {
pub fn subs(who: &T::AccountId) -> Vec<(T::AccountId, Data)> {
SubsOf::<T>::get(who)
.1
.into_iter()
.filter_map(|a| SuperOf::<T>::get(&a).map(|x| (a, x.1)))
.collect()
}
fn subs_deposit(subs: u32) -> BalanceOf<T> {
T::SubAccountDeposit::get().saturating_mul(BalanceOf::<T>::from(subs))
}
fn rejig_deposit(
who: &T::AccountId,
current: BalanceOf<T>,
new: BalanceOf<T>,
) -> DispatchResult {
if new > current {
T::Currency::reserve(who, new - current)?;
} else if new < current {
let err_amount = T::Currency::unreserve(who, current - new);
debug_assert!(err_amount.is_zero());
}
Ok(())
}
pub fn has_identity(
who: &T::AccountId,
fields: <T::IdentityInformation as IdentityInformationProvider>::FieldsIdentifier,
) -> bool {
IdentityOf::<T>::get(who)
.map_or(false, |registration| (registration.info.has_identity(fields)))
}
fn calculate_identity_deposit(info: &T::IdentityInformation) -> BalanceOf<T> {
let bytes = info.encoded_size() as u32;
let byte_deposit = T::ByteDeposit::get().saturating_mul(BalanceOf::<T>::from(bytes));
T::BasicDeposit::get().saturating_add(byte_deposit)
}
fn validate_username(username: &Vec<u8>) -> Result<Suffix<T>, DispatchError> {
ensure!(
username.len() <= T::MaxUsernameLength::get() as usize,
Error::<T>::InvalidUsername
);
ensure!(!username.is_empty(), Error::<T>::InvalidUsername);
let separator_idx =
username.iter().rposition(|c| *c == b'.').ok_or(Error::<T>::InvalidUsername)?;
ensure!(separator_idx > 0, Error::<T>::InvalidUsername);
let suffix_start = separator_idx.checked_add(1).ok_or(Error::<T>::InvalidUsername)?;
ensure!(suffix_start < username.len(), Error::<T>::InvalidUsername);
ensure!(
username
.iter()
.take(separator_idx)
.all(|byte| byte.is_ascii_digit() || byte.is_ascii_lowercase()),
Error::<T>::InvalidUsername
);
let suffix: Suffix<T> = (&username[suffix_start..])
.to_vec()
.try_into()
.map_err(|_| Error::<T>::InvalidUsername)?;
Ok(suffix)
}
fn suffix_of_username(username: &Username<T>) -> Option<Suffix<T>> {
let separator_idx = username.iter().rposition(|c| *c == b'.')?;
let suffix_start = separator_idx.checked_add(1)?;
if suffix_start >= username.len() {
return None;
}
(&username[suffix_start..]).to_vec().try_into().ok()
}
fn validate_suffix(suffix: &Vec<u8>) -> Result<(), DispatchError> {
ensure!(suffix.len() <= T::MaxSuffixLength::get() as usize, Error::<T>::InvalidSuffix);
ensure!(!suffix.is_empty(), Error::<T>::InvalidSuffix);
ensure!(
suffix.iter().all(|byte| byte.is_ascii_digit() || byte.is_ascii_lowercase()),
Error::<T>::InvalidSuffix
);
Ok(())
}
pub fn validate_signature(
data: &[u8],
signature: &T::OffchainSignature,
signer: &T::AccountId,
) -> DispatchResult {
if signature.verify(data, &signer) {
return Ok(())
}
let prefix = b"<Bytes>";
let suffix = b"</Bytes>";
let mut wrapped: Vec<u8> = Vec::with_capacity(data.len() + prefix.len() + suffix.len());
wrapped.extend(prefix);
wrapped.extend(data);
wrapped.extend(suffix);
ensure!(signature.verify(&wrapped[..], &signer), Error::<T>::InvalidSignature);
Ok(())
}
pub fn insert_username(who: &T::AccountId, username: Username<T>, provider: ProviderOf<T>) {
let (primary_username, new_is_primary) = match UsernameOf::<T>::get(&who) {
Some(primary) => (primary, false),
None => (username.clone(), true),
};
if new_is_primary {
UsernameOf::<T>::insert(&who, primary_username);
}
let username_info = UsernameInformation { owner: who.clone(), provider };
UsernameInfoOf::<T>::insert(username.clone(), username_info);
Self::deposit_event(Event::UsernameSet { who: who.clone(), username: username.clone() });
if new_is_primary {
Self::deposit_event(Event::PrimaryUsernameSet { who: who.clone(), username });
}
}
pub fn queue_acceptance(who: &T::AccountId, username: Username<T>, provider: ProviderOf<T>) {
let now = frame_system::Pallet::<T>::block_number();
let expiration = now.saturating_add(T::PendingUsernameExpiration::get());
PendingUsernames::<T>::insert(&username, (who.clone(), expiration, provider));
Self::deposit_event(Event::UsernameQueued { who: who.clone(), username, expiration });
}
pub fn reap_identity(who: &T::AccountId) -> Result<(u32, u32, u32), DispatchError> {
let id = IdentityOf::<T>::take(&who).ok_or(Error::<T>::NoIdentity)?;
let registrars = id.judgements.len() as u32;
let encoded_byte_size = id.info.encoded_size() as u32;
let (subs_deposit, sub_ids) = SubsOf::<T>::take(&who);
let actual_subs = sub_ids.len() as u32;
for sub in sub_ids.iter() {
SuperOf::<T>::remove(sub);
}
let deposit = id.total_deposit().saturating_add(subs_deposit);
let err_amount = T::Currency::unreserve(&who, deposit);
debug_assert!(err_amount.is_zero());
Ok((registrars, encoded_byte_size, actual_subs))
}
pub fn poke_deposit(
target: &T::AccountId,
) -> Result<(BalanceOf<T>, BalanceOf<T>), DispatchError> {
let new_id_deposit = IdentityOf::<T>::try_mutate(
&target,
|identity_of| -> Result<BalanceOf<T>, DispatchError> {
let reg = identity_of.as_mut().ok_or(Error::<T>::NoIdentity)?;
let encoded_byte_size = reg.info.encoded_size() as u32;
let byte_deposit =
T::ByteDeposit::get().saturating_mul(BalanceOf::<T>::from(encoded_byte_size));
let new_id_deposit = T::BasicDeposit::get().saturating_add(byte_deposit);
Self::rejig_deposit(&target, reg.deposit, new_id_deposit)?;
reg.deposit = new_id_deposit;
Ok(new_id_deposit)
},
)?;
let new_subs_deposit = if SubsOf::<T>::contains_key(&target) {
SubsOf::<T>::try_mutate(
&target,
|(current_subs_deposit, subs_of)| -> Result<BalanceOf<T>, DispatchError> {
let new_subs_deposit = Self::subs_deposit(subs_of.len() as u32);
Self::rejig_deposit(&target, *current_subs_deposit, new_subs_deposit)?;
*current_subs_deposit = new_subs_deposit;
Ok(new_subs_deposit)
},
)?
} else {
Zero::zero()
};
Ok((new_id_deposit, new_subs_deposit))
}
#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
pub fn set_identity_no_deposit(
who: &T::AccountId,
info: T::IdentityInformation,
) -> DispatchResult {
IdentityOf::<T>::insert(
&who,
Registration {
judgements: Default::default(),
deposit: Zero::zero(),
info: info.clone(),
},
);
Ok(())
}
#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
pub fn set_subs_no_deposit(
who: &T::AccountId,
subs: Vec<(T::AccountId, Data)>,
) -> DispatchResult {
let mut sub_accounts = BoundedVec::<T::AccountId, T::MaxSubAccounts>::default();
for (sub, name) in subs {
SuperOf::<T>::insert(&sub, (who.clone(), name));
sub_accounts
.try_push(sub)
.expect("benchmark should not pass more than T::MaxSubAccounts");
}
SubsOf::<T>::insert::<
&T::AccountId,
(BalanceOf<T>, BoundedVec<T::AccountId, T::MaxSubAccounts>),
>(&who, (Zero::zero(), sub_accounts));
Ok(())
}
}