1#![warn(missing_docs)]
21#![cfg_attr(not(feature = "std"), no_std)]
22
23extern crate alloc;
24
25pub use sp_core::crypto::{key_types, CryptoTypeId, DeriveJunction, KeyTypeId, Ss58Codec};
26#[doc(hidden)]
27pub use sp_core::crypto::{DeriveError, Pair, SecretStringError};
28#[doc(hidden)]
29pub use sp_core::{
30 self,
31 crypto::{ByteArray, CryptoType, Derive, IsWrappedBy, Public, Signature, UncheckedFrom, Wraps},
32 proof_of_possession::{ProofOfPossessionGenerator, ProofOfPossessionVerifier},
33};
34
35#[doc(hidden)]
36pub use alloc::vec::Vec;
37#[doc(hidden)]
38pub use codec;
39#[doc(hidden)]
40pub use core::ops::Deref;
41#[doc(hidden)]
42pub use scale_info;
43#[doc(hidden)]
44#[cfg(feature = "serde")]
45pub use serde;
46
47#[cfg(feature = "bandersnatch-experimental")]
48pub mod bandersnatch;
49#[cfg(feature = "bls-experimental")]
50pub mod bls381;
51pub mod ecdsa;
52#[cfg(feature = "bls-experimental")]
53pub mod ecdsa_bls381;
54pub mod ed25519;
55pub mod sr25519;
56mod traits;
57
58pub use traits::*;
59
60#[cfg(feature = "full_crypto")]
71#[macro_export]
72macro_rules! app_crypto {
73 ($module:ident, $key_type:expr) => {
74 $crate::app_crypto_public_full_crypto!($module::Public, $key_type, $module::CRYPTO_ID);
75 $crate::app_crypto_public_common!(
76 $module::Public,
77 $module::Signature,
78 $key_type,
79 $module::CRYPTO_ID
80 );
81 $crate::app_crypto_signature_full_crypto!(
82 $module::Signature,
83 $key_type,
84 $module::CRYPTO_ID
85 );
86 $crate::app_crypto_signature_common!($module::Signature, $key_type);
87 $crate::app_crypto_proof_of_possession_full_crypto!(
88 $module::ProofOfPossession,
89 $key_type,
90 $module::CRYPTO_ID
91 );
92 $crate::app_crypto_proof_of_possession_common!($module::ProofOfPossession, $key_type);
93 $crate::app_crypto_pair_common!($module::Pair, $key_type, $module::CRYPTO_ID);
94 };
95}
96
97#[cfg(not(feature = "full_crypto"))]
108#[macro_export]
109macro_rules! app_crypto {
110 ($module:ident, $key_type:expr) => {
111 $crate::app_crypto_public_not_full_crypto!($module::Public, $key_type, $module::CRYPTO_ID);
112 $crate::app_crypto_public_common!(
113 $module::Public,
114 $module::Signature,
115 $key_type,
116 $module::CRYPTO_ID
117 );
118 $crate::app_crypto_signature_not_full_crypto!(
119 $module::Signature,
120 $key_type,
121 $module::CRYPTO_ID
122 );
123 $crate::app_crypto_signature_common!($module::Signature, $key_type);
124 $crate::app_crypto_proof_of_possession_not_full_crypto!(
125 $module::ProofOfPossession,
126 $key_type,
127 $module::CRYPTO_ID
128 );
129 $crate::app_crypto_proof_of_possession_common!($module::ProofOfPossession, $key_type);
130 $crate::app_crypto_pair_common!($module::Pair, $key_type, $module::CRYPTO_ID);
131 };
132}
133
134#[macro_export]
138macro_rules! app_crypto_pair_common {
139 ($pair:ty, $key_type:expr, $crypto_type:expr) => {
140 $crate::wrap! {
141 #[derive(Clone)]
143 pub struct Pair($pair);
144 }
145
146 impl $crate::CryptoType for Pair {
147 type Pair = Pair;
148 }
149
150 impl $crate::Pair for Pair {
151 type Public = Public;
152 type Seed = <$pair as $crate::Pair>::Seed;
153 type Signature = Signature;
154 type ProofOfPossession = <$pair as $crate::Pair>::ProofOfPossession;
155
156 $crate::app_crypto_pair_functions_if_std!($pair);
157 $crate::app_crypto_pair_functions_if_full_crypto!($pair);
158
159 fn from_phrase(
160 phrase: &str,
161 password: Option<&str>,
162 ) -> Result<(Self, Self::Seed), $crate::SecretStringError> {
163 <$pair>::from_phrase(phrase, password).map(|r| (Self(r.0), r.1))
164 }
165 fn derive<Iter: Iterator<Item = $crate::DeriveJunction>>(
166 &self,
167 path: Iter,
168 seed: Option<Self::Seed>,
169 ) -> Result<(Self, Option<Self::Seed>), $crate::DeriveError> {
170 self.0.derive(path, seed).map(|x| (Self(x.0), x.1))
171 }
172 fn from_seed(seed: &Self::Seed) -> Self {
173 Self(<$pair>::from_seed(seed))
174 }
175 fn from_seed_slice(seed: &[u8]) -> Result<Self, $crate::SecretStringError> {
176 <$pair>::from_seed_slice(seed).map(Self)
177 }
178 fn verify<M: AsRef<[u8]>>(
179 sig: &Self::Signature,
180 message: M,
181 pubkey: &Self::Public,
182 ) -> bool {
183 <$pair>::verify(&sig.0, message, pubkey.as_ref())
184 }
185 fn public(&self) -> Self::Public {
186 Public(self.0.public())
187 }
188 fn to_raw_vec(&self) -> $crate::Vec<u8> {
189 self.0.to_raw_vec()
190 }
191 }
192
193 impl $crate::ProofOfPossessionVerifier for Pair {
194 fn verify_proof_of_possession(
195 owner: &[u8],
196 proof_of_possession: &Self::ProofOfPossession,
197 allegedly_possessed_pubkey: &Self::Public,
198 ) -> bool {
199 <$pair>::verify_proof_of_possession(
200 owner,
201 &proof_of_possession,
202 allegedly_possessed_pubkey.as_ref(),
203 )
204 }
205 }
206
207 impl $crate::AppCrypto for Pair {
208 type Public = Public;
209 type Pair = Pair;
210 type Signature = Signature;
211 type ProofOfPossession = ProofOfPossession;
212 const ID: $crate::KeyTypeId = $key_type;
213 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
214 }
215
216 impl $crate::AppPair for Pair {
217 type Generic = $pair;
218 }
219
220 impl Pair {
221 pub fn into_inner(self) -> $pair {
223 self.0
224 }
225 }
226 };
227}
228
229#[doc(hidden)]
231#[cfg(feature = "std")]
232#[macro_export]
233macro_rules! app_crypto_pair_functions_if_std {
234 ($pair:ty) => {
235 fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) {
236 let r = <$pair>::generate_with_phrase(password);
237 (Self(r.0), r.1, r.2)
238 }
239 };
240}
241
242#[doc(hidden)]
243#[cfg(not(feature = "std"))]
244#[macro_export]
245macro_rules! app_crypto_pair_functions_if_std {
246 ($pair:ty) => {};
247}
248
249#[doc(hidden)]
251#[cfg(feature = "full_crypto")]
252#[macro_export]
253macro_rules! app_crypto_pair_functions_if_full_crypto {
254 ($pair:ty) => {
255 fn sign(&self, msg: &[u8]) -> Self::Signature {
256 Signature(self.0.sign(msg))
257 }
258 };
259}
260
261#[doc(hidden)]
262#[cfg(not(feature = "full_crypto"))]
263#[macro_export]
264macro_rules! app_crypto_pair_functions_if_full_crypto {
265 ($pair:ty) => {};
266}
267
268#[doc(hidden)]
273#[macro_export]
274macro_rules! app_crypto_public_full_crypto {
275 ($public:ty, $key_type:expr, $crypto_type:expr) => {
276 $crate::wrap! {
277 #[derive(
279 Clone, Eq, Hash, PartialEq, PartialOrd, Ord,
280 $crate::codec::Encode,
281 $crate::codec::Decode,
282 $crate::codec::DecodeWithMemTracking,
283 Debug,
284 $crate::codec::MaxEncodedLen,
285 $crate::scale_info::TypeInfo,
286 )]
287 #[codec(crate = $crate::codec)]
288 pub struct Public($public);
289 }
290
291 impl $crate::CryptoType for Public {
292 type Pair = Pair;
293 }
294
295 impl $crate::AppCrypto for Public {
296 type Public = Public;
297 type Pair = Pair;
298 type Signature = Signature;
299 type ProofOfPossession = ProofOfPossession;
300 const ID: $crate::KeyTypeId = $key_type;
301 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
302 }
303 };
304}
305
306#[doc(hidden)]
311#[macro_export]
312macro_rules! app_crypto_public_not_full_crypto {
313 ($public:ty, $key_type:expr, $crypto_type:expr) => {
314 $crate::wrap! {
315 #[derive(
317 Clone, Eq, Hash, PartialEq, Ord, PartialOrd,
318 $crate::codec::Encode,
319 $crate::codec::Decode,
320 $crate::codec::DecodeWithMemTracking,
321 Debug,
322 $crate::codec::MaxEncodedLen,
323 $crate::scale_info::TypeInfo,
324 )]
325 pub struct Public($public);
326 }
327
328 impl $crate::CryptoType for Public {
329 type Pair = Pair;
330 }
331
332 impl $crate::AppCrypto for Public {
333 type Public = Public;
334 type Pair = Pair;
335 type Signature = Signature;
336 type ProofOfPossession = ProofOfPossession;
337
338 const ID: $crate::KeyTypeId = $key_type;
339 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
340 }
341 };
342}
343
344#[doc(hidden)]
348#[macro_export]
349macro_rules! app_crypto_public_common {
350 ($public:ty, $sig:ty, $key_type:expr, $crypto_type:expr) => {
351 $crate::app_crypto_public_common_if_serde!();
352
353 impl AsRef<[u8]> for Public {
354 fn as_ref(&self) -> &[u8] {
355 self.0.as_ref()
356 }
357 }
358
359 impl AsMut<[u8]> for Public {
360 fn as_mut(&mut self) -> &mut [u8] {
361 self.0.as_mut()
362 }
363 }
364
365 impl $crate::ByteArray for Public {
366 const LEN: usize = <$public>::LEN;
367 }
368
369 impl $crate::Public for Public {}
370
371 impl $crate::AppPublic for Public {
372 type Generic = $public;
373 }
374
375 impl<'a> TryFrom<&'a [u8]> for Public {
376 type Error = ();
377
378 fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
379 <$public>::try_from(data).map(Into::into)
380 }
381 }
382
383 impl Public {
384 pub fn into_inner(self) -> $public {
386 self.0
387 }
388 }
389 };
390}
391
392#[doc(hidden)]
393pub mod module_format_string_prelude {
394 #[cfg(all(not(feature = "std"), feature = "serde"))]
395 pub use alloc::{format, string::String};
396 #[cfg(feature = "std")]
397 pub use std::{format, string::String};
398}
399
400#[cfg(feature = "serde")]
402#[doc(hidden)]
403#[macro_export]
404macro_rules! app_crypto_public_common_if_serde {
405 () => {
406 impl $crate::Derive for Public {
407 fn derive<Iter: Iterator<Item = $crate::DeriveJunction>>(
408 &self,
409 path: Iter,
410 ) -> Option<Self> {
411 self.0.derive(path).map(Self)
412 }
413 }
414
415 impl core::fmt::Display for Public {
416 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
417 use $crate::Ss58Codec;
418 write!(f, "{}", self.0.to_ss58check())
419 }
420 }
421
422 impl $crate::serde::Serialize for Public {
423 fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
424 where
425 S: $crate::serde::Serializer,
426 {
427 use $crate::Ss58Codec;
428 serializer.serialize_str(&self.to_ss58check())
429 }
430 }
431
432 impl<'de> $crate::serde::Deserialize<'de> for Public {
433 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
434 where
435 D: $crate::serde::Deserializer<'de>,
436 {
437 use $crate::{module_format_string_prelude::*, Ss58Codec};
438
439 Public::from_ss58check(&String::deserialize(deserializer)?)
440 .map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e)))
441 }
442 }
443 };
444}
445
446#[cfg(not(feature = "serde"))]
447#[doc(hidden)]
448#[macro_export]
449macro_rules! app_crypto_public_common_if_serde {
450 () => {
451 impl $crate::Derive for Public {}
452 };
453}
454
455#[doc(hidden)]
460#[macro_export]
461macro_rules! app_crypto_signature_full_crypto {
462 ($sig:ty, $key_type:expr, $crypto_type:expr) => {
463 $crate::wrap! {
464 #[derive(Clone, Eq, PartialEq,
466 $crate::codec::Encode,
467 $crate::codec::Decode,
468 $crate::codec::DecodeWithMemTracking,
469 Debug,
470 $crate::scale_info::TypeInfo,
471 )]
472 #[derive(Hash)]
473 pub struct Signature($sig);
474 }
475
476 impl $crate::CryptoType for Signature {
477 type Pair = Pair;
478 }
479
480 impl $crate::AppCrypto for Signature {
481 type Public = Public;
482 type Pair = Pair;
483 type Signature = Signature;
484 type ProofOfPossession = ProofOfPossession;
485 const ID: $crate::KeyTypeId = $key_type;
486 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
487 }
488 };
489}
490
491#[doc(hidden)]
496#[macro_export]
497macro_rules! app_crypto_signature_not_full_crypto {
498 ($sig:ty, $key_type:expr, $crypto_type:expr) => {
499 $crate::wrap! {
500 #[derive(Clone, Eq, PartialEq,
502 $crate::codec::Encode,
503 $crate::codec::Decode,
504 $crate::codec::DecodeWithMemTracking,
505 Debug,
506 $crate::scale_info::TypeInfo,
507 )]
508 pub struct Signature($sig);
509 }
510
511 impl $crate::CryptoType for Signature {
512 type Pair = Pair;
513 }
514
515 impl $crate::AppCrypto for Signature {
516 type Public = Public;
517 type Pair = Pair;
518 type Signature = Signature;
519 type ProofOfPossession = ProofOfPossession;
520 const ID: $crate::KeyTypeId = $key_type;
521 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
522 }
523 };
524}
525
526#[doc(hidden)]
530#[macro_export]
531macro_rules! app_crypto_signature_common {
532 ($sig:ty, $key_type:expr) => {
533 impl $crate::Deref for Signature {
534 type Target = [u8];
535
536 fn deref(&self) -> &Self::Target {
537 self.0.as_ref()
538 }
539 }
540
541 impl AsRef<[u8]> for Signature {
542 fn as_ref(&self) -> &[u8] {
543 self.0.as_ref()
544 }
545 }
546
547 impl AsMut<[u8]> for Signature {
548 fn as_mut(&mut self) -> &mut [u8] {
549 self.0.as_mut()
550 }
551 }
552
553 impl $crate::AppSignature for Signature {
554 type Generic = $sig;
555 }
556
557 impl<'a> TryFrom<&'a [u8]> for Signature {
558 type Error = ();
559
560 fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
561 <$sig>::try_from(data).map(Into::into)
562 }
563 }
564
565 impl TryFrom<$crate::Vec<u8>> for Signature {
566 type Error = ();
567
568 fn try_from(data: $crate::Vec<u8>) -> Result<Self, Self::Error> {
569 Self::try_from(&data[..])
570 }
571 }
572
573 impl $crate::Signature for Signature {}
574
575 impl $crate::ByteArray for Signature {
576 const LEN: usize = <$sig>::LEN;
577 }
578
579 impl Signature {
580 pub fn into_inner(self) -> $sig {
582 self.0
583 }
584 }
585 };
586}
587
588#[doc(hidden)]
593#[macro_export]
594macro_rules! app_crypto_proof_of_possession_full_crypto {
595 ($sig:ty, $key_type:expr, $crypto_type:expr) => {
596 $crate::wrap! {
597 #[derive(Clone, Eq, PartialEq, Hash,
599 $crate::codec::Encode,
600 $crate::codec::Decode,
601 $crate::codec::DecodeWithMemTracking,
602 Debug,
603 $crate::scale_info::TypeInfo,
604 )]
605 pub struct ProofOfPossession($sig);
606 }
607
608 impl $crate::CryptoType for ProofOfPossession {
609 type Pair = Pair;
610 }
611
612 impl $crate::AppCrypto for ProofOfPossession {
613 type Public = Public;
614 type Pair = Pair;
615 type Signature = Signature;
616 type ProofOfPossession = ProofOfPossession;
617 const ID: $crate::KeyTypeId = $key_type;
618 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
619 }
620 };
621}
622
623#[doc(hidden)]
628#[macro_export]
629macro_rules! app_crypto_proof_of_possession_not_full_crypto {
630 ($sig:ty, $key_type:expr, $crypto_type:expr) => {
631 $crate::wrap! {
632 #[derive(Clone, Eq, PartialEq,
634 $crate::codec::Encode,
635 $crate::codec::Decode,
636 $crate::codec::DecodeWithMemTracking,
637 Debug,
638 $crate::scale_info::TypeInfo,
639 )]
640 pub struct ProofOfPossession($sig);
641 }
642
643 impl $crate::CryptoType for ProofOfPossession {
644 type Pair = Pair;
645 }
646
647 impl $crate::AppCrypto for ProofOfPossession {
648 type Public = Public;
649 type Pair = Pair;
650 type Signature = Signature;
651 type ProofOfPossession = ProofOfPossession;
652 const ID: $crate::KeyTypeId = $key_type;
653 const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
654 }
655 };
656}
657
658#[doc(hidden)]
662#[macro_export]
663macro_rules! app_crypto_proof_of_possession_common {
664 ($sig:ty, $key_type:expr) => {
665 impl $crate::Deref for ProofOfPossession {
666 type Target = [u8];
667
668 fn deref(&self) -> &Self::Target {
669 self.0.as_ref()
670 }
671 }
672
673 impl AsRef<[u8]> for ProofOfPossession {
674 fn as_ref(&self) -> &[u8] {
675 self.0.as_ref()
676 }
677 }
678
679 impl AsMut<[u8]> for ProofOfPossession {
680 fn as_mut(&mut self) -> &mut [u8] {
681 self.0.as_mut()
682 }
683 }
684
685 impl $crate::AppSignature for ProofOfPossession {
686 type Generic = $sig;
687 }
688
689 impl<'a> TryFrom<&'a [u8]> for ProofOfPossession {
690 type Error = ();
691
692 fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
693 <$sig>::try_from(data).map(Into::into)
694 }
695 }
696
697 impl TryFrom<$crate::Vec<u8>> for ProofOfPossession {
698 type Error = ();
699
700 fn try_from(data: $crate::Vec<u8>) -> Result<Self, Self::Error> {
701 Self::try_from(&data[..])
702 }
703 }
704
705 impl $crate::Signature for ProofOfPossession {}
706
707 impl $crate::ByteArray for ProofOfPossession {
708 const LEN: usize = <$sig>::LEN;
709 }
710
711 impl ProofOfPossession {
712 pub fn into_inner(self) -> $sig {
714 self.0
715 }
716 }
717 };
718}
719
720#[macro_export]
728macro_rules! wrap {
729 ($( #[ $attr:meta ] )* struct $outer:ident($inner:ty);) => {
730 $( #[ $attr ] )*
731 struct $outer( $inner );
732 $crate::wrap!($inner, $outer);
733 };
734 ($( #[ $attr:meta ] )* pub struct $outer:ident($inner:ty);) => {
735 $( #[ $attr ] )*
736 pub struct $outer( $inner );
737 $crate::wrap!($inner, $outer);
738 };
739 ($inner:ty, $outer:ty) => {
740 impl $crate::Wraps for $outer {
741 type Inner = $inner;
742 }
743 impl From<$inner> for $outer {
744 fn from(inner: $inner) -> Self {
745 Self(inner)
746 }
747 }
748 impl From<$outer> for $inner {
749 fn from(outer: $outer) -> Self {
750 outer.0
751 }
752 }
753 impl AsRef<$inner> for $outer {
754 fn as_ref(&self) -> &$inner {
755 &self.0
756 }
757 }
758 impl AsMut<$inner> for $outer {
759 fn as_mut(&mut self) -> &mut $inner {
760 &mut self.0
761 }
762 }
763 }
764}
765
766#[macro_export]
778#[cfg(any(feature = "std", feature = "full_crypto"))]
779macro_rules! with_pair {
780 ( $( $def:tt )* ) => {
781 $( $def )*
782 }
783}
784
785#[doc(hidden)]
786#[macro_export]
787#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
788macro_rules! with_pair {
789 ( $( $def:tt )* ) => {};
790}