referrerpolicy=no-referrer-when-downgrade

pallet_identity/
legacy.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#[cfg(feature = "runtime-benchmarks")]
19use alloc::vec;
20use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
21#[cfg(feature = "runtime-benchmarks")]
22use enumflags2::BitFlag;
23use enumflags2::{bitflags, BitFlags};
24use frame_support::{traits::Get, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound};
25use scale_info::{build::Variants, Path, Type, TypeInfo};
26use sp_runtime::{BoundedVec, RuntimeDebug};
27
28use crate::types::{Data, IdentityInformationProvider};
29
30/// The fields that we use to identify the owner of an account with. Each corresponds to a field
31/// in the `IdentityInfo` struct.
32#[bitflags]
33#[repr(u64)]
34#[derive(Clone, Copy, PartialEq, Eq, RuntimeDebug)]
35pub enum IdentityField {
36	Display,
37	Legal,
38	Web,
39	Riot,
40	Email,
41	PgpFingerprint,
42	Image,
43	Twitter,
44}
45
46impl TypeInfo for IdentityField {
47	type Identity = Self;
48
49	fn type_info() -> scale_info::Type {
50		Type::builder().path(Path::new("IdentityField", module_path!())).variant(
51			Variants::new()
52				.variant("Display", |v| v.index(0))
53				.variant("Legal", |v| v.index(1))
54				.variant("Web", |v| v.index(2))
55				.variant("Riot", |v| v.index(3))
56				.variant("Email", |v| v.index(4))
57				.variant("PgpFingerprint", |v| v.index(5))
58				.variant("Image", |v| v.index(6))
59				.variant("Twitter", |v| v.index(7)),
60		)
61	}
62}
63
64/// Information concerning the identity of the controller of an account.
65///
66/// NOTE: This should be stored at the end of the storage item to facilitate the addition of extra
67/// fields in a backwards compatible way through a specialized `Decode` impl.
68#[derive(
69	CloneNoBound,
70	Encode,
71	Decode,
72	DecodeWithMemTracking,
73	EqNoBound,
74	MaxEncodedLen,
75	PartialEqNoBound,
76	RuntimeDebugNoBound,
77	TypeInfo,
78)]
79#[codec(mel_bound())]
80#[scale_info(skip_type_params(FieldLimit))]
81pub struct IdentityInfo<FieldLimit: Get<u32>> {
82	/// Additional fields of the identity that are not catered for with the struct's explicit
83	/// fields.
84	pub additional: BoundedVec<(Data, Data), FieldLimit>,
85
86	/// A reasonable display name for the controller of the account. This should be whatever it is
87	/// that it is typically known as and should not be confusable with other entities, given
88	/// reasonable context.
89	///
90	/// Stored as UTF-8.
91	pub display: Data,
92
93	/// The full legal name in the local jurisdiction of the entity. This might be a bit
94	/// long-winded.
95	///
96	/// Stored as UTF-8.
97	pub legal: Data,
98
99	/// A representative website held by the controller of the account.
100	///
101	/// NOTE: `https://` is automatically prepended.
102	///
103	/// Stored as UTF-8.
104	pub web: Data,
105
106	/// The Riot/Matrix handle held by the controller of the account.
107	///
108	/// Stored as UTF-8.
109	pub riot: Data,
110
111	/// The email address of the controller of the account.
112	///
113	/// Stored as UTF-8.
114	pub email: Data,
115
116	/// The PGP/GPG public key of the controller of the account.
117	pub pgp_fingerprint: Option<[u8; 20]>,
118
119	/// A graphic image representing the controller of the account. Should be a company,
120	/// organization or project logo or a headshot in the case of a human.
121	pub image: Data,
122
123	/// The Twitter identity. The leading `@` character may be elided.
124	pub twitter: Data,
125}
126
127impl<FieldLimit: Get<u32> + 'static> IdentityInformationProvider for IdentityInfo<FieldLimit> {
128	type FieldsIdentifier = u64;
129
130	fn has_identity(&self, fields: Self::FieldsIdentifier) -> bool {
131		self.fields().bits() & fields == fields
132	}
133
134	#[cfg(feature = "runtime-benchmarks")]
135	fn create_identity_info() -> Self {
136		let data = Data::Raw(vec![0; 32].try_into().unwrap());
137
138		IdentityInfo {
139			additional: vec![(data.clone(), data.clone()); FieldLimit::get().try_into().unwrap()]
140				.try_into()
141				.unwrap(),
142			display: data.clone(),
143			legal: data.clone(),
144			web: data.clone(),
145			riot: data.clone(),
146			email: data.clone(),
147			pgp_fingerprint: Some([0; 20]),
148			image: data.clone(),
149			twitter: data,
150		}
151	}
152
153	#[cfg(feature = "runtime-benchmarks")]
154	fn all_fields() -> Self::FieldsIdentifier {
155		IdentityField::all().bits()
156	}
157}
158
159impl<FieldLimit: Get<u32>> Default for IdentityInfo<FieldLimit> {
160	fn default() -> Self {
161		IdentityInfo {
162			additional: BoundedVec::default(),
163			display: Data::None,
164			legal: Data::None,
165			web: Data::None,
166			riot: Data::None,
167			email: Data::None,
168			pgp_fingerprint: None,
169			image: Data::None,
170			twitter: Data::None,
171		}
172	}
173}
174
175impl<FieldLimit: Get<u32>> IdentityInfo<FieldLimit> {
176	pub(crate) fn fields(&self) -> BitFlags<IdentityField> {
177		let mut res = BitFlags::<IdentityField>::empty();
178		if !self.display.is_none() {
179			res.insert(IdentityField::Display);
180		}
181		if !self.legal.is_none() {
182			res.insert(IdentityField::Legal);
183		}
184		if !self.web.is_none() {
185			res.insert(IdentityField::Web);
186		}
187		if !self.riot.is_none() {
188			res.insert(IdentityField::Riot);
189		}
190		if !self.email.is_none() {
191			res.insert(IdentityField::Email);
192		}
193		if self.pgp_fingerprint.is_some() {
194			res.insert(IdentityField::PgpFingerprint);
195		}
196		if !self.image.is_none() {
197			res.insert(IdentityField::Image);
198		}
199		if !self.twitter.is_none() {
200			res.insert(IdentityField::Twitter);
201		}
202		res
203	}
204}