@@ -329,7 +329,7 @@ impl KeystoreInner {
329329 /// Open the store at the given path.
330330 ///
331331 /// Optionally takes a password that will be used to encrypt/decrypt the keys.
332- pub fn open < T : Into < PathBuf > > ( path : T , password : Option < SecretString > ) -> Result < Self > {
332+ fn open < T : Into < PathBuf > > ( path : T , password : Option < SecretString > ) -> Result < Self > {
333333 let path = path. into ( ) ;
334334 fs:: create_dir_all ( & path) ?;
335335
@@ -345,7 +345,7 @@ impl KeystoreInner {
345345 }
346346
347347 /// Create a new in-memory store.
348- pub fn new_in_memory ( ) -> Self {
348+ fn new_in_memory ( ) -> Self {
349349 Self {
350350 path : None ,
351351 additional : HashMap :: new ( ) ,
@@ -373,8 +373,8 @@ impl KeystoreInner {
373373
374374 /// Insert a new key with anonymous crypto.
375375 ///
376- /// Places it into the file system store.
377- pub fn insert_unknown ( & self , key_type : KeyTypeId , suri : & str , public : & [ u8 ] ) -> Result < ( ) > {
376+ /// Places it into the file system store, if a path is configured .
377+ fn insert_unknown ( & self , key_type : KeyTypeId , suri : & str , public : & [ u8 ] ) -> Result < ( ) > {
378378 if let Some ( path) = self . key_file_path ( public, key_type) {
379379 let mut file = File :: create ( path) . map_err ( Error :: Io ) ?;
380380 serde_json:: to_writer ( & file, & suri) . map_err ( Error :: Json ) ?;
@@ -385,21 +385,24 @@ impl KeystoreInner {
385385
386386 /// Generate a new key.
387387 ///
388- /// Places it into the file system store.
389- pub fn generate_by_type < Pair : PairT > ( & self , key_type : KeyTypeId ) -> Result < Pair > {
388+ /// Places it into the file system store, if a path is configured. Otherwise insert
389+ /// it into the memory cache only.
390+ fn generate_by_type < Pair : PairT > ( & mut self , key_type : KeyTypeId ) -> Result < Pair > {
390391 let ( pair, phrase, _) = Pair :: generate_with_phrase ( self . password ( ) ) ;
391392 if let Some ( path) = self . key_file_path ( pair. public ( ) . as_slice ( ) , key_type) {
392393 let mut file = File :: create ( path) ?;
393394 serde_json:: to_writer ( & file, & phrase) ?;
394395 file. flush ( ) ?;
396+ } else {
397+ self . insert_ephemeral_pair ( & pair, & phrase, key_type) ;
395398 }
396399 Ok ( pair)
397400 }
398401
399402 /// Create a new key from seed.
400403 ///
401404 /// Does not place it into the file system store.
402- pub fn insert_ephemeral_from_seed_by_type < Pair : PairT > (
405+ fn insert_ephemeral_from_seed_by_type < Pair : PairT > (
403406 & mut self ,
404407 seed : & str ,
405408 key_type : KeyTypeId ,
@@ -422,7 +425,7 @@ impl KeystoreInner {
422425 }
423426
424427 /// Get a key pair for the given public key and key type.
425- pub fn key_pair_by_type < Pair : PairT > ( & self ,
428+ fn key_pair_by_type < Pair : PairT > ( & self ,
426429 public : & Pair :: Public ,
427430 key_type : KeyTypeId ,
428431 ) -> Result < Pair > {
@@ -501,6 +504,8 @@ mod tests {
501504 str:: FromStr ,
502505 } ;
503506
507+ const TEST_KEY_TYPE : KeyTypeId = KeyTypeId ( * b"test" ) ;
508+
504509 impl KeystoreInner {
505510 fn insert_ephemeral_from_seed < Pair : AppPair > ( & mut self , seed : & str ) -> Result < Pair > {
506511 self . insert_ephemeral_from_seed_by_type :: < Pair :: Generic > ( seed, Pair :: ID ) . map ( Into :: into)
@@ -515,15 +520,15 @@ mod tests {
515520 } )
516521 }
517522
518- fn generate < Pair : AppPair > ( & self ) -> Result < Pair > {
523+ fn generate < Pair : AppPair > ( & mut self ) -> Result < Pair > {
519524 self . generate_by_type :: < Pair :: Generic > ( Pair :: ID ) . map ( Into :: into)
520525 }
521526 }
522527
523528 #[ test]
524529 fn basic_store ( ) {
525530 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
526- let store = KeystoreInner :: open ( temp_dir. path ( ) , None ) . unwrap ( ) ;
531+ let mut store = KeystoreInner :: open ( temp_dir. path ( ) , None ) . unwrap ( ) ;
527532
528533 assert ! ( store. public_keys:: <ed25519:: AppPublic >( ) . unwrap( ) . is_empty( ) ) ;
529534
@@ -558,7 +563,7 @@ mod tests {
558563 fn password_being_used ( ) {
559564 let password = String :: from ( "password" ) ;
560565 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
561- let store = KeystoreInner :: open (
566+ let mut store = KeystoreInner :: open (
562567 temp_dir. path ( ) ,
563568 Some ( FromStr :: from_str ( password. as_str ( ) ) . unwrap ( ) ) ,
564569 ) . unwrap ( ) ;
@@ -640,4 +645,27 @@ mod tests {
640645 SyncCryptoStore :: sr25519_public_keys( & store, SR25519 ) . is_empty( ) ,
641646 ) ;
642647 }
648+
649+ #[ test]
650+ fn generate_with_seed_is_not_stored ( ) {
651+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
652+ let store = LocalKeystore :: open ( temp_dir. path ( ) , None ) . unwrap ( ) ;
653+ let _alice_tmp_key = SyncCryptoStore :: sr25519_generate_new ( & store, TEST_KEY_TYPE , Some ( "//Alice" ) ) . unwrap ( ) ;
654+
655+ assert_eq ! ( SyncCryptoStore :: sr25519_public_keys( & store, TEST_KEY_TYPE ) . len( ) , 1 ) ;
656+
657+ drop ( store) ;
658+ let store = LocalKeystore :: open ( temp_dir. path ( ) , None ) . unwrap ( ) ;
659+ assert_eq ! ( SyncCryptoStore :: sr25519_public_keys( & store, TEST_KEY_TYPE ) . len( ) , 0 ) ;
660+ }
661+
662+ #[ test]
663+ fn generate_can_be_fetched_in_memory ( ) {
664+ let store = LocalKeystore :: in_memory ( ) ;
665+ SyncCryptoStore :: sr25519_generate_new ( & store, TEST_KEY_TYPE , Some ( "//Alice" ) ) . unwrap ( ) ;
666+
667+ assert_eq ! ( SyncCryptoStore :: sr25519_public_keys( & store, TEST_KEY_TYPE ) . len( ) , 1 ) ;
668+ SyncCryptoStore :: sr25519_generate_new ( & store, TEST_KEY_TYPE , None ) . unwrap ( ) ;
669+ assert_eq ! ( SyncCryptoStore :: sr25519_public_keys( & store, TEST_KEY_TYPE ) . len( ) , 2 ) ;
670+ }
643671}
0 commit comments