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