diff --git a/src/Core/Services/CipherService.cs b/src/Core/Services/CipherService.cs index aa3aa4522..13ec23209 100644 --- a/src/Core/Services/CipherService.cs +++ b/src/Core/Services/CipherService.cs @@ -578,8 +578,7 @@ namespace Bit.Core.Services //If the cipher doesn't have a key, we update it if(await ShouldUseCipherKeyEncryptionAsync() && cipherView.Key == null) { - cipher = await EncryptAsync(cipherView); - await UpdateAndUpsertAsync(() => _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher))); + await UpdateAndUpsertAsync(cipherView, cipher => _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher))); cipher = await GetAsync(cipherView.Id); cipherView = await cipher.DecryptAsync(); } @@ -596,8 +595,15 @@ namespace Bit.Core.Services await Task.WhenAll(attachmentTasks); cipherView.OrganizationId = organizationId; cipherView.CollectionIds = collectionIds; - cipher = await EncryptAsync(cipherView); - await UpdateAndUpsertAsync(() => _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds); + await UpdateAndUpsertAsync(cipherView, cipher => _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds); + + async Task UpdateAndUpsertAsync(CipherView cipherView, Func> callPutCipherApi, HashSet collectionIds = null) + { + var cipher = await EncryptAsync(cipherView); + var response = await callPutCipherApi(cipher); + var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds); + await UpsertAsync(data); + } } public async Task SaveAttachmentRawWithServerAsync(Cipher cipher, CipherView cipherView, string filename, byte[] data) @@ -1360,13 +1366,6 @@ namespace Bit.Core.Services } } - private async Task UpdateAndUpsertAsync(Func> callPutCipherApi, HashSet collectionIds = null) - { - var response = await callPutCipherApi(); - var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds); - await UpsertAsync(data); - } - private class CipherLocaleComparer : IComparer { private readonly II18nService _i18nService;