mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 19:41:05 +00:00
Introduce sign_with method in keystore (#4925)
* Add KEY_KIND_ID to the public trait This change is being introduced for the purpose of identifying a public key with it's identifier and algorithm "kind". * Use `sign_with` as implemented in BareCryptoStore * Implement `sign_with` in sc_keystore * Fix inconsistencies, use *_KIND_ID in sp_core testing * Rename KeyKindId to CryptoTypeId * Remove pair-returning functions from BareCryptoStore trait * Define CryptoTypeId in app-crypto macros * Add functions to get keys supported by keystore * Fix sign_with signature to include CryptoTypePublicPair * Add `sign_with_any` and `sign_with_all` * Use keystore.sign_with in auth_discovery * Rename get_supported_keys -> supported_keys * Added headers to function docstrings * Use chain instead of extending a temp vector * Fixed some code formatting * Restrict size of CryptoTypeId This is to be able to use Encode/Decode derives and the overcome having the size being unknown at compile-time. * Implement sign_with in the trait itself * Remove whitespace * Use key_type also as a CryptoTypeId in app_crypto macros * Rename `get_keys` to `keys` in BareCryptoStore * Remove usage of key_pair funcs in tests * Adjust docstring for *_CYPTO_ID constants * Fix failures * Simplify mapping on keys * Remove one let * Fixed typo * PR feedback * remove whitespace * Zip keys and signatures * Use into_iter & remove cloned * Pass index to MissingSignature * Use typed errors instead of strings for BareCryptoStore * Implement Debug for trait error * Use hashsets for better performance for supported_keys * Make sure keys are inserted into the keystore * Make sign_with_all return type consistent with `sign_with` * Rename Error to BareCryptoStoreError * Rename CRYPT_TYPE_ID -> CRYPTO_ID * Remove unnecessary CRYPTO_ID declaration in Public trait * Convert pub key to CryptoTypePublicPair * Fix use * Fix code style * Implement From on CryptoTypePublicPair in app_crypto macros * Change CryptoTypePublicPair to a struct * Implement Display on CryptoTypePublicPair * Pass CryptoTypePublicPair to MissingSignature error * Adjust docs according to function signature * Unify keys implementation * Fix RPC author tests * Fix stackoverflow * Tabify spaces * Pass KeyTypeId to error for easier debugging * Fix asserts * Use ToHex to format public key * Use constants from sp_core * Rename testing KeyTypeId constants * Please compiler * Restore KeyTypeId names apparently, they're not only used in tests * Use BareCryptoStoreError instead of String * Document return value * Fix borrow check * Convert to hashset internally * WIP - iter_keys * Return raw_public_keys * Address PR feedback * Address PR Feedback * Fix hexdisplay import error * Update primitives/core/src/traits.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -16,14 +16,35 @@
|
||||
|
||||
//! Shareable Substrate traits.
|
||||
|
||||
use crate::{crypto::KeyTypeId, ed25519, sr25519};
|
||||
use crate::{
|
||||
crypto::{KeyTypeId, CryptoTypePublicPair},
|
||||
ed25519, sr25519,
|
||||
};
|
||||
|
||||
use std::{
|
||||
fmt::{Debug, Display}, panic::UnwindSafe, sync::Arc, borrow::Cow,
|
||||
borrow::Cow,
|
||||
fmt::{Debug, Display},
|
||||
panic::UnwindSafe,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub use sp_externalities::{Externalities, ExternalitiesExt};
|
||||
|
||||
/// BareCryptoStore error
|
||||
#[derive(Debug)]
|
||||
pub enum BareCryptoStoreError {
|
||||
/// Public key type is not supported
|
||||
KeyNotSupported(KeyTypeId),
|
||||
/// Pair not found for public key and KeyTypeId
|
||||
PairNotFound(String),
|
||||
/// Validation error
|
||||
ValidationError(String),
|
||||
/// Keystore unavailable
|
||||
Unavailable,
|
||||
/// Programming errors
|
||||
Other(String)
|
||||
}
|
||||
|
||||
/// Something that generates, stores and provides access to keys.
|
||||
pub trait BareCryptoStore: Send + Sync {
|
||||
/// Returns all sr25519 public keys for the given key type.
|
||||
@@ -37,10 +58,7 @@ pub trait BareCryptoStore: Send + Sync {
|
||||
&mut self,
|
||||
id: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<sr25519::Public, String>;
|
||||
/// Returns the sr25519 key pair for the given key type and public key combination.
|
||||
fn sr25519_key_pair(&self, id: KeyTypeId, pub_key: &sr25519::Public) -> Option<sr25519::Pair>;
|
||||
|
||||
) -> Result<sr25519::Public, BareCryptoStoreError>;
|
||||
/// Returns all ed25519 public keys for the given key type.
|
||||
fn ed25519_public_keys(&self, id: KeyTypeId) -> Vec<ed25519::Public>;
|
||||
/// Generate a new ed25519 key pair for the given key type and an optional seed.
|
||||
@@ -52,10 +70,7 @@ pub trait BareCryptoStore: Send + Sync {
|
||||
&mut self,
|
||||
id: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ed25519::Public, String>;
|
||||
|
||||
/// Returns the ed25519 key pair for the given key type and public key combination.
|
||||
fn ed25519_key_pair(&self, id: KeyTypeId, pub_key: &ed25519::Public) -> Option<ed25519::Pair>;
|
||||
) -> Result<ed25519::Public, BareCryptoStoreError>;
|
||||
|
||||
/// Insert a new key. This doesn't require any known of the crypto; but a public key must be
|
||||
/// manually provided.
|
||||
@@ -67,11 +82,78 @@ pub trait BareCryptoStore: Send + Sync {
|
||||
|
||||
/// Get the password for this store.
|
||||
fn password(&self) -> Option<&str>;
|
||||
/// Find intersection between provided keys and supported keys
|
||||
///
|
||||
/// Provided a list of (CryptoTypeId,[u8]) pairs, this would return
|
||||
/// a filtered set of public keys which are supported by the keystore.
|
||||
fn supported_keys(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
keys: Vec<CryptoTypePublicPair>
|
||||
) -> Result<Vec<CryptoTypePublicPair>, BareCryptoStoreError>;
|
||||
/// List all supported keys
|
||||
///
|
||||
/// Returns a set of public keys the signer supports.
|
||||
fn keys(&self, id: KeyTypeId) -> Result<Vec<CryptoTypePublicPair>, BareCryptoStoreError>;
|
||||
|
||||
/// Checks if the private keys for the given public key and key type combinations exist.
|
||||
///
|
||||
/// Returns `true` iff all private keys could be found.
|
||||
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
|
||||
|
||||
/// Sign with key
|
||||
///
|
||||
/// Signs a message with the private key that matches
|
||||
/// the public key passed.
|
||||
///
|
||||
/// Returns the SCALE encoded signature if key is found & supported,
|
||||
/// an error otherwise.
|
||||
fn sign_with(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
key: &CryptoTypePublicPair,
|
||||
msg: &[u8],
|
||||
) -> Result<Vec<u8>, BareCryptoStoreError>;
|
||||
|
||||
/// Sign with any key
|
||||
///
|
||||
/// Given a list of public keys, find the first supported key and
|
||||
/// sign the provided message with that key.
|
||||
///
|
||||
/// Returns a tuple of the used key and the signature
|
||||
fn sign_with_any(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
keys: Vec<CryptoTypePublicPair>,
|
||||
msg: &[u8]
|
||||
) -> Result<(CryptoTypePublicPair, Vec<u8>), BareCryptoStoreError> {
|
||||
if keys.len() == 1 {
|
||||
return self.sign_with(id, &keys[0], msg).map(|s| (keys[0].clone(), s));
|
||||
} else {
|
||||
for k in self.supported_keys(id, keys)? {
|
||||
if let Ok(sign) = self.sign_with(id, &k, msg) {
|
||||
return Ok((k, sign));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(BareCryptoStoreError::KeyNotSupported(id))
|
||||
}
|
||||
|
||||
/// Sign with all keys
|
||||
///
|
||||
/// Provided a list of public keys, sign a message with
|
||||
/// each key given that the key is supported.
|
||||
///
|
||||
/// Returns a list of `Result`s each representing the signature of each key or
|
||||
/// a BareCryptoStoreError for non-supported keys.
|
||||
fn sign_with_all(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
keys: Vec<CryptoTypePublicPair>,
|
||||
msg: &[u8],
|
||||
) -> Result<Vec<Result<Vec<u8>, BareCryptoStoreError>>, ()>{
|
||||
Ok(keys.iter().map(|k| self.sign_with(id, k, msg)).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// A pointer to the key store.
|
||||
|
||||
Reference in New Issue
Block a user