Introduces author_hasKey and author_hasSessionKeys rpc endpoints (#4720)

* Introduces `author_hasKey` and `author_hasSessionKeys` rpc endpoints

Both endpoints can be used to check if a key is present in the keystore.

- `hasKey` works on with an individual public key and key type. It
checks if a private key for the given combination exists in the
keystore.
- `hasSessionKeys` works with the full encoded session key blob stored
on-chain in `nextKeys`. This requires that the given blob can be decoded
by the runtime. It will return `true`, iff all public keys of the
session key exist in the storage.

Fixes: https://github.com/paritytech/substrate/issues/4696

* Update client/rpc-api/src/author/error.rs

Co-Authored-By: Nikolay Volf <nikvolf@gmail.com>

* Indentation

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
This commit is contained in:
Bastian Köcher
2020-01-24 15:20:45 +01:00
committed by GitHub
parent 1614ce0996
commit fc99887de0
21 changed files with 223 additions and 34 deletions
@@ -53,4 +53,8 @@ impl RuntimePublic for Public {
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
sp_io::crypto::ed25519_verify(&signature, msg.as_ref(), self)
}
fn to_raw_vec(&self) -> Vec<u8> {
sp_core::crypto::Public::to_raw_vec(self)
}
}
@@ -301,6 +301,10 @@ macro_rules! app_crypto_public_common {
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
<$public as $crate::RuntimePublic>::verify(self.as_ref(), msg, &signature.as_ref())
}
fn to_raw_vec(&self) -> $crate::Vec<u8> {
<$public as $crate::RuntimePublic>::to_raw_vec(&self.0)
}
}
}
}
@@ -53,4 +53,8 @@ impl RuntimePublic for Public {
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
sp_io::crypto::sr25519_verify(&signature, msg.as_ref(), self)
}
fn to_raw_vec(&self) -> Vec<u8> {
sp_core::crypto::Public::to_raw_vec(self)
}
}
@@ -106,10 +106,13 @@ pub trait RuntimePublic: Sized {
/// Verify that the given signature matches the given message using this public key.
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool;
/// Returns `Self` as raw vec.
fn to_raw_vec(&self) -> Vec<u8>;
}
/// A runtime interface for an application's public key.
pub trait RuntimeAppPublic: Sized {
pub trait RuntimeAppPublic: Sized {
/// An identifier for this application-specific key type.
const ID: KeyTypeId;
@@ -136,6 +139,9 @@ pub trait RuntimeAppPublic: Sized {
/// Verify that the given signature matches the given message using this public key.
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool;
/// Returns `Self` as raw vec.
fn to_raw_vec(&self) -> Vec<u8>;
}
/// Something that bound to a fixed `RuntimeAppPublic`.
+1 -3
View File
@@ -19,7 +19,6 @@
// end::description[]
use sp_std::hash::Hash;
#[cfg(feature = "full_crypto")]
use sp_std::vec::Vec;
#[cfg(feature = "std")]
use sp_std::convert::TryInto;
@@ -520,8 +519,7 @@ pub trait Public: AsRef<[u8]> + AsMut<[u8]> + Default + Derive + CryptoType + Pa
fn from_slice(data: &[u8]) -> Self;
/// Return a `Vec<u8>` filled with raw data.
#[cfg(feature = "std")]
fn to_raw_vec(&self) -> Vec<u8> { self.as_slice().to_owned() }
fn to_raw_vec(&self) -> Vec<u8> { self.as_slice().to_vec() }
/// Return a slice filled with raw data.
fn as_slice(&self) -> &[u8] { self.as_ref() }
+4
View File
@@ -127,6 +127,10 @@ impl crate::traits::BareCryptoStore for KeyStore {
fn password(&self) -> Option<&str> {
None
}
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
public_keys.iter().all(|(k, t)| self.keys.get(&t).and_then(|s| s.get(k)).is_some())
}
}
/// Macro for exporting functions from wasm in with the expected signature for using it with the
+5
View File
@@ -69,6 +69,11 @@ pub trait BareCryptoStore: Send + Sync {
/// Get the password for this store.
fn password(&self) -> Option<&str>;
/// 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;
}
/// A pointer to the key store.
+1 -3
View File
@@ -19,8 +19,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
use sp_runtime::traits::NumberFor;
/// Local Storage Prefix used by the Offchain Worker API to
pub const STORAGE_PREFIX: &[u8] = b"storage";
@@ -31,7 +29,7 @@ sp_api::decl_runtime_apis! {
/// Starts the off-chain task for given block number.
#[skip_initialize_block]
#[changed_in(2)]
fn offchain_worker(number: NumberFor<Block>);
fn offchain_worker(number: sp_runtime::traits::NumberFor<Block>);
/// Starts the off-chain task for given block header.
#[skip_initialize_block]
@@ -115,6 +115,10 @@ impl sp_application_crypto::RuntimeAppPublic for UintAuthorityId {
u64::from_le_bytes(msg_signature) == *signature
}
fn to_raw_vec(&self) -> Vec<u8> {
AsRef::<[u8]>::as_ref(self).to_vec()
}
}
impl OpaqueKeys for UintAuthorityId {
@@ -1106,12 +1106,14 @@ macro_rules! count {
#[macro_export]
macro_rules! impl_opaque_keys {
(
$( #[ $attr:meta ] )*
pub struct $name:ident {
$(
pub $field:ident: $type:ty,
)*
}
) => {
$( #[ $attr ] )*
#[derive(
Default, Clone, PartialEq, Eq,
$crate::codec::Encode,
@@ -1143,6 +1145,37 @@ macro_rules! impl_opaque_keys {
};
$crate::codec::Encode::encode(&keys)
}
/// Converts `Self` into a `Vec` of `(raw public key, KeyTypeId)`.
pub fn into_raw_public_keys(
self,
) -> $crate::sp_std::vec::Vec<($crate::sp_std::vec::Vec<u8>, $crate::KeyTypeId)> {
let mut keys = Vec::new();
$(
keys.push((
$crate::RuntimeAppPublic::to_raw_vec(&self.$field),
<
<
$type as $crate::BoundToRuntimeAppPublic
>::Public as $crate::RuntimeAppPublic
>::ID,
));
)*
keys
}
/// Decode `Self` from the given `encoded` slice and convert `Self` into the raw public
/// keys (see [`Self::into_raw_public_keys`]).
///
/// Returns `None` when the decoding failed, otherwise `Some(_)`.
pub fn decode_into_raw_public_keys(
encoded: &[u8],
) -> Option<$crate::sp_std::vec::Vec<($crate::sp_std::vec::Vec<u8>, $crate::KeyTypeId)>> {
<Self as $crate::codec::Decode>::decode(&mut &encoded[..])
.ok()
.map(|s| s.into_raw_public_keys())
}
}
impl $crate::traits::OpaqueKeys for $name {
+2 -1
View File
@@ -7,8 +7,9 @@ edition = "2018"
[dependencies]
sp-api = { version = "2.0.0", default-features = false, path = "../api" }
sp-std = { version = "2.0.0", default-features = false, path = "../std" }
sp-core = { version = "2.0.0", default-features = false, path = "../core" }
sp-runtime = { version = "2.0.0", optional = true, path = "../runtime" }
[features]
default = [ "std" ]
std = [ "sp-api/std", "sp-std/std", "sp-runtime" ]
std = [ "sp-api/std", "sp-std/std", "sp-runtime", "sp-core/std" ]
+7
View File
@@ -25,6 +25,8 @@ use sp_runtime::{generic::BlockId, traits::Block as BlockT};
#[cfg(feature = "std")]
use sp_api::ProvideRuntimeApi;
use sp_core::crypto::KeyTypeId;
sp_api::decl_runtime_apis! {
/// Session keys runtime api.
pub trait SessionKeys {
@@ -36,6 +38,11 @@ sp_api::decl_runtime_apis! {
///
/// Returns the concatenated SCALE encoded public keys.
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8>;
/// Decode the given public session keys.
///
/// Returns the list of public raw public keys + key type.
fn decode_session_keys(encoded: Vec<u8>) -> Option<Vec<(Vec<u8>, KeyTypeId)>>;
}
}