@msssystems/mss-link-sdk 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ import type { DriveItemType, DriveWrappedItemKey } from './drive-sharing.contracts';
2
+ export type DriveRecipientPublicKeyAlgorithm = 'X25519';
3
+ export type DriveWrappedItemKeyAlgorithm = 'MSS-DRIVE-PUBLIC-KEY-WRAPPED-ITEM-KEY-v1';
4
+ export interface DriveRecipientPublicKey {
5
+ algorithm: DriveRecipientPublicKeyAlgorithm;
6
+ keyId: string;
7
+ publicKeyBase64: string;
8
+ }
9
+ export interface DriveRecipientKeyTarget {
10
+ accountId?: string;
11
+ userId?: string;
12
+ username?: string;
13
+ publicKey: DriveRecipientPublicKey;
14
+ }
15
+ export interface DriveItemContentKeyEnvelope {
16
+ algorithm?: 'AES-256-GCM' | string | null;
17
+ keyId?: string | null;
18
+ nonce?: string | null;
19
+ wrappedKey: string;
20
+ }
21
+ export interface DrivePublicKeyWrappingInput {
22
+ additionalData: string;
23
+ payload: DriveWrappedItemKeyPayload;
24
+ recipient: DriveRecipientKeyTarget;
25
+ }
26
+ export interface DrivePublicKeyWrappingOutput {
27
+ encryptedItemKey: string;
28
+ keyAlgorithm?: DriveWrappedItemKeyAlgorithm | string;
29
+ keyNonce?: string;
30
+ keyRecipientId?: string;
31
+ }
32
+ export interface DrivePublicKeyWrappingAdapter {
33
+ wrapDriveItemKey(input: DrivePublicKeyWrappingInput): Promise<DrivePublicKeyWrappingOutput>;
34
+ }
35
+ export interface CreateDriveWrappedItemKeyForRecipientInput {
36
+ itemId: string;
37
+ itemType: DriveItemType;
38
+ contentKey: DriveItemContentKeyEnvelope;
39
+ recipient: DriveRecipientKeyTarget;
40
+ wrappingAdapter: DrivePublicKeyWrappingAdapter;
41
+ }
42
+ export interface DriveWrappedItemKeyPayload {
43
+ contentAlgorithm: string;
44
+ contentKey: string;
45
+ contentKeyId?: string;
46
+ contentNonce?: string;
47
+ createdAt: string;
48
+ itemId: string;
49
+ itemType: DriveItemType;
50
+ schema: 'mss.link.drive.item-key.v1';
51
+ }
52
+ export interface DriveWrappedItemKeyForRecipientResult {
53
+ payload: DriveWrappedItemKeyPayload;
54
+ wrappedItemKey: DriveWrappedItemKey;
55
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { CreateDriveWrappedItemKeyForRecipientInput, DriveWrappedItemKeyForRecipientResult } from './drive-key-wrapping.contracts';
2
+ export declare function createDriveWrappedItemKeyForRecipient(input: CreateDriveWrappedItemKeyForRecipientInput): Promise<DriveWrappedItemKeyForRecipientResult>;
@@ -0,0 +1,112 @@
1
+ const CONTENT_KEY_SCHEMA = 'mss.link.drive.item-key.v1';
2
+ const WRAPPED_ITEM_KEY_ALGORITHM = 'MSS-DRIVE-PUBLIC-KEY-WRAPPED-ITEM-KEY-v1';
3
+ export async function createDriveWrappedItemKeyForRecipient(input) {
4
+ const itemId = normalizeRequiredText(input.itemId, 'Drive item id is required for key wrapping.');
5
+ const itemType = input.itemType;
6
+ const recipient = normalizeRecipient(input.recipient);
7
+ const contentKey = normalizeContentKey(input.contentKey);
8
+ const wrappingAdapter = normalizeWrappingAdapter(input.wrappingAdapter);
9
+ const payload = buildWrappedItemKeyPayload({
10
+ contentKey,
11
+ itemId,
12
+ itemType,
13
+ });
14
+ const additionalData = buildWrappingAdditionalData({
15
+ itemId,
16
+ itemType,
17
+ recipientPublicKeyId: recipient.publicKey.keyId,
18
+ });
19
+ const wrapped = await wrappingAdapter.wrapDriveItemKey({
20
+ additionalData,
21
+ payload,
22
+ recipient,
23
+ });
24
+ return {
25
+ payload,
26
+ wrappedItemKey: normalizeWrappedItemKeyOutput({
27
+ output: wrapped,
28
+ recipientPublicKeyId: recipient.publicKey.keyId,
29
+ }),
30
+ };
31
+ }
32
+ function buildWrappedItemKeyPayload({ contentKey, itemId, itemType, }) {
33
+ return {
34
+ schema: CONTENT_KEY_SCHEMA,
35
+ itemId,
36
+ itemType,
37
+ contentAlgorithm: contentKey.algorithm,
38
+ contentKey: contentKey.wrappedKey,
39
+ createdAt: new Date().toISOString(),
40
+ ...(contentKey.keyId ? { contentKeyId: contentKey.keyId } : {}),
41
+ ...(contentKey.nonce ? { contentNonce: contentKey.nonce } : {}),
42
+ };
43
+ }
44
+ function normalizeRecipient(recipient) {
45
+ const accountId = normalizeOptionalText(recipient.accountId) ?? '';
46
+ const userId = normalizeOptionalText(recipient.userId) ?? '';
47
+ const username = normalizeOptionalText(recipient.username)?.replace(/^@/, '') ?? '';
48
+ const publicKey = normalizeRecipientPublicKey(recipient.publicKey);
49
+ if (!accountId && !userId && !username) {
50
+ throw new Error('Drive share recipient must include accountId, userId or username.');
51
+ }
52
+ return {
53
+ accountId,
54
+ userId,
55
+ username,
56
+ publicKey,
57
+ };
58
+ }
59
+ function normalizeRecipientPublicKey(key) {
60
+ if (key.algorithm !== 'X25519') {
61
+ throw new Error(`Unsupported Drive recipient public key algorithm: ${key.algorithm}`);
62
+ }
63
+ return {
64
+ algorithm: key.algorithm,
65
+ keyId: normalizeRequiredText(key.keyId, 'Drive recipient public key id is required.'),
66
+ publicKeyBase64: normalizeRequiredText(key.publicKeyBase64, 'Drive recipient public key material is required.'),
67
+ };
68
+ }
69
+ function normalizeContentKey(contentKey) {
70
+ const algorithm = contentKey.algorithm?.trim() || 'AES-256-GCM';
71
+ if (algorithm !== 'AES-256-GCM') {
72
+ throw new Error(`Unsupported Drive item content key algorithm: ${algorithm}`);
73
+ }
74
+ return {
75
+ algorithm,
76
+ keyId: normalizeOptionalText(contentKey.keyId) ?? '',
77
+ nonce: normalizeOptionalText(contentKey.nonce) ?? '',
78
+ wrappedKey: normalizeRequiredText(contentKey.wrappedKey, 'Drive item content key is required.'),
79
+ };
80
+ }
81
+ function normalizeWrappingAdapter(adapter) {
82
+ if (!adapter || typeof adapter.wrapDriveItemKey !== 'function') {
83
+ throw new Error('Drive public-key wrapping adapter is not configured.');
84
+ }
85
+ return adapter;
86
+ }
87
+ function normalizeWrappedItemKeyOutput({ output, recipientPublicKeyId, }) {
88
+ const encryptedItemKey = normalizeRequiredText(output.encryptedItemKey, 'Drive public-key wrapping adapter returned empty encrypted item key.');
89
+ const keyAlgorithm = normalizeOptionalText(output.keyAlgorithm) ?? WRAPPED_ITEM_KEY_ALGORITHM;
90
+ const keyNonce = normalizeOptionalText(output.keyNonce);
91
+ const keyRecipientId = normalizeOptionalText(output.keyRecipientId) ?? recipientPublicKeyId;
92
+ return {
93
+ encryptedItemKey,
94
+ keyAlgorithm,
95
+ ...(keyNonce ? { keyNonce } : {}),
96
+ keyRecipientId,
97
+ };
98
+ }
99
+ function buildWrappingAdditionalData({ itemId, itemType, recipientPublicKeyId, }) {
100
+ return `${CONTENT_KEY_SCHEMA}:${itemType}:${itemId}:${recipientPublicKeyId}`;
101
+ }
102
+ function normalizeRequiredText(value, message) {
103
+ const normalized = normalizeOptionalText(value);
104
+ if (!normalized) {
105
+ throw new Error(message);
106
+ }
107
+ return normalized;
108
+ }
109
+ function normalizeOptionalText(value) {
110
+ const normalized = value?.trim();
111
+ return normalized ? normalized : undefined;
112
+ }
@@ -1,4 +1,4 @@
1
- const DEFAULT_KEY_ALGORITHM = 'X25519-AES-256-GCM';
1
+ const DEFAULT_KEY_ALGORITHM = 'MSS-DRIVE-PUBLIC-KEY-WRAPPED-ITEM-KEY-v1';
2
2
  export function createDriveShareGrantPayload(input) {
3
3
  return {
4
4
  ...normalizeWrappedItemKey(input.wrappedItemKey),
@@ -3,6 +3,8 @@ export * from './client-options';
3
3
  export * from './decryption-errors';
4
4
  export * from './drive-file-runtime';
5
5
  export * from './drive-file-runtime.contracts';
6
+ export * from './drive-key-wrapping';
7
+ export * from './drive-key-wrapping.contracts';
6
8
  export * from './drive-sharing';
7
9
  export * from './drive-sharing.contracts';
8
10
  export * from './local-crypto-health';
@@ -3,6 +3,8 @@ export * from './client-options';
3
3
  export * from './decryption-errors';
4
4
  export * from './drive-file-runtime';
5
5
  export * from './drive-file-runtime.contracts';
6
+ export * from './drive-key-wrapping';
7
+ export * from './drive-key-wrapping.contracts';
6
8
  export * from './drive-sharing';
7
9
  export * from './drive-sharing.contracts';
8
10
  export * from './local-crypto-health';
@@ -4,6 +4,7 @@ import type { DecryptMediaBlobInput, DecryptedMediaBlobResult, EncryptedMediaBlo
4
4
  import type { SendMediaMessageParams } from './media-message.contracts';
5
5
  import type { DecryptedMessage } from './message.contracts';
6
6
  import type { DecryptDriveFileMetadataInput, DecryptDriveFolderMetadataInput, DecryptedDriveFileMetadata, DecryptedDriveFolderMetadata, EncryptedDriveFileMetadata, EncryptedDriveFolderMetadata, EncryptDriveFileMetadataInput, EncryptDriveFolderMetadataInput } from './storage-metadata-crypto.contracts';
7
+ import type { CreateDriveWrappedItemKeyForRecipientInput, DriveWrappedItemKeyForRecipientResult } from './drive-key-wrapping.contracts';
7
8
  export declare class MssLinkBrowserClient {
8
9
  private readonly userId;
9
10
  private readonly apiBaseUrl;
@@ -34,6 +35,7 @@ export declare class MssLinkBrowserClient {
34
35
  decryptDriveFolderMetadata(input: DecryptDriveFolderMetadataInput): Promise<DecryptedDriveFolderMetadata>;
35
36
  encryptDriveFileMetadata(input: EncryptDriveFileMetadataInput): Promise<EncryptedDriveFileMetadata>;
36
37
  decryptDriveFileMetadata(input: DecryptDriveFileMetadataInput): Promise<DecryptedDriveFileMetadata>;
38
+ createDriveWrappedItemKeyForRecipient(input: CreateDriveWrappedItemKeyForRecipientInput): Promise<DriveWrappedItemKeyForRecipientResult>;
37
39
  syncMessages(): Promise<void>;
38
40
  getLocalMessages(chatId: string): Promise<DecryptedMessage[]>;
39
41
  reset(): void;
@@ -4,6 +4,7 @@ import { checkLocalCryptoHealth } from './local-crypto-health';
4
4
  import { buildMediaMessageMetadataPayload, extractMediaAssetIds, serializeMediaMessageMetadata, } from './media-message-metadata';
5
5
  import { mapLocalSdkMessage } from './message.mapper';
6
6
  import { buildDecryptedDriveFileMetadata, buildDecryptedDriveFolderMetadata, buildEncryptedDriveFileMetadata, buildEncryptedDriveFolderMetadata, normalizeDriveFileMetadataInput, normalizeDriveFolderMetadataInput, normalizeEncryptedDriveFileMetadataInput, normalizeEncryptedDriveFolderMetadataInput, } from './storage-metadata-crypto';
7
+ import { createDriveWrappedItemKeyForRecipient } from './drive-key-wrapping';
7
8
  import { WasmCallQueue } from './wasm-call-queue';
8
9
  const DEFAULT_DEVICE_ID = 1;
9
10
  const DEFAULT_REGISTRATION_ID = 1;
@@ -209,6 +210,9 @@ export class MssLinkBrowserClient {
209
210
  throw normalizeLocalCryptoError(error);
210
211
  }
211
212
  }
213
+ createDriveWrappedItemKeyForRecipient(input) {
214
+ return createDriveWrappedItemKeyForRecipient(input);
215
+ }
212
216
  async syncMessages() {
213
217
  try {
214
218
  await this.runWithClient((client) => client.syncMessages());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@msssystems/mss-link-sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Official managed application SDK for MSS ecosystem",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",