Keystore overhaul (iter 2) (#13634)

* Remove bloat about remote keystore

* Update docs and remove unused 'KeystoreRef' trait

* Use wherever possible, MemoryKeystore for testing

* Remove unrequired fully qualified method syntax for Keystore
This commit is contained in:
Davide Galassi
2023-03-20 19:21:26 +01:00
committed by GitHub
parent faaa0c2851
commit 480396fe06
44 changed files with 312 additions and 457 deletions
@@ -34,7 +34,7 @@ fn ecdsa_works_in_runtime() {
.test_ecdsa_crypto(test_client.chain_info().genesis_hash)
.expect("Tests `ecdsa` crypto.");
let supported_keys = Keystore::keys(&*keystore, ECDSA).unwrap();
let supported_keys = keystore.keys(ECDSA).unwrap();
assert!(supported_keys.contains(&public.clone().into()));
assert!(AppPair::verify(&signature, "ecdsa", &AppPublic::from(public)));
}
@@ -35,7 +35,7 @@ fn ed25519_works_in_runtime() {
.test_ed25519_crypto(test_client.chain_info().genesis_hash)
.expect("Tests `ed25519` crypto.");
let supported_keys = Keystore::keys(&*keystore, ED25519).unwrap();
let supported_keys = keystore.keys(ED25519).unwrap();
assert!(supported_keys.contains(&public.clone().into()));
assert!(AppPair::verify(&signature, "ed25519", &AppPublic::from(public)));
}
@@ -35,7 +35,7 @@ fn sr25519_works_in_runtime() {
.test_sr25519_crypto(test_client.chain_info().genesis_hash)
.expect("Tests `sr25519` crypto.");
let supported_keys = Keystore::keys(&*keystore, SR25519).unwrap();
let supported_keys = keystore.keys(SR25519).unwrap();
assert!(supported_keys.contains(&public.clone().into()));
assert!(AppPair::verify(&signature, "sr25519", &AppPublic::from(public)));
}
@@ -253,7 +253,7 @@ mod tests {
use crate::{crypto, known_payloads, KEY_TYPE};
use codec::Decode;
use sp_core::{keccak_256, Pair};
use sp_keystore::{testing::MemoryKeystore, Keystore, KeystorePtr};
use sp_keystore::{testing::MemoryKeystore, KeystorePtr};
type TestCommitment = Commitment<u128>;
type TestSignedCommitment = SignedCommitment<u128, crypto::Signature>;
@@ -266,17 +266,13 @@ mod tests {
let store: KeystorePtr = MemoryKeystore::new().into();
let alice = sp_core::ecdsa::Pair::from_string("//Alice", None).unwrap();
let _ = Keystore::insert(&*store, KEY_TYPE, "//Alice", alice.public().as_ref()).unwrap();
store.insert(KEY_TYPE, "//Alice", alice.public().as_ref()).unwrap();
let msg = keccak_256(b"This is the first message");
let sig1 = Keystore::ecdsa_sign_prehashed(&*store, KEY_TYPE, &alice.public(), &msg)
.unwrap()
.unwrap();
let sig1 = store.ecdsa_sign_prehashed(KEY_TYPE, &alice.public(), &msg).unwrap().unwrap();
let msg = keccak_256(b"This is the second message");
let sig2 = Keystore::ecdsa_sign_prehashed(&*store, KEY_TYPE, &alice.public(), &msg)
.unwrap()
.unwrap();
let sig2 = store.ecdsa_sign_prehashed(KEY_TYPE, &alice.public(), &msg).unwrap().unwrap();
(sig1.into(), sig2.into())
}
@@ -74,9 +74,8 @@ impl<TBlockNumber, TMerkleRoot> SignedCommitmentWitness<TBlockNumber, TMerkleRoo
#[cfg(test)]
mod tests {
use sp_core::{keccak_256, Pair};
use sp_keystore::{testing::MemoryKeystore, Keystore, KeystorePtr};
use sp_keystore::{testing::MemoryKeystore, KeystorePtr};
use super::*;
use codec::Decode;
@@ -93,17 +92,13 @@ mod tests {
let store: KeystorePtr = MemoryKeystore::new().into();
let alice = sp_core::ecdsa::Pair::from_string("//Alice", None).unwrap();
let _ = Keystore::insert(&*store, KEY_TYPE, "//Alice", alice.public().as_ref()).unwrap();
store.insert(KEY_TYPE, "//Alice", alice.public().as_ref()).unwrap();
let msg = keccak_256(b"This is the first message");
let sig1 = Keystore::ecdsa_sign_prehashed(&*store, KEY_TYPE, &alice.public(), &msg)
.unwrap()
.unwrap();
let sig1 = store.ecdsa_sign_prehashed(KEY_TYPE, &alice.public(), &msg).unwrap().unwrap();
let msg = keccak_256(b"This is the second message");
let sig2 = Keystore::ecdsa_sign_prehashed(&*store, KEY_TYPE, &alice.public(), &msg)
.unwrap()
.unwrap();
let sig2 = store.ecdsa_sign_prehashed(KEY_TYPE, &alice.public(), &msg).unwrap().unwrap();
(sig1.into(), sig2.into())
}
@@ -28,7 +28,7 @@ use serde::Serialize;
use codec::{Codec, Decode, Encode, Input};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use sp_keystore::{Keystore, KeystorePtr};
use sp_keystore::KeystorePtr;
use sp_runtime::{
traits::{Header as HeaderT, NumberFor},
ConsensusEngineId, RuntimeDebug,
@@ -455,16 +455,12 @@ where
use sp_core::crypto::Public;
let encoded = localized_payload(round, set_id, &message);
let signature = Keystore::sign_with(
&*keystore,
AuthorityId::ID,
&public.to_public_crypto_pair(),
&encoded[..],
)
.ok()
.flatten()?
.try_into()
.ok()?;
let signature = keystore
.sign_with(AuthorityId::ID, &public.to_public_crypto_pair(), &encoded[..])
.ok()
.flatten()?
.try_into()
.ok()?;
Some(grandpa::SignedMessage { message, signature, id: public })
}
+36 -41
View File
@@ -43,7 +43,7 @@ use sp_core::{
traits::TaskExecutorExt,
};
#[cfg(feature = "std")]
use sp_keystore::{Keystore, KeystoreExt};
use sp_keystore::KeystoreExt;
use sp_core::{
crypto::KeyTypeId,
@@ -731,10 +731,9 @@ impl Default for UseDalekExt {
pub trait Crypto {
/// Returns all `ed25519` public keys for the given key id from the keystore.
fn ed25519_public_keys(&mut self, id: KeyTypeId) -> Vec<ed25519::Public> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::ed25519_public_keys(keystore, id)
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.ed25519_public_keys(id)
}
/// Generate an `ed22519` key for the given key type using an optional `seed` and
@@ -745,10 +744,10 @@ pub trait Crypto {
/// Returns the public key.
fn ed25519_generate(&mut self, id: KeyTypeId, seed: Option<Vec<u8>>) -> ed25519::Public {
let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::ed25519_generate_new(keystore, id, seed).expect("`ed25519_generate` failed")
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.ed25519_generate_new(id, seed)
.expect("`ed25519_generate` failed")
}
/// Sign the given `msg` with the `ed25519` key that corresponds to the given public key and
@@ -761,10 +760,9 @@ pub trait Crypto {
pub_key: &ed25519::Public,
msg: &[u8],
) -> Option<ed25519::Signature> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::sign_with(keystore, id, &pub_key.into(), msg)
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.sign_with(id, &pub_key.into(), msg)
.ok()
.flatten()
.and_then(|sig| ed25519::Signature::from_slice(&sig))
@@ -873,10 +871,9 @@ pub trait Crypto {
/// Returns all `sr25519` public keys for the given key id from the keystore.
fn sr25519_public_keys(&mut self, id: KeyTypeId) -> Vec<sr25519::Public> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::sr25519_public_keys(keystore, id)
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.sr25519_public_keys(id)
}
/// Generate an `sr22519` key for the given key type using an optional seed and
@@ -887,10 +884,10 @@ pub trait Crypto {
/// Returns the public key.
fn sr25519_generate(&mut self, id: KeyTypeId, seed: Option<Vec<u8>>) -> sr25519::Public {
let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::sr25519_generate_new(keystore, id, seed).expect("`sr25519_generate` failed")
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.sr25519_generate_new(id, seed)
.expect("`sr25519_generate` failed")
}
/// Sign the given `msg` with the `sr25519` key that corresponds to the given public key and
@@ -903,10 +900,9 @@ pub trait Crypto {
pub_key: &sr25519::Public,
msg: &[u8],
) -> Option<sr25519::Signature> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::sign_with(keystore, id, &pub_key.into(), msg)
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.sign_with(id, &pub_key.into(), msg)
.ok()
.flatten()
.and_then(|sig| sr25519::Signature::from_slice(&sig))
@@ -922,10 +918,9 @@ pub trait Crypto {
/// Returns all `ecdsa` public keys for the given key id from the keystore.
fn ecdsa_public_keys(&mut self, id: KeyTypeId) -> Vec<ecdsa::Public> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::ecdsa_public_keys(keystore, id)
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.ecdsa_public_keys(id)
}
/// Generate an `ecdsa` key for the given key type using an optional `seed` and
@@ -936,10 +931,10 @@ pub trait Crypto {
/// Returns the public key.
fn ecdsa_generate(&mut self, id: KeyTypeId, seed: Option<Vec<u8>>) -> ecdsa::Public {
let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!"));
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::ecdsa_generate_new(keystore, id, seed).expect("`ecdsa_generate` failed")
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.ecdsa_generate_new(id, seed)
.expect("`ecdsa_generate` failed")
}
/// Sign the given `msg` with the `ecdsa` key that corresponds to the given public key and
@@ -952,10 +947,9 @@ pub trait Crypto {
pub_key: &ecdsa::Public,
msg: &[u8],
) -> Option<ecdsa::Signature> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::sign_with(keystore, id, &pub_key.into(), msg)
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.sign_with(id, &pub_key.into(), msg)
.ok()
.flatten()
.and_then(|sig| ecdsa::Signature::from_slice(&sig))
@@ -971,10 +965,11 @@ pub trait Crypto {
pub_key: &ecdsa::Public,
msg: &[u8; 32],
) -> Option<ecdsa::Signature> {
let keystore = &***self
.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!");
Keystore::ecdsa_sign_prehashed(keystore, id, pub_key, msg).ok().flatten()
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.ecdsa_sign_prehashed(id, pub_key, msg)
.ok()
.flatten()
}
/// Verify `ecdsa` signature.
+8 -1
View File
@@ -150,10 +150,17 @@ pub trait Keystore: Send + Sync {
) -> Result<Option<ecdsa::Signature>, Error>;
}
/// A pointer to a keystore.
/// A shared pointer to a keystore implementation.
pub type KeystorePtr = Arc<dyn Keystore>;
sp_externalities::decl_extension! {
/// The keystore extension to register/retrieve from the externalities.
pub struct KeystoreExt(KeystorePtr);
}
impl KeystoreExt {
/// Create a new instance of `KeystoreExt`
pub fn new<T: Keystore + 'static>(keystore: T) -> Self {
Self(Arc::new(keystore))
}
}
+12 -16
View File
@@ -304,9 +304,9 @@ mod tests {
fn store_key_and_extract() {
let store = MemoryKeystore::new();
let public = Keystore::ed25519_generate_new(&store, ED25519, None).expect("Generates key");
let public = store.ed25519_generate_new(ED25519, None).expect("Generates key");
let public_keys = Keystore::keys(&store, ED25519).unwrap();
let public_keys = store.keys(ED25519).unwrap();
assert!(public_keys.contains(&public.into()));
}
@@ -318,10 +318,11 @@ mod tests {
let secret_uri = "//Alice";
let key_pair = sr25519::Pair::from_string(secret_uri, None).expect("Generates key pair");
Keystore::insert(&store, SR25519, secret_uri, key_pair.public().as_ref())
store
.insert(SR25519, secret_uri, key_pair.public().as_ref())
.expect("Inserts unknown key");
let public_keys = Keystore::keys(&store, SR25519).unwrap();
let public_keys = store.keys(SR25519).unwrap();
assert!(public_keys.contains(&key_pair.public().into()));
}
@@ -342,19 +343,14 @@ mod tests {
],
};
let result = Keystore::sr25519_vrf_sign(
&store,
SR25519,
&key_pair.public(),
transcript_data.clone(),
);
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), transcript_data.clone());
assert!(result.unwrap().is_none());
Keystore::insert(&store, SR25519, secret_uri, key_pair.public().as_ref())
store
.insert(SR25519, secret_uri, key_pair.public().as_ref())
.expect("Inserts unknown key");
let result =
Keystore::sr25519_vrf_sign(&store, SR25519, &key_pair.public(), transcript_data);
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), transcript_data);
assert!(result.unwrap().is_some());
}
@@ -369,13 +365,13 @@ mod tests {
let msg = sp_core::keccak_256(b"this should be a hashed message");
// no key in key store
let res = Keystore::ecdsa_sign_prehashed(&store, ECDSA, &pair.public(), &msg).unwrap();
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap();
assert!(res.is_none());
// insert key, sign again
Keystore::insert(&store, ECDSA, suri, pair.public().as_ref()).unwrap();
store.insert(ECDSA, suri, pair.public().as_ref()).unwrap();
let res = Keystore::ecdsa_sign_prehashed(&store, ECDSA, &pair.public(), &msg).unwrap();
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap();
assert!(res.is_some());
}
}