git.delta.rocks / unique-network / refs/commits / fd8f5c47fa0c

difftreelog

source

pallets/identity/src/lib.rs39.4 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// Original license:18// This file is part of Substrate.1920// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.21// SPDX-License-Identifier: Apache-2.02223// Licensed under the Apache License, Version 2.0 (the "License");24// you may not use this file except in compliance with the License.25// You may obtain a copy of the License at26//27// 	http://www.apache.org/licenses/LICENSE-2.028//29// Unless required by applicable law or agreed to in writing, software30// distributed under the License is distributed on an "AS IS" BASIS,31// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.32// See the License for the specific language governing permissions and33// limitations under the License.3435//! # Identity Pallet36//!37//! - [`Config`]38//! - [`Call`]39//!40//! ## Overview41//!42//! A federated naming system, allowing for multiple registrars to be added from a specified origin.43//! Registrars can set a fee to provide identity-verification service. Anyone can put forth a44//! proposed identity for a fixed deposit and ask for review by any number of registrars (paying45//! each of their fees). Registrar judgements are given as an `enum`, allowing for sophisticated,46//! multi-tier opinions.47//!48//! Some judgements are identified as *sticky*, which means they cannot be removed except by49//! complete removal of the identity, or by the registrar. Judgements are allowed to represent a50//! portion of funds that have been reserved for the registrar.51//!52//! A super-user can remove accounts and in doing so, slash the deposit.53//!54//! All accounts may also have a limited number of sub-accounts which may be specified by the owner;55//! by definition, these have equivalent ownership and each has an individual name.56//!57//! The number of registrars should be limited, and the deposit made sufficiently large, to ensure58//! no state-bloat attack is viable.59//!60//! ## Interface61//!62//! ### Dispatchable Functions63//!64//! #### For general users65//! * `set_identity` - Set the associated identity of an account; a small deposit is reserved if not66//!   already taken.67//! * `clear_identity` - Remove an account's associated identity; the deposit is returned.68//! * `request_judgement` - Request a judgement from a registrar, paying a fee.69//! * `cancel_request` - Cancel the previous request for a judgement.70//!71//! #### For general users with sub-identities72//! * `set_subs` - Set the sub-accounts of an identity.73//! * `add_sub` - Add a sub-identity to an identity.74//! * `remove_sub` - Remove a sub-identity of an identity.75//! * `rename_sub` - Rename a sub-identity of an identity.76//! * `quit_sub` - Remove a sub-identity of an identity (called by the sub-identity).77//!78//! #### For registrars79//! * `set_fee` - Set the fee required to be paid for a judgement to be given by the registrar.80//! * `set_fields` - Set the fields that a registrar cares about in their judgements.81//! * `provide_judgement` - Provide a judgement to an identity.82//!83//! #### For super-users84//! * `add_registrar` - Add a new registrar to the system.85//! * `kill_identity` - Forcibly remove the associated identity; the deposit is lost.86//!87//! [`Call`]: ./enum.Call.html88//! [`Config`]: ./trait.Config.html8990#![cfg_attr(not(feature = "std"), no_std)]9192mod benchmarking;93#[cfg(test)]94mod tests;95mod types;96pub mod weights;9798use frame_support::traits::{BalanceStatus, Currency, OnUnbalanced, ReservableCurrency};99use sp_runtime::traits::{AppendZerosInput, Hash, Saturating, StaticLookup, Zero};100use sp_std::prelude::*;101pub use weights::WeightInfo;102103pub use pallet::*;104pub use types::{105	Data, IdentityField, IdentityFields, IdentityInfo, Judgement, RegistrarIndex, RegistrarInfo,106	Registration,107};108109pub type BalanceOf<T> =110	<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;111type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<112	<T as frame_system::Config>::AccountId,113>>::NegativeImbalance;114type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;115116#[frame_support::pallet]117pub mod pallet {118	use super::*;119	use frame_support::pallet_prelude::*;120	use frame_system::pallet_prelude::*;121122	#[pallet::config]123	pub trait Config: frame_system::Config {124		/// The overarching event type.125		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;126127		/// The currency trait.128		type Currency: ReservableCurrency<Self::AccountId>;129130		/// The amount held on deposit for a registered identity131		#[pallet::constant]132		type BasicDeposit: Get<BalanceOf<Self>>;133134		/// The amount held on deposit per additional field for a registered identity.135		#[pallet::constant]136		type FieldDeposit: Get<BalanceOf<Self>>;137138		/// The amount held on deposit for a registered subaccount. This should account for the fact139		/// that one storage item's value will increase by the size of an account ID, and there will140		/// be another trie item whose value is the size of an account ID plus 32 bytes.141		#[pallet::constant]142		type SubAccountDeposit: Get<BalanceOf<Self>>;143144		/// The maximum number of sub-accounts allowed per identified account.145		#[pallet::constant]146		type MaxSubAccounts: Get<u32>;147148		/// Maximum number of additional fields that may be stored in an ID. Needed to bound the I/O149		/// required to access an identity, but can be pretty high.150		#[pallet::constant]151		type MaxAdditionalFields: Get<u32>;152153		/// Maxmimum number of registrars allowed in the system. Needed to bound the complexity154		/// of, e.g., updating judgements.155		#[pallet::constant]156		type MaxRegistrars: Get<u32>;157158		/// What to do with slashed funds.159		type Slashed: OnUnbalanced<NegativeImbalanceOf<Self>>;160161		/// The origin which may forcibly set or remove a name. Root can always do this.162		type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;163164		/// The origin which may add or remove registrars. Root can always do this.165		type RegistrarOrigin: EnsureOrigin<Self::RuntimeOrigin>;166167		/// Weight information for extrinsics in this pallet.168		type WeightInfo: WeightInfo;169	}170171	#[pallet::pallet]172	pub struct Pallet<T>(_);173174	/// Information that is pertinent to identify the entity behind an account.175	///176	/// TWOX-NOTE: OK ― `AccountId` is a secure hash.177	#[pallet::storage]178	#[pallet::getter(fn identity)]179	pub(super) type IdentityOf<T: Config> = StorageMap<180		_,181		Twox64Concat,182		T::AccountId,183		Registration<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields>,184		OptionQuery,185	>;186187	/// The super-identity of an alternative "sub" identity together with its name, within that188	/// context. If the account is not some other account's sub-identity, then just `None`.189	#[pallet::storage]190	#[pallet::getter(fn super_of)]191	pub(super) type SuperOf<T: Config> =192		StorageMap<_, Blake2_128Concat, T::AccountId, (T::AccountId, Data), OptionQuery>;193194	/// Alternative "sub" identities of this account.195	///196	/// The first item is the deposit, the second is a vector of the accounts.197	///198	/// TWOX-NOTE: OK ― `AccountId` is a secure hash.199	#[pallet::storage]200	#[pallet::getter(fn subs_of)]201	pub(super) type SubsOf<T: Config> = StorageMap<202		_,203		Twox64Concat,204		T::AccountId,205		(BalanceOf<T>, BoundedVec<T::AccountId, T::MaxSubAccounts>),206		ValueQuery,207	>;208209	/// The set of registrars. Not expected to get very big as can only be added through a210	/// special origin (likely a council motion).211	///212	/// The index into this can be cast to `RegistrarIndex` to get a valid value.213	#[pallet::storage]214	#[pallet::getter(fn registrars)]215	pub(super) type Registrars<T: Config> = StorageValue<216		_,217		BoundedVec<Option<RegistrarInfo<BalanceOf<T>, T::AccountId>>, T::MaxRegistrars>,218		ValueQuery,219	>;220221	#[pallet::error]222	pub enum Error<T> {223		/// Too many subs-accounts.224		TooManySubAccounts,225		/// Account isn't found.226		NotFound,227		/// Account isn't named.228		NotNamed,229		/// Empty index.230		EmptyIndex,231		/// Fee is changed.232		FeeChanged,233		/// No identity found.234		NoIdentity,235		/// Sticky judgement.236		StickyJudgement,237		/// Judgement given.238		JudgementGiven,239		/// Invalid judgement.240		InvalidJudgement,241		/// The index is invalid.242		InvalidIndex,243		/// The target is invalid.244		InvalidTarget,245		/// Too many additional fields.246		TooManyFields,247		/// Maximum amount of registrars reached. Cannot add any more.248		TooManyRegistrars,249		/// Account ID is already named.250		AlreadyClaimed,251		/// Sender is not a sub-account.252		NotSub,253		/// Sub-account isn't owned by sender.254		NotOwned,255		/// The provided judgement was for a different identity.256		JudgementForDifferentIdentity,257		/// Error that occurs when there is an issue paying for judgement.258		JudgementPaymentFailed,259	}260261	#[pallet::event]262	#[pallet::generate_deposit(pub(super) fn deposit_event)]263	pub enum Event<T: Config> {264		/// A name was set or reset (which will remove all judgements).265		IdentitySet { who: T::AccountId },266		/// A name was cleared, and the given balance returned.267		IdentityCleared {268			who: T::AccountId,269			deposit: BalanceOf<T>,270		},271		/// A name was removed and the given balance slashed.272		IdentityKilled {273			who: T::AccountId,274			deposit: BalanceOf<T>,275		},276		/// A number of identities and associated info were forcibly inserted.277		IdentitiesInserted { amount: u32 },278		/// A number of identities and all associated info were forcibly removed.279		IdentitiesRemoved { amount: u32 },280		/// A judgement was asked from a registrar.281		JudgementRequested {282			who: T::AccountId,283			registrar_index: RegistrarIndex,284		},285		/// A judgement request was retracted.286		JudgementUnrequested {287			who: T::AccountId,288			registrar_index: RegistrarIndex,289		},290		/// A judgement was given by a registrar.291		JudgementGiven {292			target: T::AccountId,293			registrar_index: RegistrarIndex,294		},295		/// A registrar was added.296		RegistrarAdded { registrar_index: RegistrarIndex },297		/// A sub-identity was added to an identity and the deposit paid.298		SubIdentityAdded {299			sub: T::AccountId,300			main: T::AccountId,301			deposit: BalanceOf<T>,302		},303		/// A sub-identity was removed from an identity and the deposit freed.304		SubIdentityRemoved {305			sub: T::AccountId,306			main: T::AccountId,307			deposit: BalanceOf<T>,308		},309		/// A sub-identity was cleared, and the given deposit repatriated from the310		/// main identity account to the sub-identity account.311		SubIdentityRevoked {312			sub: T::AccountId,313			main: T::AccountId,314			deposit: BalanceOf<T>,315		},316		/// A number of identities were forcibly updated with new sub-identities.317		SubIdentitiesInserted { amount: u32 },318	}319320	#[pallet::call]321	/// Identity pallet declaration.322	impl<T: Config> Pallet<T> {323		/// Add a registrar to the system.324		///325		/// The dispatch origin for this call must be `T::RegistrarOrigin`.326		///327		/// - `account`: the account of the registrar.328		///329		/// Emits `RegistrarAdded` if successful.330		///331		/// # <weight>332		/// - `O(R)` where `R` registrar-count (governance-bounded and code-bounded).333		/// - One storage mutation (codec `O(R)`).334		/// - One event.335		/// # </weight>336		#[pallet::call_index(0)]337		#[pallet::weight(T::WeightInfo::add_registrar(T::MaxRegistrars::get()))]338		pub fn add_registrar(339			origin: OriginFor<T>,340			account: AccountIdLookupOf<T>,341		) -> DispatchResultWithPostInfo {342			T::RegistrarOrigin::ensure_origin(origin)?;343			let account = T::Lookup::lookup(account)?;344345			let (i, registrar_count) = <Registrars<T>>::try_mutate(346				|registrars| -> Result<(RegistrarIndex, usize), DispatchError> {347					registrars348						.try_push(Some(RegistrarInfo {349							account,350							fee: Zero::zero(),351							fields: Default::default(),352						}))353						.map_err(|_| Error::<T>::TooManyRegistrars)?;354					Ok(((registrars.len() - 1) as RegistrarIndex, registrars.len()))355				},356			)?;357358			Self::deposit_event(Event::RegistrarAdded { registrar_index: i });359360			Ok(Some(T::WeightInfo::add_registrar(registrar_count as u32)).into())361		}362363		/// Set an account's identity information and reserve the appropriate deposit.364		///365		/// If the account already has identity information, the deposit is taken as part payment366		/// for the new deposit.367		///368		/// The dispatch origin for this call must be _Signed_.369		///370		/// - `info`: The identity information.371		///372		/// Emits `IdentitySet` if successful.373		///374		/// # <weight>375		/// - `O(X + X' + R)`376		///   - where `X` additional-field-count (deposit-bounded and code-bounded)377		///   - where `R` judgements-count (registrar-count-bounded)378		/// - One balance reserve operation.379		/// - One storage mutation (codec-read `O(X' + R)`, codec-write `O(X + R)`).380		/// - One event.381		/// # </weight>382		#[pallet::call_index(1)]383		#[pallet::weight( T::WeightInfo::set_identity(384			T::MaxRegistrars::get(), // R385			T::MaxAdditionalFields::get(), // X386		))]387		pub fn set_identity(388			origin: OriginFor<T>,389			info: Box<IdentityInfo<T::MaxAdditionalFields>>,390		) -> DispatchResultWithPostInfo {391			let sender = ensure_signed(origin)?;392			let extra_fields = info.additional.len() as u32;393			ensure!(394				extra_fields <= T::MaxAdditionalFields::get(),395				Error::<T>::TooManyFields396			);397			let fd = <BalanceOf<T>>::from(extra_fields) * T::FieldDeposit::get();398399			let mut id = match <IdentityOf<T>>::get(&sender) {400				Some(mut id) => {401					// Only keep non-positive judgements.402					id.judgements.retain(|j| j.1.is_sticky());403					id.info = *info;404					id405				}406				None => Registration {407					info: *info,408					judgements: BoundedVec::default(),409					deposit: Zero::zero(),410				},411			};412413			let old_deposit = id.deposit;414			id.deposit = T::BasicDeposit::get() + fd;415			if id.deposit > old_deposit {416				T::Currency::reserve(&sender, id.deposit - old_deposit)?;417			}418			if old_deposit > id.deposit {419				let err_amount = T::Currency::unreserve(&sender, old_deposit - id.deposit);420				debug_assert!(err_amount.is_zero());421			}422423			let judgements = id.judgements.len();424			<IdentityOf<T>>::insert(&sender, id);425			Self::deposit_event(Event::IdentitySet { who: sender });426427			Ok(Some(T::WeightInfo::set_identity(428				judgements as u32, // R429				extra_fields,      // X430			))431			.into())432		}433434		/// Set the sub-accounts of the sender.435		///436		/// Payment: Any aggregate balance reserved by previous `set_subs` calls will be returned437		/// and an amount `SubAccountDeposit` will be reserved for each item in `subs`.438		///439		/// The dispatch origin for this call must be _Signed_ and the sender must have a registered440		/// identity.441		///442		/// - `subs`: The identity's (new) sub-accounts.443		///444		/// # <weight>445		/// - `O(P + S)`446		///   - where `P` old-subs-count (hard- and deposit-bounded).447		///   - where `S` subs-count (hard- and deposit-bounded).448		/// - At most one balance operations.449		/// - DB:450		///   - `P + S` storage mutations (codec complexity `O(1)`)451		///   - One storage read (codec complexity `O(P)`).452		///   - One storage write (codec complexity `O(S)`).453		///   - One storage-exists (`IdentityOf::contains_key`).454		/// # </weight>455		// TODO: This whole extrinsic screams "not optimized". For example we could456		// filter any overlap between new and old subs, and avoid reading/writing457		// to those values... We could also ideally avoid needing to write to458		// N storage items for N sub accounts. Right now the weight on this function459		// is a large overestimate due to the fact that it could potentially write460		// to 2 x T::MaxSubAccounts::get().461		#[pallet::call_index(2)]462		#[pallet::weight(T::WeightInfo::set_subs_old(T::MaxSubAccounts::get()) // P: Assume max sub accounts removed.463			.saturating_add(T::WeightInfo::set_subs_new(subs.len() as u32)) // S: Assume all subs are new.464		)]465		pub fn set_subs(466			origin: OriginFor<T>,467			subs: Vec<(T::AccountId, Data)>,468		) -> DispatchResultWithPostInfo {469			let sender = ensure_signed(origin)?;470			ensure!(<IdentityOf<T>>::contains_key(&sender), Error::<T>::NotFound);471			ensure!(472				subs.len() <= T::MaxSubAccounts::get() as usize,473				Error::<T>::TooManySubAccounts474			);475476			let (old_deposit, old_ids) = <SubsOf<T>>::get(&sender);477			let new_deposit = T::SubAccountDeposit::get() * <BalanceOf<T>>::from(subs.len() as u32);478479			let not_other_sub = subs480				.iter()481				.filter_map(|i| SuperOf::<T>::get(&i.0))482				.all(|i| i.0 == sender);483			ensure!(not_other_sub, Error::<T>::AlreadyClaimed);484485			if old_deposit < new_deposit {486				T::Currency::reserve(&sender, new_deposit - old_deposit)?;487			} else if old_deposit > new_deposit {488				let err_amount = T::Currency::unreserve(&sender, old_deposit - new_deposit);489				debug_assert!(err_amount.is_zero());490			}491			// do nothing if they're equal.492493			for s in old_ids.iter() {494				<SuperOf<T>>::remove(s);495			}496			let mut ids = BoundedVec::<T::AccountId, T::MaxSubAccounts>::default();497			for (id, name) in subs {498				<SuperOf<T>>::insert(&id, (sender.clone(), name));499				ids.try_push(id)500					.expect("subs length is less than T::MaxSubAccounts; qed");501			}502			let new_subs = ids.len();503504			if ids.is_empty() {505				<SubsOf<T>>::remove(&sender);506			} else {507				<SubsOf<T>>::insert(&sender, (new_deposit, ids));508			}509510			Ok(Some(511				T::WeightInfo::set_subs_old(old_ids.len() as u32) // P: Real number of old accounts removed.512					// S: New subs added513					.saturating_add(T::WeightInfo::set_subs_new(new_subs as u32)),514			)515			.into())516		}517518		/// Clear an account's identity info and all sub-accounts and return all deposits.519		///520		/// Payment: All reserved balances on the account are returned.521		///522		/// The dispatch origin for this call must be _Signed_ and the sender must have a registered523		/// identity.524		///525		/// Emits `IdentityCleared` if successful.526		///527		/// # <weight>528		/// - `O(R + S + X)`529		///   - where `R` registrar-count (governance-bounded).530		///   - where `S` subs-count (hard- and deposit-bounded).531		///   - where `X` additional-field-count (deposit-bounded and code-bounded).532		/// - One balance-unreserve operation.533		/// - `2` storage reads and `S + 2` storage deletions.534		/// - One event.535		/// # </weight>536		#[pallet::call_index(3)]537		#[pallet::weight(T::WeightInfo::clear_identity(538			T::MaxRegistrars::get(), // R539			T::MaxSubAccounts::get(), // S540			T::MaxAdditionalFields::get(), // X541		))]542		pub fn clear_identity(origin: OriginFor<T>) -> DispatchResultWithPostInfo {543			let sender = ensure_signed(origin)?;544545			let (subs_deposit, sub_ids) = <SubsOf<T>>::take(&sender);546			let id = <IdentityOf<T>>::take(&sender).ok_or(Error::<T>::NotNamed)?;547			let deposit = id.total_deposit() + subs_deposit;548			for sub in sub_ids.iter() {549				<SuperOf<T>>::remove(sub);550			}551552			let err_amount = T::Currency::unreserve(&sender, deposit);553			debug_assert!(err_amount.is_zero());554555			Self::deposit_event(Event::IdentityCleared {556				who: sender,557				deposit,558			});559560			Ok(Some(T::WeightInfo::clear_identity(561				id.judgements.len() as u32,      // R562				sub_ids.len() as u32,            // S563				id.info.additional.len() as u32, // X564			))565			.into())566		}567568		/// Request a judgement from a registrar.569		///570		/// Payment: At most `max_fee` will be reserved for payment to the registrar if judgement571		/// given.572		///573		/// The dispatch origin for this call must be _Signed_ and the sender must have a574		/// registered identity.575		///576		/// - `reg_index`: The index of the registrar whose judgement is requested.577		/// - `max_fee`: The maximum fee that may be paid. This should just be auto-populated as:578		///579		/// ```nocompile580		/// Self::registrars().get(reg_index).unwrap().fee581		/// ```582		///583		/// Emits `JudgementRequested` if successful.584		///585		/// # <weight>586		/// - `O(R + X)`.587		/// - One balance-reserve operation.588		/// - Storage: 1 read `O(R)`, 1 mutate `O(X + R)`.589		/// - One event.590		/// # </weight>591		#[pallet::call_index(4)]592		#[pallet::weight(T::WeightInfo::request_judgement(593			T::MaxRegistrars::get(), // R594			T::MaxAdditionalFields::get(), // X595		))]596		pub fn request_judgement(597			origin: OriginFor<T>,598			#[pallet::compact] reg_index: RegistrarIndex,599			#[pallet::compact] max_fee: BalanceOf<T>,600		) -> DispatchResultWithPostInfo {601			let sender = ensure_signed(origin)?;602			let registrars = <Registrars<T>>::get();603			let registrar = registrars604				.get(reg_index as usize)605				.and_then(Option::as_ref)606				.ok_or(Error::<T>::EmptyIndex)?;607			ensure!(max_fee >= registrar.fee, Error::<T>::FeeChanged);608			let mut id = <IdentityOf<T>>::get(&sender).ok_or(Error::<T>::NoIdentity)?;609610			let item = (reg_index, Judgement::FeePaid(registrar.fee));611			match id.judgements.binary_search_by_key(&reg_index, |x| x.0) {612				Ok(i) => {613					if id.judgements[i].1.is_sticky() {614						return Err(Error::<T>::StickyJudgement.into());615					} else {616						id.judgements[i] = item617					}618				}619				Err(i) => id620					.judgements621					.try_insert(i, item)622					.map_err(|_| Error::<T>::TooManyRegistrars)?,623			}624625			T::Currency::reserve(&sender, registrar.fee)?;626627			let judgements = id.judgements.len();628			let extra_fields = id.info.additional.len();629			<IdentityOf<T>>::insert(&sender, id);630631			Self::deposit_event(Event::JudgementRequested {632				who: sender,633				registrar_index: reg_index,634			});635636			Ok(Some(T::WeightInfo::request_judgement(637				judgements as u32,638				extra_fields as u32,639			))640			.into())641		}642643		/// Cancel a previous request.644		///645		/// Payment: A previously reserved deposit is returned on success.646		///647		/// The dispatch origin for this call must be _Signed_ and the sender must have a648		/// registered identity.649		///650		/// - `reg_index`: The index of the registrar whose judgement is no longer requested.651		///652		/// Emits `JudgementUnrequested` if successful.653		///654		/// # <weight>655		/// - `O(R + X)`.656		/// - One balance-reserve operation.657		/// - One storage mutation `O(R + X)`.658		/// - One event659		/// # </weight>660		#[pallet::call_index(5)]661		#[pallet::weight(T::WeightInfo::cancel_request(662			T::MaxRegistrars::get(), // R663			T::MaxAdditionalFields::get(), // X664		))]665		pub fn cancel_request(666			origin: OriginFor<T>,667			reg_index: RegistrarIndex,668		) -> DispatchResultWithPostInfo {669			let sender = ensure_signed(origin)?;670			let mut id = <IdentityOf<T>>::get(&sender).ok_or(Error::<T>::NoIdentity)?;671672			let pos = id673				.judgements674				.binary_search_by_key(&reg_index, |x| x.0)675				.map_err(|_| Error::<T>::NotFound)?;676			let fee = if let Judgement::FeePaid(fee) = id.judgements.remove(pos).1 {677				fee678			} else {679				return Err(Error::<T>::JudgementGiven.into());680			};681682			let err_amount = T::Currency::unreserve(&sender, fee);683			debug_assert!(err_amount.is_zero());684			let judgements = id.judgements.len();685			let extra_fields = id.info.additional.len();686			<IdentityOf<T>>::insert(&sender, id);687688			Self::deposit_event(Event::JudgementUnrequested {689				who: sender,690				registrar_index: reg_index,691			});692693			Ok(Some(T::WeightInfo::cancel_request(694				judgements as u32,695				extra_fields as u32,696			))697			.into())698		}699700		/// Set the fee required for a judgement to be requested from a registrar.701		///702		/// The dispatch origin for this call must be _Signed_ and the sender must be the account703		/// of the registrar whose index is `index`.704		///705		/// - `index`: the index of the registrar whose fee is to be set.706		/// - `fee`: the new fee.707		///708		/// # <weight>709		/// - `O(R)`.710		/// - One storage mutation `O(R)`.711		/// - Benchmark: 7.315 + R * 0.329 µs (min squares analysis)712		/// # </weight>713		#[pallet::call_index(6)]714		#[pallet::weight(T::WeightInfo::set_fee(T::MaxRegistrars::get()))] // R715		pub fn set_fee(716			origin: OriginFor<T>,717			#[pallet::compact] index: RegistrarIndex,718			#[pallet::compact] fee: BalanceOf<T>,719		) -> DispatchResultWithPostInfo {720			let who = ensure_signed(origin)?;721722			let registrars = <Registrars<T>>::mutate(|rs| -> Result<usize, DispatchError> {723				rs.get_mut(index as usize)724					.and_then(|x| x.as_mut())725					.and_then(|r| {726						if r.account == who {727							r.fee = fee;728							Some(())729						} else {730							None731						}732					})733					.ok_or_else(|| DispatchError::from(Error::<T>::InvalidIndex))?;734				Ok(rs.len())735			})?;736			Ok(Some(T::WeightInfo::set_fee(registrars as u32)).into()) // R737		}738739		/// Change the account associated with a registrar.740		///741		/// The dispatch origin for this call must be _Signed_ and the sender must be the account742		/// of the registrar whose index is `index`.743		///744		/// - `index`: the index of the registrar whose fee is to be set.745		/// - `new`: the new account ID.746		///747		/// # <weight>748		/// - `O(R)`.749		/// - One storage mutation `O(R)`.750		/// - Benchmark: 8.823 + R * 0.32 µs (min squares analysis)751		/// # </weight>752		#[pallet::call_index(7)]753		#[pallet::weight(T::WeightInfo::set_account_id(T::MaxRegistrars::get()))] // R754		pub fn set_account_id(755			origin: OriginFor<T>,756			#[pallet::compact] index: RegistrarIndex,757			new: AccountIdLookupOf<T>,758		) -> DispatchResultWithPostInfo {759			let who = ensure_signed(origin)?;760			let new = T::Lookup::lookup(new)?;761762			let registrars = <Registrars<T>>::mutate(|rs| -> Result<usize, DispatchError> {763				rs.get_mut(index as usize)764					.and_then(|x| x.as_mut())765					.and_then(|r| {766						if r.account == who {767							r.account = new;768							Some(())769						} else {770							None771						}772					})773					.ok_or_else(|| DispatchError::from(Error::<T>::InvalidIndex))?;774				Ok(rs.len())775			})?;776			Ok(Some(T::WeightInfo::set_account_id(registrars as u32)).into()) // R777		}778779		/// Set the field information for a registrar.780		///781		/// The dispatch origin for this call must be _Signed_ and the sender must be the account782		/// of the registrar whose index is `index`.783		///784		/// - `index`: the index of the registrar whose fee is to be set.785		/// - `fields`: the fields that the registrar concerns themselves with.786		///787		/// # <weight>788		/// - `O(R)`.789		/// - One storage mutation `O(R)`.790		/// - Benchmark: 7.464 + R * 0.325 µs (min squares analysis)791		/// # </weight>792		#[pallet::call_index(8)]793		#[pallet::weight(T::WeightInfo::set_fields(T::MaxRegistrars::get()))] // R794		pub fn set_fields(795			origin: OriginFor<T>,796			#[pallet::compact] index: RegistrarIndex,797			fields: IdentityFields,798		) -> DispatchResultWithPostInfo {799			let who = ensure_signed(origin)?;800801			let registrars = <Registrars<T>>::mutate(|rs| -> Result<usize, DispatchError> {802				rs.get_mut(index as usize)803					.and_then(|x| x.as_mut())804					.and_then(|r| {805						if r.account == who {806							r.fields = fields;807							Some(())808						} else {809							None810						}811					})812					.ok_or_else(|| DispatchError::from(Error::<T>::InvalidIndex))?;813				Ok(rs.len())814			})?;815			Ok(Some(T::WeightInfo::set_fields(816				registrars as u32, // R817			))818			.into())819		}820821		/// Provide a judgement for an account's identity.822		///823		/// The dispatch origin for this call must be _Signed_ and the sender must be the account824		/// of the registrar whose index is `reg_index`.825		///826		/// - `reg_index`: the index of the registrar whose judgement is being made.827		/// - `target`: the account whose identity the judgement is upon. This must be an account828		///   with a registered identity.829		/// - `judgement`: the judgement of the registrar of index `reg_index` about `target`.830		/// - `identity`: The hash of the [`IdentityInfo`] for that the judgement is provided.831		///832		/// Emits `JudgementGiven` if successful.833		///834		/// # <weight>835		/// - `O(R + X)`.836		/// - One balance-transfer operation.837		/// - Up to one account-lookup operation.838		/// - Storage: 1 read `O(R)`, 1 mutate `O(R + X)`.839		/// - One event.840		/// # </weight>841		#[pallet::call_index(9)]842		#[pallet::weight(T::WeightInfo::provide_judgement(843			T::MaxRegistrars::get(), // R844			T::MaxAdditionalFields::get(), // X845		))]846		pub fn provide_judgement(847			origin: OriginFor<T>,848			#[pallet::compact] reg_index: RegistrarIndex,849			target: AccountIdLookupOf<T>,850			judgement: Judgement<BalanceOf<T>>,851			identity: T::Hash,852		) -> DispatchResultWithPostInfo {853			let sender = ensure_signed(origin)?;854			let target = T::Lookup::lookup(target)?;855			ensure!(!judgement.has_deposit(), Error::<T>::InvalidJudgement);856			<Registrars<T>>::get()857				.get(reg_index as usize)858				.and_then(Option::as_ref)859				.filter(|r| r.account == sender)860				.ok_or(Error::<T>::InvalidIndex)?;861			let mut id = <IdentityOf<T>>::get(&target).ok_or(Error::<T>::InvalidTarget)?;862863			if T::Hashing::hash_of(&id.info) != identity {864				return Err(Error::<T>::JudgementForDifferentIdentity.into());865			}866867			let item = (reg_index, judgement);868			match id.judgements.binary_search_by_key(&reg_index, |x| x.0) {869				Ok(position) => {870					if let Judgement::FeePaid(fee) = id.judgements[position].1 {871						T::Currency::repatriate_reserved(872							&target,873							&sender,874							fee,875							BalanceStatus::Free,876						)877						.map_err(|_| Error::<T>::JudgementPaymentFailed)?;878					}879					id.judgements[position] = item880				}881				Err(position) => id882					.judgements883					.try_insert(position, item)884					.map_err(|_| Error::<T>::TooManyRegistrars)?,885			}886887			let judgements = id.judgements.len();888			let extra_fields = id.info.additional.len();889			<IdentityOf<T>>::insert(&target, id);890			Self::deposit_event(Event::JudgementGiven {891				target,892				registrar_index: reg_index,893			});894895			Ok(Some(T::WeightInfo::provide_judgement(896				judgements as u32,897				extra_fields as u32,898			))899			.into())900		}901902		/// Remove an account's identity and sub-account information and slash the deposits.903		///904		/// Payment: Reserved balances from `set_subs` and `set_identity` are slashed and handled by905		/// `Slash`. Verification request deposits are not returned; they should be cancelled906		/// manually using `cancel_request`.907		///908		/// The dispatch origin for this call must match `T::ForceOrigin`.909		///910		/// - `target`: the account whose identity the judgement is upon. This must be an account911		///   with a registered identity.912		///913		/// Emits `IdentityKilled` if successful.914		///915		/// # <weight>916		/// - `O(R + S + X)`.917		/// - One balance-reserve operation.918		/// - `S + 2` storage mutations.919		/// - One event.920		/// # </weight>921		#[pallet::call_index(10)]922		#[pallet::weight(T::WeightInfo::kill_identity(923			T::MaxRegistrars::get(), // R924			T::MaxSubAccounts::get(), // S925			T::MaxAdditionalFields::get(), // X926		))]927		pub fn kill_identity(928			origin: OriginFor<T>,929			target: AccountIdLookupOf<T>,930		) -> DispatchResultWithPostInfo {931			T::ForceOrigin::ensure_origin(origin)?;932933			// Figure out who we're meant to be clearing.934			let target = T::Lookup::lookup(target)?;935			// Grab their deposit (and check that they have one).936			let (subs_deposit, sub_ids) = <SubsOf<T>>::take(&target);937			let id = <IdentityOf<T>>::take(&target).ok_or(Error::<T>::NotNamed)?;938			let deposit = id.total_deposit() + subs_deposit;939			for sub in sub_ids.iter() {940				<SuperOf<T>>::remove(sub);941			}942			// Slash their deposit from them.943			T::Slashed::on_unbalanced(T::Currency::slash_reserved(&target, deposit).0);944945			Self::deposit_event(Event::IdentityKilled {946				who: target,947				deposit,948			});949950			Ok(Some(T::WeightInfo::kill_identity(951				id.judgements.len() as u32,      // R952				sub_ids.len() as u32,            // S953				id.info.additional.len() as u32, // X954			))955			.into())956		}957958		/// Add the given account to the sender's subs.959		///960		/// Payment: Balance reserved by a previous `set_subs` call for one sub will be repatriated961		/// to the sender.962		///963		/// The dispatch origin for this call must be _Signed_ and the sender must have a registered964		/// sub identity of `sub`.965		#[pallet::call_index(11)]966		#[pallet::weight(T::WeightInfo::add_sub(T::MaxSubAccounts::get()))]967		pub fn add_sub(968			origin: OriginFor<T>,969			sub: AccountIdLookupOf<T>,970			data: Data,971		) -> DispatchResult {972			let sender = ensure_signed(origin)?;973			let sub = T::Lookup::lookup(sub)?;974			ensure!(975				IdentityOf::<T>::contains_key(&sender),976				Error::<T>::NoIdentity977			);978979			// Check if it's already claimed as sub-identity.980			ensure!(981				!SuperOf::<T>::contains_key(&sub),982				Error::<T>::AlreadyClaimed983			);984985			SubsOf::<T>::try_mutate(&sender, |(ref mut subs_deposit, ref mut sub_ids)| {986				// Ensure there is space and that the deposit is paid.987				ensure!(988					sub_ids.len() < T::MaxSubAccounts::get() as usize,989					Error::<T>::TooManySubAccounts990				);991				let deposit = T::SubAccountDeposit::get();992				T::Currency::reserve(&sender, deposit)?;993994				SuperOf::<T>::insert(&sub, (sender.clone(), data));995				sub_ids996					.try_push(sub.clone())997					.expect("sub ids length checked above; qed");998				*subs_deposit = subs_deposit.saturating_add(deposit);9991000				Self::deposit_event(Event::SubIdentityAdded {1001					sub,1002					main: sender.clone(),1003					deposit,1004				});1005				Ok(())1006			})1007		}10081009		/// Alter the associated name of the given sub-account.1010		///1011		/// The dispatch origin for this call must be _Signed_ and the sender must have a registered1012		/// sub identity of `sub`.1013		#[pallet::call_index(12)]1014		#[pallet::weight(T::WeightInfo::rename_sub(T::MaxSubAccounts::get()))]1015		pub fn rename_sub(1016			origin: OriginFor<T>,1017			sub: AccountIdLookupOf<T>,1018			data: Data,1019		) -> DispatchResult {1020			let sender = ensure_signed(origin)?;1021			let sub = T::Lookup::lookup(sub)?;1022			ensure!(1023				IdentityOf::<T>::contains_key(&sender),1024				Error::<T>::NoIdentity1025			);1026			ensure!(1027				SuperOf::<T>::get(&sub).map_or(false, |x| x.0 == sender),1028				Error::<T>::NotOwned1029			);1030			SuperOf::<T>::insert(&sub, (sender, data));1031			Ok(())1032		}10331034		/// Remove the given account from the sender's subs.1035		///1036		/// Payment: Balance reserved by a previous `set_subs` call for one sub will be repatriated1037		/// to the sender.1038		///1039		/// The dispatch origin for this call must be _Signed_ and the sender must have a registered1040		/// sub identity of `sub`.1041		#[pallet::call_index(13)]1042		#[pallet::weight(T::WeightInfo::remove_sub(T::MaxSubAccounts::get()))]1043		pub fn remove_sub(origin: OriginFor<T>, sub: AccountIdLookupOf<T>) -> DispatchResult {1044			let sender = ensure_signed(origin)?;1045			ensure!(1046				IdentityOf::<T>::contains_key(&sender),1047				Error::<T>::NoIdentity1048			);1049			let sub = T::Lookup::lookup(sub)?;1050			let (sup, _) = SuperOf::<T>::get(&sub).ok_or(Error::<T>::NotSub)?;1051			ensure!(sup == sender, Error::<T>::NotOwned);1052			SuperOf::<T>::remove(&sub);1053			SubsOf::<T>::mutate(&sup, |(ref mut subs_deposit, ref mut sub_ids)| {1054				sub_ids.retain(|x| x != &sub);1055				let deposit = T::SubAccountDeposit::get().min(*subs_deposit);1056				*subs_deposit -= deposit;1057				let err_amount = T::Currency::unreserve(&sender, deposit);1058				debug_assert!(err_amount.is_zero());1059				Self::deposit_event(Event::SubIdentityRemoved {1060					sub,1061					main: sender,1062					deposit,1063				});1064			});1065			Ok(())1066		}10671068		/// Remove the sender as a sub-account.1069		///1070		/// Payment: Balance reserved by a previous `set_subs` call for one sub will be repatriated1071		/// to the sender (*not* the original depositor).1072		///1073		/// The dispatch origin for this call must be _Signed_ and the sender must have a registered1074		/// super-identity.1075		///1076		/// NOTE: This should not normally be used, but is provided in the case that the non-1077		/// controller of an account is maliciously registered as a sub-account.1078		#[pallet::call_index(14)]1079		#[pallet::weight(T::WeightInfo::quit_sub(T::MaxSubAccounts::get()))]1080		pub fn quit_sub(origin: OriginFor<T>) -> DispatchResult {1081			let sender = ensure_signed(origin)?;1082			let (sup, _) = SuperOf::<T>::take(&sender).ok_or(Error::<T>::NotSub)?;1083			SubsOf::<T>::mutate(&sup, |(ref mut subs_deposit, ref mut sub_ids)| {1084				sub_ids.retain(|x| x != &sender);1085				let deposit = T::SubAccountDeposit::get().min(*subs_deposit);1086				*subs_deposit -= deposit;1087				let _ =1088					T::Currency::repatriate_reserved(&sup, &sender, deposit, BalanceStatus::Free);1089				Self::deposit_event(Event::SubIdentityRevoked {1090					sub: sender,1091					main: sup.clone(),1092					deposit,1093				});1094			});1095			Ok(())1096		}10971098		/// Set identities to be associated with the provided accounts as force origin.1099		///1100		/// This is not meant to operate in tandem with the identity pallet as is,1101		/// and be instead used to keep identities made and verified externally,1102		/// forbidden from interacting with an ordinary user, since it ignores any safety mechanism.1103		#[pallet::call_index(15)]1104		#[pallet::weight(T::WeightInfo::force_insert_identities(1105			T::MaxAdditionalFields::get(), // X1106			identities.len() as u32, // N1107		))]1108		pub fn force_insert_identities(1109			origin: OriginFor<T>,1110			identities: Vec<(1111				T::AccountId,1112				Registration<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields>,1113			)>,1114		) -> DispatchResult {1115			T::ForceOrigin::ensure_origin(origin)?;1116			for identity in identities.clone() {1117				IdentityOf::<T>::insert(identity.0, identity.1);1118			}1119			Self::deposit_event(Event::IdentitiesInserted {1120				amount: identities.len() as u32,1121			});1122			Ok(())1123		}11241125		/// Remove identities associated with the provided accounts as force origin.1126		///1127		/// This is not meant to operate in tandem with the identity pallet as is,1128		/// and be instead used to keep identities made and verified externally,1129		/// forbidden from interacting with an ordinary user, since it ignores any safety mechanism.1130		#[pallet::call_index(16)]1131		#[pallet::weight(T::WeightInfo::force_remove_identities(1132			T::MaxAdditionalFields::get(), // X1133			identities.len() as u32, // N1134		))]1135		pub fn force_remove_identities(1136			origin: OriginFor<T>,1137			identities: Vec<T::AccountId>,1138		) -> DispatchResult {1139			T::ForceOrigin::ensure_origin(origin)?;1140			for identity in identities.clone() {1141				let (_, sub_ids) = <SubsOf<T>>::take(&identity);1142				<IdentityOf<T>>::remove(&identity);1143				for sub in sub_ids.iter() {1144					<SuperOf<T>>::remove(sub);1145				}1146			}1147			Self::deposit_event(Event::IdentitiesRemoved {1148				amount: identities.len() as u32,1149			});1150			Ok(())1151		}11521153		/// Set sub-identities to be associated with the provided accounts as force origin.1154		///1155		/// This is not meant to operate in tandem with the identity pallet as is,1156		/// and be instead used to keep identities made and verified externally,1157		/// forbidden from interacting with an ordinary user, since it ignores any safety mechanism.1158		#[pallet::call_index(17)]1159		#[pallet::weight(T::WeightInfo::force_set_subs(1160			T::MaxSubAccounts::get(), // S1161			subs.len() as u32, // N1162		))]1163		pub fn force_set_subs(1164			origin: OriginFor<T>,1165			subs: Vec<(1166				T::AccountId,1167				(1168					BalanceOf<T>,1169					BoundedVec<(T::AccountId, Data), T::MaxSubAccounts>,1170				),1171			)>,1172		) -> DispatchResult {1173			T::ForceOrigin::ensure_origin(origin)?;1174			for identity in subs.clone() {1175				let account = identity.0;1176				let (_, old_subs) = <SubsOf<T>>::get(&account);1177				for old_sub in old_subs {1178					<SuperOf<T>>::remove(old_sub);1179				}11801181				let mut ids = BoundedVec::<T::AccountId, T::MaxSubAccounts>::default();1182				for (id, name) in identity.1 .1 {1183					<SuperOf<T>>::insert(&id, (account.clone(), name));1184					ids.try_push(id)1185						.expect("subs length is less than T::MaxSubAccounts; qed");1186				}11871188				if ids.is_empty() {1189					<SubsOf<T>>::remove(&account);1190				} else {1191					<SubsOf<T>>::insert(account, (identity.1 .0, ids));1192				}1193			}1194			Self::deposit_event(Event::SubIdentitiesInserted {1195				amount: subs.len() as u32,1196			});1197			Ok(())1198		}1199	}1200}12011202impl<T: Config> Pallet<T> {1203	/// Get the subs of an account.1204	pub fn subs(who: &T::AccountId) -> Vec<(T::AccountId, Data)> {1205		SubsOf::<T>::get(who)1206			.11207			.into_iter()1208			.filter_map(|a| SuperOf::<T>::get(&a).map(|x| (a, x.1)))1209			.collect()1210	}12111212	/// Check if the account has corresponding identity information by the identity field.1213	pub fn has_identity(who: &T::AccountId, fields: u64) -> bool {1214		IdentityOf::<T>::get(who).map_or(false, |registration| {1215			(registration.info.fields().0.bits() & fields) == fields1216		})1217	}1218}