referrerpolicy=no-referrer-when-downgrade

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		owner: &[u8],
339	) -> Result<Option<bls381::ProofOfPossession>, Error>;
340
341	/// Generate a (ecdsa,bls381) signature pair for a given message.
342	///
343	/// Receives [`KeyTypeId`] and a [`ecdsa_bls381::Public`] key to be able to map
344	/// them to a private key that exists in the keystore.
345	///
346	/// Returns an [`ecdsa_bls381::Signature`] or `None` in case the given `key_type`
347	/// and `public` combination doesn't exist in the keystore.
348	/// An `Err` will be returned if generating the signature itself failed.
349	#[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	/// Hashes the `message` using keccak256 and then signs it using ECDSA
358	/// algorithm. It does not affect the behavior of BLS12-381 component. It generates
359	/// BLS12-381 Signature according to IETF standard.
360	///
361	/// Receives [`KeyTypeId`] and a [`ecdsa_bls381::Public`] key to be able to map
362	/// them to a private key that exists in the keystore.
363	///
364	/// Returns an [`ecdsa_bls381::Signature`] or `None` in case the given `key_type`
365	/// and `public` combination doesn't exist in the keystore.
366	/// An `Err` will be returned if generating the signature itself failed.
367	#[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	/// Insert a new secret key.
376	fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()>;
377
378	/// List all supported keys of a given type.
379	///
380	/// Returns a set of public keys the signer supports in raw format.
381	fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error>;
382
383	/// Checks if the private keys for the given public key and key type combinations exist.
384	///
385	/// Returns `true` iff all private keys could be found.
386	fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
387
388	/// Convenience method to sign a message using the given key type and a raw public key
389	/// for secret lookup.
390	///
391	/// The message is signed using the cryptographic primitive specified by `crypto_id`.
392	///
393	/// Schemes supported by the default trait implementation:
394	/// - sr25519
395	/// - ed25519
396	/// - ecdsa
397	/// - bandersnatch
398	/// - bls381
399	/// - (ecdsa,bls381) paired keys
400	///
401	/// To support more schemes you can overwrite this method.
402	///
403	/// Returns the SCALE encoded signature if key is found and supported, `None` if the key doesn't
404	/// exist or an error when something failed.
405	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
682/// A shared pointer to a keystore implementation.
683pub type KeystorePtr = Arc<dyn Keystore>;
684
685sp_externalities::decl_extension! {
686	/// The keystore extension to register/retrieve from the externalities.
687	pub struct KeystoreExt(KeystorePtr);
688}
689
690impl KeystoreExt {
691	/// Create a new instance of `KeystoreExt`
692	///
693	/// This is more performant as we don't need to wrap keystore in another [`Arc`].
694	pub fn from(keystore: KeystorePtr) -> Self {
695		Self(keystore)
696	}
697
698	/// Create a new instance of `KeystoreExt` using the given `keystore`.
699	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);