def run_create(args): from getpass import getpass from fetchai.ledger.crypto import Entity from pocketbook.key_store import KeyStore key_store = KeyStore() existing_keys = set(key_store.list_keys()) # get the name for the new key while True: name = input('Enter name for key: ') if name in existing_keys: print('Key name already exists') continue break # prompt the user for the password while True: password = getpass('Enter password for key...: ') if not Entity.is_strong_password(password): print('Password too simple, try again') continue confirm = getpass('Confirm password for key.: ') if password != confirm: print('Passwords did not match, try again') continue break key_store.add_key(name, password, Entity())
def test_unable_to_load_saved_key(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) with self.assertRaises(UnableToDecodeKeyError): key_store.load_key('sample', '!' + SUPER_SECURE_PASSWORD)
def test_loading_saved_key(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) recovered = key_store.load_key('sample', SUPER_SECURE_PASSWORD) self.assertEqual(recovered.private_key, entity.private_key)
def test_duplicate_adds(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) with self.assertRaises(DuplicateKeyNameError): key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity)
def test_add_key(self): with TemporaryPocketBookRoot() as ctx: key_store = KeyStore(root=ctx.root) # create an entity and add it to the store entity = Entity() key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) self.assertEqual(len(key_store.list_keys()), 1)
def test_remove_reflected_on_disk(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) self.assertTrue(key_store.remove_key('sample')) self.assertKeyIsNotPresentOnDisk('sample', ctx)
def test_remove(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) self.assertTrue(key_store.remove_key('sample')) self.assertNotIn('sample', key_store.list_keys())
def test_rename_is_persisted_to_disk(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) self.assertTrue(key_store.rename_key('sample', 'sample2')) self.assertKeyIsPresentOnDisk('sample2', SUPER_SECURE_PASSWORD, entity, ctx)
def test_loading_of_multiple_stores(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store1 = KeyStore(root=ctx.root) key_store1.add_key('sample', SUPER_SECURE_PASSWORD, entity) key_store2 = KeyStore(root=ctx.root) self.assertEqual(set(key_store2.list_keys()), set(key_store1.list_keys()))
def test_address_lookup(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) expected_address = Address(entity) recovered_address = key_store.lookup_address('sample') self.assertEqual(str(expected_address), str(recovered_address))
def test_rename_failure_already_exists(self): with TemporaryPocketBookRoot() as ctx: entity1 = Entity() entity2 = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample1', SUPER_SECURE_PASSWORD, entity1) key_store.add_key('sample2', SUPER_SECURE_PASSWORD, entity2) self.assertFalse(key_store.rename_key('sample1', 'sample2'))
def test_added_key_is_flushed_to_disk(self): with TemporaryPocketBookRoot() as ctx: key_store = KeyStore(root=ctx.root) # create an entity and add it to the store entity = Entity() key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) # check the files on have been set correctly self.assertKeyIsPresentOnDisk('sample', SUPER_SECURE_PASSWORD, entity, ctx)
def test_corrupted_metadata(self): with TemporaryPocketBookRoot() as ctx: entity1 = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample1', SUPER_SECURE_PASSWORD, entity1) with patch.object(key_store, '_lookup_meta_data', return_value=None): self.assertFalse(key_store.rename_key('sample1', 'sample2'))
def test_remove_with_corrupted_files(self): with TemporaryPocketBookRoot() as ctx: entity = Entity() key_store = KeyStore(root=ctx.root) key_store.add_key('sample', SUPER_SECURE_PASSWORD, entity) # corrupt the files key_path = key_store._format_key_path('sample') self.assertTrue(os.path.isfile(key_path)) os.remove(key_path) with self.assertRaises(RuntimeError): key_store.remove_key('sample')