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 = (
25 const share = await parseShareResponse(encryptedShare);
26 if (!share) throw new Error(c('Error').t`Could not open created vault`);
31 export const editVault = async (
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 = (
45 url: `pass/v1/vault/${shareId}`,
47 data: encryptedVaultUpdate,
51 const share = await parseShareResponse(encryptedShare, { shareKeys });
52 if (!share) throw new Error(c('Error').t`Could not open updated vault`);
57 export const deleteVault = async (shareId: string) => api({ url: `pass/v1/vault/${shareId}`, method: 'delete' });
59 export const vaultTransferOwner = async ({ shareId, userShareId }: VaultTransferOwnerIntent) =>
61 url: `pass/v1/vault/${shareId}/owner`,
63 data: { NewOwnerShareID: userShareId },