@haneullabs/seal 0.1.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.
- package/CHANGELOG.md +459 -0
- package/README.md +4 -0
- package/dist/cjs/bcs.d.ts +71 -0
- package/dist/cjs/bcs.js +74 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/bls12381.d.ts +44 -0
- package/dist/cjs/bls12381.js +151 -0
- package/dist/cjs/bls12381.js.map +7 -0
- package/dist/cjs/client.d.ts +84 -0
- package/dist/cjs/client.js +414 -0
- package/dist/cjs/client.js.map +7 -0
- package/dist/cjs/decrypt.d.ts +22 -0
- package/dist/cjs/decrypt.js +109 -0
- package/dist/cjs/decrypt.js.map +7 -0
- package/dist/cjs/dem.d.ts +38 -0
- package/dist/cjs/dem.js +185 -0
- package/dist/cjs/dem.js.map +7 -0
- package/dist/cjs/elgamal.d.ts +13 -0
- package/dist/cjs/elgamal.js +46 -0
- package/dist/cjs/elgamal.js.map +7 -0
- package/dist/cjs/encrypt.d.ts +32 -0
- package/dist/cjs/encrypt.js +104 -0
- package/dist/cjs/encrypt.js.map +7 -0
- package/dist/cjs/error.d.ts +86 -0
- package/dist/cjs/error.js +239 -0
- package/dist/cjs/error.js.map +7 -0
- package/dist/cjs/ibe.d.ts +98 -0
- package/dist/cjs/ibe.js +167 -0
- package/dist/cjs/ibe.js.map +7 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +33 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/kdf.d.ts +30 -0
- package/dist/cjs/kdf.js +97 -0
- package/dist/cjs/kdf.js.map +7 -0
- package/dist/cjs/key-server.d.ts +98 -0
- package/dist/cjs/key-server.js +171 -0
- package/dist/cjs/key-server.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/session-key.d.ts +74 -0
- package/dist/cjs/session-key.js +245 -0
- package/dist/cjs/session-key.js.map +7 -0
- package/dist/cjs/shamir.d.ts +91 -0
- package/dist/cjs/shamir.js +770 -0
- package/dist/cjs/shamir.js.map +7 -0
- package/dist/cjs/types.d.ts +83 -0
- package/dist/cjs/types.js +17 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/utils.d.ts +47 -0
- package/dist/cjs/utils.js +106 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/cjs/version.d.ts +1 -0
- package/dist/cjs/version.js +25 -0
- package/dist/cjs/version.js.map +7 -0
- package/dist/esm/bcs.d.ts +71 -0
- package/dist/esm/bcs.js +54 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/bls12381.d.ts +44 -0
- package/dist/esm/bls12381.js +131 -0
- package/dist/esm/bls12381.js.map +7 -0
- package/dist/esm/client.d.ts +84 -0
- package/dist/esm/client.js +407 -0
- package/dist/esm/client.js.map +7 -0
- package/dist/esm/decrypt.d.ts +22 -0
- package/dist/esm/decrypt.js +94 -0
- package/dist/esm/decrypt.js.map +7 -0
- package/dist/esm/dem.d.ts +38 -0
- package/dist/esm/dem.js +165 -0
- package/dist/esm/dem.js.map +7 -0
- package/dist/esm/elgamal.d.ts +13 -0
- package/dist/esm/elgamal.js +26 -0
- package/dist/esm/elgamal.js.map +7 -0
- package/dist/esm/encrypt.d.ts +32 -0
- package/dist/esm/encrypt.js +84 -0
- package/dist/esm/encrypt.js.map +7 -0
- package/dist/esm/error.d.ts +86 -0
- package/dist/esm/error.js +219 -0
- package/dist/esm/error.js.map +7 -0
- package/dist/esm/ibe.d.ts +98 -0
- package/dist/esm/ibe.js +147 -0
- package/dist/esm/ibe.js.map +7 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/kdf.d.ts +30 -0
- package/dist/esm/kdf.js +83 -0
- package/dist/esm/kdf.js.map +7 -0
- package/dist/esm/key-server.d.ts +98 -0
- package/dist/esm/key-server.js +151 -0
- package/dist/esm/key-server.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/session-key.d.ts +74 -0
- package/dist/esm/session-key.js +230 -0
- package/dist/esm/session-key.js.map +7 -0
- package/dist/esm/shamir.d.ts +91 -0
- package/dist/esm/shamir.js +750 -0
- package/dist/esm/shamir.js.map +7 -0
- package/dist/esm/types.d.ts +83 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/utils.d.ts +47 -0
- package/dist/esm/utils.js +86 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/esm/version.d.ts +1 -0
- package/dist/esm/version.js +5 -0
- package/dist/esm/version.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export declare class SealError extends Error {
|
|
2
|
+
}
|
|
3
|
+
export declare class UserError extends SealError {
|
|
4
|
+
}
|
|
5
|
+
export declare class SealAPIError extends SealError {
|
|
6
|
+
#private;
|
|
7
|
+
requestId?: string | undefined;
|
|
8
|
+
status?: number | undefined;
|
|
9
|
+
constructor(message: string, requestId?: string | undefined, status?: number | undefined);
|
|
10
|
+
static assertResponse(response: Response, requestId: string): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export declare class InvalidPTBError extends SealAPIError {
|
|
13
|
+
constructor(requestId?: string, message?: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class InvalidPackageError extends SealAPIError {
|
|
16
|
+
constructor(requestId?: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class InvalidParameterError extends SealAPIError {
|
|
19
|
+
constructor(requestId?: string);
|
|
20
|
+
}
|
|
21
|
+
export declare class InvalidUserSignatureError extends SealAPIError {
|
|
22
|
+
constructor(requestId?: string);
|
|
23
|
+
}
|
|
24
|
+
export declare class InvalidSessionKeySignatureError extends SealAPIError {
|
|
25
|
+
constructor(requestId?: string);
|
|
26
|
+
}
|
|
27
|
+
export declare class InvalidMVRNameError extends SealAPIError {
|
|
28
|
+
constructor(requestId?: string);
|
|
29
|
+
}
|
|
30
|
+
/** Server error indicating that the requested key server object id is invalid */
|
|
31
|
+
export declare class InvalidKeyServerObjectIdError extends SealAPIError {
|
|
32
|
+
constructor(requestId?: string);
|
|
33
|
+
}
|
|
34
|
+
/** Server error indicating that the requested package id is not supported (i.e., key server is running in Permissioned mode) */
|
|
35
|
+
export declare class UnsupportedPackageIdError extends SealAPIError {
|
|
36
|
+
constructor(requestId?: string);
|
|
37
|
+
}
|
|
38
|
+
export declare class InvalidSDKVersionError extends SealAPIError {
|
|
39
|
+
constructor(requestId?: string);
|
|
40
|
+
}
|
|
41
|
+
export declare class InvalidSDKTypeError extends SealAPIError {
|
|
42
|
+
constructor(requestId?: string);
|
|
43
|
+
}
|
|
44
|
+
export declare class DeprecatedSDKVersionError extends SealAPIError {
|
|
45
|
+
constructor(requestId?: string);
|
|
46
|
+
}
|
|
47
|
+
/** Server error indicating that the user does not have access to one or more of the requested keys */
|
|
48
|
+
export declare class NoAccessError extends SealAPIError {
|
|
49
|
+
constructor(requestId?: string);
|
|
50
|
+
}
|
|
51
|
+
/** Server error indicating that the session key has expired */
|
|
52
|
+
export declare class ExpiredSessionKeyError extends SealAPIError {
|
|
53
|
+
constructor(requestId?: string);
|
|
54
|
+
}
|
|
55
|
+
/** Internal server error, caller should retry */
|
|
56
|
+
export declare class InternalError extends SealAPIError {
|
|
57
|
+
constructor(requestId?: string);
|
|
58
|
+
}
|
|
59
|
+
/** General server errors that are not specific to the Seal API (e.g., 404 "Not Found") */
|
|
60
|
+
export declare class GeneralError extends SealAPIError {
|
|
61
|
+
}
|
|
62
|
+
export declare class InvalidPersonalMessageSignatureError extends UserError {
|
|
63
|
+
}
|
|
64
|
+
export declare class InvalidGetObjectError extends UserError {
|
|
65
|
+
}
|
|
66
|
+
export declare class UnsupportedFeatureError extends UserError {
|
|
67
|
+
}
|
|
68
|
+
export declare class UnsupportedNetworkError extends UserError {
|
|
69
|
+
}
|
|
70
|
+
export declare class InvalidKeyServerError extends UserError {
|
|
71
|
+
}
|
|
72
|
+
export declare class InvalidKeyServerVersionError extends UserError {
|
|
73
|
+
}
|
|
74
|
+
export declare class InvalidCiphertextError extends UserError {
|
|
75
|
+
}
|
|
76
|
+
export declare class InvalidThresholdError extends UserError {
|
|
77
|
+
}
|
|
78
|
+
export declare class InconsistentKeyServersError extends UserError {
|
|
79
|
+
}
|
|
80
|
+
export declare class DecryptionError extends UserError {
|
|
81
|
+
}
|
|
82
|
+
export declare class InvalidClientOptionsError extends UserError {
|
|
83
|
+
}
|
|
84
|
+
export declare class TooManyFailedFetchKeyRequestsError extends UserError {
|
|
85
|
+
}
|
|
86
|
+
export declare function toMajorityError(errors: Error[]): Error;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
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);
|
|
6
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
7
|
+
var _SealAPIError_static, generate_fn;
|
|
8
|
+
class SealError extends Error {
|
|
9
|
+
}
|
|
10
|
+
class UserError extends SealError {
|
|
11
|
+
}
|
|
12
|
+
const _SealAPIError = class _SealAPIError extends SealError {
|
|
13
|
+
constructor(message, requestId, status) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.requestId = requestId;
|
|
16
|
+
this.status = status;
|
|
17
|
+
}
|
|
18
|
+
static async assertResponse(response, requestId) {
|
|
19
|
+
var _a;
|
|
20
|
+
if (response.ok) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
let errorInstance;
|
|
24
|
+
try {
|
|
25
|
+
const text = await response.text();
|
|
26
|
+
const error = JSON.parse(text)["error"];
|
|
27
|
+
const message = JSON.parse(text)["message"];
|
|
28
|
+
errorInstance = __privateMethod(_a = _SealAPIError, _SealAPIError_static, generate_fn).call(_a, error, message, requestId);
|
|
29
|
+
} catch {
|
|
30
|
+
errorInstance = new GeneralError(response.statusText, requestId, response.status);
|
|
31
|
+
}
|
|
32
|
+
throw errorInstance;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
_SealAPIError_static = new WeakSet();
|
|
36
|
+
generate_fn = function(error, message, requestId, status) {
|
|
37
|
+
switch (error) {
|
|
38
|
+
case "InvalidPTB":
|
|
39
|
+
return new InvalidPTBError(requestId, message);
|
|
40
|
+
case "InvalidPackage":
|
|
41
|
+
return new InvalidPackageError(requestId);
|
|
42
|
+
case "NoAccess":
|
|
43
|
+
return new NoAccessError(requestId);
|
|
44
|
+
case "InvalidSignature":
|
|
45
|
+
return new InvalidUserSignatureError(requestId);
|
|
46
|
+
case "InvalidSessionSignature":
|
|
47
|
+
return new InvalidSessionKeySignatureError(requestId);
|
|
48
|
+
case "InvalidCertificate":
|
|
49
|
+
return new ExpiredSessionKeyError(requestId);
|
|
50
|
+
case "InvalidSDKVersion":
|
|
51
|
+
return new InvalidSDKVersionError(requestId);
|
|
52
|
+
case "InvalidSDKType":
|
|
53
|
+
return new InvalidSDKTypeError(requestId);
|
|
54
|
+
case "DeprecatedSDKVersion":
|
|
55
|
+
return new DeprecatedSDKVersionError(requestId);
|
|
56
|
+
case "InvalidParameter":
|
|
57
|
+
return new InvalidParameterError(requestId);
|
|
58
|
+
case "InvalidMVRName":
|
|
59
|
+
return new InvalidMVRNameError(requestId);
|
|
60
|
+
case "InvalidServiceId":
|
|
61
|
+
return new InvalidKeyServerObjectIdError(requestId);
|
|
62
|
+
case "UnsupportedPackageId":
|
|
63
|
+
return new UnsupportedPackageIdError(requestId);
|
|
64
|
+
case "Failure":
|
|
65
|
+
return new InternalError(requestId);
|
|
66
|
+
default:
|
|
67
|
+
return new GeneralError(message, requestId, status);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
__privateAdd(_SealAPIError, _SealAPIError_static);
|
|
71
|
+
let SealAPIError = _SealAPIError;
|
|
72
|
+
class InvalidPTBError extends SealAPIError {
|
|
73
|
+
constructor(requestId, message) {
|
|
74
|
+
super("PTB does not conform to the expected format " + message, requestId);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
class InvalidPackageError extends SealAPIError {
|
|
78
|
+
constructor(requestId) {
|
|
79
|
+
super("Package ID used in PTB is invalid", requestId);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
class InvalidParameterError extends SealAPIError {
|
|
83
|
+
constructor(requestId) {
|
|
84
|
+
super(
|
|
85
|
+
"PTB contains an invalid parameter, possibly a newly created object that the FN has not yet seen",
|
|
86
|
+
requestId
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
class InvalidUserSignatureError extends SealAPIError {
|
|
91
|
+
constructor(requestId) {
|
|
92
|
+
super("User signature on the session key is invalid", requestId);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
class InvalidSessionKeySignatureError extends SealAPIError {
|
|
96
|
+
constructor(requestId) {
|
|
97
|
+
super("Session key signature is invalid", requestId);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
class InvalidMVRNameError extends SealAPIError {
|
|
101
|
+
constructor(requestId) {
|
|
102
|
+
super("MVR name is invalid or not consistent with the first version of the package", requestId);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
class InvalidKeyServerObjectIdError extends SealAPIError {
|
|
106
|
+
constructor(requestId) {
|
|
107
|
+
super("Key server object ID is invalid", requestId);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
class UnsupportedPackageIdError extends SealAPIError {
|
|
111
|
+
constructor(requestId) {
|
|
112
|
+
super("Requested package is not supported", requestId);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
class InvalidSDKVersionError extends SealAPIError {
|
|
116
|
+
constructor(requestId) {
|
|
117
|
+
super("SDK version is invalid", requestId);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
class InvalidSDKTypeError extends SealAPIError {
|
|
121
|
+
constructor(requestId) {
|
|
122
|
+
super("SDK type is invalid", requestId);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
class DeprecatedSDKVersionError extends SealAPIError {
|
|
126
|
+
constructor(requestId) {
|
|
127
|
+
super("SDK version is deprecated", requestId);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
class NoAccessError extends SealAPIError {
|
|
131
|
+
constructor(requestId) {
|
|
132
|
+
super("User does not have access to one or more of the requested keys", requestId);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
class ExpiredSessionKeyError extends SealAPIError {
|
|
136
|
+
constructor(requestId) {
|
|
137
|
+
super("Session key has expired", requestId);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
class InternalError extends SealAPIError {
|
|
141
|
+
constructor(requestId) {
|
|
142
|
+
super("Internal server error, caller should retry", requestId);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
class GeneralError extends SealAPIError {
|
|
146
|
+
}
|
|
147
|
+
class InvalidPersonalMessageSignatureError extends UserError {
|
|
148
|
+
}
|
|
149
|
+
class InvalidGetObjectError extends UserError {
|
|
150
|
+
}
|
|
151
|
+
class UnsupportedFeatureError extends UserError {
|
|
152
|
+
}
|
|
153
|
+
class UnsupportedNetworkError extends UserError {
|
|
154
|
+
}
|
|
155
|
+
class InvalidKeyServerError extends UserError {
|
|
156
|
+
}
|
|
157
|
+
class InvalidKeyServerVersionError extends UserError {
|
|
158
|
+
}
|
|
159
|
+
class InvalidCiphertextError extends UserError {
|
|
160
|
+
}
|
|
161
|
+
class InvalidThresholdError extends UserError {
|
|
162
|
+
}
|
|
163
|
+
class InconsistentKeyServersError extends UserError {
|
|
164
|
+
}
|
|
165
|
+
class DecryptionError extends UserError {
|
|
166
|
+
}
|
|
167
|
+
class InvalidClientOptionsError extends UserError {
|
|
168
|
+
}
|
|
169
|
+
class TooManyFailedFetchKeyRequestsError extends UserError {
|
|
170
|
+
}
|
|
171
|
+
function toMajorityError(errors) {
|
|
172
|
+
let maxCount = 0;
|
|
173
|
+
let majorityError = errors[0];
|
|
174
|
+
const counts = /* @__PURE__ */ new Map();
|
|
175
|
+
for (const error of errors) {
|
|
176
|
+
const errorName = error.constructor.name;
|
|
177
|
+
const newCount = (counts.get(errorName) || 0) + 1;
|
|
178
|
+
counts.set(errorName, newCount);
|
|
179
|
+
if (newCount > maxCount) {
|
|
180
|
+
maxCount = newCount;
|
|
181
|
+
majorityError = error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return majorityError;
|
|
185
|
+
}
|
|
186
|
+
export {
|
|
187
|
+
DecryptionError,
|
|
188
|
+
DeprecatedSDKVersionError,
|
|
189
|
+
ExpiredSessionKeyError,
|
|
190
|
+
GeneralError,
|
|
191
|
+
InconsistentKeyServersError,
|
|
192
|
+
InternalError,
|
|
193
|
+
InvalidCiphertextError,
|
|
194
|
+
InvalidClientOptionsError,
|
|
195
|
+
InvalidGetObjectError,
|
|
196
|
+
InvalidKeyServerError,
|
|
197
|
+
InvalidKeyServerObjectIdError,
|
|
198
|
+
InvalidKeyServerVersionError,
|
|
199
|
+
InvalidMVRNameError,
|
|
200
|
+
InvalidPTBError,
|
|
201
|
+
InvalidPackageError,
|
|
202
|
+
InvalidParameterError,
|
|
203
|
+
InvalidPersonalMessageSignatureError,
|
|
204
|
+
InvalidSDKTypeError,
|
|
205
|
+
InvalidSDKVersionError,
|
|
206
|
+
InvalidSessionKeySignatureError,
|
|
207
|
+
InvalidThresholdError,
|
|
208
|
+
InvalidUserSignatureError,
|
|
209
|
+
NoAccessError,
|
|
210
|
+
SealAPIError,
|
|
211
|
+
SealError,
|
|
212
|
+
TooManyFailedFetchKeyRequestsError,
|
|
213
|
+
UnsupportedFeatureError,
|
|
214
|
+
UnsupportedNetworkError,
|
|
215
|
+
UnsupportedPackageIdError,
|
|
216
|
+
UserError,
|
|
217
|
+
toMajorityError
|
|
218
|
+
};
|
|
219
|
+
//# 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(error: string, message: string, requestId: string, status?: number) {\n\t\tswitch (error) {\n\t\t\tcase 'InvalidPTB':\n\t\t\t\treturn new InvalidPTBError(requestId, message);\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 '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 'InvalidCertificate':\n\t\t\t\treturn new ExpiredSessionKeyError(requestId);\n\t\t\tcase 'InvalidSDKVersion':\n\t\t\t\treturn new InvalidSDKVersionError(requestId);\n\t\t\tcase 'InvalidSDKType':\n\t\t\t\treturn new InvalidSDKTypeError(requestId);\n\t\t\tcase 'DeprecatedSDKVersion':\n\t\t\t\treturn new DeprecatedSDKVersionError(requestId);\n\t\t\tcase 'InvalidParameter':\n\t\t\t\treturn new InvalidParameterError(requestId);\n\t\t\tcase 'InvalidMVRName':\n\t\t\t\treturn new InvalidMVRNameError(requestId);\n\t\t\tcase 'InvalidServiceId':\n\t\t\t\treturn new InvalidKeyServerObjectIdError(requestId);\n\t\t\tcase 'UnsupportedPackageId':\n\t\t\t\treturn new UnsupportedPackageIdError(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\tconst message = JSON.parse(text)['message'];\n\t\t\terrorInstance = SealAPIError.#generate(error, message, requestId);\n\t\t} catch {\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, message?: string) {\n\t\tsuper('PTB does not conform to the expected format ' + message, 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 InvalidParameterError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper(\n\t\t\t'PTB contains an invalid parameter, possibly a newly created object that the FN has not yet seen',\n\t\t\trequestId,\n\t\t);\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\nexport class InvalidMVRNameError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('MVR name is invalid or not consistent with the first version of the package', requestId);\n\t}\n}\n\n/** Server error indicating that the requested key server object id is invalid */\nexport class InvalidKeyServerObjectIdError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Key server object ID is invalid', requestId);\n\t}\n}\n\n/** Server error indicating that the requested package id is not supported (i.e., key server is running in Permissioned mode) */\nexport class UnsupportedPackageIdError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('Requested package is not supported', requestId);\n\t}\n}\n\n// Errors returned by the Seal server that indicate that the SDK version is invalid (implying that HTTP headers used by the SDK are being removed) or deprecated (implying that the SDK should be upgraded).\n\nexport class InvalidSDKVersionError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('SDK version is invalid', requestId);\n\t}\n}\n\nexport class InvalidSDKTypeError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('SDK type is invalid', requestId);\n\t}\n}\n\nexport class DeprecatedSDKVersionError extends SealAPIError {\n\tconstructor(requestId?: string) {\n\t\tsuper('SDK version is deprecated', 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 InvalidKeyServerVersionError extends UserError {}\nexport class InvalidCiphertextError extends UserError {}\nexport class InvalidThresholdError extends UserError {}\nexport class InconsistentKeyServersError extends UserError {}\nexport class DecryptionError extends UserError {}\nexport class InvalidClientOptionsError extends UserError {}\nexport class TooManyFailedFetchKeyRequestsError 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;AAGO,MAAM,kBAAkB,MAAM;AAAC;AAE/B,MAAM,kBAAkB,UAAU;AAAC;AAGnC,MAAM,gBAAN,MAAM,sBAAqB,UAAU;AAAA,EAC3C,YACC,SACO,WACA,QACN;AACD,UAAM,OAAO;AAHN;AACA;AAAA,EAGR;AAAA,EAqCA,aAAa,eAAe,UAAoB,WAAmB;AApDpE;AAqDE,QAAI,SAAS,IAAI;AAChB;AAAA,IACD;AACA,QAAI;AACJ,QAAI;AACH,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,YAAM,QAAQ,KAAK,MAAM,IAAI,EAAE,OAAO;AACtC,YAAM,UAAU,KAAK,MAAM,IAAI,EAAE,SAAS;AAC1C,sBAAgB,oCAAa,mCAAb,SAAuB,OAAO,SAAS;AAAA,IACxD,QAAQ;AAGP,sBAAgB,IAAI,aAAa,SAAS,YAAY,WAAW,SAAS,MAAM;AAAA,IACjF;AACA,UAAM;AAAA,EACP;AACD;AA7DO;AASC,cAAS,SAAC,OAAe,SAAiB,WAAmB,QAAiB;AACpF,UAAQ,OAAO;AAAA,IACd,KAAK;AACJ,aAAO,IAAI,gBAAgB,WAAW,OAAO;AAAA,IAC9C,KAAK;AACJ,aAAO,I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wBAAwB,UAAU;AAAC;AACzC,MAAM,kCAAkC,UAAU;AAAC;AACnD,MAAM,2CAA2C,UAAU;AAAC;AAE5D,SAAS,gBAAgB,QAAwB;AACvD,MAAI,WAAW;AACf,MAAI,gBAAgB,OAAO,CAAC;AAC5B,QAAM,SAAS,oBAAI,IAAoB;AACvC,aAAW,SAAS,QAAQ;AAC3B,UAAM,YAAY,MAAM,YAAY;AACpC,UAAM,YAAY,OAAO,IAAI,SAAS,KAAK,KAAK;AAChD,WAAO,IAAI,WAAW,QAAQ;AAE9B,QAAI,WAAW,UAAU;AACxB,iBAAW;AACX,sBAAgB;AAAA,IACjB;AAAA,EACD;AAEA,SAAO;AACR;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { IBEEncryptions } from './bcs.js';
|
|
2
|
+
import type { G1Element } from './bls12381.js';
|
|
3
|
+
import { G2Element } from './bls12381.js';
|
|
4
|
+
import type { KeyServer } from './key-server.js';
|
|
5
|
+
import type { Share } from './shamir.js';
|
|
6
|
+
/**
|
|
7
|
+
* The domain separation tag for the signing proof of possession.
|
|
8
|
+
*/
|
|
9
|
+
export declare const DST_POP: Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* The interface for the key servers.
|
|
12
|
+
*/
|
|
13
|
+
export declare abstract class IBEServers {
|
|
14
|
+
objectIds: string[];
|
|
15
|
+
constructor(objectIds: string[]);
|
|
16
|
+
/**
|
|
17
|
+
* Encrypt a batch of messages for the given identity.
|
|
18
|
+
*
|
|
19
|
+
* @param id The identity.
|
|
20
|
+
* @param msgAndIndices The messages and the corresponding indices of the share being encrypted.
|
|
21
|
+
* @returns The encrypted messages.
|
|
22
|
+
*/
|
|
23
|
+
abstract encryptBatched(id: Uint8Array, shares: Share[], baseKey: Uint8Array, threshold: number): typeof IBEEncryptions.$inferType;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Identity-based encryption based on the Boneh-Franklin IBE scheme (https://eprint.iacr.org/2001/090).
|
|
27
|
+
* Note that this implementation is of the "BasicIdent" protocol which on its own is not CCA secure, so this IBE implementation should not be used on its own.
|
|
28
|
+
*
|
|
29
|
+
* This object represents a set of key servers that can be used to encrypt messages for a given identity.
|
|
30
|
+
*/
|
|
31
|
+
export declare class BonehFranklinBLS12381Services extends IBEServers {
|
|
32
|
+
readonly publicKeys: G2Element[];
|
|
33
|
+
constructor(services: KeyServer[]);
|
|
34
|
+
encryptBatched(id: Uint8Array, shares: Share[], baseKey: Uint8Array, threshold: number): typeof IBEEncryptions.$inferType;
|
|
35
|
+
/**
|
|
36
|
+
* Returns true if the user secret key is valid for the given public key and id.
|
|
37
|
+
* @param user_secret_key - The user secret key.
|
|
38
|
+
* @param id - The identity.
|
|
39
|
+
* @param public_key - The public key.
|
|
40
|
+
* @returns True if the user secret key is valid for the given public key and id.
|
|
41
|
+
*/
|
|
42
|
+
static verifyUserSecretKey(userSecretKey: G1Element, id: string, publicKey: G2Element): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Identity-based decryption.
|
|
45
|
+
*
|
|
46
|
+
* @param nonce The encryption nonce.
|
|
47
|
+
* @param sk The user secret key.
|
|
48
|
+
* @param ciphertext The encrypted message.
|
|
49
|
+
* @param id The identity.
|
|
50
|
+
* @param [objectId, index] The object id and index of the share.
|
|
51
|
+
* @returns The decrypted message.
|
|
52
|
+
*/
|
|
53
|
+
static decrypt(nonce: G2Element, sk: G1Element, ciphertext: Uint8Array, id: Uint8Array, [objectId, index]: [string, number]): Uint8Array;
|
|
54
|
+
/**
|
|
55
|
+
* Decrypt all shares and verify that the randomness was used to create the given nonce.
|
|
56
|
+
*
|
|
57
|
+
* @param randomness - The randomness.
|
|
58
|
+
* @param encryptedShares - The encrypted shares.
|
|
59
|
+
* @param services - The services.
|
|
60
|
+
* @param publicKeys - The public keys.
|
|
61
|
+
* @param nonce - The nonce.
|
|
62
|
+
* @param id - The id.
|
|
63
|
+
* @returns All decrypted shares.
|
|
64
|
+
*/
|
|
65
|
+
static decryptAllSharesUsingRandomness(randomness: Uint8Array, encryptedShares: Uint8Array[], services: [string, number][], publicKeys: G2Element[], nonce: G2Element, id: Uint8Array): {
|
|
66
|
+
index: number;
|
|
67
|
+
share: Uint8Array;
|
|
68
|
+
}[];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Verify that the given randomness was used to crate the nonce.
|
|
72
|
+
* Throws an error if the given randomness is invalid (not a BLS scalar).
|
|
73
|
+
*
|
|
74
|
+
* @param randomness - The randomness.
|
|
75
|
+
* @param nonce - The nonce.
|
|
76
|
+
* @param useBE - Flag to indicate if BE encoding is used for the randomness. Defaults to true.
|
|
77
|
+
* @returns True if the randomness was used to create the nonce, false otherwise.
|
|
78
|
+
*/
|
|
79
|
+
export declare function verifyNonce(nonce: G2Element, randomness: Uint8Array, useBE?: boolean): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Decrypt the randomness using a key.
|
|
82
|
+
*
|
|
83
|
+
* @param encrypted_randomness - The encrypted randomness.
|
|
84
|
+
* @param derived_key - The derived key.
|
|
85
|
+
* @returns The randomness. Returns both the scalar interpreted in big-endian and little-endian encoding.
|
|
86
|
+
*/
|
|
87
|
+
export declare function decryptRandomness(encryptedRandomness: Uint8Array, randomnessKey: Uint8Array): Uint8Array;
|
|
88
|
+
/**
|
|
89
|
+
* Verify that the given randomness was used to crate the nonce.
|
|
90
|
+
* Check using both big-endian and little-endian encoding of the randomness.
|
|
91
|
+
*
|
|
92
|
+
* Throws an error if the nonce check doesn't pass using LE encoding _and_ the randomness is invalid as a BE encoded scalar.
|
|
93
|
+
*
|
|
94
|
+
* @param randomness - The randomness.
|
|
95
|
+
* @param nonce - The nonce.
|
|
96
|
+
* @returns True if the randomness was used to create the nonce using either LE or BE encoding, false otherwise.
|
|
97
|
+
*/
|
|
98
|
+
export declare function verifyNonceWithLE(nonce: G2Element, randomness: Uint8Array): boolean;
|
package/dist/esm/ibe.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { fromHex } from "@haneullabs/bcs";
|
|
2
|
+
import { G2Element, Scalar } from "./bls12381.js";
|
|
3
|
+
import { deriveKey, hashToG1, kdf, KeyPurpose } from "./kdf.js";
|
|
4
|
+
import { xor } from "./utils.js";
|
|
5
|
+
import { InvalidCiphertextError } from "./error.js";
|
|
6
|
+
const DST_POP = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-POP-00");
|
|
7
|
+
class IBEServers {
|
|
8
|
+
constructor(objectIds) {
|
|
9
|
+
this.objectIds = objectIds;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class BonehFranklinBLS12381Services extends IBEServers {
|
|
13
|
+
constructor(services) {
|
|
14
|
+
super(services.map((service) => service.objectId));
|
|
15
|
+
this.publicKeys = services.map((service) => G2Element.fromBytes(service.pk));
|
|
16
|
+
}
|
|
17
|
+
encryptBatched(id, shares, baseKey, threshold) {
|
|
18
|
+
if (this.publicKeys.length === 0 || this.publicKeys.length !== shares.length) {
|
|
19
|
+
throw new Error("Invalid public keys");
|
|
20
|
+
}
|
|
21
|
+
const [r, nonce, keys] = encapBatched(this.publicKeys, id);
|
|
22
|
+
const encryptedShares = shares.map(
|
|
23
|
+
({ share, index }, i) => xor(share, kdf(keys[i], nonce, id, this.objectIds[i], index))
|
|
24
|
+
);
|
|
25
|
+
const randomnessKey = deriveKey(
|
|
26
|
+
KeyPurpose.EncryptedRandomness,
|
|
27
|
+
baseKey,
|
|
28
|
+
encryptedShares,
|
|
29
|
+
threshold,
|
|
30
|
+
this.objectIds
|
|
31
|
+
);
|
|
32
|
+
const encryptedRandomness = xor(randomnessKey, r.toBytes());
|
|
33
|
+
return {
|
|
34
|
+
BonehFranklinBLS12381: {
|
|
35
|
+
nonce: nonce.toBytes(),
|
|
36
|
+
encryptedShares,
|
|
37
|
+
encryptedRandomness
|
|
38
|
+
},
|
|
39
|
+
$kind: "BonehFranklinBLS12381"
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns true if the user secret key is valid for the given public key and id.
|
|
44
|
+
* @param user_secret_key - The user secret key.
|
|
45
|
+
* @param id - The identity.
|
|
46
|
+
* @param public_key - The public key.
|
|
47
|
+
* @returns True if the user secret key is valid for the given public key and id.
|
|
48
|
+
*/
|
|
49
|
+
static verifyUserSecretKey(userSecretKey, id, publicKey) {
|
|
50
|
+
const lhs = userSecretKey.pairing(G2Element.generator());
|
|
51
|
+
const rhs = hashToG1(fromHex(id)).pairing(publicKey);
|
|
52
|
+
return lhs.equals(rhs);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Identity-based decryption.
|
|
56
|
+
*
|
|
57
|
+
* @param nonce The encryption nonce.
|
|
58
|
+
* @param sk The user secret key.
|
|
59
|
+
* @param ciphertext The encrypted message.
|
|
60
|
+
* @param id The identity.
|
|
61
|
+
* @param [objectId, index] The object id and index of the share.
|
|
62
|
+
* @returns The decrypted message.
|
|
63
|
+
*/
|
|
64
|
+
static decrypt(nonce, sk, ciphertext, id, [objectId, index]) {
|
|
65
|
+
return xor(ciphertext, kdf(decap(nonce, sk), nonce, id, objectId, index));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Decrypt all shares and verify that the randomness was used to create the given nonce.
|
|
69
|
+
*
|
|
70
|
+
* @param randomness - The randomness.
|
|
71
|
+
* @param encryptedShares - The encrypted shares.
|
|
72
|
+
* @param services - The services.
|
|
73
|
+
* @param publicKeys - The public keys.
|
|
74
|
+
* @param nonce - The nonce.
|
|
75
|
+
* @param id - The id.
|
|
76
|
+
* @returns All decrypted shares.
|
|
77
|
+
*/
|
|
78
|
+
static decryptAllSharesUsingRandomness(randomness, encryptedShares, services, publicKeys, nonce, id) {
|
|
79
|
+
if (publicKeys.length !== encryptedShares.length || publicKeys.length !== services.length) {
|
|
80
|
+
throw new Error("The number of public keys, encrypted shares and services must be the same");
|
|
81
|
+
}
|
|
82
|
+
let r;
|
|
83
|
+
try {
|
|
84
|
+
r = Scalar.fromBytes(randomness);
|
|
85
|
+
} catch {
|
|
86
|
+
throw new InvalidCiphertextError("Invalid randomness");
|
|
87
|
+
}
|
|
88
|
+
const gid_r = hashToG1(id).multiply(r);
|
|
89
|
+
return services.map(([objectId, index], i) => {
|
|
90
|
+
return {
|
|
91
|
+
index,
|
|
92
|
+
share: xor(
|
|
93
|
+
encryptedShares[i],
|
|
94
|
+
kdf(gid_r.pairing(publicKeys[i]), nonce, id, objectId, index)
|
|
95
|
+
)
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function encapBatched(publicKeys, id) {
|
|
101
|
+
if (publicKeys.length === 0) {
|
|
102
|
+
throw new Error("No public keys provided");
|
|
103
|
+
}
|
|
104
|
+
const r = Scalar.random();
|
|
105
|
+
const nonce = G2Element.generator().multiply(r);
|
|
106
|
+
const gid_r = hashToG1(id).multiply(r);
|
|
107
|
+
return [r, nonce, publicKeys.map((public_key) => gid_r.pairing(public_key))];
|
|
108
|
+
}
|
|
109
|
+
function decap(nonce, usk) {
|
|
110
|
+
return usk.pairing(nonce);
|
|
111
|
+
}
|
|
112
|
+
function verifyNonce(nonce, randomness, useBE = true) {
|
|
113
|
+
try {
|
|
114
|
+
const r = decodeRandomness(randomness, useBE);
|
|
115
|
+
return G2Element.generator().multiply(r).equals(nonce);
|
|
116
|
+
} catch {
|
|
117
|
+
throw new InvalidCiphertextError("Invalid randomness");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function decodeRandomness(bytes, useBE) {
|
|
121
|
+
if (useBE) {
|
|
122
|
+
return Scalar.fromBytes(bytes);
|
|
123
|
+
} else {
|
|
124
|
+
return Scalar.fromBytesLE(bytes);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function decryptRandomness(encryptedRandomness, randomnessKey) {
|
|
128
|
+
return xor(encryptedRandomness, randomnessKey);
|
|
129
|
+
}
|
|
130
|
+
function verifyNonceWithLE(nonce, randomness) {
|
|
131
|
+
try {
|
|
132
|
+
if (verifyNonce(nonce, randomness, false)) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
}
|
|
137
|
+
return verifyNonce(nonce, randomness, true);
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
BonehFranklinBLS12381Services,
|
|
141
|
+
DST_POP,
|
|
142
|
+
IBEServers,
|
|
143
|
+
decryptRandomness,
|
|
144
|
+
verifyNonce,
|
|
145
|
+
verifyNonceWithLE
|
|
146
|
+
};
|
|
147
|
+
//# sourceMappingURL=ibe.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/ibe.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@haneullabs/bcs';\n\nimport type { IBEEncryptions } from './bcs.js';\nimport type { G1Element, GTElement } from './bls12381.js';\nimport { G2Element, Scalar } from './bls12381.js';\nimport { deriveKey, hashToG1, kdf, KeyPurpose } from './kdf.js';\nimport type { KeyServer } from './key-server.js';\nimport { xor } from './utils.js';\nimport type { Share } from './shamir.js';\nimport { InvalidCiphertextError } from './error.js';\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 * Encrypt a batch of messages for the given identity.\n\t *\n\t * @param id The identity.\n\t * @param msgAndIndices The messages and the corresponding indices of the share being encrypted.\n\t * @returns The encrypted messages.\n\t */\n\tabstract encryptBatched(\n\t\tid: Uint8Array,\n\t\tshares: Share[],\n\t\tbaseKey: Uint8Array,\n\t\tthreshold: number,\n\t): typeof IBEEncryptions.$inferType;\n}\n\n/**\n * Identity-based encryption based on the Boneh-Franklin IBE scheme (https://eprint.iacr.org/2001/090).\n * Note that this implementation is of the \"BasicIdent\" protocol which on its own is not CCA secure, so this IBE implementation should not be used on its own.\n *\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\tshares: Share[],\n\t\tbaseKey: Uint8Array,\n\t\tthreshold: number,\n\t): typeof IBEEncryptions.$inferType {\n\t\tif (this.publicKeys.length === 0 || this.publicKeys.length !== shares.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 = shares.map(({ share, index }, i) =>\n\t\t\txor(share, kdf(keys[i], nonce, id, this.objectIds[i], index)),\n\t\t);\n\t\tconst randomnessKey = deriveKey(\n\t\t\tKeyPurpose.EncryptedRandomness,\n\t\t\tbaseKey,\n\t\t\tencryptedShares,\n\t\t\tthreshold,\n\t\t\tthis.objectIds,\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());\n\t\tconst rhs = hashToG1(fromHex(id)).pairing(publicKey);\n\t\treturn lhs.equals(rhs);\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 id The identity.\n\t * @param [objectId, index] The object id and index of the share.\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\tid: Uint8Array,\n\t\t[objectId, index]: [string, number],\n\t): Uint8Array {\n\t\treturn xor(ciphertext, kdf(decap(nonce, sk), nonce, id, objectId, index));\n\t}\n\n\t/**\n\t * Decrypt all shares and verify that the randomness was used to create the given nonce.\n\t *\n\t * @param randomness - The randomness.\n\t * @param encryptedShares - The encrypted shares.\n\t * @param services - The services.\n\t * @param publicKeys - The public keys.\n\t * @param nonce - The nonce.\n\t * @param id - The id.\n\t * @returns All decrypted shares.\n\t */\n\tstatic decryptAllSharesUsingRandomness(\n\t\trandomness: Uint8Array,\n\t\tencryptedShares: Uint8Array[],\n\t\tservices: [string, number][],\n\t\tpublicKeys: G2Element[],\n\t\tnonce: G2Element,\n\t\tid: Uint8Array,\n\t): { index: number; share: Uint8Array }[] {\n\t\tif (publicKeys.length !== encryptedShares.length || publicKeys.length !== services.length) {\n\t\t\tthrow new Error('The number of public keys, encrypted shares and services must be the same');\n\t\t}\n\t\tlet r;\n\t\ttry {\n\t\t\tr = Scalar.fromBytes(randomness);\n\t\t} catch {\n\t\t\tthrow new InvalidCiphertextError('Invalid randomness');\n\t\t}\n\t\tconst gid_r = hashToG1(id).multiply(r);\n\t\treturn services.map(([objectId, index], i) => {\n\t\t\treturn {\n\t\t\t\tindex,\n\t\t\t\tshare: xor(\n\t\t\t\t\tencryptedShares[i],\n\t\t\t\t\tkdf(gid_r.pairing(publicKeys[i]), nonce, id, objectId, index),\n\t\t\t\t),\n\t\t\t};\n\t\t});\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 The randomness, a common nonce of the keys and a list of keys.\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_r = hashToG1(id).multiply(r);\n\treturn [r, nonce, publicKeys.map((public_key) => gid_r.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\n/**\n * Verify that the given randomness was used to crate the nonce.\n * Throws an error if the given randomness is invalid (not a BLS scalar).\n *\n * @param randomness - The randomness.\n * @param nonce - The nonce.\n * @param useBE - Flag to indicate if BE encoding is used for the randomness. Defaults to true.\n * @returns True if the randomness was used to create the nonce, false otherwise.\n */\nexport function verifyNonce(\n\tnonce: G2Element,\n\trandomness: Uint8Array,\n\tuseBE: boolean = true,\n): boolean {\n\ttry {\n\t\tconst r = decodeRandomness(randomness, useBE);\n\t\treturn G2Element.generator().multiply(r).equals(nonce);\n\t} catch {\n\t\tthrow new InvalidCiphertextError('Invalid randomness');\n\t}\n}\n\nfunction decodeRandomness(bytes: Uint8Array, useBE: boolean): Scalar {\n\tif (useBE) {\n\t\treturn Scalar.fromBytes(bytes);\n\t} else {\n\t\treturn Scalar.fromBytesLE(bytes);\n\t}\n}\n\n/**\n * Decrypt the randomness using a key.\n *\n * @param encrypted_randomness - The encrypted randomness.\n * @param derived_key - The derived key.\n * @returns The randomness. Returns both the scalar interpreted in big-endian and little-endian encoding.\n */\nexport function decryptRandomness(\n\tencryptedRandomness: Uint8Array,\n\trandomnessKey: Uint8Array,\n): Uint8Array {\n\treturn xor(encryptedRandomness, randomnessKey);\n}\n\n/**\n * Verify that the given randomness was used to crate the nonce.\n * Check using both big-endian and little-endian encoding of the randomness.\n *\n * Throws an error if the nonce check doesn't pass using LE encoding _and_ the randomness is invalid as a BE encoded scalar.\n *\n * @param randomness - The randomness.\n * @param nonce - The nonce.\n * @returns True if the randomness was used to create the nonce using either LE or BE encoding, false otherwise.\n */\nexport function verifyNonceWithLE(nonce: G2Element, randomness: Uint8Array): boolean {\n\ttry {\n\t\t// First try little-endian encoding\n\t\tif (verifyNonce(nonce, randomness, false)) {\n\t\t\treturn true;\n\t\t}\n\t} catch {\n\t\t// Ignore error and try big-endian encoding\n\t}\n\treturn verifyNonce(nonce, randomness, true);\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,eAAe;AAIxB,SAAS,WAAW,cAAc;AAClC,SAAS,WAAW,UAAU,KAAK,kBAAkB;AAErD,SAAS,WAAW;AAEpB,SAAS,8BAA8B;AAKhC,MAAM,UAAsB,IAAI,YAAY,EAAE,OAAO,8BAA8B;AAKnF,MAAe,WAAW;AAAA,EAGhC,YAAY,WAAqB;AAChC,SAAK,YAAY;AAAA,EAClB;AAeD;AAQO,MAAM,sCAAsC,WAAW;AAAA,EAG7D,YAAY,UAAuB;AAClC,UAAM,SAAS,IAAI,CAAC,YAAY,QAAQ,QAAQ,CAAC;AACjD,SAAK,aAAa,SAAS,IAAI,CAAC,YAAY,UAAU,UAAU,QAAQ,EAAE,CAAC;AAAA,EAC5E;AAAA,EAEA,eACC,IACA,QACA,SACA,WACmC;AACnC,QAAI,KAAK,WAAW,WAAW,KAAK,KAAK,WAAW,WAAW,OAAO,QAAQ;AAC7E,YAAM,IAAI,MAAM,qBAAqB;AAAA,IACtC;AACA,UAAM,CAAC,GAAG,OAAO,IAAI,IAAI,aAAa,KAAK,YAAY,EAAE;AACzD,UAAM,kBAAkB,OAAO;AAAA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G;AAC1C,aAAO;AAAA,IACR;AAAA,EACD,QAAQ;AAAA,EAER;AACA,SAAO,YAAY,OAAO,YAAY,IAAI;AAC3C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { EncryptedObject } from './bcs.js';
|
|
2
|
+
export { SealClient } from './client.js';
|
|
3
|
+
export { SessionKey, type ExportedSessionKey } from './session-key.js';
|
|
4
|
+
export * from './error.js';
|
|
5
|
+
export type { SealCompatibleClient, SealClientOptions, SealClientExtensionOptions, KeyServerConfig, EncryptOptions, DecryptOptions, FetchKeysOptions, GetDerivedKeysOptions, } from './types.js';
|
|
6
|
+
export { DemType } from './encrypt.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EncryptedObject } from "./bcs.js";
|
|
2
|
+
import { SealClient } from "./client.js";
|
|
3
|
+
import { SessionKey } from "./session-key.js";
|
|
4
|
+
export * from "./error.js";
|
|
5
|
+
import { DemType } from "./encrypt.js";
|
|
6
|
+
export {
|
|
7
|
+
DemType,
|
|
8
|
+
EncryptedObject,
|
|
9
|
+
SealClient,
|
|
10
|
+
SessionKey
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { EncryptedObject } from './bcs.js';\nexport { SealClient } from './client.js';\nexport { SessionKey, type ExportedSessionKey } from './session-key.js';\nexport * from './error.js';\nexport type {\n\tSealCompatibleClient,\n\tSealClientOptions,\n\tSealClientExtensionOptions,\n\tKeyServerConfig,\n\tEncryptOptions,\n\tDecryptOptions,\n\tFetchKeysOptions,\n\tGetDerivedKeysOptions,\n} from './types.js';\nexport { DemType } from './encrypt.js';\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,uBAAuB;AAChC,SAAS,kBAAkB;AAC3B,SAAS,kBAA2C;AACpD,cAAc;AAWd,SAAS,eAAe;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { G1Element } from './bls12381.js';
|
|
2
|
+
import type { G2Element, GTElement } from './bls12381.js';
|
|
3
|
+
/**
|
|
4
|
+
* Hash an id to a G1Element.
|
|
5
|
+
*
|
|
6
|
+
* @param id The id to hash.
|
|
7
|
+
* @returns The G1Element.
|
|
8
|
+
*/
|
|
9
|
+
export declare function hashToG1(id: Uint8Array): G1Element;
|
|
10
|
+
/**
|
|
11
|
+
* The default key derivation function.
|
|
12
|
+
*
|
|
13
|
+
* @returns The derived key.
|
|
14
|
+
*/
|
|
15
|
+
export declare function kdf(element: GTElement, nonce: G2Element, id: Uint8Array, objectId: string, index: number): Uint8Array;
|
|
16
|
+
export declare enum KeyPurpose {
|
|
17
|
+
EncryptedRandomness = 0,
|
|
18
|
+
DEM = 1
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Derive a key from a base key and a list of encrypted shares.
|
|
22
|
+
*
|
|
23
|
+
* @param purpose The purpose of the key.
|
|
24
|
+
* @param baseKey The base key.
|
|
25
|
+
* @param encryptedShares The encrypted shares.
|
|
26
|
+
* @param threshold The threshold.
|
|
27
|
+
* @param keyServers The object ids of the key servers.
|
|
28
|
+
* @returns The derived key.
|
|
29
|
+
*/
|
|
30
|
+
export declare function deriveKey(purpose: KeyPurpose, baseKey: Uint8Array, encryptedShares: Uint8Array[], threshold: number, keyServers: string[]): Uint8Array;
|