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 owner: &[u8],
339 ) -> Result<Option<bls381::ProofOfPossession>, Error>;
340
341 #[cfg(feature = "bls-experimental")]
350 fn ecdsa_bls381_sign(
351 &self,
352 key_type: KeyTypeId,
353 public: &ecdsa_bls381::Public,
354 msg: &[u8],
355 ) -> Result<Option<ecdsa_bls381::Signature>, Error>;
356
357 #[cfg(feature = "bls-experimental")]
368 fn ecdsa_bls381_sign_with_keccak256(
369 &self,
370 key_type: KeyTypeId,
371 public: &ecdsa_bls381::Public,
372 msg: &[u8],
373 ) -> Result<Option<ecdsa_bls381::Signature>, Error>;
374
375 fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()>;
377
378 fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error>;
382
383 fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
387
388 fn sign_with(
406 &self,
407 id: KeyTypeId,
408 crypto_id: CryptoTypeId,
409 public: &[u8],
410 msg: &[u8],
411 ) -> Result<Option<Vec<u8>>, Error> {
412 use codec::Encode;
413
414 let signature = match crypto_id {
415 sr25519::CRYPTO_ID => {
416 let public = sr25519::Public::from_slice(public)
417 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
418 self.sr25519_sign(id, &public, msg)?.map(|s| s.encode())
419 },
420 ed25519::CRYPTO_ID => {
421 let public = ed25519::Public::from_slice(public)
422 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
423 self.ed25519_sign(id, &public, msg)?.map(|s| s.encode())
424 },
425 ecdsa::CRYPTO_ID => {
426 let public = ecdsa::Public::from_slice(public)
427 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
428
429 self.ecdsa_sign(id, &public, msg)?.map(|s| s.encode())
430 },
431 #[cfg(feature = "bandersnatch-experimental")]
432 bandersnatch::CRYPTO_ID => {
433 let public = bandersnatch::Public::from_slice(public)
434 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
435 self.bandersnatch_sign(id, &public, msg)?.map(|s| s.encode())
436 },
437 #[cfg(feature = "bls-experimental")]
438 bls381::CRYPTO_ID => {
439 let public = bls381::Public::from_slice(public)
440 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
441 self.bls381_sign(id, &public, msg)?.map(|s| s.encode())
442 },
443 #[cfg(feature = "bls-experimental")]
444 ecdsa_bls381::CRYPTO_ID => {
445 let public = ecdsa_bls381::Public::from_slice(public)
446 .map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
447 self.ecdsa_bls381_sign(id, &public, msg)?.map(|s| s.encode())
448 },
449 _ => return Err(Error::KeyNotSupported(id)),
450 };
451 Ok(signature)
452 }
453}
454
455impl<T: Keystore + ?Sized> Keystore for Arc<T> {
456 fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public> {
457 (**self).sr25519_public_keys(key_type)
458 }
459
460 fn sr25519_generate_new(
461 &self,
462 key_type: KeyTypeId,
463 seed: Option<&str>,
464 ) -> Result<sr25519::Public, Error> {
465 (**self).sr25519_generate_new(key_type, seed)
466 }
467
468 fn sr25519_sign(
469 &self,
470 key_type: KeyTypeId,
471 public: &sr25519::Public,
472 msg: &[u8],
473 ) -> Result<Option<sr25519::Signature>, Error> {
474 (**self).sr25519_sign(key_type, public, msg)
475 }
476
477 fn sr25519_vrf_sign(
478 &self,
479 key_type: KeyTypeId,
480 public: &sr25519::Public,
481 data: &sr25519::vrf::VrfSignData,
482 ) -> Result<Option<sr25519::vrf::VrfSignature>, Error> {
483 (**self).sr25519_vrf_sign(key_type, public, data)
484 }
485
486 fn sr25519_vrf_pre_output(
487 &self,
488 key_type: KeyTypeId,
489 public: &sr25519::Public,
490 input: &sr25519::vrf::VrfInput,
491 ) -> Result<Option<sr25519::vrf::VrfPreOutput>, Error> {
492 (**self).sr25519_vrf_pre_output(key_type, public, input)
493 }
494
495 fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
496 (**self).ed25519_public_keys(key_type)
497 }
498
499 fn ed25519_generate_new(
500 &self,
501 key_type: KeyTypeId,
502 seed: Option<&str>,
503 ) -> Result<ed25519::Public, Error> {
504 (**self).ed25519_generate_new(key_type, seed)
505 }
506
507 fn ed25519_sign(
508 &self,
509 key_type: KeyTypeId,
510 public: &ed25519::Public,
511 msg: &[u8],
512 ) -> Result<Option<ed25519::Signature>, Error> {
513 (**self).ed25519_sign(key_type, public, msg)
514 }
515
516 fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public> {
517 (**self).ecdsa_public_keys(key_type)
518 }
519
520 fn ecdsa_generate_new(
521 &self,
522 key_type: KeyTypeId,
523 seed: Option<&str>,
524 ) -> Result<ecdsa::Public, Error> {
525 (**self).ecdsa_generate_new(key_type, seed)
526 }
527
528 fn ecdsa_sign(
529 &self,
530 key_type: KeyTypeId,
531 public: &ecdsa::Public,
532 msg: &[u8],
533 ) -> Result<Option<ecdsa::Signature>, Error> {
534 (**self).ecdsa_sign(key_type, public, msg)
535 }
536
537 fn ecdsa_sign_prehashed(
538 &self,
539 key_type: KeyTypeId,
540 public: &ecdsa::Public,
541 msg: &[u8; 32],
542 ) -> Result<Option<ecdsa::Signature>, Error> {
543 (**self).ecdsa_sign_prehashed(key_type, public, msg)
544 }
545
546 #[cfg(feature = "bandersnatch-experimental")]
547 fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public> {
548 (**self).bandersnatch_public_keys(key_type)
549 }
550
551 #[cfg(feature = "bandersnatch-experimental")]
552 fn bandersnatch_generate_new(
553 &self,
554 key_type: KeyTypeId,
555 seed: Option<&str>,
556 ) -> Result<bandersnatch::Public, Error> {
557 (**self).bandersnatch_generate_new(key_type, seed)
558 }
559
560 #[cfg(feature = "bandersnatch-experimental")]
561 fn bandersnatch_sign(
562 &self,
563 key_type: KeyTypeId,
564 public: &bandersnatch::Public,
565 msg: &[u8],
566 ) -> Result<Option<bandersnatch::Signature>, Error> {
567 (**self).bandersnatch_sign(key_type, public, msg)
568 }
569
570 #[cfg(feature = "bandersnatch-experimental")]
571 fn bandersnatch_vrf_sign(
572 &self,
573 key_type: KeyTypeId,
574 public: &bandersnatch::Public,
575 input: &bandersnatch::vrf::VrfSignData,
576 ) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error> {
577 (**self).bandersnatch_vrf_sign(key_type, public, input)
578 }
579
580 #[cfg(feature = "bandersnatch-experimental")]
581 fn bandersnatch_vrf_pre_output(
582 &self,
583 key_type: KeyTypeId,
584 public: &bandersnatch::Public,
585 input: &bandersnatch::vrf::VrfInput,
586 ) -> Result<Option<bandersnatch::vrf::VrfPreOutput>, Error> {
587 (**self).bandersnatch_vrf_pre_output(key_type, public, input)
588 }
589
590 #[cfg(feature = "bandersnatch-experimental")]
591 fn bandersnatch_ring_vrf_sign(
592 &self,
593 key_type: KeyTypeId,
594 public: &bandersnatch::Public,
595 input: &bandersnatch::vrf::VrfSignData,
596 prover: &bandersnatch::ring_vrf::RingProver,
597 ) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error> {
598 (**self).bandersnatch_ring_vrf_sign(key_type, public, input, prover)
599 }
600
601 #[cfg(feature = "bls-experimental")]
602 fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public> {
603 (**self).bls381_public_keys(id)
604 }
605
606 #[cfg(feature = "bls-experimental")]
607 fn ecdsa_bls381_public_keys(&self, id: KeyTypeId) -> Vec<ecdsa_bls381::Public> {
608 (**self).ecdsa_bls381_public_keys(id)
609 }
610
611 #[cfg(feature = "bls-experimental")]
612 fn bls381_generate_new(
613 &self,
614 key_type: KeyTypeId,
615 seed: Option<&str>,
616 ) -> Result<bls381::Public, Error> {
617 (**self).bls381_generate_new(key_type, seed)
618 }
619
620 #[cfg(feature = "bls-experimental")]
621 fn ecdsa_bls381_generate_new(
622 &self,
623 key_type: KeyTypeId,
624 seed: Option<&str>,
625 ) -> Result<ecdsa_bls381::Public, Error> {
626 (**self).ecdsa_bls381_generate_new(key_type, seed)
627 }
628
629 #[cfg(feature = "bls-experimental")]
630 fn bls381_sign(
631 &self,
632 key_type: KeyTypeId,
633 public: &bls381::Public,
634 msg: &[u8],
635 ) -> Result<Option<bls381::Signature>, Error> {
636 (**self).bls381_sign(key_type, public, msg)
637 }
638
639 #[cfg(feature = "bls-experimental")]
640 fn bls381_generate_proof_of_possession(
641 &self,
642 key_type: KeyTypeId,
643 public: &bls381::Public,
644 owner: &[u8],
645 ) -> Result<Option<bls381::ProofOfPossession>, Error> {
646 (**self).bls381_generate_proof_of_possession(key_type, public, owner)
647 }
648
649 #[cfg(feature = "bls-experimental")]
650 fn ecdsa_bls381_sign(
651 &self,
652 key_type: KeyTypeId,
653 public: &ecdsa_bls381::Public,
654 msg: &[u8],
655 ) -> Result<Option<ecdsa_bls381::Signature>, Error> {
656 (**self).ecdsa_bls381_sign(key_type, public, msg)
657 }
658
659 #[cfg(feature = "bls-experimental")]
660 fn ecdsa_bls381_sign_with_keccak256(
661 &self,
662 key_type: KeyTypeId,
663 public: &ecdsa_bls381::Public,
664 msg: &[u8],
665 ) -> Result<Option<ecdsa_bls381::Signature>, Error> {
666 (**self).ecdsa_bls381_sign_with_keccak256(key_type, public, msg)
667 }
668
669 fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {
670 (**self).insert(key_type, suri, public)
671 }
672
673 fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error> {
674 (**self).keys(key_type)
675 }
676
677 fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
678 (**self).has_keys(public_keys)
679 }
680}
681
682pub type KeystorePtr = Arc<dyn Keystore>;
684
685sp_externalities::decl_extension! {
686 pub struct KeystoreExt(KeystorePtr);
688}
689
690impl KeystoreExt {
691 pub fn from(keystore: KeystorePtr) -> Self {
695 Self(keystore)
696 }
697
698 pub fn new<T: Keystore + 'static>(keystore: T) -> Self {
700 Self(Arc::new(keystore))
701 }
702}
703
704sp_core::generate_feature_enabled_macro!(
705 bandersnatch_experimental_enabled,
706 feature = "bandersnatch-experimental",
707 $
708);
709
710sp_core::generate_feature_enabled_macro!(
711 bls_experimental_enabled,
712 feature = "bls-experimental",
713 $
714);