sp_keystore/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Keystore traits
19
20#![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/// Keystore error
39#[derive(Debug)]
40pub enum Error {
41	/// Public key type is not supported
42	KeyNotSupported(KeyTypeId),
43	/// Validation error
44	ValidationError(String),
45	/// Keystore unavailable
46	Unavailable,
47	/// Programming errors
48	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
65/// Something that generates, stores and provides access to secret keys.
66pub trait Keystore: Send + Sync {
67	/// Returns all the sr25519 public keys for the given key type.
68	fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public>;
69
70	/// Generate a new sr25519 key pair for the given key type and an optional seed.
71	///
72	/// Returns an `sr25519::Public` key of the generated key pair or an `Err` if
73	/// something failed during key generation.
74	fn sr25519_generate_new(
75		&self,
76		key_type: KeyTypeId,
77		seed: Option<&str>,
78	) -> Result<sr25519::Public, Error>;
79
80	/// Generate an sr25519 signature for a given message.
81	///
82	/// Receives [`KeyTypeId`] and an [`sr25519::Public`] key to be able to map
83	/// them to a private key that exists in the keystore.
84	///
85	/// Returns an [`sr25519::Signature`] or `None` in case the given `key_type`
86	/// and `public` combination doesn't exist in the keystore.
87	/// An `Err` will be returned if generating the signature itself failed.
88	fn sr25519_sign(
89		&self,
90		key_type: KeyTypeId,
91		public: &sr25519::Public,
92		msg: &[u8],
93	) -> Result<Option<sr25519::Signature>, Error>;
94
95	/// Generate an sr25519 VRF signature for the given data.
96	///
97	/// Receives [`KeyTypeId`] and an [`sr25519::Public`] key to be able to map
98	/// them to a private key that exists in the keystore.
99	///
100	/// Returns `None` if the given `key_type` and `public` combination doesn't
101	/// exist in the keystore or an `Err` when something failed.
102	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	/// Generate an sr25519 VRF pre-output for a given input data.
110	///
111	/// Receives [`KeyTypeId`] and an [`sr25519::Public`] key to be able to map
112	/// them to a private key that exists in the keystore.
113	///
114	/// Returns `None` if the given `key_type` and `public` combination doesn't
115	/// exist in the keystore or an `Err` when something failed.
116	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	/// Returns all ed25519 public keys for the given key type.
124	fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public>;
125
126	/// Generate a new ed25519 key pair for the given key type and an optional seed.
127	///
128	/// Returns an `ed25519::Public` key of the generated key pair or an `Err` if
129	/// something failed during key generation.
130	fn ed25519_generate_new(
131		&self,
132		key_type: KeyTypeId,
133		seed: Option<&str>,
134	) -> Result<ed25519::Public, Error>;
135
136	/// Generate an ed25519 signature for a given message.
137	///
138	/// Receives [`KeyTypeId`] and an [`ed25519::Public`] key to be able to map
139	/// them to a private key that exists in the keystore.
140	///
141	/// Returns an [`ed25519::Signature`] or `None` in case the given `key_type`
142	/// and `public` combination doesn't exist in the keystore.
143	/// An `Err` will be returned if generating the signature itself failed.
144	fn ed25519_sign(
145		&self,
146		key_type: KeyTypeId,
147		public: &ed25519::Public,
148		msg: &[u8],
149	) -> Result<Option<ed25519::Signature>, Error>;
150
151	/// Returns all ecdsa public keys for the given key type.
152	fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public>;
153
154	/// Generate a new ecdsa key pair for the given key type and an optional seed.
155	///
156	/// Returns an `ecdsa::Public` key of the generated key pair or an `Err` if
157	/// something failed during key generation.
158	fn ecdsa_generate_new(
159		&self,
160		key_type: KeyTypeId,
161		seed: Option<&str>,
162	) -> Result<ecdsa::Public, Error>;
163
164	/// Generate an ecdsa signature for a given message.
165	///
166	/// Receives [`KeyTypeId`] and an [`ecdsa::Public`] key to be able to map
167	/// them to a private key that exists in the keystore.
168	///
169	/// Returns an [`ecdsa::Signature`] or `None` in case the given `key_type`
170	/// and `public` combination doesn't exist in the keystore.
171	/// An `Err` will be returned if generating the signature itself failed.
172	fn ecdsa_sign(
173		&self,
174		key_type: KeyTypeId,
175		public: &ecdsa::Public,
176		msg: &[u8],
177	) -> Result<Option<ecdsa::Signature>, Error>;
178
179	/// Generate an ecdsa signature for a given pre-hashed message.
180	///
181	/// Receives [`KeyTypeId`] and an [`ecdsa::Public`] key to be able to map
182	/// them to a private key that exists in the keystore.
183	///
184	/// Returns an [`ecdsa::Signature`] or `None` in case the given `key_type`
185	/// and `public` combination doesn't exist in the keystore.
186	/// An `Err` will be returned if generating the signature itself failed.
187	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	/// Returns all the bandersnatch public keys for the given key type.
195	#[cfg(feature = "bandersnatch-experimental")]
196	fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public>;
197
198	/// Generate a new bandersnatch key pair for the given key type and an optional seed.
199	///
200	/// Returns an `bandersnatch::Public` key of the generated key pair or an `Err` if
201	/// something failed during key generation.
202	#[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	/// Generate an bandersnatch signature for a given message.
210	///
211	/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
212	/// them to a private key that exists in the keystore.
213	///
214	/// Returns an [`bandersnatch::Signature`] or `None` in case the given `key_type`
215	/// and `public` combination doesn't exist in the keystore.
216	/// An `Err` will be returned if generating the signature itself failed.
217	#[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	/// Generate a bandersnatch VRF signature for the given data.
226	///
227	/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
228	/// them to a private key that exists in the keystore.
229	///
230	/// Returns `None` if the given `key_type` and `public` combination doesn't
231	/// exist in the keystore or an `Err` when something failed.
232	#[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	/// Generate a bandersnatch VRF pre-output for a given input data.
241	///
242	/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
243	/// them to a private key that exists in the keystore.
244	///
245	/// Returns `None` if the given `key_type` and `public` combination doesn't
246	/// exist in the keystore or an `Err` when something failed.
247	#[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	/// Generate a bandersnatch ring-VRF signature for the given data.
256	///
257	/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
258	/// them to a private key that exists in the keystore.
259	///
260	/// Also takes a [`bandersnatch::ring_vrf::RingProver`] instance obtained from
261	/// a valid [`bandersnatch::ring_vrf::RingContext`].
262	///
263	/// The ring signature is verifiable if the public key corresponding to the
264	/// signing [`bandersnatch::Pair`] is part of the ring from which the
265	/// [`bandersnatch::ring_vrf::RingProver`] has been constructed.
266	/// If not, the produced signature is just useless.
267	///
268	/// Returns `None` if the given `key_type` and `public` combination doesn't
269	/// exist in the keystore or an `Err` when something failed.
270	#[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	/// Returns all bls12-381 public keys for the given key type.
280	#[cfg(feature = "bls-experimental")]
281	fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public>;
282
283	/// Returns all (ecdsa,bls12-381) paired public keys for the given key type.
284	#[cfg(feature = "bls-experimental")]
285	fn ecdsa_bls381_public_keys(&self, id: KeyTypeId) -> Vec<ecdsa_bls381::Public>;
286
287	/// Generate a new bls381 key pair for the given key type and an optional seed.
288	///
289	/// Returns an `bls381::Public` key of the generated key pair or an `Err` if
290	/// something failed during key generation.
291	#[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	/// Generate a new (ecdsa,bls381) key pair for the given key type and an optional seed.
299	///
300	/// Returns an `ecdsa_bls381::Public` key of the generated key pair or an `Err` if
301	/// something failed during key generation.
302	#[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	/// Generate a bls381 signature for a given message.
310	///
311	/// Receives [`KeyTypeId`] and a [`bls381::Public`] key to be able to map
312	/// them to a private key that exists in the keystore.
313	///
314	/// Returns an [`bls381::Signature`] or `None` in case the given `key_type`
315	/// and `public` combination doesn't exist in the keystore.
316	/// An `Err` will be returned if generating the signature itself failed.
317	#[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	/// Generate a bls381 Proof of Possession for a given public key
326	///
327	/// Receives ['KeyTypeId'] and a ['bls381::Public'] key to be able to map
328	/// them to a private key that exists in the keystore
329	///
330	/// Returns an ['bls381::Signature'] or 'None' in case the given 'key_type'
331	/// and 'public' combination doesn't exist in the keystore.
332	/// An 'Err' will be returned if generating the proof of possession itself failed.
333	#[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	/// Generate a (ecdsa,bls381) signature pair for a given message.
341	///
342	/// Receives [`KeyTypeId`] and a [`ecdsa_bls381::Public`] key to be able to map
343	/// them to a private key that exists in the keystore.
344	///
345	/// Returns an [`ecdsa_bls381::Signature`] or `None` in case the given `key_type`
346	/// and `public` combination doesn't exist in the keystore.
347	/// An `Err` will be returned if generating the signature itself failed.
348	#[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	/// Hashes the `message` using keccak256 and then signs it using ECDSA
357	/// algorithm. It does not affect the behavior of BLS12-381 component. It generates
358	/// BLS12-381 Signature according to IETF standard.
359	///
360	/// Receives [`KeyTypeId`] and a [`ecdsa_bls381::Public`] key to be able to map
361	/// them to a private key that exists in the keystore.
362	///
363	/// Returns an [`ecdsa_bls381::Signature`] or `None` in case the given `key_type`
364	/// and `public` combination doesn't exist in the keystore.
365	/// An `Err` will be returned if generating the signature itself failed.
366	#[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	/// Insert a new secret key.
375	fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()>;
376
377	/// List all supported keys of a given type.
378	///
379	/// Returns a set of public keys the signer supports in raw format.
380	fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error>;
381
382	/// Checks if the private keys for the given public key and key type combinations exist.
383	///
384	/// Returns `true` iff all private keys could be found.
385	fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
386
387	/// Convenience method to sign a message using the given key type and a raw public key
388	/// for secret lookup.
389	///
390	/// The message is signed using the cryptographic primitive specified by `crypto_id`.
391	///
392	/// Schemes supported by the default trait implementation:
393	/// - sr25519
394	/// - ed25519
395	/// - ecdsa
396	/// - bandersnatch
397	/// - bls381
398	/// - (ecdsa,bls381) paired keys
399	///
400	/// To support more schemes you can overwrite this method.
401	///
402	/// Returns the SCALE encoded signature if key is found and supported, `None` if the key doesn't
403	/// exist or an error when something failed.
404	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
680/// A shared pointer to a keystore implementation.
681pub type KeystorePtr = Arc<dyn Keystore>;
682
683sp_externalities::decl_extension! {
684	/// The keystore extension to register/retrieve from the externalities.
685	pub struct KeystoreExt(KeystorePtr);
686}
687
688impl KeystoreExt {
689	/// Create a new instance of `KeystoreExt`
690	///
691	/// This is more performant as we don't need to wrap keystore in another [`Arc`].
692	pub fn from(keystore: KeystorePtr) -> Self {
693		Self(keystore)
694	}
695
696	/// Create a new instance of `KeystoreExt` using the given `keystore`.
697	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);