1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use frame_support::{CloneNoBound, DebugNoBound, EqNoBound, PartialEqNoBound};
use sp_std::fmt::Debug;
use super::Limits;
#[cfg(feature = "serde")]
use crate::util::btree_set;
use crate::{
common::{AuthorizeTarget, ForSigType},
did::{DidKey, DidMethodKey, DidOrDidMethodKey},
util::{Associated, InclusionRule},
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::BoundedBTreeSet;
use sp_runtime::traits::TryCollect;
#[derive(
Encode,
Decode,
CloneNoBound,
PartialEqNoBound,
EqNoBound,
DebugNoBound,
MaxEncodedLen,
scale_info_derive::TypeInfo,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[scale_info(skip_type_params(T))]
#[scale_info(omit_prefix)]
pub enum Policy<T: Limits> {
OneOf(
#[cfg_attr(feature = "serde", serde(with = "btree_set"))]
BoundedBTreeSet<DidOrDidMethodKey, T::MaxPolicyControllers>,
),
}
impl<T: Limits> Policy<T> {
pub fn one_of<CI>(controllers: CI) -> Result<Self, PolicyValidationError>
where
CI: IntoIterator,
CI::IntoIter: ExactSizeIterator,
<CI::IntoIter as Iterator>::Item: Into<DidOrDidMethodKey>,
{
controllers
.into_iter()
.map(Into::into)
.try_collect()
.map_err(|_| PolicyValidationError::TooManyControllers)
.map(Self::OneOf)
}
pub fn expand(&self) -> InclusionRule<PolicyExecutor> {
let Self::OneOf(items) = self;
InclusionRule::any_of(items.iter().copied().map(Into::into))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PolicyValidationError {
Empty,
TooManyControllers,
}
impl<T: Limits> Policy<T> {
pub fn ensure_valid(&self) -> Result<(), PolicyValidationError> {
if self.is_empty() {
Err(PolicyValidationError::Empty)?
}
Ok(())
}
pub fn len(&self) -> u32 {
match self {
Self::OneOf(controllers) => controllers.len() as u32,
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, Copy, Ord, PartialOrd, MaxEncodedLen)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[derive(scale_info_derive::TypeInfo)]
#[scale_info(omit_prefix)]
pub struct PolicyExecutor(pub DidOrDidMethodKey);
crate::impl_wrapper!(PolicyExecutor(DidOrDidMethodKey));
impl<T, Target> AuthorizeTarget<T, Target, DidKey> for PolicyExecutor where Target: Associated<T> {}
impl<T, Target> AuthorizeTarget<T, Target, DidMethodKey> for PolicyExecutor where
Target: Associated<T>
{
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, MaxEncodedLen)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(scale_info_derive::TypeInfo)]
#[scale_info(omit_prefix)]
pub struct SignatureWithNonce<N, S> {
pub sig: S,
pub nonce: N,
}
impl<N, S> SignatureWithNonce<N, S> {
pub fn new(sig: S, nonce: N) -> Self {
Self {
sig: sig.into(),
nonce,
}
}
pub fn into_data(self) -> S {
self.sig
}
}
impl<N, S> ForSigType for SignatureWithNonce<N, S>
where
S: ForSigType,
{
fn for_sig_type<R>(
&self,
for_sr25519: impl FnOnce() -> R,
for_ed25519: impl FnOnce() -> R,
for_secp256k1: impl FnOnce() -> R,
) -> Option<R> {
self.sig
.for_sig_type(for_sr25519, for_ed25519, for_secp256k1)
}
}