1use frame_support::{dispatch::DispatchResult, traits::Get, BoundedBTreeMap, BoundedVec};
2use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, Error, MaxEncodedLen};
3use scale_info::TypeInfo;
4#[cfg(feature = "std")]
5use serde::{Deserialize, Serialize};
6use sp_runtime::{
7 traits::{AtLeast32BitUnsigned, Zero},
8 DispatchError, MultiSignature, RuntimeDebug,
9};
10extern crate alloc;
11use alloc::vec::Vec;
12
13pub use crate::schema::SchemaId;
14
15pub type MessageSourceId = u64;
17
18pub use sp_core::H160;
20
21#[derive(TypeInfo, Encode, Decode)]
23pub struct AccountId20Response {
24 pub account_id: H160,
26
27 pub account_id_checksummed: alloc::string::String,
29}
30
31#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
35#[derive(TypeInfo, Default, Debug, Clone, Copy, PartialEq, MaxEncodedLen, Eq)]
36pub struct DelegatorId(pub MessageSourceId);
37
38impl EncodeLike for DelegatorId {}
39
40impl Encode for DelegatorId {
41 fn encode(&self) -> Vec<u8> {
42 self.0.encode()
43 }
44}
45
46impl DecodeWithMemTracking for DelegatorId {}
47
48impl Decode for DelegatorId {
49 fn decode<I: parity_scale_codec::Input>(
50 input: &mut I,
51 ) -> Result<Self, parity_scale_codec::Error> {
52 match <u64>::decode(input) {
53 Ok(x) => Ok(DelegatorId(x)),
54 _ => Err(Error::from("Could not decode DelegatorId")),
55 }
56 }
57}
58
59impl From<MessageSourceId> for DelegatorId {
60 fn from(t: MessageSourceId) -> Self {
61 DelegatorId(t)
62 }
63}
64
65impl From<DelegatorId> for MessageSourceId {
66 fn from(t: DelegatorId) -> MessageSourceId {
67 t.0
68 }
69}
70
71#[cfg_attr(feature = "std", derive(Deserialize, Serialize))]
73#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
74pub struct DelegationResponse<SchemaId, BlockNumber> {
75 pub provider_id: ProviderId,
77 pub permissions: Vec<SchemaGrant<SchemaId, BlockNumber>>,
79}
80
81#[cfg_attr(feature = "std", derive(Deserialize, Serialize))]
83#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
84pub struct SchemaGrant<SchemaId, BlockNumber> {
85 pub schema_id: SchemaId,
87 pub revoked_at: BlockNumber,
89}
90
91impl<SchemaId, BlockNumber> PartialEq for DelegationResponse<SchemaId, BlockNumber>
92where
93 SchemaId: PartialEq,
94 BlockNumber: PartialEq,
95{
96 fn eq(&self, other: &Self) -> bool {
97 self.provider_id == other.provider_id && self.permissions == other.permissions
98 }
99}
100
101impl<SchemaId, BlockNumber> SchemaGrant<SchemaId, BlockNumber> {
102 pub fn new(schema_id: SchemaId, revoked_at: BlockNumber) -> Self {
104 SchemaGrant { schema_id, revoked_at }
105 }
106}
107
108impl<SchemaId, BlockNumber> PartialEq for SchemaGrant<SchemaId, BlockNumber>
109where
110 SchemaId: PartialEq,
111 BlockNumber: PartialEq,
112{
113 fn eq(&self, other: &Self) -> bool {
114 self.schema_id == other.schema_id && self.revoked_at == other.revoked_at
115 }
116}
117
118#[derive(TypeInfo, RuntimeDebug, Clone, Decode, Encode, MaxEncodedLen, Eq)]
120#[scale_info(skip_type_params(MaxSchemaGrantsPerDelegation))]
121pub struct Delegation<SchemaId, BlockNumber, MaxSchemaGrantsPerDelegation>
122where
123 MaxSchemaGrantsPerDelegation: Get<u32>,
124{
125 pub revoked_at: BlockNumber,
127 pub schema_permissions: BoundedBTreeMap<SchemaId, BlockNumber, MaxSchemaGrantsPerDelegation>,
129}
130
131impl<SchemaId, BlockNumber, MaxSchemaGrantsPerDelegation> PartialEq
133 for Delegation<SchemaId, BlockNumber, MaxSchemaGrantsPerDelegation>
134where
135 SchemaId: PartialEq,
136 BlockNumber: PartialEq,
137 MaxSchemaGrantsPerDelegation: Get<u32>,
138{
139 fn eq(&self, other: &Self) -> bool {
140 self.revoked_at == other.revoked_at && self.schema_permissions == other.schema_permissions
141 }
142}
143
144impl<
145 SchemaId: Ord + Default,
146 BlockNumber: Ord + Copy + Zero + AtLeast32BitUnsigned + Default,
147 MaxSchemaGrantsPerDelegation: Get<u32>,
148 > Default for Delegation<SchemaId, BlockNumber, MaxSchemaGrantsPerDelegation>
149{
150 fn default() -> Self {
152 Delegation {
153 revoked_at: BlockNumber::default(),
154 schema_permissions: BoundedBTreeMap::<
155 SchemaId,
156 BlockNumber,
157 MaxSchemaGrantsPerDelegation,
158 >::new(),
159 }
160 }
161}
162
163#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
167#[derive(TypeInfo, Default, Debug, Clone, Copy, PartialEq, MaxEncodedLen, Eq)]
168pub struct ProviderId(pub MessageSourceId);
169
170impl EncodeLike for ProviderId {}
171
172impl Encode for ProviderId {
173 fn encode(&self) -> Vec<u8> {
174 self.0.encode()
175 }
176}
177
178impl DecodeWithMemTracking for ProviderId {}
179
180impl Decode for ProviderId {
181 fn decode<I: parity_scale_codec::Input>(
182 input: &mut I,
183 ) -> Result<Self, parity_scale_codec::Error> {
184 match <u64>::decode(input) {
185 Ok(x) => Ok(ProviderId(x)),
186 _ => Err(Error::from("Could not decode ProviderId")),
187 }
188 }
189}
190
191impl From<MessageSourceId> for ProviderId {
192 fn from(t: MessageSourceId) -> Self {
193 ProviderId(t)
194 }
195}
196
197impl From<ProviderId> for MessageSourceId {
198 fn from(t: ProviderId) -> MessageSourceId {
199 t.0
200 }
201}
202
203#[derive(MaxEncodedLen, TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Eq)]
206#[scale_info(skip_type_params(T))]
207pub struct ProviderRegistryEntry<T>
208where
209 T: Get<u32>,
210{
211 pub provider_name: BoundedVec<u8, T>,
213}
214
215#[derive(MaxEncodedLen, TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Eq)]
217pub struct SignatureRegistryPointer<BlockNumber> {
218 pub newest: MultiSignature,
220
221 pub newest_expires_at: BlockNumber,
223
224 pub oldest: MultiSignature,
226
227 pub count: u32,
230}
231
232pub trait MsaLookup {
234 type AccountId;
236
237 fn get_msa_id(key: &Self::AccountId) -> Option<MessageSourceId>;
239}
240
241pub trait MsaValidator {
243 type AccountId;
245
246 fn ensure_valid_msa_key(key: &Self::AccountId) -> Result<MessageSourceId, DispatchError>;
249}
250
251pub trait ProviderLookup {
253 type BlockNumber;
255 type MaxSchemaGrantsPerDelegation: Get<u32>;
257 type SchemaId;
259
260 fn get_delegation_of(
262 delegator: DelegatorId,
263 provider: ProviderId,
264 ) -> Option<Delegation<Self::SchemaId, Self::BlockNumber, Self::MaxSchemaGrantsPerDelegation>>;
265}
266
267pub trait DelegationValidator {
269 type BlockNumber;
271 type MaxSchemaGrantsPerDelegation: Get<u32>;
273 type SchemaId;
275
276 fn ensure_valid_delegation(
278 provider: ProviderId,
279 delegator: DelegatorId,
280 block_number: Option<Self::BlockNumber>,
281 ) -> Result<
282 Delegation<Self::SchemaId, Self::BlockNumber, Self::MaxSchemaGrantsPerDelegation>,
283 DispatchError,
284 >;
285}
286
287pub trait SchemaGrantValidator<BlockNumber> {
289 fn ensure_valid_schema_grant(
291 provider_id: ProviderId,
292 delegator_id: DelegatorId,
293 schema_id: SchemaId,
294 block_number: BlockNumber,
295 ) -> DispatchResult;
296}
297
298pub trait MsaKeyProvider {
300 type AccountId;
302 fn key_eligible_for_subsidized_addition(
304 old_key: Self::AccountId,
305 new_key: Self::AccountId,
306 msa_id: MessageSourceId,
307 ) -> bool;
308}
309
310#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
312#[derive(TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Default, MaxEncodedLen)]
313pub struct KeyInfoResponse<AccountId> {
314 pub msa_id: MessageSourceId,
316 pub msa_keys: Vec<AccountId>,
318}
319
320#[cfg(test)]
321mod tests {
322 use super::*;
323
324 #[test]
325 fn decoding_provider_id_failure() {
326 let mut da: &[u8] = b"\xf6\xf5";
327 let decoded = DelegatorId::decode(&mut da);
328 assert!(decoded.is_err());
329 }
330
331 #[test]
332 fn decoding_provider_id_success() {
333 let val = 16777215_u64.encode();
334 let decoded = ProviderId::decode(&mut &val[..]);
335 assert_eq!(decoded, Ok(ProviderId(16777215)))
336 }
337
338 #[test]
339 fn decoding_delegate_id_failure() {
340 let mut da: &[u8] = b"\xf6\xf5";
341 let decoded = DelegatorId::decode(&mut da);
342 assert!(decoded.is_err());
343 }
344
345 #[test]
346 fn decoding_delegator_id_success() {
347 let val = 42_u64.encode();
348 let decoded = DelegatorId::decode(&mut &val[..]);
349 assert_eq!(decoded, Ok(DelegatorId(42)))
350 }
351}