@mysten/seal 0.4.3 → 0.4.5

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/cjs/client.d.ts +5 -5
  3. package/dist/cjs/client.js +74 -65
  4. package/dist/cjs/client.js.map +2 -2
  5. package/dist/cjs/decrypt.js +14 -21
  6. package/dist/cjs/decrypt.js.map +2 -2
  7. package/dist/cjs/dem.js +23 -21
  8. package/dist/cjs/dem.js.map +2 -2
  9. package/dist/cjs/elgamal.d.ts +1 -1
  10. package/dist/cjs/elgamal.js +5 -5
  11. package/dist/cjs/elgamal.js.map +2 -2
  12. package/dist/cjs/encrypt.js +20 -8
  13. package/dist/cjs/encrypt.js.map +2 -2
  14. package/dist/cjs/error.d.ts +6 -0
  15. package/dist/cjs/error.js +9 -0
  16. package/dist/cjs/error.js.map +2 -2
  17. package/dist/cjs/ibe.d.ts +4 -7
  18. package/dist/cjs/ibe.js +10 -5
  19. package/dist/cjs/ibe.js.map +2 -2
  20. package/dist/cjs/kdf.d.ts +19 -3
  21. package/dist/cjs/kdf.js +28 -12
  22. package/dist/cjs/kdf.js.map +2 -2
  23. package/dist/cjs/key-server.d.ts +1 -1
  24. package/dist/cjs/key-server.js +1 -1
  25. package/dist/cjs/key-server.js.map +2 -2
  26. package/dist/cjs/keys.js +1 -1
  27. package/dist/cjs/keys.js.map +2 -2
  28. package/dist/cjs/session-key.d.ts +5 -2
  29. package/dist/cjs/session-key.js +10 -8
  30. package/dist/cjs/session-key.js.map +2 -2
  31. package/dist/cjs/utils.d.ts +4 -3
  32. package/dist/cjs/utils.js +6 -2
  33. package/dist/cjs/utils.js.map +3 -3
  34. package/dist/cjs/version.d.ts +1 -1
  35. package/dist/cjs/version.js +1 -1
  36. package/dist/cjs/version.js.map +1 -1
  37. package/dist/esm/client.d.ts +5 -5
  38. package/dist/esm/client.js +79 -68
  39. package/dist/esm/client.js.map +2 -2
  40. package/dist/esm/decrypt.js +16 -23
  41. package/dist/esm/decrypt.js.map +2 -2
  42. package/dist/esm/dem.js +25 -23
  43. package/dist/esm/dem.js.map +2 -2
  44. package/dist/esm/elgamal.d.ts +1 -1
  45. package/dist/esm/elgamal.js +5 -5
  46. package/dist/esm/elgamal.js.map +2 -2
  47. package/dist/esm/encrypt.js +21 -9
  48. package/dist/esm/encrypt.js.map +2 -2
  49. package/dist/esm/error.d.ts +6 -0
  50. package/dist/esm/error.js +9 -0
  51. package/dist/esm/error.js.map +2 -2
  52. package/dist/esm/ibe.d.ts +4 -7
  53. package/dist/esm/ibe.js +12 -7
  54. package/dist/esm/ibe.js.map +2 -2
  55. package/dist/esm/kdf.d.ts +19 -3
  56. package/dist/esm/kdf.js +28 -12
  57. package/dist/esm/kdf.js.map +2 -2
  58. package/dist/esm/key-server.d.ts +1 -1
  59. package/dist/esm/key-server.js +2 -2
  60. package/dist/esm/key-server.js.map +2 -2
  61. package/dist/esm/keys.js +1 -1
  62. package/dist/esm/keys.js.map +2 -2
  63. package/dist/esm/session-key.d.ts +5 -2
  64. package/dist/esm/session-key.js +10 -8
  65. package/dist/esm/session-key.js.map +2 -2
  66. package/dist/esm/utils.d.ts +4 -3
  67. package/dist/esm/utils.js +6 -2
  68. package/dist/esm/utils.js.map +3 -3
  69. package/dist/esm/version.d.ts +1 -1
  70. package/dist/esm/version.js +1 -1
  71. package/dist/esm/version.js.map +1 -1
  72. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  73. package/dist/tsconfig.tsbuildinfo +1 -1
  74. package/package.json +2 -2
@@ -6,7 +6,7 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
6
6
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
7
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
8
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
- var _suiClient, _serverObjectIds, _verifyKeyServers, _keyServers, _cachedKeys, _timeout, _SealClient_instances, createEncryptionInput_fn, validateEncryptionServices_fn, loadKeyServers_fn;
9
+ var _suiClient, _weights, _keyServers, _verifyKeyServers, _cachedKeys, _timeout, _totalWeight, _SealClient_instances, createEncryptionInput_fn, weight_fn, validateEncryptionServices_fn, getWeightedKeyServers_fn, loadKeyServers_fn;
10
10
  import { EncryptedObject } from "./bcs.js";
11
11
  import { G1Element, G2Element } from "./bls12381.js";
12
12
  import { decrypt } from "./decrypt.js";
@@ -14,11 +14,13 @@ import { AesGcm256, Hmac256Ctr } from "./dem.js";
14
14
  import { DemType, encrypt, KemType } from "./encrypt.js";
15
15
  import {
16
16
  InconsistentKeyServersError,
17
+ InvalidClientOptionsError,
17
18
  InvalidKeyServerError,
18
19
  InvalidThresholdError,
19
- toMajorityError
20
+ toMajorityError,
21
+ TooManyFailedFetchKeyRequestsError
20
22
  } from "./error.js";
21
- import { BonehFranklinBLS12381Services, DST } from "./ibe.js";
23
+ import { BonehFranklinBLS12381Services } from "./ibe.js";
22
24
  import {
23
25
  BonehFranklinBLS12381DerivedKey,
24
26
  KeyServerType,
@@ -26,19 +28,24 @@ import {
26
28
  verifyKeyServer
27
29
  } from "./key-server.js";
28
30
  import { fetchKeysForAllIds } from "./keys.js";
29
- import { createFullId } from "./utils.js";
31
+ import { createFullId, count } from "./utils.js";
30
32
  const _SealClient = class _SealClient {
31
33
  constructor(options) {
32
34
  __privateAdd(this, _SealClient_instances);
33
35
  __privateAdd(this, _suiClient);
34
- __privateAdd(this, _serverObjectIds);
35
- __privateAdd(this, _verifyKeyServers);
36
+ __privateAdd(this, _weights);
36
37
  __privateAdd(this, _keyServers, null);
38
+ __privateAdd(this, _verifyKeyServers);
37
39
  // A caching map for: fullId:object_id -> partial key.
38
40
  __privateAdd(this, _cachedKeys, /* @__PURE__ */ new Map());
39
41
  __privateAdd(this, _timeout);
42
+ __privateAdd(this, _totalWeight);
40
43
  __privateSet(this, _suiClient, options.suiClient);
41
- __privateSet(this, _serverObjectIds, options.serverObjectIds);
44
+ if (new Set(options.serverObjectIds.map(([objectId, _]) => objectId)).size !== options.serverObjectIds.length) {
45
+ throw new InvalidClientOptionsError("Duplicate object IDs");
46
+ }
47
+ __privateSet(this, _weights, new Map(options.serverObjectIds));
48
+ __privateSet(this, _totalWeight, options.serverObjectIds.map(([_, weight]) => weight).reduce((sum, term) => sum + term, 0));
42
49
  __privateSet(this, _verifyKeyServers, options.verifyKeyServers ?? true);
43
50
  __privateSet(this, _timeout, options.timeout ?? 1e4);
44
51
  }
@@ -76,7 +83,7 @@ const _SealClient = class _SealClient {
76
83
  aad = new Uint8Array()
77
84
  }) {
78
85
  return encrypt({
79
- keyServers: await this.getKeyServers(),
86
+ keyServers: await __privateMethod(this, _SealClient_instances, getWeightedKeyServers_fn).call(this),
80
87
  kemType,
81
88
  threshold,
82
89
  packageId,
@@ -88,8 +95,7 @@ const _SealClient = class _SealClient {
88
95
  * Decrypt the given encrypted bytes using cached keys.
89
96
  * Calls fetchKeys in case one or more of the required keys is not cached yet.
90
97
  * The function throws an error if the client's key servers are not a subset of
91
- * the encrypted object's key servers (including the same weights) or if the
92
- * threshold cannot be met.
98
+ * the encrypted object's key servers or if the threshold cannot be met.
93
99
  *
94
100
  * @param data - The encrypted bytes to decrypt.
95
101
  * @param sessionKey - The session key to use.
@@ -138,32 +144,29 @@ const _SealClient = class _SealClient {
138
144
  sessionKey,
139
145
  threshold
140
146
  }) {
141
- const keyServers = await this.getKeyServers();
142
- if (threshold > keyServers.length || threshold < 1 || keyServers.length < 1) {
147
+ if (threshold > __privateGet(this, _totalWeight) || threshold < 1) {
143
148
  throw new InvalidThresholdError(
144
- `Invalid threshold ${threshold} for ${keyServers.length} servers`
149
+ `Invalid threshold ${threshold} servers with weights ${__privateGet(this, _weights)}`
145
150
  );
146
151
  }
147
- let completedServerCount = 0;
148
- const remainingKeyServers = /* @__PURE__ */ new Set();
149
- const fullIds = ids.map((id) => createFullId(DST, sessionKey.getPackageId(), id));
150
- for (const server of keyServers) {
151
- let hasAllKeys = true;
152
- for (const fullId of fullIds) {
153
- if (!__privateGet(this, _cachedKeys).has(`${fullId}:${server.objectId}`)) {
154
- hasAllKeys = false;
155
- remainingKeyServers.add(server);
156
- break;
157
- }
158
- }
159
- if (hasAllKeys) {
160
- completedServerCount++;
152
+ const keyServers = await this.getKeyServers();
153
+ const fullIds = ids.map((id) => createFullId(sessionKey.getPackageId(), id));
154
+ let completedWeight = 0;
155
+ const remainingKeyServers = [];
156
+ let remainingKeyServersWeight = 0;
157
+ for (const objectId of keyServers.keys()) {
158
+ if (fullIds.every((fullId) => __privateGet(this, _cachedKeys).has(`${fullId}:${objectId}`))) {
159
+ completedWeight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
160
+ } else {
161
+ remainingKeyServers.push(objectId);
162
+ remainingKeyServersWeight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
161
163
  }
162
164
  }
163
- if (completedServerCount >= threshold) {
165
+ if (completedWeight >= threshold) {
164
166
  return;
165
167
  }
166
- for (const server of remainingKeyServers) {
168
+ for (const objectId of remainingKeyServers) {
169
+ const server = keyServers.get(objectId);
167
170
  if (server.keyType !== KeyServerType.BonehFranklinBLS12381) {
168
171
  throw new InvalidKeyServerError(
169
172
  `Server ${server.objectId} has invalid key type: ${server.keyType}`
@@ -174,7 +177,8 @@ const _SealClient = class _SealClient {
174
177
  const signedRequest = await sessionKey.createRequestParams(txBytes);
175
178
  const controller = new AbortController();
176
179
  const errors = [];
177
- const keyFetches = [...remainingKeyServers].map(async (server) => {
180
+ const keyFetches = remainingKeyServers.map(async (objectId) => {
181
+ const server = keyServers.get(objectId);
178
182
  try {
179
183
  const allKeys = await fetchKeysForAllIds(
180
184
  server.url,
@@ -185,7 +189,6 @@ const _SealClient = class _SealClient {
185
189
  __privateGet(this, _timeout),
186
190
  controller.signal
187
191
  );
188
- const receivedIds = /* @__PURE__ */ new Set();
189
192
  for (const { fullId, key } of allKeys) {
190
193
  const keyElement = G1Element.fromBytes(key);
191
194
  if (!BonehFranklinBLS12381Services.verifyUserSecretKey(
@@ -197,13 +200,10 @@ const _SealClient = class _SealClient {
197
200
  continue;
198
201
  }
199
202
  __privateGet(this, _cachedKeys).set(`${fullId}:${server.objectId}`, keyElement);
200
- receivedIds.add(fullId);
201
203
  }
202
- const expectedIds = new Set(fullIds);
203
- const hasAllKeys = receivedIds.size === expectedIds.size && [...receivedIds].every((id) => expectedIds.has(id));
204
- if (hasAllKeys) {
205
- completedServerCount++;
206
- if (completedServerCount >= threshold) {
204
+ if (fullIds.every((fullId) => __privateGet(this, _cachedKeys).has(`${fullId}:${server.objectId}`))) {
205
+ completedWeight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
206
+ if (completedWeight >= threshold) {
207
207
  controller.abort();
208
208
  }
209
209
  }
@@ -211,13 +211,15 @@ const _SealClient = class _SealClient {
211
211
  if (!controller.signal.aborted) {
212
212
  errors.push(error);
213
213
  }
214
- if (remainingKeyServers.size - errors.length < threshold - completedServerCount) {
215
- controller.abort(error);
214
+ } finally {
215
+ remainingKeyServersWeight -= __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
216
+ if (remainingKeyServersWeight < threshold - completedWeight) {
217
+ controller.abort(new TooManyFailedFetchKeyRequestsError());
216
218
  }
217
219
  }
218
220
  });
219
221
  await Promise.allSettled(keyFetches);
220
- if (completedServerCount < threshold) {
222
+ if (completedWeight < threshold) {
221
223
  throw toMajorityError(errors);
222
224
  }
223
225
  }
@@ -240,9 +242,9 @@ const _SealClient = class _SealClient {
240
242
  switch (kemType) {
241
243
  case KemType.BonehFranklinBLS12381DemCCA:
242
244
  const keyServers = await this.getKeyServers();
243
- if (threshold > __privateGet(this, _serverObjectIds).length) {
245
+ if (threshold > __privateGet(this, _totalWeight)) {
244
246
  throw new InvalidThresholdError(
245
- `Invalid threshold ${threshold} for ${__privateGet(this, _serverObjectIds).length} servers`
247
+ `Invalid threshold ${threshold} for ${__privateGet(this, _totalWeight)} servers`
246
248
  );
247
249
  }
248
250
  await this.fetchKeys({
@@ -251,14 +253,15 @@ const _SealClient = class _SealClient {
251
253
  sessionKey,
252
254
  threshold
253
255
  });
254
- const fullId = createFullId(DST, sessionKey.getPackageId(), id);
256
+ const fullId = createFullId(sessionKey.getPackageId(), id);
255
257
  const derivedKeys = /* @__PURE__ */ new Map();
256
- let servicesAdded = 0;
257
- for (const keyServer of keyServers) {
258
- const cachedKey = __privateGet(this, _cachedKeys).get(`${fullId}:${keyServer.objectId}`);
258
+ let weight = 0;
259
+ for (const objectId of keyServers.keys()) {
260
+ const cachedKey = __privateGet(this, _cachedKeys).get(`${fullId}:${objectId}`);
259
261
  if (cachedKey) {
260
- derivedKeys.set(keyServer.objectId, new BonehFranklinBLS12381DerivedKey(cachedKey));
261
- if (++servicesAdded === threshold) {
262
+ derivedKeys.set(objectId, new BonehFranklinBLS12381DerivedKey(cachedKey));
263
+ weight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
264
+ if (weight >= threshold) {
262
265
  break;
263
266
  }
264
267
  }
@@ -268,11 +271,12 @@ const _SealClient = class _SealClient {
268
271
  }
269
272
  };
270
273
  _suiClient = new WeakMap();
271
- _serverObjectIds = new WeakMap();
272
- _verifyKeyServers = new WeakMap();
274
+ _weights = new WeakMap();
273
275
  _keyServers = new WeakMap();
276
+ _verifyKeyServers = new WeakMap();
274
277
  _cachedKeys = new WeakMap();
275
278
  _timeout = new WeakMap();
279
+ _totalWeight = new WeakMap();
276
280
  _SealClient_instances = new WeakSet();
277
281
  createEncryptionInput_fn = function(type, data, aad) {
278
282
  switch (type) {
@@ -282,31 +286,38 @@ createEncryptionInput_fn = function(type, data, aad) {
282
286
  return new Hmac256Ctr(data, aad);
283
287
  }
284
288
  };
289
+ weight_fn = function(objectId) {
290
+ return __privateGet(this, _weights).get(objectId) ?? 0;
291
+ };
285
292
  validateEncryptionServices_fn = function(services, threshold) {
286
- const serverObjectIdsMap = /* @__PURE__ */ new Map();
287
- for (const objectId of __privateGet(this, _serverObjectIds)) {
288
- serverObjectIdsMap.set(objectId, (serverObjectIdsMap.get(objectId) ?? 0) + 1);
289
- }
290
- const servicesMap = /* @__PURE__ */ new Map();
291
- for (const service of services) {
292
- servicesMap.set(service, (servicesMap.get(service) ?? 0) + 1);
293
- }
294
- for (const [objectId, count] of serverObjectIdsMap) {
295
- if (servicesMap.get(objectId) !== count) {
296
- throw new InconsistentKeyServersError(
297
- `Client's key servers must be a subset of the encrypted object's key servers`
298
- );
299
- }
293
+ if (services.some((objectId) => {
294
+ const countInClient = __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
295
+ return countInClient > 0 && countInClient !== count(services, objectId);
296
+ })) {
297
+ throw new InconsistentKeyServersError(
298
+ `Client's key servers must be a subset of the encrypted object's key servers`
299
+ );
300
300
  }
301
- if (threshold > __privateGet(this, _serverObjectIds).length) {
301
+ if (threshold > __privateGet(this, _totalWeight)) {
302
302
  throw new InvalidThresholdError(
303
- `Invalid threshold ${threshold} for ${__privateGet(this, _serverObjectIds).length} servers`
303
+ `Invalid threshold ${threshold} for ${__privateGet(this, _totalWeight)} servers`
304
304
  );
305
305
  }
306
306
  };
307
+ getWeightedKeyServers_fn = async function() {
308
+ const keyServers = await this.getKeyServers();
309
+ const keyServersWithMultiplicity = [];
310
+ for (const [objectId, weight] of __privateGet(this, _weights)) {
311
+ const keyServer = keyServers.get(objectId);
312
+ for (let i = 0; i < weight; i++) {
313
+ keyServersWithMultiplicity.push(keyServer);
314
+ }
315
+ }
316
+ return keyServersWithMultiplicity;
317
+ };
307
318
  loadKeyServers_fn = async function() {
308
319
  const keyServers = await retrieveKeyServers({
309
- objectIds: __privateGet(this, _serverObjectIds),
320
+ objectIds: [...__privateGet(this, _weights)].map(([objectId]) => objectId),
310
321
  client: __privateGet(this, _suiClient)
311
322
  });
312
323
  if (keyServers.length === 0) {
@@ -321,7 +332,7 @@ loadKeyServers_fn = async function() {
321
332
  })
322
333
  );
323
334
  }
324
- return keyServers;
335
+ return new Map(keyServers.map((server) => [server.objectId, server]));
325
336
  };
326
337
  let SealClient = _SealClient;
327
338
  export {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/client.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { EncryptedObject } from './bcs.js';\nimport { G1Element, G2Element } from './bls12381.js';\nimport { decrypt } from './decrypt.js';\nimport type { EncryptionInput } from './dem.js';\nimport { AesGcm256, Hmac256Ctr } from './dem.js';\nimport { DemType, encrypt, KemType } from './encrypt.js';\nimport {\n\tInconsistentKeyServersError,\n\tInvalidKeyServerError,\n\tInvalidThresholdError,\n\ttoMajorityError,\n} from './error.js';\nimport { BonehFranklinBLS12381Services, DST } from './ibe.js';\nimport {\n\tBonehFranklinBLS12381DerivedKey,\n\tKeyServerType,\n\tretrieveKeyServers,\n\tverifyKeyServer,\n} from './key-server.js';\nimport type { DerivedKey, KeyServer } from './key-server.js';\nimport { fetchKeysForAllIds } from './keys.js';\nimport type { SessionKey } from './session-key.js';\nimport type { KeyCacheKey, SealCompatibleClient } from './types.js';\nimport { createFullId } from './utils.js';\n\n/**\n * Configuration options for initializing a SealClient\n * @property serverObjectIds: Array of object IDs for the key servers to use.\n * @property verifyKeyServers: Whether to verify the key servers' authenticity.\n * \t Should be false if servers are pre-verified (e.g., getAllowlistedKeyServers).\n * \t Defaults to true.\n * @property timeout: Timeout in milliseconds for network requests. Defaults to 10 seconds.\n */\n\nexport interface SealClientExtensionOptions {\n\tserverObjectIds: string[];\n\tverifyKeyServers?: boolean;\n\ttimeout?: number;\n}\n\nexport interface SealClientOptions extends SealClientExtensionOptions {\n\tsuiClient: SealCompatibleClient;\n}\n\nexport class SealClient {\n\t#suiClient: SealCompatibleClient;\n\t#serverObjectIds: string[];\n\t#verifyKeyServers: boolean;\n\t#keyServers: Promise<KeyServer[]> | null = null;\n\t// A caching map for: fullId:object_id -> partial key.\n\t#cachedKeys = new Map<KeyCacheKey, G1Element>();\n\t#timeout: number;\n\n\tconstructor(options: SealClientOptions) {\n\t\tthis.#suiClient = options.suiClient;\n\t\tthis.#serverObjectIds = options.serverObjectIds;\n\t\tthis.#verifyKeyServers = options.verifyKeyServers ?? true;\n\t\tthis.#timeout = options.timeout ?? 10_000;\n\t}\n\n\tstatic experimental_asClientExtension(options: SealClientExtensionOptions) {\n\t\treturn {\n\t\t\tname: 'seal' as const,\n\t\t\tregister: (client: SealCompatibleClient) => {\n\t\t\t\treturn new SealClient({\n\t\t\t\t\tsuiClient: client,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Return an encrypted message under the identity.\n\t *\n\t * @param kemType - The type of KEM to use.\n\t * @param demType - The type of DEM to use.\n\t * @param threshold - The threshold for the TSS encryption.\n\t * @param packageId - the packageId namespace.\n\t * @param id - the identity to use.\n\t * @param data - the data to encrypt.\n\t * @param aad - optional additional authenticated data.\n\t * @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.\n\t * \tSince the symmetric key can be used to decrypt, it should not be shared but can be used e.g. for backup.\n\t */\n\tasync encrypt({\n\t\tkemType = KemType.BonehFranklinBLS12381DemCCA,\n\t\tdemType = DemType.AesGcm256,\n\t\tthreshold,\n\t\tpackageId,\n\t\tid,\n\t\tdata,\n\t\taad = new Uint8Array(),\n\t}: {\n\t\tkemType?: KemType;\n\t\tdemType?: DemType;\n\t\tthreshold: number;\n\t\tpackageId: string;\n\t\tid: string;\n\t\tdata: Uint8Array;\n\t\taad?: Uint8Array;\n\t}) {\n\t\t// TODO: Verify that packageId is first version of its package (else throw error).\n\t\treturn encrypt({\n\t\t\tkeyServers: await this.getKeyServers(),\n\t\t\tkemType,\n\t\t\tthreshold,\n\t\t\tpackageId,\n\t\t\tid,\n\t\t\tencryptionInput: this.#createEncryptionInput(demType, data, aad),\n\t\t});\n\t}\n\n\t#createEncryptionInput(type: DemType, data: Uint8Array, aad: Uint8Array): EncryptionInput {\n\t\tswitch (type) {\n\t\t\tcase DemType.AesGcm256:\n\t\t\t\treturn new AesGcm256(data, aad);\n\t\t\tcase DemType.Hmac256Ctr:\n\t\t\t\treturn new Hmac256Ctr(data, aad);\n\t\t}\n\t}\n\n\t/**\n\t * Decrypt the given encrypted bytes using cached keys.\n\t * Calls fetchKeys in case one or more of the required keys is not cached yet.\n\t * The function throws an error if the client's key servers are not a subset of\n\t * the encrypted object's key servers (including the same weights) or if the\n\t * threshold cannot be met.\n\t *\n\t * @param data - The encrypted bytes to decrypt.\n\t * @param sessionKey - The session key to use.\n\t * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).\n\t * @returns - The decrypted plaintext corresponding to ciphertext.\n\t */\n\tasync decrypt({\n\t\tdata,\n\t\tsessionKey,\n\t\ttxBytes,\n\t}: {\n\t\tdata: Uint8Array;\n\t\tsessionKey: SessionKey;\n\t\ttxBytes: Uint8Array;\n\t}) {\n\t\tconst encryptedObject = EncryptedObject.parse(data);\n\n\t\tthis.#validateEncryptionServices(\n\t\t\tencryptedObject.services.map((s) => s[0]),\n\t\t\tencryptedObject.threshold,\n\t\t);\n\n\t\tawait this.fetchKeys({\n\t\t\tids: [encryptedObject.id],\n\t\t\ttxBytes,\n\t\t\tsessionKey,\n\t\t\tthreshold: encryptedObject.threshold,\n\t\t});\n\n\t\treturn decrypt({ encryptedObject, keys: this.#cachedKeys });\n\t}\n\n\t#validateEncryptionServices(services: string[], threshold: number) {\n\t\t// Check that the client's key servers are a subset of the encrypted object's key servers.\n\t\tconst serverObjectIdsMap = new Map<string, number>();\n\t\tfor (const objectId of this.#serverObjectIds) {\n\t\t\tserverObjectIdsMap.set(objectId, (serverObjectIdsMap.get(objectId) ?? 0) + 1);\n\t\t}\n\t\tconst servicesMap = new Map<string, number>();\n\t\tfor (const service of services) {\n\t\t\tservicesMap.set(service, (servicesMap.get(service) ?? 0) + 1);\n\t\t}\n\t\tfor (const [objectId, count] of serverObjectIdsMap) {\n\t\t\tif (servicesMap.get(objectId) !== count) {\n\t\t\t\tthrow new InconsistentKeyServersError(\n\t\t\t\t\t`Client's key servers must be a subset of the encrypted object's key servers`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\t// Check that the threshold can be met with the client's key servers.\n\t\tif (threshold > this.#serverObjectIds.length) {\n\t\t\tthrow new InvalidThresholdError(\n\t\t\t\t`Invalid threshold ${threshold} for ${this.#serverObjectIds.length} servers`,\n\t\t\t);\n\t\t}\n\t}\n\n\tasync getKeyServers() {\n\t\tif (!this.#keyServers) {\n\t\t\tthis.#keyServers = this.#loadKeyServers().catch((error) => {\n\t\t\t\tthis.#keyServers = null;\n\t\t\t\tthrow error;\n\t\t\t});\n\t\t}\n\n\t\treturn this.#keyServers;\n\t}\n\n\tasync #loadKeyServers(): Promise<KeyServer[]> {\n\t\tconst keyServers = await retrieveKeyServers({\n\t\t\tobjectIds: this.#serverObjectIds,\n\t\t\tclient: this.#suiClient,\n\t\t});\n\n\t\tif (keyServers.length === 0) {\n\t\t\tthrow new InvalidKeyServerError('No key servers found');\n\t\t}\n\n\t\tif (this.#verifyKeyServers) {\n\t\t\tawait Promise.all(\n\t\t\t\tkeyServers.map(async (server) => {\n\t\t\t\t\tif (!(await verifyKeyServer(server, this.#timeout))) {\n\t\t\t\t\t\tthrow new InvalidKeyServerError(`Key server ${server.objectId} is not valid`);\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\treturn keyServers;\n\t}\n\n\t/**\n\t * Fetch keys from the key servers and update the cache.\n\t *\n\t * It is recommended to call this function once for all ids of all encrypted objects if\n\t * there are multiple, then call decrypt for each object. This avoids calling fetchKey\n\t * individually for each decrypt.\n\t *\n\t * @param ids - The ids of the encrypted objects.\n\t * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).\n\t * @param sessionKey - The session key to use.\n\t * @param threshold - The threshold for the TSS encryptions. The function returns when a threshold of key servers had returned keys for all ids.\n\t */\n\tasync fetchKeys({\n\t\tids,\n\t\ttxBytes,\n\t\tsessionKey,\n\t\tthreshold,\n\t}: {\n\t\tids: string[];\n\t\ttxBytes: Uint8Array;\n\t\tsessionKey: SessionKey;\n\t\tthreshold: number;\n\t}) {\n\t\tconst keyServers = await this.getKeyServers();\n\t\tif (threshold > keyServers.length || threshold < 1 || keyServers.length < 1) {\n\t\t\tthrow new InvalidThresholdError(\n\t\t\t\t`Invalid threshold ${threshold} for ${keyServers.length} servers`,\n\t\t\t);\n\t\t}\n\n\t\tlet completedServerCount = 0;\n\t\tconst remainingKeyServers = new Set<KeyServer>();\n\t\tconst fullIds = ids.map((id) => createFullId(DST, sessionKey.getPackageId(), id));\n\n\t\t// Count a server as completed if it has keys for all fullIds.\n\t\t// Duplicated key server ids will be counted towards the threshold.\n\t\tfor (const server of keyServers) {\n\t\t\tlet hasAllKeys = true;\n\t\t\tfor (const fullId of fullIds) {\n\t\t\t\tif (!this.#cachedKeys.has(`${fullId}:${server.objectId}`)) {\n\t\t\t\t\thasAllKeys = false;\n\t\t\t\t\tremainingKeyServers.add(server);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (hasAllKeys) {\n\t\t\t\tcompletedServerCount++;\n\t\t\t}\n\t\t}\n\n\t\t// Return early if we have enough keys from cache.\n\t\tif (completedServerCount >= threshold) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Check server validities.\n\t\tfor (const server of remainingKeyServers) {\n\t\t\tif (server.keyType !== KeyServerType.BonehFranklinBLS12381) {\n\t\t\t\tthrow new InvalidKeyServerError(\n\t\t\t\t\t`Server ${server.objectId} has invalid key type: ${server.keyType}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst cert = await sessionKey.getCertificate();\n\t\tconst signedRequest = await sessionKey.createRequestParams(txBytes);\n\n\t\tconst controller = new AbortController();\n\t\tconst errors: Error[] = [];\n\n\t\tconst keyFetches = [...remainingKeyServers].map(async (server) => {\n\t\t\ttry {\n\t\t\t\tconst allKeys = await fetchKeysForAllIds(\n\t\t\t\t\tserver.url,\n\t\t\t\t\tsignedRequest.requestSignature,\n\t\t\t\t\ttxBytes,\n\t\t\t\t\tsignedRequest.decryptionKey,\n\t\t\t\t\tcert,\n\t\t\t\t\tthis.#timeout,\n\t\t\t\t\tcontroller.signal,\n\t\t\t\t);\n\t\t\t\t// Check validity of the keys and add them to the cache.\n\t\t\t\tconst receivedIds = new Set<string>();\n\t\t\t\tfor (const { fullId, key } of allKeys) {\n\t\t\t\t\tconst keyElement = G1Element.fromBytes(key);\n\t\t\t\t\tif (\n\t\t\t\t\t\t!BonehFranklinBLS12381Services.verifyUserSecretKey(\n\t\t\t\t\t\t\tkeyElement,\n\t\t\t\t\t\t\tfullId,\n\t\t\t\t\t\t\tG2Element.fromBytes(server.pk),\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tconsole.warn('Received invalid key from key server ' + server.objectId);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tthis.#cachedKeys.set(`${fullId}:${server.objectId}`, keyElement);\n\t\t\t\t\treceivedIds.add(fullId);\n\t\t\t\t}\n\n\t\t\t\t// Check if all the receivedIds are consistent with the requested fullIds.\n\t\t\t\t// If so, consider the key server got all keys and mark as completed.\n\t\t\t\tconst expectedIds = new Set(fullIds);\n\t\t\t\tconst hasAllKeys =\n\t\t\t\t\treceivedIds.size === expectedIds.size &&\n\t\t\t\t\t[...receivedIds].every((id) => expectedIds.has(id));\n\n\t\t\t\t// Return early if the completed servers is more than threshold.\n\t\t\t\tif (hasAllKeys) {\n\t\t\t\t\tcompletedServerCount++;\n\t\t\t\t\tif (completedServerCount >= threshold) {\n\t\t\t\t\t\tcontroller.abort();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tif (!controller.signal.aborted) {\n\t\t\t\t\terrors.push(error as Error);\n\t\t\t\t}\n\t\t\t\t// If there are too many errors that the threshold is not attainable, return early with error.\n\t\t\t\tif (remainingKeyServers.size - errors.length < threshold - completedServerCount) {\n\t\t\t\t\tcontroller.abort(error);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tawait Promise.allSettled(keyFetches);\n\n\t\tif (completedServerCount < threshold) {\n\t\t\tthrow toMajorityError(errors);\n\t\t}\n\t}\n\n\t/**\n\t * Get derived keys from the given services.\n\t *\n\t * @param id - The id of the encrypted object.\n\t * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).\n\t * @param sessionKey - The session key to use.\n\t * @param threshold - The threshold.\n\t * @returns - Derived keys for the given services that are in the cache as a \"service object ID\" -> derived key map. If the call is succesful, exactly threshold keys will be returned.\n\t */\n\tasync getDerivedKeys({\n\t\tkemType = KemType.BonehFranklinBLS12381DemCCA,\n\t\tid,\n\t\ttxBytes,\n\t\tsessionKey,\n\t\tthreshold,\n\t}: {\n\t\tkemType?: KemType;\n\t\tid: string;\n\t\ttxBytes: Uint8Array;\n\t\tsessionKey: SessionKey;\n\t\tthreshold: number;\n\t}): Promise<Map<string, DerivedKey>> {\n\t\tswitch (kemType) {\n\t\t\tcase KemType.BonehFranklinBLS12381DemCCA:\n\t\t\t\tconst keyServers = await this.getKeyServers();\n\t\t\t\tif (threshold > this.#serverObjectIds.length) {\n\t\t\t\t\tthrow new InvalidThresholdError(\n\t\t\t\t\t\t`Invalid threshold ${threshold} for ${this.#serverObjectIds.length} servers`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tawait this.fetchKeys({\n\t\t\t\t\tids: [id],\n\t\t\t\t\ttxBytes,\n\t\t\t\t\tsessionKey,\n\t\t\t\t\tthreshold,\n\t\t\t\t});\n\n\t\t\t\t// After calling fetchKeys, we can be sure that there are at least `threshold` of the required keys in the cache.\n\t\t\t\t// It is also checked there that the KeyServerType is BonehFranklinBLS12381 for all services.\n\n\t\t\t\tconst fullId = createFullId(DST, sessionKey.getPackageId(), id);\n\n\t\t\t\tconst derivedKeys = new Map<string, DerivedKey>();\n\t\t\t\tlet servicesAdded = 0;\n\t\t\t\tfor (const keyServer of keyServers) {\n\t\t\t\t\t// The code below assumes that the KeyServerType is BonehFranklinBLS12381.\n\t\t\t\t\tconst cachedKey = this.#cachedKeys.get(`${fullId}:${keyServer.objectId}`);\n\t\t\t\t\tif (cachedKey) {\n\t\t\t\t\t\tderivedKeys.set(keyServer.objectId, new BonehFranklinBLS12381DerivedKey(cachedKey));\n\t\t\t\t\t\tif (++servicesAdded === threshold) {\n\t\t\t\t\t\t\t// We have enough keys, so we can stop.\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn derivedKeys;\n\t\t}\n\t}\n}\n"],
5
- "mappings": ";;;;;;;;AAAA;AAGA,SAAS,uBAAuB;AAChC,SAAS,WAAW,iBAAiB;AACrC,SAAS,eAAe;AAExB,SAAS,WAAW,kBAAkB;AACtC,SAAS,SAAS,SAAS,eAAe;AAC1C;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,+BAA+B,WAAW;AACnD;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP,SAAS,0BAA0B;AAGnC,SAAS,oBAAoB;AAqBtB,MAAM,cAAN,MAAM,YAAW;AAAA,EASvB,YAAY,SAA4B;AATlC;AACN;AACA;AACA;AACA,oCAA2C;AAE3C;AAAA,oCAAc,oBAAI,IAA4B;AAC9C;AAGC,uBAAK,YAAa,QAAQ;AAC1B,uBAAK,kBAAmB,QAAQ;AAChC,uBAAK,mBAAoB,QAAQ,oBAAoB;AACrD,uBAAK,UAAW,QAAQ,WAAW;AAAA,EACpC;AAAA,EAEA,OAAO,+BAA+B,SAAqC;AAC1E,WAAO;AAAA,MACN,MAAM;AA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CAAC;AAAA,EAC7D;AACA,aAAW,CAAC,UAAU,KAAK,KAAK,oBAAoB;AACnD,QAAI,YAAY,IAAI,QAAQ,MAAM,OAAO;AACxC,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,MAAI,YAAY,mBAAK,kBAAiB,QAAQ;AAC7C,UAAM,IAAI;AAAA,MACT,qBAAqB,SAAS,QAAQ,mBAAK,kBAAiB,MAAM;AAAA,IACnE;AAAA,EACD;AACD;AAaM,oBAAe,iBAAyB;AAC7C,QAAM,aAAa,MAAM,mBAAmB;AAAA,IAC3C,WAAW,mBAAK;AAAA,IAChB,QAAQ,mBAAK;AAAA,EACd,CAAC;AAED,MAAI,WAAW,WAAW,GAAG;AAC5B,UAAM,IAAI,sBAAsB,sBAAsB;AAAA,EACvD;AAEA,MAAI,mBAAK,oBAAmB;AAC3B,UAAM,QAAQ;AAAA,MACb,WAAW,IAAI,OAAO,WAAW;AAChC,YAAI,CAAE,MAAM,gBAAgB,QAAQ,mBAAK,SAAQ,GAAI;AACpD,gBAAM,IAAI,sBAAsB,cAAc,OAAO,QAAQ,eAAe;AAAA,QAC7E;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AAEA,SAAO;AACR;AA7KM,IAAM,aAAN;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { EncryptedObject } from './bcs.js';\nimport { G1Element, G2Element } from './bls12381.js';\nimport { decrypt } from './decrypt.js';\nimport type { EncryptionInput } from './dem.js';\nimport { AesGcm256, Hmac256Ctr } from './dem.js';\nimport { DemType, encrypt, KemType } from './encrypt.js';\nimport {\n\tInconsistentKeyServersError,\n\tInvalidClientOptionsError,\n\tInvalidKeyServerError,\n\tInvalidThresholdError,\n\ttoMajorityError,\n\tTooManyFailedFetchKeyRequestsError,\n} from './error.js';\nimport { BonehFranklinBLS12381Services } from './ibe.js';\nimport {\n\tBonehFranklinBLS12381DerivedKey,\n\tKeyServerType,\n\tretrieveKeyServers,\n\tverifyKeyServer,\n} from './key-server.js';\nimport type { DerivedKey, KeyServer } from './key-server.js';\nimport { fetchKeysForAllIds } from './keys.js';\nimport type { SessionKey } from './session-key.js';\nimport type { KeyCacheKey, SealCompatibleClient } from './types.js';\nimport { createFullId, count } from './utils.js';\n\n/**\n * Configuration options for initializing a SealClient\n * @property serverObjectIds: Array of the key servers to use.\n * \t The first element is the object ID, and the second element is the weight of the key server.\n * @property verifyKeyServers: Whether to verify the key servers' authenticity.\n * \t Should be false if servers are pre-verified (e.g., getAllowlistedKeyServers).\n * \t Defaults to true.\n * @property timeout: Timeout in milliseconds for network requests. Defaults to 10 seconds.\n */\nexport interface SealClientExtensionOptions {\n\tserverObjectIds: [string, number][];\n\tverifyKeyServers?: boolean;\n\ttimeout?: number;\n}\n\nexport interface SealClientOptions extends SealClientExtensionOptions {\n\tsuiClient: SealCompatibleClient;\n}\n\nexport class SealClient {\n\t#suiClient: SealCompatibleClient;\n\t#weights: Map<string, number>;\n\t#keyServers: Promise<Map<string, KeyServer>> | null = null;\n\t#verifyKeyServers: boolean;\n\t// A caching map for: fullId:object_id -> partial key.\n\t#cachedKeys = new Map<KeyCacheKey, G1Element>();\n\t#timeout: number;\n\t#totalWeight: number;\n\n\tconstructor(options: SealClientOptions) {\n\t\tthis.#suiClient = options.suiClient;\n\n\t\tif (\n\t\t\tnew Set(options.serverObjectIds.map(([objectId, _]) => objectId)).size !==\n\t\t\toptions.serverObjectIds.length\n\t\t) {\n\t\t\tthrow new InvalidClientOptionsError('Duplicate object IDs');\n\t\t}\n\n\t\tthis.#weights = new Map(options.serverObjectIds);\n\t\tthis.#totalWeight = options.serverObjectIds\n\t\t\t.map(([_, weight]) => weight)\n\t\t\t.reduce((sum, term) => sum + term, 0);\n\n\t\tthis.#verifyKeyServers = options.verifyKeyServers ?? true;\n\t\tthis.#timeout = options.timeout ?? 10_000;\n\t}\n\n\tstatic experimental_asClientExtension(options: SealClientExtensionOptions) {\n\t\treturn {\n\t\t\tname: 'seal' as const,\n\t\t\tregister: (client: SealCompatibleClient) => {\n\t\t\t\treturn new SealClient({\n\t\t\t\t\tsuiClient: client,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Return an encrypted message under the identity.\n\t *\n\t * @param kemType - The type of KEM to use.\n\t * @param demType - The type of DEM to use.\n\t * @param threshold - The threshold for the TSS encryption.\n\t * @param packageId - the packageId namespace.\n\t * @param id - the identity to use.\n\t * @param data - the data to encrypt.\n\t * @param aad - optional additional authenticated data.\n\t * @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.\n\t * \tSince the symmetric key can be used to decrypt, it should not be shared but can be used e.g. for backup.\n\t */\n\tasync encrypt({\n\t\tkemType = KemType.BonehFranklinBLS12381DemCCA,\n\t\tdemType = DemType.AesGcm256,\n\t\tthreshold,\n\t\tpackageId,\n\t\tid,\n\t\tdata,\n\t\taad = new Uint8Array(),\n\t}: {\n\t\tkemType?: KemType;\n\t\tdemType?: DemType;\n\t\tthreshold: number;\n\t\tpackageId: string;\n\t\tid: string;\n\t\tdata: Uint8Array;\n\t\taad?: Uint8Array;\n\t}) {\n\t\t// TODO: Verify that packageId is first version of its package (else throw error).\n\t\treturn encrypt({\n\t\t\tkeyServers: await this.#getWeightedKeyServers(),\n\t\t\tkemType,\n\t\t\tthreshold,\n\t\t\tpackageId,\n\t\t\tid,\n\t\t\tencryptionInput: this.#createEncryptionInput(demType, data, aad),\n\t\t});\n\t}\n\n\t#createEncryptionInput(type: DemType, data: Uint8Array, aad: Uint8Array): EncryptionInput {\n\t\tswitch (type) {\n\t\t\tcase DemType.AesGcm256:\n\t\t\t\treturn new AesGcm256(data, aad);\n\t\t\tcase DemType.Hmac256Ctr:\n\t\t\t\treturn new Hmac256Ctr(data, aad);\n\t\t}\n\t}\n\n\t/**\n\t * Decrypt the given encrypted bytes using cached keys.\n\t * Calls fetchKeys in case one or more of the required keys is not cached yet.\n\t * The function throws an error if the client's key servers are not a subset of\n\t * the encrypted object's key servers or if the threshold cannot be met.\n\t *\n\t * @param data - The encrypted bytes to decrypt.\n\t * @param sessionKey - The session key to use.\n\t * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).\n\t * @returns - The decrypted plaintext corresponding to ciphertext.\n\t */\n\tasync decrypt({\n\t\tdata,\n\t\tsessionKey,\n\t\ttxBytes,\n\t}: {\n\t\tdata: Uint8Array;\n\t\tsessionKey: SessionKey;\n\t\ttxBytes: Uint8Array;\n\t}) {\n\t\tconst encryptedObject = EncryptedObject.parse(data);\n\n\t\tthis.#validateEncryptionServices(\n\t\t\tencryptedObject.services.map((s) => s[0]),\n\t\t\tencryptedObject.threshold,\n\t\t);\n\n\t\tawait this.fetchKeys({\n\t\t\tids: [encryptedObject.id],\n\t\t\ttxBytes,\n\t\t\tsessionKey,\n\t\t\tthreshold: encryptedObject.threshold,\n\t\t});\n\n\t\treturn decrypt({ encryptedObject, keys: this.#cachedKeys });\n\t}\n\n\t#weight(objectId: string) {\n\t\treturn this.#weights.get(objectId) ?? 0;\n\t}\n\n\t#validateEncryptionServices(services: string[], threshold: number) {\n\t\t// Check that the client's key servers are a subset of the encrypted object's key servers.\n\t\tif (\n\t\t\tservices.some((objectId) => {\n\t\t\t\tconst countInClient = this.#weight(objectId);\n\t\t\t\treturn countInClient > 0 && countInClient !== count(services, objectId);\n\t\t\t})\n\t\t) {\n\t\t\tthrow new InconsistentKeyServersError(\n\t\t\t\t`Client's key servers must be a subset of the encrypted object's key servers`,\n\t\t\t);\n\t\t}\n\t\t// Check that the threshold can be met with the client's key servers.\n\t\tif (threshold > this.#totalWeight) {\n\t\t\tthrow new InvalidThresholdError(\n\t\t\t\t`Invalid threshold ${threshold} for ${this.#totalWeight} servers`,\n\t\t\t);\n\t\t}\n\t}\n\n\tasync getKeyServers(): Promise<Map<string, KeyServer>> {\n\t\tif (!this.#keyServers) {\n\t\t\tthis.#keyServers = this.#loadKeyServers().catch((error) => {\n\t\t\t\tthis.#keyServers = null;\n\t\t\t\tthrow error;\n\t\t\t});\n\t\t}\n\t\treturn this.#keyServers;\n\t}\n\n\t/**\n\t * Returns a list of key servers with multiplicity according to their weights.\n\t * The list is used for encryption.\n\t */\n\tasync #getWeightedKeyServers() {\n\t\tconst keyServers = await this.getKeyServers();\n\t\tconst keyServersWithMultiplicity = [];\n\t\tfor (const [objectId, weight] of this.#weights) {\n\t\t\tconst keyServer = keyServers.get(objectId)!;\n\t\t\tfor (let i = 0; i < weight; i++) {\n\t\t\t\tkeyServersWithMultiplicity.push(keyServer);\n\t\t\t}\n\t\t}\n\t\treturn keyServersWithMultiplicity;\n\t}\n\n\tasync #loadKeyServers(): Promise<Map<string, KeyServer>> {\n\t\tconst keyServers = await retrieveKeyServers({\n\t\t\tobjectIds: [...this.#weights].map(([objectId]) => objectId),\n\t\t\tclient: this.#suiClient,\n\t\t});\n\n\t\tif (keyServers.length === 0) {\n\t\t\tthrow new InvalidKeyServerError('No key servers found');\n\t\t}\n\n\t\tif (this.#verifyKeyServers) {\n\t\t\tawait Promise.all(\n\t\t\t\tkeyServers.map(async (server) => {\n\t\t\t\t\tif (!(await verifyKeyServer(server, this.#timeout))) {\n\t\t\t\t\t\tthrow new InvalidKeyServerError(`Key server ${server.objectId} is not valid`);\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\treturn new Map(keyServers.map((server) => [server.objectId, server]));\n\t}\n\n\t/**\n\t * Fetch keys from the key servers and update the cache.\n\t *\n\t * It is recommended to call this function once for all ids of all encrypted objects if\n\t * there are multiple, then call decrypt for each object. This avoids calling fetchKey\n\t * individually for each decrypt.\n\t *\n\t * @param ids - The ids of the encrypted objects.\n\t * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).\n\t * @param sessionKey - The session key to use.\n\t * @param threshold - The threshold for the TSS encryptions. The function returns when a threshold of key servers had returned keys for all ids.\n\t */\n\tasync fetchKeys({\n\t\tids,\n\t\ttxBytes,\n\t\tsessionKey,\n\t\tthreshold,\n\t}: {\n\t\tids: string[];\n\t\ttxBytes: Uint8Array;\n\t\tsessionKey: SessionKey;\n\t\tthreshold: number;\n\t}) {\n\t\tif (threshold > this.#totalWeight || threshold < 1) {\n\t\t\tthrow new InvalidThresholdError(\n\t\t\t\t`Invalid threshold ${threshold} servers with weights ${this.#weights}`,\n\t\t\t);\n\t\t}\n\t\tconst keyServers = await this.getKeyServers();\n\t\tconst fullIds = ids.map((id) => createFullId(sessionKey.getPackageId(), id));\n\n\t\t// Count a server as completed if it has keys for all fullIds.\n\t\t// Duplicated key server ids will be counted towards the threshold.\n\t\tlet completedWeight = 0;\n\t\tconst remainingKeyServers = [];\n\t\tlet remainingKeyServersWeight = 0;\n\t\tfor (const objectId of keyServers.keys()) {\n\t\t\tif (fullIds.every((fullId) => this.#cachedKeys.has(`${fullId}:${objectId}`))) {\n\t\t\t\tcompletedWeight += this.#weight(objectId)!;\n\t\t\t} else {\n\t\t\t\tremainingKeyServers.push(objectId);\n\t\t\t\tremainingKeyServersWeight += this.#weight(objectId)!;\n\t\t\t}\n\t\t}\n\n\t\t// Return early if we have enough keys from cache.\n\t\tif (completedWeight >= threshold) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Check server validities.\n\t\tfor (const objectId of remainingKeyServers) {\n\t\t\tconst server = keyServers.get(objectId)!;\n\t\t\tif (server.keyType !== KeyServerType.BonehFranklinBLS12381) {\n\t\t\t\tthrow new InvalidKeyServerError(\n\t\t\t\t\t`Server ${server.objectId} has invalid key type: ${server.keyType}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst cert = await sessionKey.getCertificate();\n\t\tconst signedRequest = await sessionKey.createRequestParams(txBytes);\n\n\t\tconst controller = new AbortController();\n\t\tconst errors: Error[] = [];\n\n\t\tconst keyFetches = remainingKeyServers.map(async (objectId) => {\n\t\t\tconst server = keyServers.get(objectId)!;\n\t\t\ttry {\n\t\t\t\tconst allKeys = await fetchKeysForAllIds(\n\t\t\t\t\tserver.url,\n\t\t\t\t\tsignedRequest.requestSignature,\n\t\t\t\t\ttxBytes,\n\t\t\t\t\tsignedRequest.decryptionKey,\n\t\t\t\t\tcert,\n\t\t\t\t\tthis.#timeout,\n\t\t\t\t\tcontroller.signal,\n\t\t\t\t);\n\t\t\t\t// Check validity of the keys and add them to the cache.\n\t\t\t\tfor (const { fullId, key } of allKeys) {\n\t\t\t\t\tconst keyElement = G1Element.fromBytes(key);\n\t\t\t\t\tif (\n\t\t\t\t\t\t!BonehFranklinBLS12381Services.verifyUserSecretKey(\n\t\t\t\t\t\t\tkeyElement,\n\t\t\t\t\t\t\tfullId,\n\t\t\t\t\t\t\tG2Element.fromBytes(server.pk),\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tconsole.warn('Received invalid key from key server ' + server.objectId);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tthis.#cachedKeys.set(`${fullId}:${server.objectId}`, keyElement);\n\t\t\t\t}\n\n\t\t\t\t// Check if all the receivedIds are consistent with the requested fullIds.\n\t\t\t\t// If so, consider the key server got all keys and mark as completed.\n\t\t\t\tif (fullIds.every((fullId) => this.#cachedKeys.has(`${fullId}:${server.objectId}`))) {\n\t\t\t\t\tcompletedWeight += this.#weight(objectId)!;\n\n\t\t\t\t\t// Return early if the completed servers is more than the threshold.\n\t\t\t\t\tif (completedWeight >= threshold) {\n\t\t\t\t\t\tcontroller.abort();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tif (!controller.signal.aborted) {\n\t\t\t\t\terrors.push(error as Error);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\t// If there are too many errors that the threshold is not attainable, return early with error.\n\t\t\t\tremainingKeyServersWeight -= this.#weight(objectId)!;\n\t\t\t\tif (remainingKeyServersWeight < threshold - completedWeight) {\n\t\t\t\t\tcontroller.abort(new TooManyFailedFetchKeyRequestsError());\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tawait Promise.allSettled(keyFetches);\n\n\t\tif (completedWeight < threshold) {\n\t\t\tthrow toMajorityError(errors);\n\t\t}\n\t}\n\n\t/**\n\t * Get derived keys from the given services.\n\t *\n\t * @param id - The id of the encrypted object.\n\t * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).\n\t * @param sessionKey - The session key to use.\n\t * @param threshold - The threshold.\n\t * @returns - Derived keys for the given services that are in the cache as a \"service object ID\" -> derived key map. If the call is succesful, exactly threshold keys will be returned.\n\t */\n\tasync getDerivedKeys({\n\t\tkemType = KemType.BonehFranklinBLS12381DemCCA,\n\t\tid,\n\t\ttxBytes,\n\t\tsessionKey,\n\t\tthreshold,\n\t}: {\n\t\tkemType?: KemType;\n\t\tid: string;\n\t\ttxBytes: Uint8Array;\n\t\tsessionKey: SessionKey;\n\t\tthreshold: number;\n\t}): Promise<Map<string, DerivedKey>> {\n\t\tswitch (kemType) {\n\t\t\tcase KemType.BonehFranklinBLS12381DemCCA:\n\t\t\t\tconst keyServers = await this.getKeyServers();\n\t\t\t\tif (threshold > this.#totalWeight) {\n\t\t\t\t\tthrow new InvalidThresholdError(\n\t\t\t\t\t\t`Invalid threshold ${threshold} for ${this.#totalWeight} servers`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tawait this.fetchKeys({\n\t\t\t\t\tids: [id],\n\t\t\t\t\ttxBytes,\n\t\t\t\t\tsessionKey,\n\t\t\t\t\tthreshold,\n\t\t\t\t});\n\n\t\t\t\t// After calling fetchKeys, we can be sure that there are at least `threshold` of the required keys in the cache.\n\t\t\t\t// It is also checked there that the KeyServerType is BonehFranklinBLS12381 for all services.\n\n\t\t\t\tconst fullId = createFullId(sessionKey.getPackageId(), id);\n\n\t\t\t\tconst derivedKeys = new Map();\n\t\t\t\tlet weight = 0;\n\t\t\t\tfor (const objectId of keyServers.keys()) {\n\t\t\t\t\t// The code below assumes that the KeyServerType is BonehFranklinBLS12381.\n\t\t\t\t\tconst cachedKey = this.#cachedKeys.get(`${fullId}:${objectId}`);\n\t\t\t\t\tif (cachedKey) {\n\t\t\t\t\t\tderivedKeys.set(objectId, new BonehFranklinBLS12381DerivedKey(cachedKey));\n\t\t\t\t\t\tweight += this.#weight(objectId)!;\n\t\t\t\t\t\tif (weight >= threshold) {\n\t\t\t\t\t\t\t// We have enough keys, so we can stop.\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn derivedKeys;\n\t\t}\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;AAAA;AAGA,SAAS,uBAAuB;AAChC,SAAS,WAAW,iBAAiB;AACrC,SAAS,eAAe;AAExB,SAAS,WAAW,kBAAkB;AACtC,SAAS,SAAS,SAAS,eAAe;AAC1C;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,qCAAqC;AAC9C;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP,SAAS,0BAA0B;AAGnC,SAAS,cAAc,aAAa;AAqB7B,MAAM,cAAN,MAAM,YAAW;AAAA,EAUvB,YAAY,SAA4B;AAVlC;AACN;AACA;AACA,oCAAsD;AACtD;AAEA;AAAA,oCAAc,oBAAI,IAA4B;AAC9C;AACA;AAGC,uBAAK,YAAa,QAAQ;AAE1B,QACC,IAAI,IAAI,QAAQ,gBAAgB,IAAI,CAAC,CAAC,UAAU,CAAC,MAAM,QAAQ,CAAC,EAAE,SAClE,QAAQ,gBAAgB,QACvB;AACD,YAAM,IAAI,0BAA0B,sBAAsB;AAAA,IAC3D;AAEA,uBAAK,UAAW,IAAI,IAAI,QAAQ,eAAe;AAC/C,uBAAK,cAAe,QAAQ,gBAC1B,IAAI,CAAC,CAAC,GAAG,MAAM,MAAM,MAAM,EAC3B,OAAO,CAAC,KAAK,SAAS,MAAM,MAAM,CAA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
6
  "names": []
7
7
  }
@@ -3,14 +3,14 @@ import { combine as externalCombine } from "shamir-secret-sharing";
3
3
  import { G2Element } from "./bls12381.js";
4
4
  import { AesGcm256, Hmac256Ctr } from "./dem.js";
5
5
  import { InvalidCiphertextError, UnsupportedFeatureError } from "./error.js";
6
- import { BonehFranklinBLS12381Services, DST } from "./ibe.js";
6
+ import { BonehFranklinBLS12381Services } from "./ibe.js";
7
7
  import { deriveKey, KeyPurpose } from "./kdf.js";
8
- import { createFullId } from "./utils.js";
8
+ import { createFullId, flatten } from "./utils.js";
9
9
  async function decrypt({ encryptedObject, keys }) {
10
10
  if (!encryptedObject.encryptedShares.BonehFranklinBLS12381) {
11
11
  throw new UnsupportedFeatureError("Encryption mode not supported");
12
12
  }
13
- const fullId = createFullId(DST, encryptedObject.packageId, encryptedObject.id);
13
+ const fullId = createFullId(encryptedObject.packageId, encryptedObject.id);
14
14
  const inKeystore = encryptedObject.services.map((_, i) => i).filter((i) => keys.has(`${fullId}:${encryptedObject.services[i][0]}`));
15
15
  if (inKeystore.length < encryptedObject.threshold) {
16
16
  throw new Error("Not enough shares. Please fetch more keys.");
@@ -33,24 +33,22 @@ async function decrypt({ encryptedObject, keys }) {
33
33
  );
34
34
  return { index, share };
35
35
  });
36
- const key = await combine(shares);
37
- const demKey = deriveKey(KeyPurpose.DEM, key);
36
+ const baseKey = await combine(shares);
37
+ const demKey = deriveKey(
38
+ KeyPurpose.DEM,
39
+ baseKey,
40
+ encryptedObject.encryptedShares.BonehFranklinBLS12381.encryptedShares,
41
+ encryptedObject.threshold,
42
+ encryptedObject.services.map(([objectId, _]) => objectId)
43
+ );
38
44
  if (encryptedObject.ciphertext.Aes256Gcm) {
39
- try {
40
- return AesGcm256.decrypt(demKey, encryptedObject.ciphertext);
41
- } catch {
42
- throw new Error("Decryption failed");
43
- }
45
+ return AesGcm256.decrypt(demKey, encryptedObject.ciphertext);
46
+ } else if (encryptedObject.ciphertext.Hmac256Ctr) {
47
+ return Hmac256Ctr.decrypt(demKey, encryptedObject.ciphertext);
44
48
  } else if (encryptedObject.ciphertext.Plain) {
45
49
  return demKey;
46
- } else if (encryptedObject.ciphertext.Hmac256Ctr) {
47
- try {
48
- return Hmac256Ctr.decrypt(demKey, encryptedObject.ciphertext);
49
- } catch {
50
- throw new Error("Decryption failed");
51
- }
52
50
  } else {
53
- throw new Error("Invalid encrypted object");
51
+ throw new InvalidCiphertextError("Invalid ciphertext type");
54
52
  }
55
53
  }
56
54
  async function combine(shares) {
@@ -60,12 +58,7 @@ async function combine(shares) {
60
58
  return Promise.resolve(shares[0].share);
61
59
  }
62
60
  return externalCombine(
63
- shares.map(({ index, share }) => {
64
- const packedShare = new Uint8Array(share.length + 1);
65
- packedShare.set(share, 0);
66
- packedShare[share.length] = index;
67
- return packedShare;
68
- })
61
+ shares.map(({ index, share }) => flatten([share, new Uint8Array([index])]))
69
62
  );
70
63
  }
71
64
  export {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/decrypt.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@mysten/bcs';\nimport { combine as externalCombine } from 'shamir-secret-sharing';\n\nimport type { EncryptedObject } from './bcs.js';\nimport type { G1Element } from './bls12381.js';\nimport { G2Element } from './bls12381.js';\nimport { AesGcm256, Hmac256Ctr } from './dem.js';\nimport { InvalidCiphertextError, UnsupportedFeatureError } from './error.js';\nimport { BonehFranklinBLS12381Services, DST } from './ibe.js';\nimport { deriveKey, KeyPurpose } from './kdf.js';\nimport type { KeyCacheKey } from './types.js';\nimport { createFullId } from './utils.js';\n\nexport interface DecryptOptions {\n\tencryptedObject: typeof EncryptedObject.$inferType;\n\tkeys: Map<KeyCacheKey, G1Element>;\n}\n\n/**\n * Decrypt the given encrypted bytes with the given cached secret keys for the full ID.\n * It's assumed that fetchKeys has been called to fetch the secret keys for enough key servers\n * otherwise, this will throw an error.\n *\n * @returns - The decrypted plaintext corresponding to ciphertext.\n */\nexport async function decrypt({ encryptedObject, keys }: DecryptOptions): Promise<Uint8Array> {\n\tif (!encryptedObject.encryptedShares.BonehFranklinBLS12381) {\n\t\tthrow new UnsupportedFeatureError('Encryption mode not supported');\n\t}\n\n\tconst fullId = createFullId(DST, encryptedObject.packageId, encryptedObject.id);\n\n\t// Get the indices of the service whose keys are in the keystore.\n\tconst inKeystore = encryptedObject.services\n\t\t.map((_, i) => i)\n\t\t.filter((i) => keys.has(`${fullId}:${encryptedObject.services[i][0]}`));\n\n\tif (inKeystore.length < encryptedObject.threshold) {\n\t\tthrow new Error('Not enough shares. Please fetch more keys.');\n\t}\n\n\tconst encryptedShares = encryptedObject.encryptedShares.BonehFranklinBLS12381.encryptedShares;\n\tif (encryptedShares.length !== encryptedObject.services.length) {\n\t\tthrow new InvalidCiphertextError(\n\t\t\t`Mismatched shares ${encryptedShares.length} and services ${encryptedObject.services.length}`,\n\t\t);\n\t}\n\n\tconst nonce = G2Element.fromBytes(encryptedObject.encryptedShares.BonehFranklinBLS12381.nonce);\n\n\t// Decrypt each share.\n\tconst shares = inKeystore.map((i: number) => {\n\t\tconst [objectId, index] = encryptedObject.services[i];\n\t\t// Use the index as the unique info parameter to allow for multiple shares per key server.\n\t\tconst share = BonehFranklinBLS12381Services.decrypt(\n\t\t\tnonce,\n\t\t\tkeys.get(`${fullId}:${objectId}`)!,\n\t\t\tencryptedShares[i],\n\t\t\tfromHex(fullId),\n\t\t\t[objectId, index],\n\t\t);\n\t\t// The Shamir secret sharing library expects the index/x-coordinate to be at the end of the share.\n\t\treturn { index, share };\n\t});\n\n\t// Combine the decrypted shares into the key.\n\tconst key = await combine(shares);\n\tconst demKey = deriveKey(KeyPurpose.DEM, key);\n\tif (encryptedObject.ciphertext.Aes256Gcm) {\n\t\ttry {\n\t\t\t// Decrypt the ciphertext with the key.\n\t\t\treturn AesGcm256.decrypt(demKey, encryptedObject.ciphertext);\n\t\t} catch {\n\t\t\tthrow new Error('Decryption failed');\n\t\t}\n\t} else if (encryptedObject.ciphertext.Plain) {\n\t\t// In case `Plain` mode is used, return the key.\n\t\treturn demKey;\n\t} else if (encryptedObject.ciphertext.Hmac256Ctr) {\n\t\ttry {\n\t\t\treturn Hmac256Ctr.decrypt(demKey, encryptedObject.ciphertext);\n\t\t} catch {\n\t\t\tthrow new Error('Decryption failed');\n\t\t}\n\t} else {\n\t\tthrow new Error('Invalid encrypted object');\n\t}\n}\n\n/**\n * Helper function that combines the shares into the key.\n * @param shares - The shares to combine.\n * @returns - The combined key.\n */\nasync function combine(shares: { index: number; share: Uint8Array }[]): Promise<Uint8Array> {\n\tif (shares.length === 0) {\n\t\tthrow new Error('Invalid shares length');\n\t} else if (shares.length === 1) {\n\t\t// The Shamir secret sharing library expects at least two shares.\n\t\t// If there is only one and the threshold is 1, the reconstructed secret is the same as the share.\n\t\treturn Promise.resolve(shares[0].share);\n\t}\n\n\t// The Shamir secret sharing library expects the index/x-coordinate to be at the end of the share\n\treturn externalCombine(\n\t\tshares.map(({ index, share }) => {\n\t\t\tconst packedShare = new Uint8Array(share.length + 1);\n\t\t\tpackedShare.set(share, 0);\n\t\t\tpackedShare[share.length] = index;\n\t\t\treturn packedShare;\n\t\t}),\n\t);\n}\n"],
5
- "mappings": "AAGA,SAAS,eAAe;AACxB,SAAS,WAAW,uBAAuB;AAI3C,SAAS,iBAAiB;AAC1B,SAAS,WAAW,kBAAkB;AACtC,SAAS,wBAAwB,+BAA+B;AAChE,SAAS,+BAA+B,WAAW;AACnD,SAAS,WAAW,kBAAkB;AAEtC,SAAS,oBAAoB;AAc7B,eAAsB,QAAQ,EAAE,iBAAiB,KAAK,GAAwC;AAC7F,MAAI,CAAC,gBAAgB,gBAAgB,uBAAuB;AAC3D,UAAM,IAAI,wBAAwB,+BAA+B;AAAA,EAClE;AAEA,QAAM,SAAS,aAAa,KAAK,gBAAgB,WAAW,gBAAgB,EAAE;AAG9E,QAAM,aAAa,gBAAgB,SACjC,I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IAAI,MAAM,mBAAmB;AAAA,IACpC;AAAA,EACD,OAAO;AACN,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC3C;AACD;AAOA,eAAe,QAAQ,QAAqE;AAC3F,MAAI,OAAO,WAAW,GAAG;AACxB,UAAM,IAAI,MAAM,uBAAuB;AAAA,EACxC,WAAW,OAAO,WAAW,GAAG;AAG/B,WAAO,QAAQ,QAAQ,OAAO,CAAC,EAAE,KAAK;AAAA,EACvC;AAGA,SAAO;AAAA,IACN,OAAO,IAAI,CAAC,EAAE,OAAO,MAAM,MAAM;AAChC,YAAM,cAAc,IAAI,WAAW,MAAM,SAAS,CAAC;AACnD,kBAAY,IAAI,OAAO,CAAC;AACxB,kBAAY,MAAM,MAAM,IAAI;AAC5B,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AACD;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@mysten/bcs';\nimport { combine as externalCombine } from 'shamir-secret-sharing';\n\nimport type { EncryptedObject } from './bcs.js';\nimport type { G1Element } from './bls12381.js';\nimport { G2Element } from './bls12381.js';\nimport { AesGcm256, Hmac256Ctr } from './dem.js';\nimport { InvalidCiphertextError, UnsupportedFeatureError } from './error.js';\nimport { BonehFranklinBLS12381Services } from './ibe.js';\nimport { deriveKey, KeyPurpose } from './kdf.js';\nimport type { KeyCacheKey } from './types.js';\nimport { createFullId, flatten } from './utils.js';\n\nexport interface DecryptOptions {\n\tencryptedObject: typeof EncryptedObject.$inferType;\n\tkeys: Map<KeyCacheKey, G1Element>;\n}\n\n/**\n * Decrypt the given encrypted bytes with the given cached secret keys for the full ID.\n * It's assumed that fetchKeys has been called to fetch the secret keys for enough key servers\n * otherwise, this will throw an error.\n *\n * @returns - The decrypted plaintext corresponding to ciphertext.\n */\nexport async function decrypt({ encryptedObject, keys }: DecryptOptions): Promise<Uint8Array> {\n\tif (!encryptedObject.encryptedShares.BonehFranklinBLS12381) {\n\t\tthrow new UnsupportedFeatureError('Encryption mode not supported');\n\t}\n\n\tconst fullId = createFullId(encryptedObject.packageId, encryptedObject.id);\n\n\t// Get the indices of the service whose keys are in the keystore.\n\tconst inKeystore = encryptedObject.services\n\t\t.map((_, i) => i)\n\t\t.filter((i) => keys.has(`${fullId}:${encryptedObject.services[i][0]}`));\n\n\tif (inKeystore.length < encryptedObject.threshold) {\n\t\tthrow new Error('Not enough shares. Please fetch more keys.');\n\t}\n\n\tconst encryptedShares = encryptedObject.encryptedShares.BonehFranklinBLS12381.encryptedShares;\n\tif (encryptedShares.length !== encryptedObject.services.length) {\n\t\tthrow new InvalidCiphertextError(\n\t\t\t`Mismatched shares ${encryptedShares.length} and services ${encryptedObject.services.length}`,\n\t\t);\n\t}\n\n\tconst nonce = G2Element.fromBytes(encryptedObject.encryptedShares.BonehFranklinBLS12381.nonce);\n\n\t// Decrypt each share.\n\tconst shares = inKeystore.map((i) => {\n\t\tconst [objectId, index] = encryptedObject.services[i];\n\t\t// Use the index as the unique info parameter to allow for multiple shares per key server.\n\t\tconst share = BonehFranklinBLS12381Services.decrypt(\n\t\t\tnonce,\n\t\t\tkeys.get(`${fullId}:${objectId}`)!,\n\t\t\tencryptedShares[i],\n\t\t\tfromHex(fullId),\n\t\t\t[objectId, index],\n\t\t);\n\t\t// The Shamir secret sharing library expects the index/x-coordinate to be at the end of the share.\n\t\treturn { index, share };\n\t});\n\n\t// Combine the decrypted shares into the key.\n\tconst baseKey = await combine(shares);\n\n\tconst demKey = deriveKey(\n\t\tKeyPurpose.DEM,\n\t\tbaseKey,\n\t\tencryptedObject.encryptedShares.BonehFranklinBLS12381.encryptedShares,\n\t\tencryptedObject.threshold,\n\t\tencryptedObject.services.map(([objectId, _]) => objectId),\n\t);\n\n\tif (encryptedObject.ciphertext.Aes256Gcm) {\n\t\treturn AesGcm256.decrypt(demKey, encryptedObject.ciphertext);\n\t} else if (encryptedObject.ciphertext.Hmac256Ctr) {\n\t\treturn Hmac256Ctr.decrypt(demKey, encryptedObject.ciphertext);\n\t} else if (encryptedObject.ciphertext.Plain) {\n\t\t// In case `Plain` mode is used, return the key.\n\t\treturn demKey;\n\t} else {\n\t\tthrow new InvalidCiphertextError('Invalid ciphertext type');\n\t}\n}\n\n/**\n * Helper function that combines the shares into the key.\n * @param shares - The shares to combine.\n * @returns - The combined key.\n */\nasync function combine(shares: { index: number; share: Uint8Array }[]): Promise<Uint8Array> {\n\tif (shares.length === 0) {\n\t\tthrow new Error('Invalid shares length');\n\t} else if (shares.length === 1) {\n\t\t// The Shamir secret sharing library expects at least two shares.\n\t\t// If there is only one and the threshold is 1, the reconstructed secret is the same as the share.\n\t\treturn Promise.resolve(shares[0].share);\n\t}\n\n\t// The Shamir secret sharing library expects the index/x-coordinate to be at the end of the share\n\treturn externalCombine(\n\t\tshares.map(({ index, share }) => flatten([share, new Uint8Array([index])])),\n\t);\n}\n"],
5
+ "mappings": "AAGA,SAAS,eAAe;AACxB,SAAS,WAAW,uBAAuB;AAI3C,SAAS,iBAAiB;AAC1B,SAAS,WAAW,kBAAkB;AACtC,SAAS,wBAAwB,+BAA+B;AAChE,SAAS,qCAAqC;AAC9C,SAAS,WAAW,kBAAkB;AAEtC,SAAS,cAAc,eAAe;AActC,eAAsB,QAAQ,EAAE,iBAAiB,KAAK,GAAwC;AAC7F,MAAI,CAAC,gBAAgB,gBAAgB,uBAAuB;AAC3D,UAAM,IAAI,wBAAwB,+BAA+B;AAAA,EAClE;AAEA,QAAM,SAAS,aAAa,gBAAgB,WAAW,gBAAgB,EAAE;AAGzE,QAAM,aAAa,gBAAgB,SACjC,IAAI,CAAC,GAAG,MAAM,CAAC,EACf,OAAO,CAAC,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,gBAAgB,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;AAEvE,MAAI,WAAW,SAAS,gBAAgB,WAAW;AAClD,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC7D;AAEA,QAAM,kBAA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,QAAQ,QAAQ,OAAO,CAAC,EAAE,KAAK;AAAA,EACvC;AAGA,SAAO;AAAA,IACN,OAAO,IAAI,CAAC,EAAE,OAAO,MAAM,MAAM,QAAQ,CAAC,OAAO,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAAA,EAC3E;AACD;",
6
6
  "names": []
7
7
  }
package/dist/esm/dem.js CHANGED
@@ -2,8 +2,8 @@ import { bcs } from "@mysten/bcs";
2
2
  import { equalBytes } from "@noble/curves/abstract/utils";
3
3
  import { hmac } from "@noble/hashes/hmac";
4
4
  import { sha3_256 } from "@noble/hashes/sha3";
5
- import { InvalidCiphertextError } from "./error.js";
6
- import { xorUnchecked } from "./utils.js";
5
+ import { DecryptionError, InvalidCiphertextError } from "./error.js";
6
+ import { flatten, xorUnchecked } from "./utils.js";
7
7
  const iv = Uint8Array.from([
8
8
  138,
9
9
  55,
@@ -65,18 +65,22 @@ class AesGcm256 {
65
65
  if (!("Aes256Gcm" in ciphertext)) {
66
66
  throw new InvalidCiphertextError(`Invalid ciphertext ${ciphertext}`);
67
67
  }
68
- const aesCryptoKey = await crypto.subtle.importKey("raw", key, "AES-GCM", false, ["decrypt"]);
69
- return new Uint8Array(
70
- await crypto.subtle.decrypt(
71
- {
72
- name: "AES-GCM",
73
- iv,
74
- additionalData: new Uint8Array(ciphertext.Aes256Gcm.aad ?? [])
75
- },
76
- aesCryptoKey,
77
- new Uint8Array(ciphertext.Aes256Gcm.blob)
78
- )
79
- );
68
+ try {
69
+ const aesCryptoKey = await crypto.subtle.importKey("raw", key, "AES-GCM", false, ["decrypt"]);
70
+ return new Uint8Array(
71
+ await crypto.subtle.decrypt(
72
+ {
73
+ name: "AES-GCM",
74
+ iv,
75
+ additionalData: new Uint8Array(ciphertext.Aes256Gcm.aad ?? [])
76
+ },
77
+ aesCryptoKey,
78
+ new Uint8Array(ciphertext.Aes256Gcm.blob)
79
+ )
80
+ );
81
+ } catch (e) {
82
+ throw new DecryptionError(`Decryption failed`);
83
+ }
80
84
  }
81
85
  }
82
86
  class Plain {
@@ -116,23 +120,21 @@ class Hmac256Ctr {
116
120
  const blob = new Uint8Array(ciphertext.Hmac256Ctr.blob);
117
121
  const mac = Hmac256Ctr.computeMac(key, aad, blob);
118
122
  if (!equalBytes(mac, new Uint8Array(ciphertext.Hmac256Ctr.mac))) {
119
- throw new InvalidCiphertextError(`Invalid MAC ${mac}`);
123
+ throw new DecryptionError(`Invalid MAC ${mac}`);
120
124
  }
121
125
  return Hmac256Ctr.encryptInCtrMode(key, blob);
122
126
  }
123
127
  static computeMac(key, aad, ciphertext) {
124
- const macKey = hmac(sha3_256, key, MacKeyTag);
125
- const macInput = new Uint8Array([...toBytes(aad.length), ...aad, ...ciphertext]);
126
- const mac = hmac(sha3_256, macKey, macInput);
128
+ const macInput = flatten([MacKeyTag, toBytes(aad.length), aad, ciphertext]);
129
+ const mac = hmac(sha3_256, key, macInput);
127
130
  return mac;
128
131
  }
129
132
  static encryptInCtrMode(key, msg) {
130
133
  const blockSize = 32;
131
- const result = Uint8Array.from({ length: msg.length }, () => 0);
132
- const encryptionKey = hmac(sha3_256, key, EncryptionKeyTag);
134
+ const result = new Uint8Array(msg.length);
133
135
  for (let i = 0; i * blockSize < msg.length; i++) {
134
136
  const block = msg.subarray(i * blockSize, (i + 1) * blockSize);
135
- const mask = hmac(sha3_256, encryptionKey, toBytes(i));
137
+ const mask = hmac(sha3_256, key, flatten([EncryptionKeyTag, toBytes(i)]));
136
138
  const encryptedBlock = xorUnchecked(block, mask);
137
139
  result.set(encryptedBlock, i * blockSize);
138
140
  }
@@ -142,8 +144,8 @@ class Hmac256Ctr {
142
144
  function toBytes(n) {
143
145
  return bcs.u64().serialize(n).toBytes();
144
146
  }
145
- const EncryptionKeyTag = new Uint8Array([1]);
146
- const MacKeyTag = new Uint8Array([2]);
147
+ const EncryptionKeyTag = new TextEncoder().encode("HMAC-CTR-ENC");
148
+ const MacKeyTag = new TextEncoder().encode("HMAC-CTR-MAC");
147
149
  export {
148
150
  AesGcm256,
149
151
  Hmac256Ctr,