1#![cfg_attr(not(feature = "std"), no_std)]
21
22extern crate alloc;
23
24#[cfg(feature = "std")]
25pub mod testing;
26
27#[cfg(feature = "bandersnatch-experimental")]
28use sp_core::bandersnatch;
29#[cfg(feature = "bls-experimental")]
30use sp_core::{bls381, ecdsa_bls381};
31use sp_core::{
32 crypto::{ByteArray, CryptoTypeId, KeyTypeId},
33 ecdsa, ed25519, sr25519,
34};
35
36use alloc::{string::String, sync::Arc, vec::Vec};
37
38#[derive(Debug)]
40pub enum Error {
41 KeyNotSupported(KeyTypeId),
43 ValidationError(String),
45 Unavailable,
47 Other(String),
49}
50
51impl core::fmt::Display for Error {
52 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
53 match self {
54 Error::KeyNotSupported(key_type) => write!(fmt, "Key not supported: {key_type:?}"),
55 Error::ValidationError(error) => write!(fmt, "Validation error: {error}"),
56 Error::Unavailable => fmt.write_str("Keystore unavailable"),
57 Error::Other(error) => write!(fmt, "An unknown keystore error occurred: {error}"),
58 }
59 }
60}
61
62#[cfg(feature = "std")]
63impl std::error::Error for Error {}
64
65pub trait Keystore: Send + Sync {
67 fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public>;
69
70 fn sr25519_generate_new(
75 &self,
76 key_type: KeyTypeId,
77 seed: Option<&str>,
78 ) -> Result<sr25519::Public, Error>;
79
80 fn sr25519_sign(
89 &self,
90 key_type: KeyTypeId,
91 public: &sr25519::Public,
92 msg: &[u8],
93 ) -> Result<Option<sr25519::Signature>, Error>;
94
95 fn sr25519_vrf_sign(
103 &self,
104 key_type: KeyTypeId,
105 public: &sr25519::Public,
106 data: &sr25519::vrf::VrfSignData,
107 ) -> Result<Option<sr25519::vrf::VrfSignature>, Error>;
108
109 fn sr25519_vrf_pre_output(
117 &self,
118 key_type: KeyTypeId,
119 public: &sr25519::Public,
120 input: &sr25519::vrf::VrfInput,
121 ) -> Result<Option<sr25519::vrf::VrfPreOutput>, Error>;
122
123 fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public>;
125
126 fn ed25519_generate_new(
131 &self,
132 key_type: KeyTypeId,
133 seed: Option<&str>,
134 ) -> Result<ed25519::Public, Error>;
135
136 fn ed25519_sign(
145 &self,
146 key_type: KeyTypeId,
147 public: &ed25519::Public,
148 msg: &[u8],
149 ) -> Result<Option<ed25519::Signature>, Error>;
150
151 fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public>;
153
154 fn ecdsa_generate_new(
159 &self,
160 key_type: KeyTypeId,
161 seed: Option<&str>,
162 ) -> Result<ecdsa::Public, Error>;
163
164 fn ecdsa_sign(
173 &self,
174 key_type: KeyTypeId,
175 public: &ecdsa::Public,
176 msg: &[u8],
177 ) -> Result<Option<ecdsa::Signature>, Error>;
178
179 fn ecdsa_sign_prehashed(
188 &self,
189 key_type: KeyTypeId,
190 public: &ecdsa::Public,
191 msg: &[u8; 32],
192 ) -> Result<Option<ecdsa::Signature>, Error>;
193
194 #[cfg(feature = "bandersnatch-experimental")]
196 fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public>;
197
198 #[cfg(feature = "bandersnatch-experimental")]
203 fn bandersnatch_generate_new(
204 &self,
205 key_type: KeyTypeId,
206 seed: Option<&str>,
207 ) -> Result<bandersnatch::Public, Error>;
208
209 #[cfg(feature = "bandersnatch-experimental")]
218 fn bandersnatch_sign(
219 &self,
220 key_type: KeyTypeId,
221 public: &bandersnatch::Public,
222 msg: &[u8],
223 ) -> Result<Option<bandersnatch::Signature>, Error>;
224
225 #[cfg(feature = "bandersnatch-experimental")]
233 fn bandersnatch_vrf_sign(
234 &self,
235 key_type: KeyTypeId,
236 public: &bandersnatch::Public,
237 input: &bandersnatch::vrf::VrfSignData,
238 ) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error>;
239
240 #[cfg(feature = "bandersnatch-experimental")]
248 fn bandersnatch_vrf_pre_output(
249 &self,
250 key_type: KeyTypeId,
251 public: &bandersnatch::Public,
252 input: &bandersnatch::vrf::VrfInput,
253 ) -> Result<Option<bandersnatch::vrf::VrfPreOutput>, Error>;
254
255 #[cfg(feature = "bandersnatch-experimental")]
271 fn bandersnatch_ring_vrf_sign(
272 &self,
273 key_type: KeyTypeId,
274 public: &bandersnatch::Public,
275 input: &bandersnatch::vrf::VrfSignData,
276 prover: &bandersnatch::ring_vrf::RingProver,
277 ) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error>;
278
279 #[cfg(feature = "bls-experimental")]
281 fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public>;
282
283 #[cfg(feature = "bls-experimental")]
285 fn ecdsa_bls381_public_keys(&self, id: KeyTypeId) -> Vec<ecdsa_bls381::Public>;
286
287 #[cfg(feature = "bls-experimental")]
292 fn bls381_generate_new(
293 &self,
294 key_type: KeyTypeId,
295 seed: Option<&str>,
296 ) -> Result<bls381::Public, Error>;
297
298 #[cfg(feature = "bls-experimental")]
303 fn ecdsa_bls381_generate_new(
304 &self,
305 key_type: KeyTypeId,
306 seed: Option<&str>,
307 ) -> Result<ecdsa_bls381::Public, Error>;
308
309 #[cfg(feature = "bls-experimental")]
318 fn bls381_sign(
319 &self,
320 key_type: KeyTypeId,
321 public: &bls381::Public,
322 msg: &[u8],
323 ) -> Result<Option<bls381::Signature>, Error>;
324
325 #[cfg(feature = "bls-experimental")]
334 fn bls381_generate_proof_of_possession(
335 &self,
336 key_type: KeyTypeId,
337 public: &bls381::Public,
338 ) -> Result<Option<bls381::Signature>, Error>;
339
340 #[cfg(feature = "bls-experimental")]
349 fn ecdsa_bls381_sign(
350 &self,
351 key_type: KeyTypeId,
352 public: &ecdsa_bls381::Public,
353 msg: &[u8],
354 ) -> Result<Option<ecdsa_bls381::Signature>, Error>;
355
356 #[cfg(feature = "bls-experimental")]
367 fn ecdsa_bls381_sign_with_keccak256(
368 &self,
369 key_type: KeyTypeId,
370 public: &ecdsa_bls381::Public,
371 msg: &[u8],
372 ) -> Result<Option<ecdsa_bls381::Signature>, Error>;
373
374 fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()>;
376
377 fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error>;
381
382 fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
386
387 fn sign_with(
405 &self,
406 id: KeyTypeId,
407 crypto_id: CryptoTypeId,
408 public: &[u8],
409 msg: &[u8],
410 ) -> Result<Option<Vec<u8>>, Error> {
411 use codec::Encode;
412
413 let signature = match crypto_id {
414 sr25519::CRYPTO_ID => {
415 let public = sr25519::Public::from_slice(public)
416 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
417 self.sr25519_sign(id, &public, msg)?.map(|s| s.encode())
418 },
419 ed25519::CRYPTO_ID => {
420 let public = ed25519::Public::from_slice(public)
421 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
422 self.ed25519_sign(id, &public, msg)?.map(|s| s.encode())
423 },
424 ecdsa::CRYPTO_ID => {
425 let public = ecdsa::Public::from_slice(public)
426 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
427
428 self.ecdsa_sign(id, &public, msg)?.map(|s| s.encode())
429 },
430 #[cfg(feature = "bandersnatch-experimental")]
431 bandersnatch::CRYPTO_ID => {
432 let public = bandersnatch::Public::from_slice(public)
433 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
434 self.bandersnatch_sign(id, &public, msg)?.map(|s| s.encode())
435 },
436 #[cfg(feature = "bls-experimental")]
437 bls381::CRYPTO_ID => {
438 let public = bls381::Public::from_slice(public)
439 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
440 self.bls381_sign(id, &public, msg)?.map(|s| s.encode())
441 },
442 #[cfg(feature = "bls-experimental")]
443 ecdsa_bls381::CRYPTO_ID => {
444 let public = ecdsa_bls381::Public::from_slice(public)
445 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
446 self.ecdsa_bls381_sign(id, &public, msg)?.map(|s| s.encode())
447 },
448 _ => return Err(Error::KeyNotSupported(id)),
449 };
450 Ok(signature)
451 }
452}
453
454impl<T: Keystore + ?Sized> Keystore for Arc<T> {
455 fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public> {
456 (**self).sr25519_public_keys(key_type)
457 }
458
459 fn sr25519_generate_new(
460 &self,
461 key_type: KeyTypeId,
462 seed: Option<&str>,
463 ) -> Result<sr25519::Public, Error> {
464 (**self).sr25519_generate_new(key_type, seed)
465 }
466
467 fn sr25519_sign(
468 &self,
469 key_type: KeyTypeId,
470 public: &sr25519::Public,
471 msg: &[u8],
472 ) -> Result<Option<sr25519::Signature>, Error> {
473 (**self).sr25519_sign(key_type, public, msg)
474 }
475
476 fn sr25519_vrf_sign(
477 &self,
478 key_type: KeyTypeId,
479 public: &sr25519::Public,
480 data: &sr25519::vrf::VrfSignData,
481 ) -> Result<Option<sr25519::vrf::VrfSignature>, Error> {
482 (**self).sr25519_vrf_sign(key_type, public, data)
483 }
484
485 fn sr25519_vrf_pre_output(
486 &self,
487 key_type: KeyTypeId,
488 public: &sr25519::Public,
489 input: &sr25519::vrf::VrfInput,
490 ) -> Result<Option<sr25519::vrf::VrfPreOutput>, Error> {
491 (**self).sr25519_vrf_pre_output(key_type, public, input)
492 }
493
494 fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
495 (**self).ed25519_public_keys(key_type)
496 }
497
498 fn ed25519_generate_new(
499 &self,
500 key_type: KeyTypeId,
501 seed: Option<&str>,
502 ) -> Result<ed25519::Public, Error> {
503 (**self).ed25519_generate_new(key_type, seed)
504 }
505
506 fn ed25519_sign(
507 &self,
508 key_type: KeyTypeId,
509 public: &ed25519::Public,
510 msg: &[u8],
511 ) -> Result<Option<ed25519::Signature>, Error> {
512 (**self).ed25519_sign(key_type, public, msg)
513 }
514
515 fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public> {
516 (**self).ecdsa_public_keys(key_type)
517 }
518
519 fn ecdsa_generate_new(
520 &self,
521 key_type: KeyTypeId,
522 seed: Option<&str>,
523 ) -> Result<ecdsa::Public, Error> {
524 (**self).ecdsa_generate_new(key_type, seed)
525 }
526
527 fn ecdsa_sign(
528 &self,
529 key_type: KeyTypeId,
530 public: &ecdsa::Public,
531 msg: &[u8],
532 ) -> Result<Option<ecdsa::Signature>, Error> {
533 (**self).ecdsa_sign(key_type, public, msg)
534 }
535
536 fn ecdsa_sign_prehashed(
537 &self,
538 key_type: KeyTypeId,
539 public: &ecdsa::Public,
540 msg: &[u8; 32],
541 ) -> Result<Option<ecdsa::Signature>, Error> {
542 (**self).ecdsa_sign_prehashed(key_type, public, msg)
543 }
544
545 #[cfg(feature = "bandersnatch-experimental")]
546 fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public> {
547 (**self).bandersnatch_public_keys(key_type)
548 }
549
550 #[cfg(feature = "bandersnatch-experimental")]
551 fn bandersnatch_generate_new(
552 &self,
553 key_type: KeyTypeId,
554 seed: Option<&str>,
555 ) -> Result<bandersnatch::Public, Error> {
556 (**self).bandersnatch_generate_new(key_type, seed)
557 }
558
559 #[cfg(feature = "bandersnatch-experimental")]
560 fn bandersnatch_sign(
561 &self,
562 key_type: KeyTypeId,
563 public: &bandersnatch::Public,
564 msg: &[u8],
565 ) -> Result<Option<bandersnatch::Signature>, Error> {
566 (**self).bandersnatch_sign(key_type, public, msg)
567 }
568
569 #[cfg(feature = "bandersnatch-experimental")]
570 fn bandersnatch_vrf_sign(
571 &self,
572 key_type: KeyTypeId,
573 public: &bandersnatch::Public,
574 input: &bandersnatch::vrf::VrfSignData,
575 ) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error> {
576 (**self).bandersnatch_vrf_sign(key_type, public, input)
577 }
578
579 #[cfg(feature = "bandersnatch-experimental")]
580 fn bandersnatch_vrf_pre_output(
581 &self,
582 key_type: KeyTypeId,
583 public: &bandersnatch::Public,
584 input: &bandersnatch::vrf::VrfInput,
585 ) -> Result<Option<bandersnatch::vrf::VrfPreOutput>, Error> {
586 (**self).bandersnatch_vrf_pre_output(key_type, public, input)
587 }
588
589 #[cfg(feature = "bandersnatch-experimental")]
590 fn bandersnatch_ring_vrf_sign(
591 &self,
592 key_type: KeyTypeId,
593 public: &bandersnatch::Public,
594 input: &bandersnatch::vrf::VrfSignData,
595 prover: &bandersnatch::ring_vrf::RingProver,
596 ) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error> {
597 (**self).bandersnatch_ring_vrf_sign(key_type, public, input, prover)
598 }
599
600 #[cfg(feature = "bls-experimental")]
601 fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public> {
602 (**self).bls381_public_keys(id)
603 }
604
605 #[cfg(feature = "bls-experimental")]
606 fn ecdsa_bls381_public_keys(&self, id: KeyTypeId) -> Vec<ecdsa_bls381::Public> {
607 (**self).ecdsa_bls381_public_keys(id)
608 }
609
610 #[cfg(feature = "bls-experimental")]
611 fn bls381_generate_new(
612 &self,
613 key_type: KeyTypeId,
614 seed: Option<&str>,
615 ) -> Result<bls381::Public, Error> {
616 (**self).bls381_generate_new(key_type, seed)
617 }
618
619 #[cfg(feature = "bls-experimental")]
620 fn ecdsa_bls381_generate_new(
621 &self,
622 key_type: KeyTypeId,
623 seed: Option<&str>,
624 ) -> Result<ecdsa_bls381::Public, Error> {
625 (**self).ecdsa_bls381_generate_new(key_type, seed)
626 }
627
628 #[cfg(feature = "bls-experimental")]
629 fn bls381_sign(
630 &self,
631 key_type: KeyTypeId,
632 public: &bls381::Public,
633 msg: &[u8],
634 ) -> Result<Option<bls381::Signature>, Error> {
635 (**self).bls381_sign(key_type, public, msg)
636 }
637
638 #[cfg(feature = "bls-experimental")]
639 fn bls381_generate_proof_of_possession(
640 &self,
641 key_type: KeyTypeId,
642 public: &bls381::Public,
643 ) -> Result<Option<bls381::Signature>, Error> {
644 (**self).bls381_generate_proof_of_possession(key_type, public)
645 }
646
647 #[cfg(feature = "bls-experimental")]
648 fn ecdsa_bls381_sign(
649 &self,
650 key_type: KeyTypeId,
651 public: &ecdsa_bls381::Public,
652 msg: &[u8],
653 ) -> Result<Option<ecdsa_bls381::Signature>, Error> {
654 (**self).ecdsa_bls381_sign(key_type, public, msg)
655 }
656
657 #[cfg(feature = "bls-experimental")]
658 fn ecdsa_bls381_sign_with_keccak256(
659 &self,
660 key_type: KeyTypeId,
661 public: &ecdsa_bls381::Public,
662 msg: &[u8],
663 ) -> Result<Option<ecdsa_bls381::Signature>, Error> {
664 (**self).ecdsa_bls381_sign_with_keccak256(key_type, public, msg)
665 }
666
667 fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {
668 (**self).insert(key_type, suri, public)
669 }
670
671 fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error> {
672 (**self).keys(key_type)
673 }
674
675 fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
676 (**self).has_keys(public_keys)
677 }
678}
679
680pub type KeystorePtr = Arc<dyn Keystore>;
682
683sp_externalities::decl_extension! {
684 pub struct KeystoreExt(KeystorePtr);
686}
687
688impl KeystoreExt {
689 pub fn from(keystore: KeystorePtr) -> Self {
693 Self(keystore)
694 }
695
696 pub fn new<T: Keystore + 'static>(keystore: T) -> Self {
698 Self(Arc::new(keystore))
699 }
700}
701
702sp_core::generate_feature_enabled_macro!(
703 bandersnatch_experimental_enabled,
704 feature = "bandersnatch-experimental",
705 $
706);
707
708sp_core::generate_feature_enabled_macro!(
709 bls_experimental_enabled,
710 feature = "bls-experimental",
711 $
712);