@mysten/seal 0.2.0 → 0.3.0

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 (108) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/cjs/bcs.d.ts +132 -0
  3. package/dist/cjs/bcs.js +67 -0
  4. package/dist/cjs/bcs.js.map +7 -0
  5. package/dist/cjs/client.d.ts +83 -0
  6. package/dist/cjs/client.js +283 -0
  7. package/dist/cjs/client.js.map +7 -0
  8. package/dist/cjs/decrypt.d.ts +15 -0
  9. package/dist/cjs/decrypt.js +93 -0
  10. package/dist/cjs/decrypt.js.map +7 -0
  11. package/dist/cjs/dem.d.ts +36 -0
  12. package/dist/cjs/dem.js +173 -0
  13. package/dist/cjs/dem.js.map +7 -0
  14. package/dist/cjs/elgamal.js.map +2 -2
  15. package/dist/cjs/encrypt.d.ts +14 -5
  16. package/dist/cjs/encrypt.js +51 -21
  17. package/dist/cjs/encrypt.js.map +3 -3
  18. package/dist/cjs/error.d.ts +58 -0
  19. package/dist/cjs/error.js +175 -0
  20. package/dist/cjs/error.js.map +7 -0
  21. package/dist/cjs/ibe.d.ts +10 -14
  22. package/dist/cjs/ibe.js +23 -26
  23. package/dist/cjs/ibe.js.map +2 -2
  24. package/dist/cjs/index.d.ts +2 -5
  25. package/dist/cjs/index.js +3 -11
  26. package/dist/cjs/index.js.map +2 -2
  27. package/dist/cjs/kdf.d.ts +5 -0
  28. package/dist/cjs/kdf.js +28 -1
  29. package/dist/cjs/kdf.js.map +3 -3
  30. package/dist/cjs/key-server.d.ts +5 -5
  31. package/dist/cjs/key-server.js +24 -21
  32. package/dist/cjs/key-server.js.map +2 -2
  33. package/dist/cjs/keys.d.ts +17 -0
  34. package/dist/cjs/keys.js +61 -0
  35. package/dist/cjs/keys.js.map +7 -0
  36. package/dist/cjs/session-key.d.ts +20 -14
  37. package/dist/cjs/session-key.js +90 -23
  38. package/dist/cjs/session-key.js.map +2 -2
  39. package/dist/cjs/types.d.ts +1 -86
  40. package/dist/cjs/types.js +0 -32
  41. package/dist/cjs/types.js.map +2 -2
  42. package/dist/cjs/utils.d.ts +2 -1
  43. package/dist/cjs/utils.js +17 -5
  44. package/dist/cjs/utils.js.map +2 -2
  45. package/dist/cjs/version.d.ts +1 -0
  46. package/dist/cjs/version.js +25 -0
  47. package/dist/cjs/version.js.map +7 -0
  48. package/dist/esm/bcs.d.ts +132 -0
  49. package/dist/esm/bcs.js +47 -0
  50. package/dist/esm/bcs.js.map +7 -0
  51. package/dist/esm/client.d.ts +83 -0
  52. package/dist/esm/client.js +268 -0
  53. package/dist/esm/client.js.map +7 -0
  54. package/dist/esm/decrypt.d.ts +15 -0
  55. package/dist/esm/decrypt.js +73 -0
  56. package/dist/esm/decrypt.js.map +7 -0
  57. package/dist/esm/dem.d.ts +36 -0
  58. package/dist/esm/dem.js +153 -0
  59. package/dist/esm/dem.js.map +7 -0
  60. package/dist/esm/elgamal.js.map +2 -2
  61. package/dist/esm/encrypt.d.ts +14 -5
  62. package/dist/esm/encrypt.js +48 -18
  63. package/dist/esm/encrypt.js.map +3 -3
  64. package/dist/esm/error.d.ts +58 -0
  65. package/dist/esm/error.js +155 -0
  66. package/dist/esm/error.js.map +7 -0
  67. package/dist/esm/ibe.d.ts +10 -14
  68. package/dist/esm/ibe.js +23 -26
  69. package/dist/esm/ibe.js.map +2 -2
  70. package/dist/esm/index.d.ts +2 -5
  71. package/dist/esm/index.js +4 -16
  72. package/dist/esm/index.js.map +2 -2
  73. package/dist/esm/kdf.d.ts +5 -0
  74. package/dist/esm/kdf.js +28 -1
  75. package/dist/esm/kdf.js.map +3 -3
  76. package/dist/esm/key-server.d.ts +5 -5
  77. package/dist/esm/key-server.js +29 -21
  78. package/dist/esm/key-server.js.map +2 -2
  79. package/dist/esm/keys.d.ts +17 -0
  80. package/dist/esm/keys.js +41 -0
  81. package/dist/esm/keys.js.map +7 -0
  82. package/dist/esm/session-key.d.ts +20 -14
  83. package/dist/esm/session-key.js +95 -24
  84. package/dist/esm/session-key.js.map +2 -2
  85. package/dist/esm/types.d.ts +1 -86
  86. package/dist/esm/types.js +0 -28
  87. package/dist/esm/types.js.map +3 -3
  88. package/dist/esm/utils.d.ts +2 -1
  89. package/dist/esm/utils.js +17 -5
  90. package/dist/esm/utils.js.map +2 -2
  91. package/dist/esm/version.d.ts +1 -0
  92. package/dist/esm/version.js +5 -0
  93. package/dist/esm/version.js.map +7 -0
  94. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  95. package/dist/tsconfig.tsbuildinfo +1 -1
  96. package/package.json +6 -5
  97. package/dist/cjs/aes.d.ts +0 -18
  98. package/dist/cjs/aes.js +0 -111
  99. package/dist/cjs/aes.js.map +0 -7
  100. package/dist/cjs/key-store.d.ts +0 -49
  101. package/dist/cjs/key-store.js +0 -203
  102. package/dist/cjs/key-store.js.map +0 -7
  103. package/dist/esm/aes.d.ts +0 -18
  104. package/dist/esm/aes.js +0 -91
  105. package/dist/esm/aes.js.map +0 -7
  106. package/dist/esm/key-store.d.ts +0 -49
  107. package/dist/esm/key-store.js +0 -183
  108. package/dist/esm/key-store.js.map +0 -7
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
23
+ 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);
24
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
25
+ var error_exports = {};
26
+ __export(error_exports, {
27
+ ExpiredSessionKeyError: () => ExpiredSessionKeyError,
28
+ GeneralError: () => GeneralError,
29
+ InconsistentKeyServersError: () => InconsistentKeyServersError,
30
+ InternalError: () => InternalError,
31
+ InvalidCiphertextError: () => InvalidCiphertextError,
32
+ InvalidGetObjectError: () => InvalidGetObjectError,
33
+ InvalidKeyServerError: () => InvalidKeyServerError,
34
+ InvalidPTBError: () => InvalidPTBError,
35
+ InvalidPackageError: () => InvalidPackageError,
36
+ InvalidPersonalMessageSignatureError: () => InvalidPersonalMessageSignatureError,
37
+ InvalidSessionKeySignatureError: () => InvalidSessionKeySignatureError,
38
+ InvalidThresholdError: () => InvalidThresholdError,
39
+ InvalidUserSignatureError: () => InvalidUserSignatureError,
40
+ NoAccessError: () => NoAccessError,
41
+ OldPackageError: () => OldPackageError,
42
+ SealAPIError: () => SealAPIError,
43
+ SealError: () => SealError,
44
+ UnsupportedFeatureError: () => UnsupportedFeatureError,
45
+ UnsupportedNetworkError: () => UnsupportedNetworkError,
46
+ UserError: () => UserError,
47
+ toMajorityError: () => toMajorityError
48
+ });
49
+ module.exports = __toCommonJS(error_exports);
50
+ var _SealAPIError_static, generate_fn;
51
+ class SealError extends Error {
52
+ }
53
+ class UserError extends SealError {
54
+ }
55
+ const _SealAPIError = class _SealAPIError extends SealError {
56
+ constructor(message, requestId, status) {
57
+ super(message);
58
+ this.requestId = requestId;
59
+ this.status = status;
60
+ }
61
+ static async assertResponse(response, requestId) {
62
+ var _a;
63
+ if (response.ok) {
64
+ return;
65
+ }
66
+ let errorInstance;
67
+ try {
68
+ const text = await response.text();
69
+ const error = JSON.parse(text)["error"];
70
+ errorInstance = __privateMethod(_a = _SealAPIError, _SealAPIError_static, generate_fn).call(_a, error, requestId);
71
+ } catch (e) {
72
+ errorInstance = new GeneralError(response.statusText, requestId, response.status);
73
+ }
74
+ throw errorInstance;
75
+ }
76
+ };
77
+ _SealAPIError_static = new WeakSet();
78
+ generate_fn = function(message, requestId, status) {
79
+ switch (message) {
80
+ case "InvalidPTB":
81
+ return new InvalidPTBError(requestId);
82
+ case "InvalidPackage":
83
+ return new InvalidPackageError(requestId);
84
+ case "NoAccess":
85
+ return new NoAccessError(requestId);
86
+ case "InvalidCertificate":
87
+ return new ExpiredSessionKeyError(requestId);
88
+ case "OldPackageVersion":
89
+ return new OldPackageError(requestId);
90
+ case "InvalidSignature":
91
+ return new InvalidUserSignatureError(requestId);
92
+ case "InvalidSessionSignature":
93
+ return new InvalidSessionKeySignatureError(requestId);
94
+ case "Failure":
95
+ return new InternalError(requestId);
96
+ default:
97
+ return new GeneralError(message, requestId, status);
98
+ }
99
+ };
100
+ __privateAdd(_SealAPIError, _SealAPIError_static);
101
+ let SealAPIError = _SealAPIError;
102
+ class InvalidPTBError extends SealAPIError {
103
+ constructor(requestId) {
104
+ super("PTB does not conform to the expected format", requestId);
105
+ }
106
+ }
107
+ class InvalidPackageError extends SealAPIError {
108
+ constructor(requestId) {
109
+ super("Package ID used in PTB is invalid", requestId);
110
+ }
111
+ }
112
+ class OldPackageError extends SealAPIError {
113
+ constructor(requestId) {
114
+ super("PTB must call the latest version of the package", requestId);
115
+ }
116
+ }
117
+ class InvalidUserSignatureError extends SealAPIError {
118
+ constructor(requestId) {
119
+ super("User signature on the session key is invalid", requestId);
120
+ }
121
+ }
122
+ class InvalidSessionKeySignatureError extends SealAPIError {
123
+ constructor(requestId) {
124
+ super("Session key signature is invalid", requestId);
125
+ }
126
+ }
127
+ class NoAccessError extends SealAPIError {
128
+ constructor(requestId) {
129
+ super("User does not have access to one or more of the requested keys", requestId);
130
+ }
131
+ }
132
+ class ExpiredSessionKeyError extends SealAPIError {
133
+ constructor(requestId) {
134
+ super("Session key has expired", requestId);
135
+ }
136
+ }
137
+ class InternalError extends SealAPIError {
138
+ constructor(requestId) {
139
+ super("Internal server error, caller should retry", requestId);
140
+ }
141
+ }
142
+ class GeneralError extends SealAPIError {
143
+ }
144
+ class InvalidPersonalMessageSignatureError extends UserError {
145
+ }
146
+ class InvalidGetObjectError extends UserError {
147
+ }
148
+ class UnsupportedFeatureError extends UserError {
149
+ }
150
+ class UnsupportedNetworkError extends UserError {
151
+ }
152
+ class InvalidKeyServerError extends UserError {
153
+ }
154
+ class InvalidCiphertextError extends UserError {
155
+ }
156
+ class InvalidThresholdError extends UserError {
157
+ }
158
+ class InconsistentKeyServersError extends UserError {
159
+ }
160
+ function toMajorityError(errors) {
161
+ let maxCount = 0;
162
+ let majorityError = errors[0];
163
+ const counts = /* @__PURE__ */ new Map();
164
+ for (const error of errors) {
165
+ const errorName = error.constructor.name;
166
+ const newCount = (counts.get(errorName) || 0) + 1;
167
+ counts.set(errorName, newCount);
168
+ if (newCount > maxCount) {
169
+ maxCount = newCount;
170
+ majorityError = error;
171
+ }
172
+ }
173
+ return majorityError;
174
+ }
175
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/error.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport class SealError extends Error {}\n\nexport class UserError extends SealError {}\n\n// Errors returned by the Seal server\nexport class SealAPIError extends SealError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic requestId?: string,\n\t\tpublic status?: number,\n\t) {\n\t\tsuper(message);\n\t}\n\n\tstatic #generate(message: string, requestId: string, status?: number) {\n\t\tswitch (message) {\n\t\t\tcase 'InvalidPTB':\n\t\t\t\treturn new InvalidPTBError(requestId);\n\t\t\tcase 'InvalidPackage':\n\t\t\t\treturn new InvalidPackageError(requestId);\n\t\t\tcase 'NoAccess':\n\t\t\t\treturn new NoAccessError(requestId);\n\t\t\tcase 'InvalidCertificate':\n\t\t\t\treturn new ExpiredSessionKeyError(requestId);\n\t\t\tcase 'OldPackageVersion':\n\t\t\t\treturn new OldPackageError(requestId);\n\t\t\tcase 'InvalidSignature':\n\t\t\t\treturn new InvalidUserSignatureError(requestId);\n\t\t\tcase 'InvalidSessionSignature':\n\t\t\t\treturn new InvalidSessionKeySignatureError(requestId);\n\t\t\tcase 'Failure':\n\t\t\t\treturn new InternalError(requestId);\n\t\t\tdefault:\n\t\t\t\treturn new GeneralError(message, requestId, status);\n\t\t}\n\t}\n\n\tstatic async assertResponse(response: Response, requestId: string) {\n\t\tif (response.ok) {\n\t\t\treturn;\n\t\t}\n\t\tlet errorInstance: SealAPIError;\n\t\ttry {\n\t\t\tconst text = await response.text();\n\t\t\tconst error = JSON.parse(text)['error'];\n\t\t\terrorInstance = SealAPIError.#generate(error, requestId);\n\t\t} catch (e) {\n\t\t\t// If we can't parse the response as JSON or if it doesn't have the expected format,\n\t\t\t// fall back to using the status text\n\t\t\terrorInstance = new GeneralError(response.statusText, requestId, response.status);\n\t\t}\n\t\tthrow errorInstance;\n\t}\n}\n\n// Errors returned by the Seal server that indicate that the PTB is invalid\n\nexport class InvalidPTBError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('PTB does not conform to the expected format', requestId);\n\t}\n}\n\nexport class InvalidPackageError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Package ID used in PTB is invalid', requestId);\n\t}\n}\n\nexport class OldPackageError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('PTB must call the latest version of the package', requestId);\n\t}\n}\n\n// Errors returned by the Seal server that indicate that the user's signature is invalid\n\nexport class InvalidUserSignatureError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('User signature on the session key is invalid', requestId);\n\t}\n}\n\nexport class InvalidSessionKeySignatureError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Session key signature is invalid', requestId);\n\t}\n}\n\n/** Server error indicating that the user does not have access to one or more of the requested keys */\nexport class NoAccessError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('User does not have access to one or more of the requested keys', requestId);\n\t}\n}\n\n/** Server error indicating that the session key has expired */\nexport class ExpiredSessionKeyError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Session key has expired', requestId);\n\t}\n}\n\n/** Internal server error, caller should retry */\nexport class InternalError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Internal server error, caller should retry', requestId);\n\t}\n}\n\n/** General server errors that are not specific to the Seal API (e.g., 404 \"Not Found\") */\nexport class GeneralError extends SealAPIError {}\n\n// Errors returned by the SDK\nexport class InvalidPersonalMessageSignatureError extends UserError {}\nexport class InvalidGetObjectError extends UserError {}\nexport class UnsupportedFeatureError extends UserError {}\nexport class UnsupportedNetworkError extends UserError {}\nexport class InvalidKeyServerError extends UserError {}\nexport class InvalidCiphertextError extends UserError {}\nexport class InvalidThresholdError extends UserError {}\nexport class InconsistentKeyServersError extends UserError {}\n\nexport function toMajorityError(errors: Error[]): Error {\n\tlet maxCount = 0;\n\tlet majorityError = errors[0];\n\tconst counts = new Map<string, number>();\n\tfor (const error of errors) {\n\t\tconst errorName = error.constructor.name;\n\t\tconst newCount = (counts.get(errorName) || 0) + 1;\n\t\tcounts.set(errorName, newCount);\n\n\t\tif (newCount > maxCount) {\n\t\t\tmaxCount = newCount;\n\t\t\tmajorityError = error;\n\t\t}\n\t}\n\n\treturn majorityError;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,MAAM,kBAA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BAAgB;AAAA,IACjB;AAAA,EACD;AAEA,SAAO;AACR;",
6
+ "names": []
7
+ }
package/dist/cjs/ibe.d.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import type { IBEEncryptions } from './bcs.js';
1
2
  import { G1Element, G2Element } from './bls12381.js';
2
3
  import type { KeyServer } from './key-server.js';
3
- import type { IBEEncryptionsType } from './types.js';
4
4
  /**
5
5
  * The domain separation tag for the hash-to-group function.
6
6
  */
@@ -13,12 +13,8 @@ export declare const DST_POP: Uint8Array;
13
13
  * The interface for the key servers.
14
14
  */
15
15
  export declare abstract class IBEServers {
16
- protected readonly object_ids: Uint8Array[];
17
- protected constructor(object_ids: Uint8Array[]);
18
- /**
19
- * The object IDs of the key servers.
20
- */
21
- getObjectIds(): Uint8Array[];
16
+ objectIds: string[];
17
+ constructor(objectIds: string[]);
22
18
  /**
23
19
  * The number of key servers.
24
20
  */
@@ -27,25 +23,25 @@ export declare abstract class IBEServers {
27
23
  * Encrypt a batch of messages for the given identity.
28
24
  *
29
25
  * @param id The identity.
30
- * @param msg_and_infos The messages and an additional info parameter which will be included in the KDF.
26
+ * @param msgAndInfos The messages and an additional info parameter which will be included in the KDF.
31
27
  * @returns The encrypted messages.
32
28
  */
33
- abstract encryptBatched(id: Uint8Array, msg_and_infos: {
29
+ abstract encryptBatched(id: Uint8Array, msgAndInfos: {
34
30
  msg: Uint8Array;
35
31
  info: Uint8Array;
36
- }[]): IBEEncryptionsType;
32
+ }[], randomnessKey: Uint8Array): typeof IBEEncryptions.$inferType;
37
33
  }
38
34
  /**
39
35
  * Identity-based encryption based on the Boneh-Franklin IBE scheme.
40
36
  * This object represents a set of key servers that can be used to encrypt messages for a given identity.
41
37
  */
42
38
  export declare class BonehFranklinBLS12381Services extends IBEServers {
43
- readonly public_keys: G2Element[];
39
+ readonly publicKeys: G2Element[];
44
40
  constructor(services: KeyServer[]);
45
- encryptBatched(id: Uint8Array, msg_and_infos: {
41
+ encryptBatched(id: Uint8Array, msgAndInfos: {
46
42
  msg: Uint8Array;
47
43
  info: Uint8Array;
48
- }[]): IBEEncryptionsType;
44
+ }[], randomnessKey: Uint8Array): typeof IBEEncryptions.$inferType;
49
45
  /**
50
46
  * Returns true if the user secret key is valid for the given public key and id.
51
47
  * @param user_secret_key - The user secret key.
@@ -53,7 +49,7 @@ export declare class BonehFranklinBLS12381Services extends IBEServers {
53
49
  * @param public_key - The public key.
54
50
  * @returns True if the user secret key is valid for the given public key and id.
55
51
  */
56
- static verifyUserSecretKey(user_secret_key: G1Element, id: Uint8Array, public_key: G2Element): boolean;
52
+ static verifyUserSecretKey(userSecretKey: G1Element, id: string, publicKey: G2Element): boolean;
57
53
  /**
58
54
  * Identity-based decryption.
59
55
  *
package/dist/cjs/ibe.js CHANGED
@@ -24,45 +24,42 @@ __export(ibe_exports, {
24
24
  IBEServers: () => IBEServers
25
25
  });
26
26
  module.exports = __toCommonJS(ibe_exports);
27
+ var import_bcs = require("@mysten/bcs");
27
28
  var import_bls12381 = require("./bls12381.js");
28
29
  var import_kdf = require("./kdf.js");
29
30
  var import_utils = require("./utils.js");
30
31
  const DST = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-00");
31
- const DST_POP = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-00-POP");
32
+ const DST_POP = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-POP-00");
32
33
  class IBEServers {
33
- constructor(object_ids) {
34
- this.object_ids = object_ids;
35
- }
36
- /**
37
- * The object IDs of the key servers.
38
- */
39
- getObjectIds() {
40
- return this.object_ids;
34
+ constructor(objectIds) {
35
+ this.objectIds = objectIds;
41
36
  }
42
37
  /**
43
38
  * The number of key servers.
44
39
  */
45
40
  size() {
46
- return this.object_ids.length;
41
+ return this.objectIds.length;
47
42
  }
48
43
  }
49
44
  class BonehFranklinBLS12381Services extends IBEServers {
50
45
  constructor(services) {
51
46
  super(services.map((service) => service.objectId));
52
- this.public_keys = services.map((service) => import_bls12381.G2Element.fromBytes(service.pk));
47
+ this.publicKeys = services.map((service) => import_bls12381.G2Element.fromBytes(service.pk));
53
48
  }
54
- encryptBatched(id, msg_and_infos) {
55
- if (this.public_keys.length === 0 || this.public_keys.length !== msg_and_infos.length) {
56
- throw new Error("Invalid input");
49
+ encryptBatched(id, msgAndInfos, randomnessKey) {
50
+ if (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndInfos.length) {
51
+ throw new Error("Invalid public keys");
57
52
  }
58
- const [nonce, keys] = encapBatched(this.public_keys, id);
59
- const encrypted_msgs = msg_and_infos.map(
60
- (msg_and_info, i) => (0, import_utils.xor)(msg_and_info.msg, (0, import_kdf.kdf)(keys[i], msg_and_info.info))
53
+ const [r, nonce, keys] = encapBatched(this.publicKeys, id);
54
+ const encryptedShares = msgAndInfos.map(
55
+ (msgAndInfo, i) => (0, import_utils.xor)(msgAndInfo.msg, (0, import_kdf.kdf)(keys[i], msgAndInfo.info))
61
56
  );
57
+ const encryptedRandomness = (0, import_utils.xor)(randomnessKey, r.toBytes());
62
58
  return {
63
59
  BonehFranklinBLS12381: {
64
- encapsulation: nonce.toBytes(),
65
- shares: encrypted_msgs
60
+ nonce: nonce.toBytes(),
61
+ encryptedShares,
62
+ encryptedRandomness
66
63
  },
67
64
  $kind: "BonehFranklinBLS12381"
68
65
  };
@@ -74,9 +71,9 @@ class BonehFranklinBLS12381Services extends IBEServers {
74
71
  * @param public_key - The public key.
75
72
  * @returns True if the user secret key is valid for the given public key and id.
76
73
  */
77
- static verifyUserSecretKey(user_secret_key, id, public_key) {
78
- const lhs = user_secret_key.pairing(import_bls12381.G2Element.generator()).toBytes();
79
- const rhs = import_bls12381.G1Element.hashToCurve(id).pairing(public_key).toBytes();
74
+ static verifyUserSecretKey(userSecretKey, id, publicKey) {
75
+ const lhs = userSecretKey.pairing(import_bls12381.G2Element.generator()).toBytes();
76
+ const rhs = import_bls12381.G1Element.hashToCurve((0, import_bcs.fromHex)(id)).pairing(publicKey).toBytes();
80
77
  return lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);
81
78
  }
82
79
  /**
@@ -92,14 +89,14 @@ class BonehFranklinBLS12381Services extends IBEServers {
92
89
  return (0, import_utils.xor)(ciphertext, (0, import_kdf.kdf)(decap(nonce, sk), info));
93
90
  }
94
91
  }
95
- function encapBatched(public_keys, id) {
96
- if (public_keys.length === 0) {
97
- throw new Error("Invalid input");
92
+ function encapBatched(publicKeys, id) {
93
+ if (publicKeys.length === 0) {
94
+ throw new Error("No public keys provided");
98
95
  }
99
96
  const r = import_bls12381.Scalar.random();
100
97
  const nonce = import_bls12381.G2Element.generator().multiply(r);
101
98
  const gid = import_bls12381.G1Element.hashToCurve(id).multiply(r);
102
- return [nonce, public_keys.map((public_key) => gid.pairing(public_key))];
99
+ return [r, nonce, publicKeys.map((public_key) => gid.pairing(public_key))];
103
100
  }
104
101
  function decap(nonce, usk) {
105
102
  return usk.pairing(nonce);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/ibe.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { GTElement } from './bls12381.js';\nimport { G1Element, G2Element, Scalar } from './bls12381.js';\nimport { kdf } from './kdf.js';\nimport type { KeyServer } from './key-server.js';\nimport type { IBEEncryptionsType } from './types.js';\nimport { xor } from './utils.js';\n\n/**\n * The domain separation tag for the hash-to-group function.\n */\nexport const DST: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-00');\n\n/**\n * The domain separation tag for the signing proof of possession.\n */\nexport const DST_POP: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-00-POP');\n\n/**\n * The interface for the key servers.\n */\nexport abstract class IBEServers {\n\tprotected readonly object_ids: Uint8Array[];\n\n\tprotected constructor(object_ids: Uint8Array[]) {\n\t\tthis.object_ids = object_ids;\n\t}\n\n\t/**\n\t * The object IDs of the key servers.\n\t */\n\tgetObjectIds(): Uint8Array[] {\n\t\treturn this.object_ids;\n\t}\n\n\t/**\n\t * The number of key servers.\n\t */\n\tsize(): number {\n\t\treturn this.object_ids.length;\n\t}\n\n\t/**\n\t * Encrypt a batch of messages for the given identity.\n\t *\n\t * @param id The identity.\n\t * @param msg_and_infos The messages and an additional info parameter which will be included in the KDF.\n\t * @returns The encrypted messages.\n\t */\n\tabstract encryptBatched(\n\t\tid: Uint8Array,\n\t\tmsg_and_infos: { msg: Uint8Array; info: Uint8Array }[],\n\t): IBEEncryptionsType;\n}\n\n/**\n * Identity-based encryption based on the Boneh-Franklin IBE scheme.\n * This object represents a set of key servers that can be used to encrypt messages for a given identity.\n */\nexport class BonehFranklinBLS12381Services extends IBEServers {\n\treadonly public_keys: G2Element[];\n\n\tconstructor(services: KeyServer[]) {\n\t\tsuper(services.map((service) => service.objectId));\n\t\tthis.public_keys = services.map((service) => G2Element.fromBytes(service.pk));\n\t}\n\n\tencryptBatched(\n\t\tid: Uint8Array,\n\t\tmsg_and_infos: { msg: Uint8Array; info: Uint8Array }[],\n\t): IBEEncryptionsType {\n\t\tif (this.public_keys.length === 0 || this.public_keys.length !== msg_and_infos.length) {\n\t\t\tthrow new Error('Invalid input');\n\t\t}\n\t\tconst [nonce, keys] = encapBatched(this.public_keys, id);\n\t\tconst encrypted_msgs = msg_and_infos.map((msg_and_info, i) =>\n\t\t\txor(msg_and_info.msg, kdf(keys[i], msg_and_info.info)),\n\t\t);\n\n\t\treturn {\n\t\t\tBonehFranklinBLS12381: {\n\t\t\t\tencapsulation: nonce.toBytes(),\n\t\t\t\tshares: encrypted_msgs,\n\t\t\t},\n\t\t\t$kind: 'BonehFranklinBLS12381',\n\t\t};\n\t}\n\n\t/**\n\t * Returns true if the user secret key is valid for the given public key and id.\n\t * @param user_secret_key - The user secret key.\n\t * @param id - The identity.\n\t * @param public_key - The public key.\n\t * @returns True if the user secret key is valid for the given public key and id.\n\t */\n\tstatic verifyUserSecretKey(\n\t\tuser_secret_key: G1Element,\n\t\tid: Uint8Array,\n\t\tpublic_key: G2Element,\n\t): boolean {\n\t\tconst lhs = user_secret_key.pairing(G2Element.generator()).toBytes();\n\t\tconst rhs = G1Element.hashToCurve(id).pairing(public_key).toBytes();\n\t\treturn lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);\n\t}\n\n\t/**\n\t * Identity-based decryption.\n\t *\n\t * @param nonce The encryption nonce.\n\t * @param sk The user secret key.\n\t * @param ciphertext The encrypted message.\n\t * @param info An info parameter also included in the KDF.\n\t * @returns The decrypted message.\n\t */\n\tstatic decrypt(\n\t\tnonce: G2Element,\n\t\tsk: G1Element,\n\t\tciphertext: Uint8Array,\n\t\tinfo: Uint8Array,\n\t): Uint8Array {\n\t\treturn xor(ciphertext, kdf(decap(nonce, sk), info));\n\t}\n}\n\n/**\n * Batched identity-based key-encapsulation mechanism: encapsulate multiple keys for given identity using different key servers.\n *\n * @param public_keys Public keys for a set of key servers.\n * @param id The identity used to encapsulate the keys.\n * @returns A common nonce of the keys and a list of keys, 32 bytes each.\n */\nfunction encapBatched(public_keys: G2Element[], id: Uint8Array): [G2Element, GTElement[]] {\n\tif (public_keys.length === 0) {\n\t\tthrow new Error('Invalid input');\n\t}\n\tconst r = Scalar.random();\n\tconst nonce = G2Element.generator().multiply(r);\n\tconst gid = G1Element.hashToCurve(id).multiply(r);\n\treturn [nonce, public_keys.map((public_key) => gid.pairing(public_key))];\n}\n\n/**\n * Decapsulate a key using a user secret key and the nonce.\n *\n * @param usk The user secret key.\n * @param nonce The nonce.\n * @returns The encapsulated key.\n */\nfunction decap(nonce: G2Element, usk: G1Element): GTElement {\n\treturn usk.pairing(nonce);\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,sBAA6C;AAC7C,iBAAoB;AAGpB,mBAAoB;AAKb,MAAM,MAAkB,IAAI,YAAY,EAAE,OAAO,0BAA0B;AAK3E,MAAM,UAAsB,IAAI,YAAY,EAAE,OAAO,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A;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,QACN,OACA,IACA,YACA,MACa;AACb,eAAO,kBAAI,gBAAY,gBAAI,MAAM,OAAO,EAAE,GAAG,IAAI,CAAC;AAAA,EACnD;AACD;AASA,SAAS,aAAa,aAA0B,IAA0C;AACzF,MAAI,YAAY,WAAW,GAAG;AAC7B,UAAM,IAAI,MAAM,eAAe;AAAA,EAChC;AACA,QAAM,IAAI,uBAAO,OAAO;AACxB,QAAM,QAAQ,0BAAU,UAAU,EAAE,SAAS,CAAC;AAC9C,QAAM,MAAM,0BAAU,YAAY,EAAE,EAAE,SAAS,CAAC;AAChD,SAAO,CAAC,OAAO,YAAY,IAAI,CAAC,eAAe,IAAI,QAAQ,UAAU,CAAC,CAAC;AACxE;AASA,SAAS,MAAM,OAAkB,KAA2B;AAC3D,SAAO,IAAI,QAAQ,KAAK;AACzB;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@mysten/bcs';\n\nimport type { IBEEncryptions } from './bcs.js';\nimport type { GTElement } from './bls12381.js';\nimport { G1Element, G2Element, Scalar } from './bls12381.js';\nimport { kdf } from './kdf.js';\nimport type { KeyServer } from './key-server.js';\nimport { xor } from './utils.js';\n\n/**\n * The domain separation tag for the hash-to-group function.\n */\nexport const DST: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-00');\n\n/**\n * The domain separation tag for the signing proof of possession.\n */\nexport const DST_POP: Uint8Array = new TextEncoder().encode('SUI-SEAL-IBE-BLS12381-POP-00');\n\n/**\n * The interface for the key servers.\n */\nexport abstract class IBEServers {\n\tobjectIds: string[];\n\n\tconstructor(objectIds: string[]) {\n\t\tthis.objectIds = objectIds;\n\t}\n\n\t/**\n\t * The number of key servers.\n\t */\n\tsize(): number {\n\t\treturn this.objectIds.length;\n\t}\n\n\t/**\n\t * Encrypt a batch of messages for the given identity.\n\t *\n\t * @param id The identity.\n\t * @param msgAndInfos The messages and an additional info parameter which will be included in the KDF.\n\t * @returns The encrypted messages.\n\t */\n\tabstract encryptBatched(\n\t\tid: Uint8Array,\n\t\tmsgAndInfos: { msg: Uint8Array; info: Uint8Array }[],\n\t\trandomnessKey: Uint8Array,\n\t): typeof IBEEncryptions.$inferType;\n}\n\n/**\n * Identity-based encryption based on the Boneh-Franklin IBE scheme.\n * This object represents a set of key servers that can be used to encrypt messages for a given identity.\n */\nexport class BonehFranklinBLS12381Services extends IBEServers {\n\treadonly publicKeys: G2Element[];\n\n\tconstructor(services: KeyServer[]) {\n\t\tsuper(services.map((service) => service.objectId));\n\t\tthis.publicKeys = services.map((service) => G2Element.fromBytes(service.pk));\n\t}\n\n\tencryptBatched(\n\t\tid: Uint8Array,\n\t\tmsgAndInfos: { msg: Uint8Array; info: Uint8Array }[],\n\t\trandomnessKey: Uint8Array,\n\t): typeof IBEEncryptions.$inferType {\n\t\tif (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndInfos.length) {\n\t\t\tthrow new Error('Invalid public keys');\n\t\t}\n\t\tconst [r, nonce, keys] = encapBatched(this.publicKeys, id);\n\t\tconst encryptedShares = msgAndInfos.map((msgAndInfo, i) =>\n\t\t\txor(msgAndInfo.msg, kdf(keys[i], msgAndInfo.info)),\n\t\t);\n\t\tconst encryptedRandomness = xor(randomnessKey, r.toBytes());\n\n\t\treturn {\n\t\t\tBonehFranklinBLS12381: {\n\t\t\t\tnonce: nonce.toBytes(),\n\t\t\t\tencryptedShares,\n\t\t\t\tencryptedRandomness,\n\t\t\t},\n\t\t\t$kind: 'BonehFranklinBLS12381',\n\t\t};\n\t}\n\n\t/**\n\t * Returns true if the user secret key is valid for the given public key and id.\n\t * @param user_secret_key - The user secret key.\n\t * @param id - The identity.\n\t * @param public_key - The public key.\n\t * @returns True if the user secret key is valid for the given public key and id.\n\t */\n\tstatic verifyUserSecretKey(userSecretKey: G1Element, id: string, publicKey: G2Element): boolean {\n\t\tconst lhs = userSecretKey.pairing(G2Element.generator()).toBytes();\n\t\tconst rhs = G1Element.hashToCurve(fromHex(id)).pairing(publicKey).toBytes();\n\t\treturn lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);\n\t}\n\n\t/**\n\t * Identity-based decryption.\n\t *\n\t * @param nonce The encryption nonce.\n\t * @param sk The user secret key.\n\t * @param ciphertext The encrypted message.\n\t * @param info An info parameter also included in the KDF.\n\t * @returns The decrypted message.\n\t */\n\tstatic decrypt(\n\t\tnonce: G2Element,\n\t\tsk: G1Element,\n\t\tciphertext: Uint8Array,\n\t\tinfo: Uint8Array,\n\t): Uint8Array {\n\t\treturn xor(ciphertext, kdf(decap(nonce, sk), info));\n\t}\n}\n\n/**\n * Batched identity-based key-encapsulation mechanism: encapsulate multiple keys for given identity using different key servers.\n *\n * @param publicKeys Public keys for a set of key servers.\n * @param id The identity used to encapsulate the keys.\n * @returns A common nonce of the keys and a list of keys, 32 bytes each.\n */\nfunction encapBatched(publicKeys: G2Element[], id: Uint8Array): [Scalar, G2Element, GTElement[]] {\n\tif (publicKeys.length === 0) {\n\t\tthrow new Error('No public keys provided');\n\t}\n\tconst r = Scalar.random();\n\tconst nonce = G2Element.generator().multiply(r);\n\tconst gid = G1Element.hashToCurve(id).multiply(r);\n\treturn [r, nonce, publicKeys.map((public_key) => gid.pairing(public_key))];\n}\n\n/**\n * Decapsulate a key using a user secret key and the nonce.\n *\n * @param usk The user secret key.\n * @param nonce The nonce.\n * @returns The encapsulated key.\n */\nfunction decap(nonce: G2Element, usk: G1Element): GTElement {\n\treturn usk.pairing(nonce);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAwB;AAIxB,sBAA6C;AAC7C,iBAAoB;AAEpB,mBAAoB;AAKb,MAAM,MAAkB,IAAI,YAAY,EAAE,OAAO,0BAA0B;AAK3E,MAAM,UAAsB,IAAI,YAAY,EAAE,OAAO,8BAA8B;AAK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A;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,QACN,OACA,IACA,YACA,MACa;AACb,eAAO,kBAAI,gBAAY,gBAAI,MAAM,OAAO,EAAE,GAAG,IAAI,CAAC;AAAA,EACnD;AACD;AASA,SAAS,aAAa,YAAyB,IAAkD;AAChG,MAAI,WAAW,WAAW,GAAG;AAC5B,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC1C;AACA,QAAM,IAAI,uBAAO,OAAO;AACxB,QAAM,QAAQ,0BAAU,UAAU,EAAE,SAAS,CAAC;AAC9C,QAAM,MAAM,0BAAU,YAAY,EAAE,EAAE,SAAS,CAAC;AAChD,SAAO,CAAC,GAAG,OAAO,WAAW,IAAI,CAAC,eAAe,IAAI,QAAQ,UAAU,CAAC,CAAC;AAC1E;AASA,SAAS,MAAM,OAAkB,KAA2B;AAC3D,SAAO,IAAI,QAAQ,KAAK;AACzB;",
6
6
  "names": []
7
7
  }
@@ -1,6 +1,3 @@
1
- export { AesGcm256 } from './aes.js';
2
- export { encrypt } from './encrypt.js';
3
- export { getAllowlistedKeyServers, retrieveKeyServers, verifyKeyServer, type KeyServer, } from './key-server.js';
4
- export { KeyStore } from './key-store.js';
1
+ export { getAllowlistedKeyServers } from './key-server.js';
2
+ export { SealClient, type SealClientOptions } from './client.js';
5
3
  export { SessionKey } from './session-key.js';
6
- export { EncryptedObject } from './types.js';
package/dist/cjs/index.js CHANGED
@@ -18,20 +18,12 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var index_exports = {};
20
20
  __export(index_exports, {
21
- AesGcm256: () => import_aes.AesGcm256,
22
- EncryptedObject: () => import_types.EncryptedObject,
23
- KeyStore: () => import_key_store.KeyStore,
21
+ SealClient: () => import_client.SealClient,
24
22
  SessionKey: () => import_session_key.SessionKey,
25
- encrypt: () => import_encrypt.encrypt,
26
- getAllowlistedKeyServers: () => import_key_server.getAllowlistedKeyServers,
27
- retrieveKeyServers: () => import_key_server.retrieveKeyServers,
28
- verifyKeyServer: () => import_key_server.verifyKeyServer
23
+ getAllowlistedKeyServers: () => import_key_server.getAllowlistedKeyServers
29
24
  });
30
25
  module.exports = __toCommonJS(index_exports);
31
- var import_aes = require("./aes.js");
32
- var import_encrypt = require("./encrypt.js");
33
26
  var import_key_server = require("./key-server.js");
34
- var import_key_store = require("./key-store.js");
27
+ var import_client = require("./client.js");
35
28
  var import_session_key = require("./session-key.js");
36
- var import_types = require("./types.js");
37
29
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { AesGcm256 } from './aes.js';\nexport { encrypt } from './encrypt.js';\nexport {\n\tgetAllowlistedKeyServers,\n\tretrieveKeyServers,\n\tverifyKeyServer,\n\ttype KeyServer,\n} from './key-server.js';\nexport { KeyStore } from './key-store.js';\nexport { SessionKey } from './session-key.js';\nexport { EncryptedObject } from './types.js';\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA0B;AAC1B,qBAAwB;AACxB,wBAKO;AACP,uBAAyB;AACzB,yBAA2B;AAC3B,mBAAgC;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { getAllowlistedKeyServers } from './key-server.js';\nexport { SealClient, type SealClientOptions } from './client.js';\nexport { SessionKey } from './session-key.js';\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAAyC;AACzC,oBAAmD;AACnD,yBAA2B;",
6
6
  "names": []
7
7
  }
package/dist/cjs/kdf.d.ts CHANGED
@@ -7,3 +7,8 @@ import type { GTElement } from './bls12381.js';
7
7
  * @returns The derived key.
8
8
  */
9
9
  export declare function kdf(element: GTElement, info: Uint8Array): Uint8Array;
10
+ export declare enum KeyPurpose {
11
+ EncryptedRandomness = 0,
12
+ DEM = 1
13
+ }
14
+ export declare function deriveKey(purpose: KeyPurpose, baseKey: Uint8Array): Uint8Array;
package/dist/cjs/kdf.js CHANGED
@@ -18,12 +18,39 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var kdf_exports = {};
20
20
  __export(kdf_exports, {
21
+ KeyPurpose: () => KeyPurpose,
22
+ deriveKey: () => deriveKey,
21
23
  kdf: () => kdf
22
24
  });
23
25
  module.exports = __toCommonJS(kdf_exports);
24
26
  var import_hkdf = require("@noble/hashes/hkdf");
27
+ var import_hmac = require("@noble/hashes/hmac");
25
28
  var import_sha3 = require("@noble/hashes/sha3");
26
29
  function kdf(element, info) {
27
- return (0, import_hkdf.hkdf)(import_sha3.sha3_256, element.toBytes(), "", info, 32);
30
+ const GT_ELEMENT_BYTE_LENGTH = 576;
31
+ const PERMUTATION = [0, 2, 4, 1, 3, 5];
32
+ const COEFFICIENT_SIZE = GT_ELEMENT_BYTE_LENGTH / PERMUTATION.length;
33
+ const bytes = element.toBytes();
34
+ let permutedBytes = new Uint8Array(GT_ELEMENT_BYTE_LENGTH);
35
+ PERMUTATION.forEach((pi, i) => {
36
+ permutedBytes.set(
37
+ bytes.slice(i * COEFFICIENT_SIZE, (i + 1) * COEFFICIENT_SIZE),
38
+ pi * COEFFICIENT_SIZE
39
+ );
40
+ });
41
+ return (0, import_hkdf.hkdf)(import_sha3.sha3_256, permutedBytes, "", info, 32);
42
+ }
43
+ var KeyPurpose = /* @__PURE__ */ ((KeyPurpose2) => {
44
+ KeyPurpose2[KeyPurpose2["EncryptedRandomness"] = 0] = "EncryptedRandomness";
45
+ KeyPurpose2[KeyPurpose2["DEM"] = 1] = "DEM";
46
+ return KeyPurpose2;
47
+ })(KeyPurpose || {});
48
+ function deriveKey(purpose, baseKey) {
49
+ switch (purpose) {
50
+ case 0 /* EncryptedRandomness */:
51
+ return (0, import_hmac.hmac)(import_sha3.sha3_256, baseKey, new Uint8Array([0]));
52
+ case 1 /* DEM */:
53
+ return (0, import_hmac.hmac)(import_sha3.sha3_256, baseKey, new Uint8Array([1]));
54
+ }
28
55
  }
29
56
  //# sourceMappingURL=kdf.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/kdf.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { hkdf } from '@noble/hashes/hkdf';\nimport { sha3_256 } from '@noble/hashes/sha3';\n\nimport type { GTElement } from './bls12381.js';\n\n/**\n * The default key derivation function.\n *\n * @param element The GTElement to derive the key from.\n * @param info Optional context and application specific information.\n * @returns The derived key.\n */\nexport function kdf(element: GTElement, info: Uint8Array): Uint8Array {\n\treturn hkdf(sha3_256, element.toBytes(), '', info, 32);\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAqB;AACrB,kBAAyB;AAWlB,SAAS,IAAI,SAAoB,MAA8B;AACrE,aAAO,kBAAK,sBAAU,QAAQ,QAAQ,GAAG,IAAI,MAAM,EAAE;AACtD;",
6
- "names": []
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { hkdf } from '@noble/hashes/hkdf';\nimport { hmac } from '@noble/hashes/hmac';\nimport { sha3_256 } from '@noble/hashes/sha3';\n\nimport type { GTElement } from './bls12381.js';\n\n/**\n * The default key derivation function.\n *\n * @param element The GTElement to derive the key from.\n * @param info Optional context and application specific information.\n * @returns The derived key.\n */\nexport function kdf(element: GTElement, info: Uint8Array): Uint8Array {\n\t// This permutation flips the order of 6 pairs of coefficients of the GT element.\n\t// The permutation may be computed as:\n\t// for i in 0..3 {\n\t// for j in 0..2 {\n\t// PERMUTATION[i + j * 3] = i * 2 + j;\n\t// }\n\t// }\n\tconst GT_ELEMENT_BYTE_LENGTH = 576;\n\tconst PERMUTATION = [0, 2, 4, 1, 3, 5];\n\tconst COEFFICIENT_SIZE = GT_ELEMENT_BYTE_LENGTH / PERMUTATION.length;\n\n\tconst bytes = element.toBytes();\n\tlet permutedBytes = new Uint8Array(GT_ELEMENT_BYTE_LENGTH);\n\tPERMUTATION.forEach((pi, i) => {\n\t\tpermutedBytes.set(\n\t\t\tbytes.slice(i * COEFFICIENT_SIZE, (i + 1) * COEFFICIENT_SIZE),\n\t\t\tpi * COEFFICIENT_SIZE,\n\t\t);\n\t});\n\treturn hkdf(sha3_256, permutedBytes, '', info, 32);\n}\n\nexport enum KeyPurpose {\n\tEncryptedRandomness,\n\tDEM,\n}\n\nexport function deriveKey(purpose: KeyPurpose, baseKey: Uint8Array): Uint8Array {\n\tswitch (purpose) {\n\t\tcase KeyPurpose.EncryptedRandomness:\n\t\t\treturn hmac(sha3_256, baseKey, new Uint8Array([0]));\n\t\tcase KeyPurpose.DEM:\n\t\t\treturn hmac(sha3_256, baseKey, new Uint8Array([1]));\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAqB;AACrB,kBAAqB;AACrB,kBAAyB;AAWlB,SAAS,IAAI,SAAoB,MAA8B;AAQrE,QAAM,yBAAyB;AAC/B,QAAM,cAAc,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AACrC,QAAM,mBAAmB,yBAAyB,YAAY;AAE9D,QAAM,QAAQ,QAAQ,QAAQ;AAC9B,MAAI,gBAAgB,IAAI,WAAW,sBAAsB;AACzD,cAAY,QAAQ,CAAC,IAAI,MAAM;AAC9B,kBAAc;AAAA,MACb,MAAM,MAAM,IAAI,mBAAmB,IAAI,KAAK,gBAAgB;AAAA,MAC5D,KAAK;AAAA,IACN;AAAA,EACD,CAAC;AACD,aAAO,kBAAK,sBAAU,eAAe,IAAI,MAAM,EAAE;AAClD;AAEO,IAAK,aAAL,kBAAKA,gBAAL;AACN,EAAAA,wBAAA;AACA,EAAAA,wBAAA;AAFW,SAAAA;AAAA,GAAA;AAKL,SAAS,UAAU,SAAqB,SAAiC;AAC/E,UAAQ,SAAS;AAAA,IAChB,KAAK;AACJ,iBAAO,kBAAK,sBAAU,SAAS,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAAA,IACnD,KAAK;AACJ,iBAAO,kBAAK,sBAAU,SAAS,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;AAAA,EACpD;AACD;",
6
+ "names": ["KeyPurpose"]
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import type { SuiClient } from '@mysten/sui/client';
2
2
  export type KeyServer = {
3
- objectId: Uint8Array;
3
+ objectId: string;
4
4
  name: string;
5
5
  url: string;
6
6
  keyType: KeyServerType;
@@ -14,7 +14,7 @@ export declare enum KeyServerType {
14
14
  * @param network - The network to use.
15
15
  * @returns The object id's of the key servers.
16
16
  */
17
- export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): Uint8Array[];
17
+ export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): string[];
18
18
  /**
19
19
  * Given a list of key server object IDs, returns a list of SealKeyServer
20
20
  * from onchain state containing name, objectId, URL and pk.
@@ -24,15 +24,15 @@ export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet')
24
24
  * @returns - An array of SealKeyServer.
25
25
  */
26
26
  export declare function retrieveKeyServers({ objectIds, client, }: {
27
- objectIds: Uint8Array[];
27
+ objectIds: string[];
28
28
  client: SuiClient;
29
29
  }): Promise<KeyServer[]>;
30
30
  /**
31
- * Given a KeyServer, fetch the proof of possesion (PoP) from the URL and verify it
31
+ * Given a KeyServer, fetch the proof of possession (PoP) from the URL and verify it
32
32
  * against the pubkey. This should be used only rarely when the dapp uses a dynamic
33
33
  * set of key servers.
34
34
  *
35
35
  * @param server - The KeyServer to verify.
36
36
  * @returns - True if the key server is valid, false otherwise.
37
37
  */
38
- export declare function verifyKeyServer(server: KeyServer): Promise<boolean>;
38
+ export declare function verifyKeyServer(server: KeyServer, timeout: number): Promise<boolean>;
@@ -25,28 +25,23 @@ __export(key_server_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(key_server_exports);
27
27
  var import_bcs = require("@mysten/bcs");
28
- var import_bcs2 = require("@mysten/sui/bcs");
29
28
  var import_bls12_381 = require("@noble/curves/bls12-381");
29
+ var import_bcs2 = require("./bcs.js");
30
+ var import_error = require("./error.js");
30
31
  var import_ibe = require("./ibe.js");
32
+ var import_version = require("./version.js");
31
33
  var KeyServerType = /* @__PURE__ */ ((KeyServerType2) => {
32
34
  KeyServerType2[KeyServerType2["BonehFranklinBLS12381"] = 0] = "BonehFranklinBLS12381";
33
35
  return KeyServerType2;
34
36
  })(KeyServerType || {});
35
- const KeyServerMove = import_bcs2.bcs.struct("KeyServer", {
36
- id: import_bcs2.bcs.Address,
37
- name: import_bcs2.bcs.string(),
38
- url: import_bcs2.bcs.string(),
39
- key_type: import_bcs2.bcs.u8(),
40
- pk: import_bcs2.bcs.vector(import_bcs2.bcs.u8())
41
- });
42
37
  function getAllowlistedKeyServers(network) {
43
38
  if (network === "testnet") {
44
39
  return [
45
- (0, import_bcs.fromHex)("0xb35a7228d8cf224ad1e828c0217c95a5153bafc2906d6f9c178197dce26fbcf8"),
46
- (0, import_bcs.fromHex)("0x2d6cde8a9d9a65bde3b0a346566945a63b4bfb70e9a06c41bdb70807e2502b06")
40
+ "0xb35a7228d8cf224ad1e828c0217c95a5153bafc2906d6f9c178197dce26fbcf8",
41
+ "0x2d6cde8a9d9a65bde3b0a346566945a63b4bfb70e9a06c41bdb70807e2502b06"
47
42
  ];
48
43
  } else {
49
- throw new Error("Network not supported");
44
+ throw new import_error.UnsupportedNetworkError(`Unsupported network ${network}`);
50
45
  }
51
46
  }
52
47
  async function retrieveKeyServers({
@@ -56,20 +51,22 @@ async function retrieveKeyServers({
56
51
  return await Promise.all(
57
52
  objectIds.map(async (objectId) => {
58
53
  const res = await client.getObject({
59
- id: (0, import_bcs.toHex)(objectId),
54
+ id: objectId,
60
55
  options: {
61
56
  showBcs: true
62
57
  }
63
58
  });
64
59
  if (!res || res.error || !res.data) {
65
- throw new Error(`KeyServer ${objectId} not found; ${res.error}`);
60
+ throw new import_error.InvalidGetObjectError(`KeyServer ${objectId} not found; ${res.error}`);
66
61
  }
67
62
  if (!res.data.bcs || !("bcsBytes" in res.data.bcs)) {
68
- throw new Error(`Invalid KeyServer query: ${objectId}, expected object, got package`);
63
+ throw new import_error.InvalidGetObjectError(
64
+ `Invalid KeyServer query: ${objectId}, expected object, got package`
65
+ );
69
66
  }
70
- let ks = KeyServerMove.parse((0, import_bcs.fromBase64)(res.data.bcs.bcsBytes));
71
- if (ks.key_type !== 0) {
72
- throw new Error("Unsupported key type");
67
+ let ks = import_bcs2.KeyServerMove.parse((0, import_bcs.fromBase64)(res.data.bcs.bcsBytes));
68
+ if (ks.keyType !== 0) {
69
+ throw new import_error.UnsupportedFeatureError(`Unsupported key type ${ks.keyType}`);
73
70
  }
74
71
  return {
75
72
  objectId,
@@ -81,18 +78,24 @@ async function retrieveKeyServers({
81
78
  })
82
79
  );
83
80
  }
84
- async function verifyKeyServer(server) {
81
+ async function verifyKeyServer(server, timeout) {
82
+ const requestId = crypto.randomUUID();
85
83
  const response = await fetch(server.url + "/v1/service", {
86
84
  method: "GET",
87
85
  headers: {
88
- "Content-Type": "application/json"
89
- }
86
+ "Content-Type": "application/json",
87
+ "Request-Id": requestId,
88
+ "Client-Sdk-Type": "typescript",
89
+ "Client-Sdk-Version": import_version.PACKAGE_VERSION
90
+ },
91
+ signal: AbortSignal.timeout(timeout)
90
92
  });
93
+ await import_error.SealAPIError.assertResponse(response, requestId);
91
94
  const serviceResponse = await response.json();
92
95
  if (serviceResponse.service_id !== server.objectId) {
93
96
  return false;
94
97
  }
95
- const fullMsg = new Uint8Array([...import_ibe.DST_POP, ...server.pk, ...server.objectId]);
98
+ const fullMsg = new Uint8Array([...import_ibe.DST_POP, ...server.pk, ...(0, import_bcs.fromHex)(server.objectId)]);
96
99
  return import_bls12_381.bls12_381.verifyShortSignature((0, import_bcs.fromBase64)(serviceResponse.pop), fullMsg, server.pk);
97
100
  }
98
101
  //# sourceMappingURL=key-server.js.map