23
23
#[ doc( hidden) ]
24
24
pub use primitives:: { self , crypto:: { CryptoType , Public , Derive , IsWrappedBy , Wraps } , RuntimeDebug } ;
25
25
#[ doc( hidden) ]
26
- #[ cfg( feature = "std " ) ]
26
+ #[ cfg( feature = "full_crypto " ) ]
27
27
pub use primitives:: crypto:: { SecretStringError , DeriveJunction , Ss58Codec , Pair } ;
28
28
pub use primitives:: { crypto:: { KeyTypeId , key_types} } ;
29
29
@@ -50,17 +50,43 @@ pub use traits::*;
50
50
/// // of value `b"fuba"`.
51
51
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
52
52
/// ```
53
+ #[ cfg( feature = "full_crypto" ) ]
53
54
#[ macro_export]
54
55
macro_rules! app_crypto {
55
56
( $module: ident, $key_type: expr) => {
56
- #[ cfg( feature="std" ) ]
57
- $crate:: app_crypto!( $module:: Pair , $module:: Public , $module:: Signature , $key_type) ;
58
- #[ cfg( not( feature="std" ) ) ]
59
- $crate:: app_crypto!( $module:: Public , $module:: Signature , $key_type) ;
57
+ $crate:: app_crypto_public_full_crypto!( $module:: Public , $key_type) ;
58
+ $crate:: app_crypto_public_common!( $module:: Public , $module:: Signature , $key_type) ;
59
+ $crate:: app_crypto_signature_full_crypto!( $module:: Signature , $key_type) ;
60
+ $crate:: app_crypto_signature_common!( $module:: Signature , $key_type) ;
61
+ $crate:: app_crypto_pair!( $module:: Pair , $key_type) ;
60
62
} ;
61
- ( $pair: ty, $public: ty, $sig: ty, $key_type: expr) => {
62
- $crate:: app_crypto!( $public, $sig, $key_type) ;
63
+ }
63
64
65
+ /// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
66
+ /// Application-specific types whose identifier is `$key_type`.
67
+ ///
68
+ /// ```rust
69
+ ///# use substrate_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
70
+ /// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
71
+ /// // of value `b"fuba"`.
72
+ /// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
73
+ /// ```
74
+ #[ cfg( not( feature = "full_crypto" ) ) ]
75
+ #[ macro_export]
76
+ macro_rules! app_crypto {
77
+ ( $module: ident, $key_type: expr) => {
78
+ $crate:: app_crypto_public_not_full_crypto!( $module:: Public , $key_type) ;
79
+ $crate:: app_crypto_public_common!( $module:: Public , $module:: Signature , $key_type) ;
80
+ $crate:: app_crypto_signature_not_full_crypto!( $module:: Signature , $key_type) ;
81
+ $crate:: app_crypto_signature_common!( $module:: Signature , $key_type) ;
82
+ } ;
83
+ }
84
+
85
+ /// Declares Pair type which is functionally equivalent to `$pair`, but is new
86
+ /// Application-specific type whose identifier is `$key_type`.
87
+ #[ macro_export]
88
+ macro_rules! app_crypto_pair {
89
+ ( $pair: ty, $key_type: expr) => {
64
90
$crate:: wrap!{
65
91
/// A generic `AppPublic` wrapper type over $pair crypto; this has no specific App.
66
92
#[ derive( Clone ) ]
@@ -71,16 +97,18 @@ macro_rules! app_crypto {
71
97
type Pair = Pair ;
72
98
}
73
99
74
- #[ cfg( feature = "std" ) ]
75
100
impl $crate:: Pair for Pair {
76
101
type Public = Public ;
77
102
type Seed = <$pair as $crate:: Pair >:: Seed ;
78
103
type Signature = Signature ;
79
104
type DeriveError = <$pair as $crate:: Pair >:: DeriveError ;
105
+
106
+ #[ cfg( feature = "std" ) ]
80
107
fn generate_with_phrase( password: Option <& str >) -> ( Self , String , Self :: Seed ) {
81
108
let r = <$pair>:: generate_with_phrase( password) ;
82
109
( Self ( r. 0 ) , r. 1 , r. 2 )
83
110
}
111
+ #[ cfg( feature = "std" ) ]
84
112
fn from_phrase( phrase: & str , password: Option <& str >)
85
113
-> Result <( Self , Self :: Seed ) , $crate:: SecretStringError >
86
114
{
@@ -115,18 +143,28 @@ macro_rules! app_crypto {
115
143
fn public( & self ) -> Self :: Public { Public ( self . 0 . public( ) ) }
116
144
fn to_raw_vec( & self ) -> Vec <u8 > { self . 0 . to_raw_vec( ) }
117
145
}
146
+
118
147
impl $crate:: AppKey for Pair {
119
148
type UntypedGeneric = $pair;
120
149
type Public = Public ;
121
150
type Pair = Pair ;
122
151
type Signature = Signature ;
123
152
const ID : $crate:: KeyTypeId = $key_type;
124
153
}
154
+
125
155
impl $crate:: AppPair for Pair {
126
156
type Generic = $pair;
127
157
}
128
158
} ;
129
- ( $public: ty, $sig: ty, $key_type: expr) => {
159
+ }
160
+
161
+ /// Declares Public type which is functionally equivalent to `$public`, but is new
162
+ /// Application-specific type whose identifier is `$key_type`.
163
+ /// can only be used together with `full_crypto` feature
164
+ /// For full functionality, app_crypto_public_common! must be called too.
165
+ #[ macro_export]
166
+ macro_rules! app_crypto_public_full_crypto {
167
+ ( $public: ty, $key_type: expr) => {
130
168
$crate:: wrap!{
131
169
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
132
170
#[ derive(
@@ -135,10 +173,59 @@ macro_rules! app_crypto {
135
173
$crate:: codec:: Decode ,
136
174
$crate:: RuntimeDebug ,
137
175
) ]
138
- #[ cfg_attr ( feature = "std" , derive( Hash ) ) ]
176
+ #[ derive( Hash ) ]
139
177
pub struct Public ( $public) ;
140
178
}
141
179
180
+ impl $crate:: CryptoType for Public {
181
+ type Pair = Pair ;
182
+ }
183
+
184
+ impl $crate:: AppKey for Public {
185
+ type UntypedGeneric = $public;
186
+ type Public = Public ;
187
+ type Pair = Pair ;
188
+ type Signature = Signature ;
189
+ const ID : $crate:: KeyTypeId = $key_type;
190
+ }
191
+ }
192
+ }
193
+
194
+ /// Declares Public type which is functionally equivalent to `$public`, but is new
195
+ /// Application-specific type whose identifier is `$key_type`.
196
+ /// can only be used without `full_crypto` feature
197
+ /// For full functionality, app_crypto_public_common! must be called too.
198
+ #[ macro_export]
199
+ macro_rules! app_crypto_public_not_full_crypto {
200
+ ( $public: ty, $key_type: expr) => {
201
+ $crate:: wrap!{
202
+ /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
203
+ #[ derive(
204
+ Clone , Default , Eq , PartialEq , Ord , PartialOrd ,
205
+ $crate:: codec:: Encode ,
206
+ $crate:: codec:: Decode ,
207
+ $crate:: RuntimeDebug ,
208
+ ) ]
209
+ pub struct Public ( $public) ;
210
+ }
211
+
212
+ impl $crate:: CryptoType for Public { }
213
+
214
+ impl $crate:: AppKey for Public {
215
+ type UntypedGeneric = $public;
216
+ type Public = Public ;
217
+ type Signature = Signature ;
218
+ const ID : $crate:: KeyTypeId = $key_type;
219
+ }
220
+ }
221
+ }
222
+
223
+ /// Declares Public type which is functionally equivalent to `$public`, but is new
224
+ /// Application-specific type whose identifier is `$key_type`.
225
+ /// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
226
+ #[ macro_export]
227
+ macro_rules! app_crypto_public_common {
228
+ ( $public: ty, $sig: ty, $key_type: expr) => {
142
229
impl $crate:: Derive for Public {
143
230
#[ cfg( feature = "std" ) ]
144
231
fn derive<Iter : Iterator <Item =$crate:: DeriveJunction >>( & self ,
@@ -183,24 +270,10 @@ macro_rules! app_crypto {
183
270
fn as_mut( & mut self ) -> & mut [ u8 ] { self . 0 . as_mut( ) }
184
271
}
185
272
186
- impl $crate:: CryptoType for Public {
187
- #[ cfg( feature="std" ) ]
188
- type Pair = Pair ;
189
- }
190
-
191
273
impl $crate:: Public for Public {
192
274
fn from_slice( x: & [ u8 ] ) -> Self { Self ( <$public>:: from_slice( x) ) }
193
275
}
194
276
195
- impl $crate:: AppKey for Public {
196
- type UntypedGeneric = $public;
197
- type Public = Public ;
198
- #[ cfg( feature="std" ) ]
199
- type Pair = Pair ;
200
- type Signature = Signature ;
201
- const ID : $crate:: KeyTypeId = $key_type;
202
- }
203
-
204
277
impl $crate:: AppPublic for Public {
205
278
type Generic = $public;
206
279
}
@@ -229,41 +302,84 @@ macro_rules! app_crypto {
229
302
<$public as $crate:: RuntimePublic >:: verify( self . as_ref( ) , msg, & signature. as_ref( ) )
230
303
}
231
304
}
305
+ }
306
+ }
232
307
308
+ /// Declares Signature type which is functionally equivalent to `$sig`, but is new
309
+ /// Application-specific type whose identifier is `$key_type`.
310
+ /// can only be used together with `full_crypto` feature
311
+ /// For full functionality, app_crypto_public_common! must be called too.
312
+ #[ macro_export]
313
+ macro_rules! app_crypto_signature_full_crypto {
314
+ ( $sig: ty, $key_type: expr) => {
233
315
$crate:: wrap! {
234
316
/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
235
317
#[ derive( Clone , Default , Eq , PartialEq ,
236
318
$crate:: codec:: Encode ,
237
319
$crate:: codec:: Decode ,
238
320
$crate:: RuntimeDebug ,
239
321
) ]
240
- #[ cfg_attr ( feature = "std" , derive( Hash ) ) ]
322
+ #[ derive( Hash ) ]
241
323
pub struct Signature ( $sig) ;
242
324
}
243
325
244
- impl $crate:: Deref for Signature {
245
- type Target = [ u8 ] ;
246
-
247
- fn deref( & self ) -> & Self :: Target { self . 0 . as_ref( ) }
326
+ impl $crate:: CryptoType for Signature {
327
+ type Pair = Pair ;
248
328
}
249
329
250
- impl AsRef <[ u8 ] > for Signature {
251
- fn as_ref( & self ) -> & [ u8 ] { self . 0 . as_ref( ) }
330
+ impl $crate:: AppKey for Signature {
331
+ type UntypedGeneric = $sig;
332
+ type Public = Public ;
333
+ type Pair = Pair ;
334
+ type Signature = Signature ;
335
+ const ID : $crate:: KeyTypeId = $key_type;
252
336
}
337
+ }
338
+ }
253
339
254
- impl $crate:: CryptoType for Signature {
255
- #[ cfg( feature="std" ) ]
256
- type Pair = Pair ;
340
+ /// Declares Signature type which is functionally equivalent to `$sig`, but is new
341
+ /// Application-specific type whose identifier is `$key_type`.
342
+ /// can only be used without `full_crypto` feature
343
+ /// For full functionality, app_crypto_public_common! must be called too.
344
+ #[ macro_export]
345
+ macro_rules! app_crypto_signature_not_full_crypto {
346
+ ( $sig: ty, $key_type: expr) => {
347
+ $crate:: wrap! {
348
+ /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
349
+ #[ derive( Clone , Default , Eq , PartialEq ,
350
+ $crate:: codec:: Encode ,
351
+ $crate:: codec:: Decode ,
352
+ $crate:: RuntimeDebug ,
353
+ ) ]
354
+ pub struct Signature ( $sig) ;
257
355
}
356
+
357
+ impl $crate:: CryptoType for Signature { }
258
358
259
359
impl $crate:: AppKey for Signature {
260
360
type UntypedGeneric = $sig;
261
361
type Public = Public ;
262
- #[ cfg( feature="std" ) ]
263
- type Pair = Pair ;
264
362
type Signature = Signature ;
265
363
const ID : $crate:: KeyTypeId = $key_type;
266
364
}
365
+ }
366
+ }
367
+
368
+ /// Declares Signature type which is functionally equivalent to `$sig`, but is new
369
+ /// Application-specific type whose identifier is `$key_type`.
370
+ /// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
371
+ #[ macro_export]
372
+ macro_rules! app_crypto_signature_common {
373
+ ( $sig: ty, $key_type: expr) => {
374
+ impl $crate:: Deref for Signature {
375
+ type Target = [ u8 ] ;
376
+
377
+ fn deref( & self ) -> & Self :: Target { self . 0 . as_ref( ) }
378
+ }
379
+
380
+ impl AsRef <[ u8 ] > for Signature {
381
+ fn as_ref( & self ) -> & [ u8 ] { self . 0 . as_ref( ) }
382
+ }
267
383
268
384
impl $crate:: AppSignature for Signature {
269
385
type Generic = $sig;
0 commit comments