1use core::fmt::Debug;
2use frame_support::{dispatch::DispatchResult, traits::Get, BoundedBTreeMap, BoundedVec};
3use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, Error, MaxEncodedLen};
4use scale_info::TypeInfo;
5#[cfg(feature = "std")]
6use serde::{Deserialize, Serialize};
7use sp_runtime::{
8 traits::{AtLeast32BitUnsigned, Zero},
9 DispatchError, MultiSignature, RuntimeDebug,
10};
11extern crate alloc;
12pub use crate::schema::{IntentId, SchemaId};
13use alloc::vec::Vec;
14use serde::{ser::SerializeStruct, Serializer};
15
16pub type ApplicationIndex = u16;
18
19pub type ApplicationContext<NameSize, LangSize, CidSize, MaxLocaleCount> =
21 ProviderRegistryEntry<NameSize, LangSize, CidSize, MaxLocaleCount>;
22
23pub type MessageSourceId = u64;
25
26pub use sp_core::H160;
28
29#[derive(TypeInfo, Encode, Decode)]
31pub struct AccountId20Response {
32 pub account_id: H160,
34
35 pub account_id_checksummed: alloc::string::String,
37}
38
39#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
43#[derive(TypeInfo, Default, Debug, Clone, Copy, PartialEq, MaxEncodedLen, Eq)]
44pub struct DelegatorId(pub MessageSourceId);
45
46impl EncodeLike for DelegatorId {}
47
48impl Encode for DelegatorId {
49 fn encode(&self) -> Vec<u8> {
50 self.0.encode()
51 }
52}
53
54impl DecodeWithMemTracking for DelegatorId {}
55
56impl Decode for DelegatorId {
57 fn decode<I: parity_scale_codec::Input>(
58 input: &mut I,
59 ) -> Result<Self, parity_scale_codec::Error> {
60 match <u64>::decode(input) {
61 Ok(x) => Ok(DelegatorId(x)),
62 _ => Err(Error::from("Could not decode DelegatorId")),
63 }
64 }
65}
66
67impl From<MessageSourceId> for DelegatorId {
68 fn from(t: MessageSourceId) -> Self {
69 DelegatorId(t)
70 }
71}
72
73impl From<DelegatorId> for MessageSourceId {
74 fn from(t: DelegatorId) -> MessageSourceId {
75 t.0
76 }
77}
78
79#[cfg_attr(feature = "std", derive(Deserialize, Serialize))]
81#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
82pub struct DelegationResponse<DelegationIdType, BlockNumber> {
83 pub provider_id: ProviderId,
85 pub permissions: Vec<DelegationGrant<DelegationIdType, BlockNumber>>,
87}
88
89#[cfg_attr(feature = "std", derive(Deserialize))]
91#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
92pub struct DelegationGrant<DelegationIdType, BlockNumber> {
93 pub granted_id: DelegationIdType,
95 pub revoked_at: BlockNumber,
97}
98
99#[cfg(feature = "std")]
103impl<DelegationIdType: Serialize, BlockNumber: Serialize> Serialize
104 for DelegationGrant<DelegationIdType, BlockNumber>
105{
106 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
107 where
108 S: Serializer,
109 {
110 let mut s = serializer.serialize_struct("DelegationGrant", 3)?;
111 s.serialize_field("schema_id", &self.granted_id)?;
112 s.serialize_field("granted_id", &self.granted_id)?;
113 s.serialize_field("revoked_at", &self.revoked_at)?;
114 s.end()
115 }
116}
117
118impl<DelegationIdType, BlockNumber> PartialEq for DelegationResponse<DelegationIdType, BlockNumber>
119where
120 DelegationIdType: PartialEq,
121 BlockNumber: PartialEq,
122{
123 fn eq(&self, other: &Self) -> bool {
124 self.provider_id == other.provider_id && self.permissions == other.permissions
125 }
126}
127
128impl<DelegationIdType, BlockNumber> DelegationGrant<DelegationIdType, BlockNumber> {
129 pub fn new(granted_id: DelegationIdType, revoked_at: BlockNumber) -> Self {
131 DelegationGrant { granted_id, revoked_at }
132 }
133}
134
135impl<DelegatedIdType, BlockNumber> PartialEq for DelegationGrant<DelegatedIdType, BlockNumber>
136where
137 DelegatedIdType: PartialEq,
138 BlockNumber: PartialEq,
139{
140 fn eq(&self, other: &Self) -> bool {
141 self.granted_id == other.granted_id && self.revoked_at == other.revoked_at
142 }
143}
144
145#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
147#[scale_info(skip_type_params(MaxGrantsPerDelegation))]
148pub struct Delegation<DelegatedIdType, BlockNumber, MaxGrantsPerDelegation>
149where
150 MaxGrantsPerDelegation: Get<u32>,
151{
152 pub revoked_at: BlockNumber,
154 pub permissions: BoundedBTreeMap<DelegatedIdType, BlockNumber, MaxGrantsPerDelegation>,
156}
157
158impl<DelegatedIdType, BlockNumber, MaxGrantsPerDelegation> PartialEq
160 for Delegation<DelegatedIdType, BlockNumber, MaxGrantsPerDelegation>
161where
162 DelegatedIdType: PartialEq,
163 BlockNumber: PartialEq,
164 MaxGrantsPerDelegation: Get<u32>,
165{
166 fn eq(&self, other: &Self) -> bool {
167 self.revoked_at == other.revoked_at && self.permissions == other.permissions
168 }
169}
170
171impl<
172 DelegatedIdType: Ord + Default,
173 BlockNumber: Ord + Copy + Zero + AtLeast32BitUnsigned + Default,
174 MaxGrantsPerDelegation: Get<u32>,
175 > Default for Delegation<DelegatedIdType, BlockNumber, MaxGrantsPerDelegation>
176{
177 fn default() -> Self {
179 Delegation {
180 revoked_at: BlockNumber::default(),
181 permissions:
182 BoundedBTreeMap::<DelegatedIdType, BlockNumber, MaxGrantsPerDelegation>::new(),
183 }
184 }
185}
186
187#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
191#[derive(TypeInfo, Default, Debug, Clone, Copy, PartialEq, MaxEncodedLen, Eq)]
192pub struct ProviderId(pub MessageSourceId);
193
194impl EncodeLike for ProviderId {}
195
196impl Encode for ProviderId {
197 fn encode(&self) -> Vec<u8> {
198 self.0.encode()
199 }
200}
201
202impl DecodeWithMemTracking for ProviderId {}
203
204impl Decode for ProviderId {
205 fn decode<I: parity_scale_codec::Input>(
206 input: &mut I,
207 ) -> Result<Self, parity_scale_codec::Error> {
208 match <u64>::decode(input) {
209 Ok(x) => Ok(ProviderId(x)),
210 _ => Err(Error::from("Could not decode ProviderId")),
211 }
212 }
213}
214
215impl From<MessageSourceId> for ProviderId {
216 fn from(t: MessageSourceId) -> Self {
217 ProviderId(t)
218 }
219}
220
221impl From<ProviderId> for MessageSourceId {
222 fn from(t: ProviderId) -> MessageSourceId {
223 t.0
224 }
225}
226
227#[derive(
231 MaxEncodedLen, TypeInfo, Clone, Debug, Decode, DecodeWithMemTracking, Encode, PartialEq, Eq,
232)]
233#[scale_info(skip_type_params(NameSize, LangSize, CidSize, MaxLocaleCount))]
234#[codec(mel_bound(
235 NameSize: Get<u32> + Debug + PartialEq + Eq,
236 LangSize: Get<u32> + Debug + PartialEq + Eq,
237 CidSize: Get<u32> + Debug + PartialEq + Eq,
238 MaxLocaleCount: Get<u32> + Debug + PartialEq + Eq,
239))]
240pub struct ProviderRegistryEntry<
241 NameSize: Get<u32> + Debug + PartialEq + Eq,
242 LangSize: Get<u32> + Debug + PartialEq + Eq,
243 CidSize: Get<u32> + Debug + PartialEq + Eq,
244 MaxLocaleCount: Get<u32> + Debug + PartialEq + Eq,
245> {
246 pub default_name: BoundedVec<u8, NameSize>,
248
249 pub localized_names:
251 BoundedBTreeMap<BoundedVec<u8, LangSize>, BoundedVec<u8, NameSize>, MaxLocaleCount>,
252
253 pub default_logo_250_100_png_cid: BoundedVec<u8, CidSize>,
255
256 pub localized_logo_250_100_png_cids:
258 BoundedBTreeMap<BoundedVec<u8, LangSize>, BoundedVec<u8, CidSize>, MaxLocaleCount>,
259}
260
261impl<NameSize, LangSize, CidSize, MaxLocaleCount> Default
262 for ProviderRegistryEntry<NameSize, LangSize, CidSize, MaxLocaleCount>
263where
264 NameSize: Get<u32> + Debug + PartialEq + Eq,
265 LangSize: Get<u32> + Debug + PartialEq + Eq,
266 CidSize: Get<u32> + Debug + PartialEq + Eq,
267 MaxLocaleCount: Get<u32> + Debug + PartialEq + Eq,
268{
269 fn default() -> Self {
270 Self {
271 default_name: BoundedVec::default(),
272 localized_names: BoundedBTreeMap::default(),
273 default_logo_250_100_png_cid: BoundedVec::default(),
274 localized_logo_250_100_png_cids: BoundedBTreeMap::default(),
275 }
276 }
277}
278
279#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
281#[derive(TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Default)]
282pub struct ProviderApplicationContext {
283 pub default_name: Vec<u8>,
285 pub provider_id: ProviderId,
287 pub default_logo_250_100_png_bytes: Option<Vec<u8>>,
289 pub application_id: Option<ApplicationIndex>,
291 pub localized_name: Option<Vec<u8>>,
293 pub localized_logo_250_100_png_bytes: Option<Vec<u8>>,
295}
296
297#[derive(MaxEncodedLen, TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Eq)]
299pub struct SignatureRegistryPointer<BlockNumber> {
300 pub newest: MultiSignature,
302
303 pub newest_expires_at: BlockNumber,
305
306 pub oldest: MultiSignature,
308
309 pub count: u32,
312}
313
314pub trait MsaLookup {
316 type AccountId;
318
319 fn get_msa_id(key: &Self::AccountId) -> Option<MessageSourceId>;
321
322 fn get_max_msa_id() -> MessageSourceId;
324}
325
326pub trait MsaValidator {
328 type AccountId;
330
331 fn ensure_valid_msa_key(key: &Self::AccountId) -> Result<MessageSourceId, DispatchError>;
334}
335
336pub trait ProviderLookup {
338 type BlockNumber;
340 type MaxGrantsPerDelegation: Get<u32>;
342 type DelegationId;
344
345 fn get_delegation_of(
347 delegator: DelegatorId,
348 provider: ProviderId,
349 ) -> Option<Delegation<Self::DelegationId, Self::BlockNumber, Self::MaxGrantsPerDelegation>>;
350}
351
352pub trait DelegationValidator {
354 type BlockNumber;
356 type MaxGrantsPerDelegation: Get<u32>;
358 type DelegationIdType;
360
361 fn ensure_valid_delegation(
363 provider: ProviderId,
364 delegator: DelegatorId,
365 block_number: Option<Self::BlockNumber>,
366 ) -> Result<
367 Delegation<Self::DelegationIdType, Self::BlockNumber, Self::MaxGrantsPerDelegation>,
368 DispatchError,
369 >;
370}
371
372pub trait GrantValidator<DelegationIdType, BlockNumber> {
374 fn ensure_valid_grant(
376 provider_id: ProviderId,
377 delegator_id: DelegatorId,
378 id_to_check: DelegationIdType,
379 block_number: BlockNumber,
380 ) -> DispatchResult;
381}
382
383pub trait MsaKeyProvider {
385 type AccountId;
387 fn key_eligible_for_subsidized_addition(
389 old_key: Self::AccountId,
390 new_key: Self::AccountId,
391 msa_id: MessageSourceId,
392 ) -> bool;
393}
394
395#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
397#[derive(TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Default, MaxEncodedLen)]
398pub struct KeyInfoResponse<AccountId> {
399 pub msa_id: MessageSourceId,
401 pub msa_keys: Vec<AccountId>,
403}
404
405#[cfg(test)]
406mod tests {
407 use super::*;
408
409 #[test]
410 fn decoding_provider_id_failure() {
411 let mut da: &[u8] = b"\xf6\xf5";
412 let decoded = DelegatorId::decode(&mut da);
413 assert!(decoded.is_err());
414 }
415
416 #[test]
417 fn decoding_provider_id_success() {
418 let val = 16777215_u64.encode();
419 let decoded = ProviderId::decode(&mut &val[..]);
420 assert_eq!(decoded, Ok(ProviderId(16777215)))
421 }
422
423 #[test]
424 fn decoding_delegate_id_failure() {
425 let mut da: &[u8] = b"\xf6\xf5";
426 let decoded = DelegatorId::decode(&mut da);
427 assert!(decoded.is_err());
428 }
429
430 #[test]
431 fn decoding_delegator_id_success() {
432 let val = 42_u64.encode();
433 let decoded = DelegatorId::decode(&mut &val[..]);
434 assert_eq!(decoded, Ok(DelegatorId(42)))
435 }
436}