From 92d0d9fccbc325362ce3caf8ef1570551923ca08 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 4 Nov 2020 11:40:56 +0100 Subject: [PATCH 01/11] Export app-crypto specific keystore functions --- client/keystore/src/local.rs | 111 ++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index 856327d46f6ea..ef83dbcdd0ebc 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -38,7 +38,7 @@ use sp_keystore::{ SyncCryptoStore, vrf::{VRFTranscriptData, VRFSignature, make_transcript}, }; -use sp_application_crypto::{ed25519, sr25519, ecdsa}; +use sp_application_crypto::{ed25519, sr25519, ecdsa, AppPublic, AppPair, AppKey, IsWrappedBy}; use crate::{Result, Error}; @@ -57,6 +57,35 @@ impl LocalKeystore { let inner = KeystoreInner::new_in_memory(); Self(RwLock::new(inner)) } + + /// Generate a new key. + /// + /// Places it into the file system store. + pub fn generate(&self) -> Result { + self.0.read().generate::() + } + + /// Create a new key from seed. + /// + /// Does not place it into the file system store. + pub fn insert_ephemeral_from_seed(&self, seed: &str) -> Result { + self.0.write().insert_ephemeral_from_seed::(seed) + } + + /// Get public keys of all stored keys that match the key type. + /// + /// This will just use the type of the public key (a list of which to be returned) in order + /// to determine the key type. Unless you use a specialized application-type public key, then + /// this only give you keys registered under generic cryptography, and will not return keys + /// registered under the application type. + pub fn public_keys(&self) -> Result> { + self.0.read().public_keys::() + } + + /// Get a key pair for the given public key. + pub fn key_pair(&self, public: &::Public) -> Result { + self.0.read().key_pair::(public) + } } #[async_trait] @@ -470,36 +499,19 @@ impl KeystoreInner { Ok(public_keys) } -} - - -#[cfg(test)] -mod tests { - use super::*; - use tempfile::TempDir; - use sp_core::{ - Pair, - crypto::{IsWrappedBy, Ss58Codec}, - testing::SR25519, - }; - use sp_application_crypto::{ed25519, sr25519, AppPublic, AppKey, AppPair}; - use std::{ - fs, - str::FromStr, - }; /// Generate a new key. /// /// Places it into the file system store. - fn generate(store: &KeystoreInner) -> Result { - store.generate_by_type::(Pair::ID).map(Into::into) + pub fn generate(&self) -> Result { + self.generate_by_type::(Pair::ID).map(Into::into) } /// Create a new key from seed. /// /// Does not place it into the file system store. - fn insert_ephemeral_from_seed(store: &mut KeystoreInner, seed: &str) -> Result { - store.insert_ephemeral_from_seed_by_type::(seed, Pair::ID).map(Into::into) + pub fn insert_ephemeral_from_seed(&mut self, seed: &str) -> Result { + self.insert_ephemeral_from_seed_by_type::(seed, Pair::ID).map(Into::into) } /// Get public keys of all stored keys that match the key type. @@ -508,8 +520,8 @@ mod tests { /// to determine the key type. Unless you use a specialized application-type public key, then /// this only give you keys registered under generic cryptography, and will not return keys /// registered under the application type. - fn public_keys(store: &KeystoreInner) -> Result> { - store.raw_public_keys(Public::ID) + pub fn public_keys(&self) -> Result> { + self.raw_public_keys(Public::ID) .map(|v| { v.into_iter() .map(|k| Public::from_slice(k.as_slice())) @@ -518,23 +530,40 @@ mod tests { } /// Get a key pair for the given public key. - fn key_pair(store: &KeystoreInner, public: &::Public) -> Result { - store.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) + pub fn key_pair(&self, public: &::Public) -> Result { + self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) } +} + + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::TempDir; + use sp_core::{ + Pair, + crypto::Ss58Codec, + testing::SR25519, + }; + use sp_application_crypto::{ed25519, sr25519}; + use std::{ + fs, + str::FromStr, + }; #[test] fn basic_store() { let temp_dir = TempDir::new().unwrap(); let store = KeystoreInner::open(temp_dir.path(), None).unwrap(); - assert!(public_keys::(&store).unwrap().is_empty()); + assert!(store.public_keys::().unwrap().is_empty()); - let key: ed25519::AppPair = generate(&store).unwrap(); - let key2: ed25519::AppPair = key_pair(&store, &key.public()).unwrap(); + let key: ed25519::AppPair = store.generate().unwrap(); + let key2: ed25519::AppPair = store.key_pair(&key.public()).unwrap(); assert_eq!(key.public(), key2.public()); - assert_eq!(public_keys::(&store).unwrap()[0], key.public()); + assert_eq!(store.public_keys::().unwrap()[0], key.public()); } #[test] @@ -542,8 +571,7 @@ mod tests { let temp_dir = TempDir::new().unwrap(); let mut store = KeystoreInner::open(temp_dir.path(), None).unwrap(); - let pair: ed25519::AppPair = insert_ephemeral_from_seed( - &mut store, + let pair: ed25519::AppPair = store.insert_ephemeral_from_seed( "0x3d97c819d68f9bafa7d6e79cb991eebcd77d966c5334c0b94d9e1fa7ad0869dc" ).unwrap(); assert_eq!( @@ -554,7 +582,7 @@ mod tests { drop(store); let store = KeystoreInner::open(temp_dir.path(), None).unwrap(); // Keys generated from seed should not be persisted! - assert!(key_pair::(&store, &pair.public()).is_err()); + assert!(store.key_pair::(&pair.public()).is_err()); } #[test] @@ -566,15 +594,15 @@ mod tests { Some(FromStr::from_str(password.as_str()).unwrap()), ).unwrap(); - let pair: ed25519::AppPair = generate(&store).unwrap(); + let pair: ed25519::AppPair = store.generate().unwrap(); assert_eq!( pair.public(), - key_pair::(&store, &pair.public()).unwrap().public(), + store.key_pair::(&pair.public()).unwrap().public(), ); // Without the password the key should not be retrievable let store = KeystoreInner::open(temp_dir.path(), None).unwrap(); - assert!(key_pair::(&store, &pair.public()).is_err()); + assert!(store.key_pair::(&pair.public()).is_err()); let store = KeystoreInner::open( temp_dir.path(), @@ -582,7 +610,7 @@ mod tests { ).unwrap(); assert_eq!( pair.public(), - key_pair::(&store, &pair.public()).unwrap().public(), + store.key_pair::(&pair.public()).unwrap().public(), ); } @@ -593,18 +621,17 @@ mod tests { let mut keys = Vec::new(); for i in 0..10 { - keys.push(generate::(&store).unwrap().public()); - keys.push(insert_ephemeral_from_seed::( - &mut store, + keys.push(store.generate::().unwrap().public()); + keys.push(store.insert_ephemeral_from_seed::( &format!("0x3d97c819d68f9bafa7d6e79cb991eebcd7{}d966c5334c0b94d9e1fa7ad0869dc", i), ).unwrap().public()); } // Generate a key of a different type - generate::(&store).unwrap(); + store.generate::().unwrap(); keys.sort(); - let mut store_pubs = public_keys::(&store).unwrap(); + let mut store_pubs = store.public_keys::().unwrap(); store_pubs.sort(); assert_eq!(keys, store_pubs); From 712904b6a4c73257f9cf521462796a60ab7fdf3b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 4 Nov 2020 11:59:18 +0100 Subject: [PATCH 02/11] Also add back the insert function --- client/keystore/src/local.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index ef83dbcdd0ebc..dd2422d396fe9 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -86,6 +86,13 @@ impl LocalKeystore { pub fn key_pair(&self, public: &::Public) -> Result { self.0.read().key_pair::(public) } + + /// Insert a new key. + /// + /// Places it into the file system store. + pub fn insert(&self, suri: &str) -> Result { + self.0.read().insert(suri) + } } #[async_trait] @@ -533,6 +540,23 @@ impl KeystoreInner { pub fn key_pair(&self, public: &::Public) -> Result { self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) } + + fn insert_by_type(&self, key_type: KeyTypeId, suri: &str) -> Result { + let pair = Pair::from_string( + suri, + self.password() + ).map_err(|_| Error::InvalidSeed)?; + self.insert_unknown(key_type, suri, pair.public().as_slice()) + .map_err(|_| Error::Unavailable)?; + Ok(pair) + } + + /// Insert a new key. + /// + /// Places it into the file system store. + pub fn insert(&self, suri: &str) -> Result { + self.insert_by_type::(Pair::ID, suri).map(Into::into) + } } From d552759f8067f607d69adc972e2fb0f0a45c69b9 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 4 Nov 2020 12:27:25 +0100 Subject: [PATCH 03/11] Switch KeystoreContainer to an enum --- client/service/src/builder.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 2a4dda477ab75..6ed8a7e918f4f 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -205,12 +205,13 @@ pub type TLightClientWithBackend = Client< TRtApi, >; -/// Construct and hold different layers of Keystore wrappers -pub struct KeystoreContainer { - keystore: Arc, - sync_keystore: SyncCryptoStorePtr, +enum KeystoreContainerInner { + Local(Arc) } +/// Construct and hold different layers of Keystore wrappers +pub enum KeystoreContainer(KeystoreContainerInner); + impl KeystoreContainer { /// Construct KeystoreContainer pub fn new(config: &KeystoreConfig) -> Result { @@ -221,22 +222,31 @@ impl KeystoreContainer { )?, KeystoreConfig::InMemory => LocalKeystore::in_memory(), }); - let sync_keystore = keystore.clone() as SyncCryptoStorePtr; - Ok(Self { - keystore, - sync_keystore, - }) + Ok(Self(KeystoreContainerInner::Local(keystore))) } /// Returns an adapter to the asynchronous keystore that implements `CryptoStore` pub fn keystore(&self) -> Arc { - self.keystore.clone() + match self.0 { + KeystoreContainerInner::Local(ref keystore) => keystore.clone(), + } } /// Returns the synchrnous keystore wrapper pub fn sync_keystore(&self) -> SyncCryptoStorePtr { - self.sync_keystore.clone() + match self.0 { + KeystoreContainerInner::Local(ref keystore) => keystore.clone() as SyncCryptoStorePtr, + } + } + + /// Returns the local keystore if available + /// + /// The function will return None if the available keystore is not a local keystore. + pub fn local_keystore(&self) -> Option { + match self.0 { + KeystoreContainerInner::Local(ref keystore) => Some(self.keystore.clone()), + } } } From 838f09778e4dfb2691ae7e02fd13ae879ee91b44 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 4 Nov 2020 12:37:11 +0100 Subject: [PATCH 04/11] Only export the bare minimal for LocalKeystore and fix service compile --- client/keystore/src/local.rs | 58 ++++++++++------------------------- client/service/src/builder.rs | 4 +-- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index dd2422d396fe9..49b008bf5ea4e 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -38,7 +38,7 @@ use sp_keystore::{ SyncCryptoStore, vrf::{VRFTranscriptData, VRFSignature, make_transcript}, }; -use sp_application_crypto::{ed25519, sr25519, ecdsa, AppPublic, AppPair, AppKey, IsWrappedBy}; +use sp_application_crypto::{ed25519, sr25519, ecdsa, AppPair, AppKey, IsWrappedBy}; use crate::{Result, Error}; @@ -65,23 +65,6 @@ impl LocalKeystore { self.0.read().generate::() } - /// Create a new key from seed. - /// - /// Does not place it into the file system store. - pub fn insert_ephemeral_from_seed(&self, seed: &str) -> Result { - self.0.write().insert_ephemeral_from_seed::(seed) - } - - /// Get public keys of all stored keys that match the key type. - /// - /// This will just use the type of the public key (a list of which to be returned) in order - /// to determine the key type. Unless you use a specialized application-type public key, then - /// this only give you keys registered under generic cryptography, and will not return keys - /// registered under the application type. - pub fn public_keys(&self) -> Result> { - self.0.read().public_keys::() - } - /// Get a key pair for the given public key. pub fn key_pair(&self, public: &::Public) -> Result { self.0.read().key_pair::(public) @@ -514,28 +497,6 @@ impl KeystoreInner { self.generate_by_type::(Pair::ID).map(Into::into) } - /// Create a new key from seed. - /// - /// Does not place it into the file system store. - pub fn insert_ephemeral_from_seed(&mut self, seed: &str) -> Result { - self.insert_ephemeral_from_seed_by_type::(seed, Pair::ID).map(Into::into) - } - - /// Get public keys of all stored keys that match the key type. - /// - /// This will just use the type of the public key (a list of which to be returned) in order - /// to determine the key type. Unless you use a specialized application-type public key, then - /// this only give you keys registered under generic cryptography, and will not return keys - /// registered under the application type. - pub fn public_keys(&self) -> Result> { - self.raw_public_keys(Public::ID) - .map(|v| { - v.into_iter() - .map(|k| Public::from_slice(k.as_slice())) - .collect() - }) - } - /// Get a key pair for the given public key. pub fn key_pair(&self, public: &::Public) -> Result { self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) @@ -569,12 +530,27 @@ mod tests { crypto::Ss58Codec, testing::SR25519, }; - use sp_application_crypto::{ed25519, sr25519}; + use sp_application_crypto::{ed25519, sr25519, AppPublic}; use std::{ fs, str::FromStr, }; + impl KeystoreInner { + fn insert_ephemeral_from_seed(&mut self, seed: &str) -> Result { + self.insert_ephemeral_from_seed_by_type::(seed, Pair::ID).map(Into::into) + } + + fn public_keys(&self) -> Result> { + self.raw_public_keys(Public::ID) + .map(|v| { + v.into_iter() + .map(|k| Public::from_slice(k.as_slice())) + .collect() + }) + } + } + #[test] fn basic_store() { let temp_dir = TempDir::new().unwrap(); diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 6ed8a7e918f4f..c69fb721a1ea8 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -210,7 +210,7 @@ enum KeystoreContainerInner { } /// Construct and hold different layers of Keystore wrappers -pub enum KeystoreContainer(KeystoreContainerInner); +pub struct KeystoreContainer(KeystoreContainerInner); impl KeystoreContainer { /// Construct KeystoreContainer @@ -245,7 +245,7 @@ impl KeystoreContainer { /// The function will return None if the available keystore is not a local keystore. pub fn local_keystore(&self) -> Option { match self.0 { - KeystoreContainerInner::Local(ref keystore) => Some(self.keystore.clone()), + KeystoreContainerInner::Local(ref keystore) => Some(keystore.clone()), } } } From 1aad4f1357d9780221a3b27ac967324f31bd76f9 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 4 Nov 2020 12:42:17 +0100 Subject: [PATCH 05/11] fix: should return Arc --- client/service/src/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index c69fb721a1ea8..8548ad387f5dc 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -243,7 +243,7 @@ impl KeystoreContainer { /// Returns the local keystore if available /// /// The function will return None if the available keystore is not a local keystore. - pub fn local_keystore(&self) -> Option { + pub fn local_keystore(&self) -> Option> { match self.0 { KeystoreContainerInner::Local(ref keystore) => Some(keystore.clone()), } From 145b893b6f1579d563a3f7ee8f1fa1400eae3ad7 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 5 Nov 2020 13:14:19 +0100 Subject: [PATCH 06/11] Add docs stating that functions only available in local keystore --- client/keystore/src/local.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index 49b008bf5ea4e..b4bb8f26cf748 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -61,11 +61,17 @@ impl LocalKeystore { /// Generate a new key. /// /// Places it into the file system store. + /// + /// This function is only available for a local keystore. If your application plans to work with + /// remote keystores, you do not want to depend on it. pub fn generate(&self) -> Result { self.0.read().generate::() } /// Get a key pair for the given public key. + /// + /// This function is only available for a local keystore. If your application plans to work with + /// remote keystores, you do not want to depend on it. pub fn key_pair(&self, public: &::Public) -> Result { self.0.read().key_pair::(public) } @@ -73,6 +79,9 @@ impl LocalKeystore { /// Insert a new key. /// /// Places it into the file system store. + /// + /// This function is only available for a local keystore. If your application plans to work with + /// remote keystores, you do not want to depend on it. pub fn insert(&self, suri: &str) -> Result { self.0.read().insert(suri) } From c4881f14135e865ae8ac312f1d3f2c245314c726 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 5 Nov 2020 13:16:56 +0100 Subject: [PATCH 07/11] Remove insert and generate functions --- client/keystore/src/local.rs | 44 ------------------------------------ 1 file changed, 44 deletions(-) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index b4bb8f26cf748..c22cafb613ca7 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -58,16 +58,6 @@ impl LocalKeystore { Self(RwLock::new(inner)) } - /// Generate a new key. - /// - /// Places it into the file system store. - /// - /// This function is only available for a local keystore. If your application plans to work with - /// remote keystores, you do not want to depend on it. - pub fn generate(&self) -> Result { - self.0.read().generate::() - } - /// Get a key pair for the given public key. /// /// This function is only available for a local keystore. If your application plans to work with @@ -75,16 +65,6 @@ impl LocalKeystore { pub fn key_pair(&self, public: &::Public) -> Result { self.0.read().key_pair::(public) } - - /// Insert a new key. - /// - /// Places it into the file system store. - /// - /// This function is only available for a local keystore. If your application plans to work with - /// remote keystores, you do not want to depend on it. - pub fn insert(&self, suri: &str) -> Result { - self.0.read().insert(suri) - } } #[async_trait] @@ -499,34 +479,10 @@ impl KeystoreInner { Ok(public_keys) } - /// Generate a new key. - /// - /// Places it into the file system store. - pub fn generate(&self) -> Result { - self.generate_by_type::(Pair::ID).map(Into::into) - } - /// Get a key pair for the given public key. pub fn key_pair(&self, public: &::Public) -> Result { self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) } - - fn insert_by_type(&self, key_type: KeyTypeId, suri: &str) -> Result { - let pair = Pair::from_string( - suri, - self.password() - ).map_err(|_| Error::InvalidSeed)?; - self.insert_unknown(key_type, suri, pair.public().as_slice()) - .map_err(|_| Error::Unavailable)?; - Ok(pair) - } - - /// Insert a new key. - /// - /// Places it into the file system store. - pub fn insert(&self, suri: &str) -> Result { - self.insert_by_type::(Pair::ID, suri).map(Into::into) - } } From 064d31d0587df5de8ceadd63ae64f391a9283481 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 5 Nov 2020 13:18:39 +0100 Subject: [PATCH 08/11] fix: generate function should be available in test --- client/keystore/src/local.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index c22cafb613ca7..e0b95a08d5caf 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -514,6 +514,10 @@ mod tests { .collect() }) } + + fn generate(&self) -> Result { + self.generate_by_type::(Pair::ID).map(Into::into) + } } #[test] From ad921b09ca73d3c09298e3a51b562ef8e0067781 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 5 Nov 2020 13:50:53 +0100 Subject: [PATCH 09/11] Add keypair function to trait --- client/keystore/src/local.rs | 109 ++++++++++++++++++++--------- primitives/keystore/src/lib.rs | 51 ++++++++++++++ primitives/keystore/src/testing.rs | 63 +++++++++++++++++ 3 files changed, 189 insertions(+), 34 deletions(-) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index e0b95a08d5caf..c9bbaa750566e 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -38,7 +38,7 @@ use sp_keystore::{ SyncCryptoStore, vrf::{VRFTranscriptData, VRFSignature, make_transcript}, }; -use sp_application_crypto::{ed25519, sr25519, ecdsa, AppPair, AppKey, IsWrappedBy}; +use sp_application_crypto::{ed25519, sr25519, ecdsa}; use crate::{Result, Error}; @@ -57,14 +57,6 @@ impl LocalKeystore { let inner = KeystoreInner::new_in_memory(); Self(RwLock::new(inner)) } - - /// Get a key pair for the given public key. - /// - /// This function is only available for a local keystore. If your application plans to work with - /// remote keystores, you do not want to depend on it. - pub fn key_pair(&self, public: &::Public) -> Result { - self.0.read().key_pair::(public) - } } #[async_trait] @@ -77,6 +69,14 @@ impl CryptoStore for LocalKeystore { SyncCryptoStore::sr25519_public_keys(self, id) } + async fn sr25519_key_pair( + &self, + id: KeyTypeId, + public: &sr25519::Public, + ) -> std::result::Result { + SyncCryptoStore::sr25519_key_pair(self, id, public) + } + async fn sr25519_generate_new( &self, id: KeyTypeId, @@ -89,6 +89,14 @@ impl CryptoStore for LocalKeystore { SyncCryptoStore::ed25519_public_keys(self, id) } + async fn ed25519_key_pair( + &self, + id: KeyTypeId, + public: &ed25519::Public, + ) -> std::result::Result { + SyncCryptoStore::ed25519_key_pair(self, id, public) + } + async fn ed25519_generate_new( &self, id: KeyTypeId, @@ -101,6 +109,14 @@ impl CryptoStore for LocalKeystore { SyncCryptoStore::ecdsa_public_keys(self, id) } + async fn ecdsa_key_pair( + &self, + id: KeyTypeId, + public: &ecdsa::Public, + ) -> std::result::Result { + SyncCryptoStore::ecdsa_key_pair(self, id, public) + } + async fn ecdsa_generate_new( &self, id: KeyTypeId, @@ -182,21 +198,21 @@ impl SyncCryptoStore for LocalKeystore { ed25519::CRYPTO_ID => { let pub_key = ed25519::Public::from_slice(key.1.as_slice()); let key_pair: ed25519::Pair = self.0.read() - .key_pair_by_type::(&pub_key, id) + .key_pair_by_type::(id, &pub_key) .map_err(|e| TraitError::from(e))?; Ok(key_pair.sign(msg).encode()) } sr25519::CRYPTO_ID => { let pub_key = sr25519::Public::from_slice(key.1.as_slice()); let key_pair: sr25519::Pair = self.0.read() - .key_pair_by_type::(&pub_key, id) + .key_pair_by_type::(id, &pub_key) .map_err(|e| TraitError::from(e))?; Ok(key_pair.sign(msg).encode()) }, ecdsa::CRYPTO_ID => { let pub_key = ecdsa::Public::from_slice(key.1.as_slice()); let key_pair: ecdsa::Pair = self.0.read() - .key_pair_by_type::(&pub_key, id) + .key_pair_by_type::(id, &pub_key) .map_err(|e| TraitError::from(e))?; Ok(key_pair.sign(msg).encode()) } @@ -214,13 +230,21 @@ impl SyncCryptoStore for LocalKeystore { .unwrap_or_default() } + fn sr25519_key_pair( + &self, + id: KeyTypeId, + public: &sr25519::Public, + ) -> std::result::Result { + Ok(self.0.read().key_pair_by_type::(id, public)?) + } + fn sr25519_generate_new( &self, id: KeyTypeId, seed: Option<&str>, ) -> std::result::Result { let pair = match seed { - Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(seed, id), + Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(id, seed), None => self.0.write().generate_by_type::(id), }.map_err(|e| -> TraitError { e.into() })?; @@ -237,13 +261,21 @@ impl SyncCryptoStore for LocalKeystore { .unwrap_or_default() } + fn ed25519_key_pair( + &self, + id: KeyTypeId, + public: &ed25519::Public, + ) -> std::result::Result { + Ok(self.0.read().key_pair_by_type::(id, public)?) + } + fn ed25519_generate_new( &self, id: KeyTypeId, seed: Option<&str>, ) -> std::result::Result { let pair = match seed { - Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(seed, id), + Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(id, seed), None => self.0.write().generate_by_type::(id), }.map_err(|e| -> TraitError { e.into() })?; @@ -260,13 +292,21 @@ impl SyncCryptoStore for LocalKeystore { .unwrap_or_default() } + fn ecdsa_key_pair( + &self, + id: KeyTypeId, + public: &ecdsa::Public, + ) -> std::result::Result { + Ok(self.0.read().key_pair_by_type::(id, public)?) + } + fn ecdsa_generate_new( &self, id: KeyTypeId, seed: Option<&str>, ) -> std::result::Result { let pair = match seed { - Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(seed, id), + Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(id, seed), None => self.0.write().generate_by_type::(id), }.map_err(|e| -> TraitError { e.into() })?; @@ -280,7 +320,7 @@ impl SyncCryptoStore for LocalKeystore { } fn has_keys(&self, public_keys: &[(Vec, KeyTypeId)]) -> bool { - public_keys.iter().all(|(p, t)| self.0.read().key_phrase_by_type(&p, *t).is_ok()) + public_keys.iter().all(|(p, t)| self.0.read().key_phrase_by_type(*t, &p).is_ok()) } fn sr25519_vrf_sign( @@ -290,7 +330,7 @@ impl SyncCryptoStore for LocalKeystore { transcript_data: VRFTranscriptData, ) -> std::result::Result { let transcript = make_transcript(transcript_data); - let pair = self.0.read().key_pair_by_type::(public, key_type) + let pair = self.0.read().key_pair_by_type::(key_type, public) .map_err(|e| TraitError::PairNotFound(e.to_string()))?; let (inout, proof, _) = pair.as_ref().vrf_sign(transcript); @@ -356,8 +396,8 @@ impl KeystoreInner { /// Get the key phrase for the given public key and key type from the in-memory store. fn get_additional_pair( &self, - public: &[u8], key_type: KeyTypeId, + public: &[u8], ) -> Option<&String> { let key = (key_type, public.to_vec()); self.additional.get(&key) @@ -366,7 +406,7 @@ impl KeystoreInner { /// Insert the given public/private key pair with the given key type. /// /// Does not place it into the file system store. - fn insert_ephemeral_pair(&mut self, pair: &Pair, seed: &str, key_type: KeyTypeId) { + fn insert_ephemeral_pair(&mut self, key_type: KeyTypeId, pair: &Pair, seed: &str) { let key = (key_type, pair.public().to_raw_vec()); self.additional.insert(key, seed.into()); } @@ -401,17 +441,17 @@ impl KeystoreInner { /// Does not place it into the file system store. pub fn insert_ephemeral_from_seed_by_type( &mut self, - seed: &str, key_type: KeyTypeId, + seed: &str, ) -> Result { let pair = Pair::from_string(seed, None).map_err(|_| Error::InvalidSeed)?; - self.insert_ephemeral_pair(&pair, seed, key_type); + self.insert_ephemeral_pair(key_type, &pair, seed); Ok(pair) } /// Get the key phrase for a given public key and key type. - fn key_phrase_by_type(&self, public: &[u8], key_type: KeyTypeId) -> Result { - if let Some(phrase) = self.get_additional_pair(public, key_type) { + fn key_phrase_by_type(&self, key_type: KeyTypeId, public: &[u8]) -> Result { + if let Some(phrase) = self.get_additional_pair(key_type, public) { return Ok(phrase.clone()) } @@ -422,11 +462,12 @@ impl KeystoreInner { } /// Get a key pair for the given public key and key type. - pub fn key_pair_by_type(&self, - public: &Pair::Public, + pub fn key_pair_by_type( + &self, key_type: KeyTypeId, + public: &Pair::Public, ) -> Result { - let phrase = self.key_phrase_by_type(public.as_slice(), key_type)?; + let phrase = self.key_phrase_by_type(key_type, public.as_slice())?; let pair = Pair::from_string( &phrase, self.password(), @@ -478,11 +519,6 @@ impl KeystoreInner { Ok(public_keys) } - - /// Get a key pair for the given public key. - pub fn key_pair(&self, public: &::Public) -> Result { - self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) - } } @@ -495,7 +531,7 @@ mod tests { crypto::Ss58Codec, testing::SR25519, }; - use sp_application_crypto::{ed25519, sr25519, AppPublic}; + use sp_application_crypto::{ed25519, sr25519, AppPublic, AppPair, AppKey, IsWrappedBy}; use std::{ fs, str::FromStr, @@ -503,7 +539,7 @@ mod tests { impl KeystoreInner { fn insert_ephemeral_from_seed(&mut self, seed: &str) -> Result { - self.insert_ephemeral_from_seed_by_type::(seed, Pair::ID).map(Into::into) + self.insert_ephemeral_from_seed_by_type::(Pair::ID, seed).map(Into::into) } fn public_keys(&self) -> Result> { @@ -518,6 +554,11 @@ mod tests { fn generate(&self) -> Result { self.generate_by_type::(Pair::ID).map(Into::into) } + + /// Get a key pair for the given public key. + fn key_pair(&self, public: &::Public) -> Result { + self.key_pair_by_type::(Pair::ID, IsWrappedBy::from_ref(public)).map(Into::into) + } } #[test] @@ -621,8 +662,8 @@ mod tests { ).expect("Inserts unknown key"); let store_key_pair = store.key_pair_by_type::( - &key_pair.public(), SR25519, + &key_pair.public(), ).expect("Gets key pair from keystore"); assert_eq!(key_pair.public(), store_key_pair.public()); diff --git a/primitives/keystore/src/lib.rs b/primitives/keystore/src/lib.rs index 068c174aecdfa..b35fb3221d82a 100644 --- a/primitives/keystore/src/lib.rs +++ b/primitives/keystore/src/lib.rs @@ -53,6 +53,14 @@ pub enum Error { pub trait CryptoStore: Send + Sync { /// Returns all sr25519 public keys for the given key type. async fn sr25519_public_keys(&self, id: KeyTypeId) -> Vec; + /// Returns an sr25519 key pair given the public key, if available. + /// + /// Note that if a key is available in the keystore, its private key may still not be retrivable. + async fn sr25519_key_pair( + &self, + id: KeyTypeId, + public: &sr25519::Public, + ) -> Result; /// Generate a new sr25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -65,6 +73,14 @@ pub trait CryptoStore: Send + Sync { ) -> Result; /// Returns all ed25519 public keys for the given key type. async fn ed25519_public_keys(&self, id: KeyTypeId) -> Vec; + /// Returns an ed25519 key pair given the public key, if available. + /// + /// Note that if a key is available in the keystore, its private key may still not be retrivable. + async fn ed25519_key_pair( + &self, + id: KeyTypeId, + public: &ed25519::Public, + ) -> Result; /// Generate a new ed25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -77,6 +93,14 @@ pub trait CryptoStore: Send + Sync { ) -> Result; /// Returns all ecdsa public keys for the given key type. async fn ecdsa_public_keys(&self, id: KeyTypeId) -> Vec; + /// Returns an ecdsa key pair given the public key, if available. + /// + /// Note that if a key is available in the keystore, its private key may still not be retrivable. + async fn ecdsa_key_pair( + &self, + id: KeyTypeId, + public: &ecdsa::Public, + ) -> Result; /// Generate a new ecdsa key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -211,6 +235,15 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Returns all sr25519 public keys for the given key type. fn sr25519_public_keys(&self, id: KeyTypeId) -> Vec; + /// Returns an sr25519 key pair given the public key, if available. + /// + /// Note that if a key is available in the keystore, its private key may still not be retrivable. + fn sr25519_key_pair( + &self, + id: KeyTypeId, + public: &sr25519::Public, + ) -> Result; + /// Generate a new sr25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -225,6 +258,15 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Returns all ed25519 public keys for the given key type. fn ed25519_public_keys(&self, id: KeyTypeId) -> Vec; + /// Returns an ed25519 key pair given the public key, if available. + /// + /// Note that if a key is available in the keystore, its private key may still not be retrivable. + fn ed25519_key_pair( + &self, + id: KeyTypeId, + public: &ed25519::Public, + ) -> Result; + /// Generate a new ed25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -239,6 +281,15 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Returns all ecdsa public keys for the given key type. fn ecdsa_public_keys(&self, id: KeyTypeId) -> Vec; + /// Returns an ecdsa key pair given the public key, if available. + /// + /// Note that if a key is available in the keystore, its private key may still not be retrivable. + fn ecdsa_key_pair( + &self, + id: KeyTypeId, + public: &ecdsa::Public, + ) -> Result; + /// Generate a new ecdsa key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. diff --git a/primitives/keystore/src/testing.rs b/primitives/keystore/src/testing.rs index a5e460951493b..c049e29def19e 100644 --- a/primitives/keystore/src/testing.rs +++ b/primitives/keystore/src/testing.rs @@ -79,6 +79,14 @@ impl CryptoStore for KeyStore { SyncCryptoStore::sr25519_public_keys(self, id) } + async fn sr25519_key_pair( + &self, + id: KeyTypeId, + public: &sr25519::Public, + ) -> Result { + SyncCryptoStore::sr25519_key_pair(self, id, public) + } + async fn sr25519_generate_new( &self, id: KeyTypeId, @@ -91,6 +99,14 @@ impl CryptoStore for KeyStore { SyncCryptoStore::ed25519_public_keys(self, id) } + async fn ed25519_key_pair( + &self, + id: KeyTypeId, + public: &ed25519::Public, + ) -> Result { + SyncCryptoStore::ed25519_key_pair(self, id, public) + } + async fn ed25519_generate_new( &self, id: KeyTypeId, @@ -103,6 +119,14 @@ impl CryptoStore for KeyStore { SyncCryptoStore::ecdsa_public_keys(self, id) } + async fn ecdsa_key_pair( + &self, + id: KeyTypeId, + public: &ecdsa::Public, + ) -> Result { + SyncCryptoStore::ecdsa_key_pair(self, id, public) + } + async fn ecdsa_generate_new( &self, id: KeyTypeId, @@ -173,6 +197,19 @@ impl SyncCryptoStore for KeyStore { .unwrap_or_default() } + fn sr25519_key_pair( + &self, + id: KeyTypeId, + public: &sr25519::Public, + ) -> Result { + let p: &[u8] = public.as_ref(); + + self.keys.read().get(&id) + .and_then(|keys| keys.get(p)) + .map(|pri| sr25519::Pair::from_string(pri, None).expect("`sr25519` seed slice is valid")) + .ok_or(Error::Unavailable) + } + fn sr25519_generate_new( &self, id: KeyTypeId, @@ -204,6 +241,19 @@ impl SyncCryptoStore for KeyStore { .unwrap_or_default() } + fn ed25519_key_pair( + &self, + id: KeyTypeId, + public: &ed25519::Public, + ) -> Result { + let p: &[u8] = public.as_ref(); + + self.keys.read().get(&id) + .and_then(|keys| keys.get(p)) + .map(|pri| ed25519::Pair::from_string(pri, None).expect("`ed25519` seed slice is valid")) + .ok_or(Error::Unavailable) + } + fn ed25519_generate_new( &self, id: KeyTypeId, @@ -235,6 +285,19 @@ impl SyncCryptoStore for KeyStore { .unwrap_or_default() } + fn ecdsa_key_pair( + &self, + id: KeyTypeId, + public: &ecdsa::Public, + ) -> Result { + let p: &[u8] = public.as_ref(); + + self.keys.read().get(&id) + .and_then(|keys| keys.get(p)) + .map(|pri| ecdsa::Pair::from_string(pri, None).expect("`ecdsa` seed slice is valid")) + .ok_or(Error::Unavailable) + } + fn ecdsa_generate_new( &self, id: KeyTypeId, From 7ab4e8eeb10147de0ad96f36057d959a4a8c4ec4 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 11 Nov 2020 10:14:01 +0100 Subject: [PATCH 10/11] Revert "Add keypair function to trait" This reverts commit ad921b09ca73d3c09298e3a51b562ef8e0067781. --- client/keystore/src/local.rs | 109 +++++++++-------------------- primitives/keystore/src/lib.rs | 51 -------------- primitives/keystore/src/testing.rs | 63 ----------------- 3 files changed, 34 insertions(+), 189 deletions(-) diff --git a/client/keystore/src/local.rs b/client/keystore/src/local.rs index c9bbaa750566e..e0b95a08d5caf 100644 --- a/client/keystore/src/local.rs +++ b/client/keystore/src/local.rs @@ -38,7 +38,7 @@ use sp_keystore::{ SyncCryptoStore, vrf::{VRFTranscriptData, VRFSignature, make_transcript}, }; -use sp_application_crypto::{ed25519, sr25519, ecdsa}; +use sp_application_crypto::{ed25519, sr25519, ecdsa, AppPair, AppKey, IsWrappedBy}; use crate::{Result, Error}; @@ -57,6 +57,14 @@ impl LocalKeystore { let inner = KeystoreInner::new_in_memory(); Self(RwLock::new(inner)) } + + /// Get a key pair for the given public key. + /// + /// This function is only available for a local keystore. If your application plans to work with + /// remote keystores, you do not want to depend on it. + pub fn key_pair(&self, public: &::Public) -> Result { + self.0.read().key_pair::(public) + } } #[async_trait] @@ -69,14 +77,6 @@ impl CryptoStore for LocalKeystore { SyncCryptoStore::sr25519_public_keys(self, id) } - async fn sr25519_key_pair( - &self, - id: KeyTypeId, - public: &sr25519::Public, - ) -> std::result::Result { - SyncCryptoStore::sr25519_key_pair(self, id, public) - } - async fn sr25519_generate_new( &self, id: KeyTypeId, @@ -89,14 +89,6 @@ impl CryptoStore for LocalKeystore { SyncCryptoStore::ed25519_public_keys(self, id) } - async fn ed25519_key_pair( - &self, - id: KeyTypeId, - public: &ed25519::Public, - ) -> std::result::Result { - SyncCryptoStore::ed25519_key_pair(self, id, public) - } - async fn ed25519_generate_new( &self, id: KeyTypeId, @@ -109,14 +101,6 @@ impl CryptoStore for LocalKeystore { SyncCryptoStore::ecdsa_public_keys(self, id) } - async fn ecdsa_key_pair( - &self, - id: KeyTypeId, - public: &ecdsa::Public, - ) -> std::result::Result { - SyncCryptoStore::ecdsa_key_pair(self, id, public) - } - async fn ecdsa_generate_new( &self, id: KeyTypeId, @@ -198,21 +182,21 @@ impl SyncCryptoStore for LocalKeystore { ed25519::CRYPTO_ID => { let pub_key = ed25519::Public::from_slice(key.1.as_slice()); let key_pair: ed25519::Pair = self.0.read() - .key_pair_by_type::(id, &pub_key) + .key_pair_by_type::(&pub_key, id) .map_err(|e| TraitError::from(e))?; Ok(key_pair.sign(msg).encode()) } sr25519::CRYPTO_ID => { let pub_key = sr25519::Public::from_slice(key.1.as_slice()); let key_pair: sr25519::Pair = self.0.read() - .key_pair_by_type::(id, &pub_key) + .key_pair_by_type::(&pub_key, id) .map_err(|e| TraitError::from(e))?; Ok(key_pair.sign(msg).encode()) }, ecdsa::CRYPTO_ID => { let pub_key = ecdsa::Public::from_slice(key.1.as_slice()); let key_pair: ecdsa::Pair = self.0.read() - .key_pair_by_type::(id, &pub_key) + .key_pair_by_type::(&pub_key, id) .map_err(|e| TraitError::from(e))?; Ok(key_pair.sign(msg).encode()) } @@ -230,21 +214,13 @@ impl SyncCryptoStore for LocalKeystore { .unwrap_or_default() } - fn sr25519_key_pair( - &self, - id: KeyTypeId, - public: &sr25519::Public, - ) -> std::result::Result { - Ok(self.0.read().key_pair_by_type::(id, public)?) - } - fn sr25519_generate_new( &self, id: KeyTypeId, seed: Option<&str>, ) -> std::result::Result { let pair = match seed { - Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(id, seed), + Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(seed, id), None => self.0.write().generate_by_type::(id), }.map_err(|e| -> TraitError { e.into() })?; @@ -261,21 +237,13 @@ impl SyncCryptoStore for LocalKeystore { .unwrap_or_default() } - fn ed25519_key_pair( - &self, - id: KeyTypeId, - public: &ed25519::Public, - ) -> std::result::Result { - Ok(self.0.read().key_pair_by_type::(id, public)?) - } - fn ed25519_generate_new( &self, id: KeyTypeId, seed: Option<&str>, ) -> std::result::Result { let pair = match seed { - Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(id, seed), + Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(seed, id), None => self.0.write().generate_by_type::(id), }.map_err(|e| -> TraitError { e.into() })?; @@ -292,21 +260,13 @@ impl SyncCryptoStore for LocalKeystore { .unwrap_or_default() } - fn ecdsa_key_pair( - &self, - id: KeyTypeId, - public: &ecdsa::Public, - ) -> std::result::Result { - Ok(self.0.read().key_pair_by_type::(id, public)?) - } - fn ecdsa_generate_new( &self, id: KeyTypeId, seed: Option<&str>, ) -> std::result::Result { let pair = match seed { - Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(id, seed), + Some(seed) => self.0.write().insert_ephemeral_from_seed_by_type::(seed, id), None => self.0.write().generate_by_type::(id), }.map_err(|e| -> TraitError { e.into() })?; @@ -320,7 +280,7 @@ impl SyncCryptoStore for LocalKeystore { } fn has_keys(&self, public_keys: &[(Vec, KeyTypeId)]) -> bool { - public_keys.iter().all(|(p, t)| self.0.read().key_phrase_by_type(*t, &p).is_ok()) + public_keys.iter().all(|(p, t)| self.0.read().key_phrase_by_type(&p, *t).is_ok()) } fn sr25519_vrf_sign( @@ -330,7 +290,7 @@ impl SyncCryptoStore for LocalKeystore { transcript_data: VRFTranscriptData, ) -> std::result::Result { let transcript = make_transcript(transcript_data); - let pair = self.0.read().key_pair_by_type::(key_type, public) + let pair = self.0.read().key_pair_by_type::(public, key_type) .map_err(|e| TraitError::PairNotFound(e.to_string()))?; let (inout, proof, _) = pair.as_ref().vrf_sign(transcript); @@ -396,8 +356,8 @@ impl KeystoreInner { /// Get the key phrase for the given public key and key type from the in-memory store. fn get_additional_pair( &self, - key_type: KeyTypeId, public: &[u8], + key_type: KeyTypeId, ) -> Option<&String> { let key = (key_type, public.to_vec()); self.additional.get(&key) @@ -406,7 +366,7 @@ impl KeystoreInner { /// Insert the given public/private key pair with the given key type. /// /// Does not place it into the file system store. - fn insert_ephemeral_pair(&mut self, key_type: KeyTypeId, pair: &Pair, seed: &str) { + fn insert_ephemeral_pair(&mut self, pair: &Pair, seed: &str, key_type: KeyTypeId) { let key = (key_type, pair.public().to_raw_vec()); self.additional.insert(key, seed.into()); } @@ -441,17 +401,17 @@ impl KeystoreInner { /// Does not place it into the file system store. pub fn insert_ephemeral_from_seed_by_type( &mut self, - key_type: KeyTypeId, seed: &str, + key_type: KeyTypeId, ) -> Result { let pair = Pair::from_string(seed, None).map_err(|_| Error::InvalidSeed)?; - self.insert_ephemeral_pair(key_type, &pair, seed); + self.insert_ephemeral_pair(&pair, seed, key_type); Ok(pair) } /// Get the key phrase for a given public key and key type. - fn key_phrase_by_type(&self, key_type: KeyTypeId, public: &[u8]) -> Result { - if let Some(phrase) = self.get_additional_pair(key_type, public) { + fn key_phrase_by_type(&self, public: &[u8], key_type: KeyTypeId) -> Result { + if let Some(phrase) = self.get_additional_pair(public, key_type) { return Ok(phrase.clone()) } @@ -462,12 +422,11 @@ impl KeystoreInner { } /// Get a key pair for the given public key and key type. - pub fn key_pair_by_type( - &self, - key_type: KeyTypeId, + pub fn key_pair_by_type(&self, public: &Pair::Public, + key_type: KeyTypeId, ) -> Result { - let phrase = self.key_phrase_by_type(key_type, public.as_slice())?; + let phrase = self.key_phrase_by_type(public.as_slice(), key_type)?; let pair = Pair::from_string( &phrase, self.password(), @@ -519,6 +478,11 @@ impl KeystoreInner { Ok(public_keys) } + + /// Get a key pair for the given public key. + pub fn key_pair(&self, public: &::Public) -> Result { + self.key_pair_by_type::(IsWrappedBy::from_ref(public), Pair::ID).map(Into::into) + } } @@ -531,7 +495,7 @@ mod tests { crypto::Ss58Codec, testing::SR25519, }; - use sp_application_crypto::{ed25519, sr25519, AppPublic, AppPair, AppKey, IsWrappedBy}; + use sp_application_crypto::{ed25519, sr25519, AppPublic}; use std::{ fs, str::FromStr, @@ -539,7 +503,7 @@ mod tests { impl KeystoreInner { fn insert_ephemeral_from_seed(&mut self, seed: &str) -> Result { - self.insert_ephemeral_from_seed_by_type::(Pair::ID, seed).map(Into::into) + self.insert_ephemeral_from_seed_by_type::(seed, Pair::ID).map(Into::into) } fn public_keys(&self) -> Result> { @@ -554,11 +518,6 @@ mod tests { fn generate(&self) -> Result { self.generate_by_type::(Pair::ID).map(Into::into) } - - /// Get a key pair for the given public key. - fn key_pair(&self, public: &::Public) -> Result { - self.key_pair_by_type::(Pair::ID, IsWrappedBy::from_ref(public)).map(Into::into) - } } #[test] @@ -662,8 +621,8 @@ mod tests { ).expect("Inserts unknown key"); let store_key_pair = store.key_pair_by_type::( - SR25519, &key_pair.public(), + SR25519, ).expect("Gets key pair from keystore"); assert_eq!(key_pair.public(), store_key_pair.public()); diff --git a/primitives/keystore/src/lib.rs b/primitives/keystore/src/lib.rs index b35fb3221d82a..068c174aecdfa 100644 --- a/primitives/keystore/src/lib.rs +++ b/primitives/keystore/src/lib.rs @@ -53,14 +53,6 @@ pub enum Error { pub trait CryptoStore: Send + Sync { /// Returns all sr25519 public keys for the given key type. async fn sr25519_public_keys(&self, id: KeyTypeId) -> Vec; - /// Returns an sr25519 key pair given the public key, if available. - /// - /// Note that if a key is available in the keystore, its private key may still not be retrivable. - async fn sr25519_key_pair( - &self, - id: KeyTypeId, - public: &sr25519::Public, - ) -> Result; /// Generate a new sr25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -73,14 +65,6 @@ pub trait CryptoStore: Send + Sync { ) -> Result; /// Returns all ed25519 public keys for the given key type. async fn ed25519_public_keys(&self, id: KeyTypeId) -> Vec; - /// Returns an ed25519 key pair given the public key, if available. - /// - /// Note that if a key is available in the keystore, its private key may still not be retrivable. - async fn ed25519_key_pair( - &self, - id: KeyTypeId, - public: &ed25519::Public, - ) -> Result; /// Generate a new ed25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -93,14 +77,6 @@ pub trait CryptoStore: Send + Sync { ) -> Result; /// Returns all ecdsa public keys for the given key type. async fn ecdsa_public_keys(&self, id: KeyTypeId) -> Vec; - /// Returns an ecdsa key pair given the public key, if available. - /// - /// Note that if a key is available in the keystore, its private key may still not be retrivable. - async fn ecdsa_key_pair( - &self, - id: KeyTypeId, - public: &ecdsa::Public, - ) -> Result; /// Generate a new ecdsa key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -235,15 +211,6 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Returns all sr25519 public keys for the given key type. fn sr25519_public_keys(&self, id: KeyTypeId) -> Vec; - /// Returns an sr25519 key pair given the public key, if available. - /// - /// Note that if a key is available in the keystore, its private key may still not be retrivable. - fn sr25519_key_pair( - &self, - id: KeyTypeId, - public: &sr25519::Public, - ) -> Result; - /// Generate a new sr25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -258,15 +225,6 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Returns all ed25519 public keys for the given key type. fn ed25519_public_keys(&self, id: KeyTypeId) -> Vec; - /// Returns an ed25519 key pair given the public key, if available. - /// - /// Note that if a key is available in the keystore, its private key may still not be retrivable. - fn ed25519_key_pair( - &self, - id: KeyTypeId, - public: &ed25519::Public, - ) -> Result; - /// Generate a new ed25519 key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. @@ -281,15 +239,6 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync { /// Returns all ecdsa public keys for the given key type. fn ecdsa_public_keys(&self, id: KeyTypeId) -> Vec; - /// Returns an ecdsa key pair given the public key, if available. - /// - /// Note that if a key is available in the keystore, its private key may still not be retrivable. - fn ecdsa_key_pair( - &self, - id: KeyTypeId, - public: &ecdsa::Public, - ) -> Result; - /// Generate a new ecdsa key pair for the given key type and an optional seed. /// /// If the given seed is `Some(_)`, the key pair will only be stored in memory. diff --git a/primitives/keystore/src/testing.rs b/primitives/keystore/src/testing.rs index c049e29def19e..a5e460951493b 100644 --- a/primitives/keystore/src/testing.rs +++ b/primitives/keystore/src/testing.rs @@ -79,14 +79,6 @@ impl CryptoStore for KeyStore { SyncCryptoStore::sr25519_public_keys(self, id) } - async fn sr25519_key_pair( - &self, - id: KeyTypeId, - public: &sr25519::Public, - ) -> Result { - SyncCryptoStore::sr25519_key_pair(self, id, public) - } - async fn sr25519_generate_new( &self, id: KeyTypeId, @@ -99,14 +91,6 @@ impl CryptoStore for KeyStore { SyncCryptoStore::ed25519_public_keys(self, id) } - async fn ed25519_key_pair( - &self, - id: KeyTypeId, - public: &ed25519::Public, - ) -> Result { - SyncCryptoStore::ed25519_key_pair(self, id, public) - } - async fn ed25519_generate_new( &self, id: KeyTypeId, @@ -119,14 +103,6 @@ impl CryptoStore for KeyStore { SyncCryptoStore::ecdsa_public_keys(self, id) } - async fn ecdsa_key_pair( - &self, - id: KeyTypeId, - public: &ecdsa::Public, - ) -> Result { - SyncCryptoStore::ecdsa_key_pair(self, id, public) - } - async fn ecdsa_generate_new( &self, id: KeyTypeId, @@ -197,19 +173,6 @@ impl SyncCryptoStore for KeyStore { .unwrap_or_default() } - fn sr25519_key_pair( - &self, - id: KeyTypeId, - public: &sr25519::Public, - ) -> Result { - let p: &[u8] = public.as_ref(); - - self.keys.read().get(&id) - .and_then(|keys| keys.get(p)) - .map(|pri| sr25519::Pair::from_string(pri, None).expect("`sr25519` seed slice is valid")) - .ok_or(Error::Unavailable) - } - fn sr25519_generate_new( &self, id: KeyTypeId, @@ -241,19 +204,6 @@ impl SyncCryptoStore for KeyStore { .unwrap_or_default() } - fn ed25519_key_pair( - &self, - id: KeyTypeId, - public: &ed25519::Public, - ) -> Result { - let p: &[u8] = public.as_ref(); - - self.keys.read().get(&id) - .and_then(|keys| keys.get(p)) - .map(|pri| ed25519::Pair::from_string(pri, None).expect("`ed25519` seed slice is valid")) - .ok_or(Error::Unavailable) - } - fn ed25519_generate_new( &self, id: KeyTypeId, @@ -285,19 +235,6 @@ impl SyncCryptoStore for KeyStore { .unwrap_or_default() } - fn ecdsa_key_pair( - &self, - id: KeyTypeId, - public: &ecdsa::Public, - ) -> Result { - let p: &[u8] = public.as_ref(); - - self.keys.read().get(&id) - .and_then(|keys| keys.get(p)) - .map(|pri| ecdsa::Pair::from_string(pri, None).expect("`ecdsa` seed slice is valid")) - .ok_or(Error::Unavailable) - } - fn ecdsa_generate_new( &self, id: KeyTypeId, From 4b748467a8dfc4428bf83931df697fd9b0ba5233 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 11 Nov 2020 10:16:20 +0100 Subject: [PATCH 11/11] Add note for local_keystore function in service --- client/service/src/builder.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 8548ad387f5dc..7d613f2bc6292 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -243,6 +243,11 @@ impl KeystoreContainer { /// Returns the local keystore if available /// /// The function will return None if the available keystore is not a local keystore. + /// + /// # Note + /// + /// Using the [`LocalKeystore`] will result in loosing the ability to use any other keystore implementation, like + /// a remote keystore for example. Only use this if you a certain that you require it! pub fn local_keystore(&self) -> Option> { match self.0 { KeystoreContainerInner::Local(ref keystore) => Some(keystore.clone()),