Merge branch 'IDTEAM-1.26.0' into 'main'
[ProtonMail-WebClient.git] / packages / pass / lib / vaults / vault.requests.ts
blob01788c9bc65d775df716fada0f29c9bb2c842713
1 import { c } from 'ttag';
3 import { api } from '@proton/pass/lib/api/api';
4 import { PassCrypto } from '@proton/pass/lib/crypto';
5 import { parseShareResponse } from '@proton/pass/lib/shares/share.parser';
6 import { getAllShareKeys } from '@proton/pass/lib/shares/share.requests';
7 import { encodeVaultContent } from '@proton/pass/lib/vaults/vault-proto.transformer';
8 import type { VaultTransferOwnerIntent } from '@proton/pass/types';
9 import { type Share, type ShareContent, type ShareType, type VaultCreateRequest } from '@proton/pass/types';
11 export const createVault = async (data: {
12     content: ShareContent<ShareType.Vault>;
13 }): Promise<Share<ShareType.Vault>> => {
14     const encoded = encodeVaultContent(data.content);
15     const encryptedVault: VaultCreateRequest = { ...(await PassCrypto.createVault(encoded)) };
17     const encryptedShare = (
18         await api({
19             url: 'pass/v1/vault',
20             method: 'post',
21             data: encryptedVault,
22         })
23     ).Share!;
25     const share = await parseShareResponse(encryptedShare);
26     if (!share) throw new Error(c('Error').t`Could not open created vault`);
28     return share;
31 export const editVault = async (
32     shareId: string,
33     content: ShareContent<ShareType.Vault>
34 ): Promise<Share<ShareType.Vault>> => {
35     /* Future-proofing : retrieve all share keys
36      * and update the share in the crypto context */
37     const shareKeys = await getAllShareKeys(shareId);
38     await PassCrypto.updateShareKeys({ shareId, shareKeys });
40     const encoded = encodeVaultContent(content);
41     const encryptedVaultUpdate = await PassCrypto.updateVault({ shareId, content: encoded });
43     const encryptedShare = (
44         await api({
45             url: `pass/v1/vault/${shareId}`,
46             method: 'put',
47             data: encryptedVaultUpdate,
48         })
49     ).Share!;
51     const share = await parseShareResponse(encryptedShare, { shareKeys });
52     if (!share) throw new Error(c('Error').t`Could not open updated vault`);
54     return share;
57 export const deleteVault = async (shareId: string) => api({ url: `pass/v1/vault/${shareId}`, method: 'delete' });
59 export const vaultTransferOwner = async ({ shareId, userShareId }: VaultTransferOwnerIntent) =>
60     api({
61         url: `pass/v1/vault/${shareId}/owner`,
62         method: 'put',
63         data: { NewOwnerShareID: userShareId },
64     });