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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use codec::Codec;
use scale_info::TypeInfo;
#[cfg(feature = "full_crypto")]
use sp_core::crypto::Pair;
use sp_core::crypto::{CryptoType, CryptoTypeId, IsWrappedBy, KeyTypeId, Public};
use sp_std::{fmt::Debug, vec::Vec};
pub trait AppCrypto: 'static + Send + Sync + Sized + CryptoType + Clone {
const ID: KeyTypeId;
const CRYPTO_ID: CryptoTypeId;
type Public: AppPublic;
type Signature: AppSignature;
#[cfg(feature = "full_crypto")]
type Pair: AppPair;
}
#[cfg(any(feature = "std", feature = "full_crypto"))]
pub trait MaybeHash: sp_std::hash::Hash {}
#[cfg(any(feature = "std", feature = "full_crypto"))]
impl<T: sp_std::hash::Hash> MaybeHash for T {}
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
pub trait MaybeHash {}
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
impl<T> MaybeHash for T {}
pub trait AppPublic:
AppCrypto + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + Codec
{
type Generic: IsWrappedBy<Self>
+ Public
+ Ord
+ PartialOrd
+ Eq
+ PartialEq
+ Debug
+ MaybeHash
+ Codec;
}
#[cfg(feature = "full_crypto")]
pub trait AppPair: AppCrypto + Pair<Public = <Self as AppCrypto>::Public> {
type Generic: IsWrappedBy<Self>
+ Pair<Public = <<Self as AppCrypto>::Public as AppPublic>::Generic>
+ Pair<Signature = <<Self as AppCrypto>::Signature as AppSignature>::Generic>;
}
pub trait AppSignature: AppCrypto + Eq + PartialEq + Debug {
type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug;
}
pub trait RuntimePublic: Sized {
type Signature: Debug + Eq + PartialEq + Clone;
fn all(key_type: KeyTypeId) -> crate::Vec<Self>;
fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self;
fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature>;
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool;
fn to_raw_vec(&self) -> Vec<u8>;
}
pub trait RuntimeAppPublic: Sized {
const ID: KeyTypeId;
type Signature: Debug + Eq + PartialEq + Clone + TypeInfo + Codec;
fn all() -> crate::Vec<Self>;
fn generate_pair(seed: Option<Vec<u8>>) -> Self;
fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature>;
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool;
fn to_raw_vec(&self) -> Vec<u8>;
}
impl<T> RuntimeAppPublic for T
where
T: AppPublic + AsRef<<T as AppPublic>::Generic>,
<T as AppPublic>::Generic: RuntimePublic,
<T as AppCrypto>::Signature: TypeInfo
+ Codec
+ From<<<T as AppPublic>::Generic as RuntimePublic>::Signature>
+ AsRef<<<T as AppPublic>::Generic as RuntimePublic>::Signature>,
{
const ID: KeyTypeId = <T as AppCrypto>::ID;
type Signature = <T as AppCrypto>::Signature;
fn all() -> crate::Vec<Self> {
<<T as AppPublic>::Generic as RuntimePublic>::all(Self::ID)
.into_iter()
.map(|p| p.into())
.collect()
}
fn generate_pair(seed: Option<Vec<u8>>) -> Self {
<<T as AppPublic>::Generic as RuntimePublic>::generate_pair(Self::ID, seed).into()
}
fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
<<T as AppPublic>::Generic as RuntimePublic>::sign(self.as_ref(), Self::ID, msg)
.map(|s| s.into())
}
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
<<T as AppPublic>::Generic as RuntimePublic>::verify(self.as_ref(), msg, signature.as_ref())
}
fn to_raw_vec(&self) -> Vec<u8> {
<<T as AppPublic>::Generic as RuntimePublic>::to_raw_vec(self.as_ref())
}
}
pub trait BoundToRuntimeAppPublic {
type Public: RuntimeAppPublic;
}
impl<T: RuntimeAppPublic> BoundToRuntimeAppPublic for T {
type Public = Self;
}