@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,151 @@
|
|
|
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 __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var bls12381_exports = {};
|
|
20
|
+
__export(bls12381_exports, {
|
|
21
|
+
G1Element: () => G1Element,
|
|
22
|
+
G2Element: () => G2Element,
|
|
23
|
+
GTElement: () => GTElement,
|
|
24
|
+
Scalar: () => Scalar
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(bls12381_exports);
|
|
27
|
+
var import_bls12_381 = require("@noble/curves/bls12-381");
|
|
28
|
+
var import_utils = require("@noble/curves/utils");
|
|
29
|
+
const _G1Element = class _G1Element {
|
|
30
|
+
constructor(point) {
|
|
31
|
+
this.point = point;
|
|
32
|
+
}
|
|
33
|
+
static generator() {
|
|
34
|
+
return new _G1Element(import_bls12_381.bls12_381.G1.Point.BASE);
|
|
35
|
+
}
|
|
36
|
+
static fromBytes(bytes) {
|
|
37
|
+
try {
|
|
38
|
+
return new _G1Element(import_bls12_381.bls12_381.G1.Point.fromBytes(bytes));
|
|
39
|
+
} catch {
|
|
40
|
+
throw new Error("Invalid G1 point");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
toBytes() {
|
|
44
|
+
return this.point.toBytes();
|
|
45
|
+
}
|
|
46
|
+
multiply(scalar) {
|
|
47
|
+
return new _G1Element(this.point.multiply(scalar.scalar));
|
|
48
|
+
}
|
|
49
|
+
add(other) {
|
|
50
|
+
return new _G1Element(this.point.add(other.point));
|
|
51
|
+
}
|
|
52
|
+
subtract(other) {
|
|
53
|
+
return new _G1Element(this.point.subtract(other.point));
|
|
54
|
+
}
|
|
55
|
+
static hashToCurve(data) {
|
|
56
|
+
return new _G1Element(import_bls12_381.bls12_381.G1.Point.fromAffine(import_bls12_381.bls12_381.G1.hashToCurve(data).toAffine()));
|
|
57
|
+
}
|
|
58
|
+
pairing(other) {
|
|
59
|
+
return new GTElement(import_bls12_381.bls12_381.pairing(this.point, other.point));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
_G1Element.SIZE = 48;
|
|
63
|
+
let G1Element = _G1Element;
|
|
64
|
+
const _G2Element = class _G2Element {
|
|
65
|
+
constructor(point) {
|
|
66
|
+
this.point = point;
|
|
67
|
+
}
|
|
68
|
+
static generator() {
|
|
69
|
+
return new _G2Element(import_bls12_381.bls12_381.G2.Point.BASE);
|
|
70
|
+
}
|
|
71
|
+
static fromBytes(bytes) {
|
|
72
|
+
try {
|
|
73
|
+
return new _G2Element(import_bls12_381.bls12_381.G2.Point.fromBytes(bytes));
|
|
74
|
+
} catch {
|
|
75
|
+
throw new Error("Invalid G2 point");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
toBytes() {
|
|
79
|
+
return this.point.toBytes();
|
|
80
|
+
}
|
|
81
|
+
multiply(scalar) {
|
|
82
|
+
return new _G2Element(this.point.multiply(scalar.scalar));
|
|
83
|
+
}
|
|
84
|
+
add(other) {
|
|
85
|
+
return new _G2Element(this.point.add(other.point));
|
|
86
|
+
}
|
|
87
|
+
static hashToCurve(data) {
|
|
88
|
+
return new _G2Element(import_bls12_381.bls12_381.G2.Point.fromAffine(import_bls12_381.bls12_381.G2.hashToCurve(data).toAffine()));
|
|
89
|
+
}
|
|
90
|
+
equals(other) {
|
|
91
|
+
return this.point.equals(other.point);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
_G2Element.SIZE = 96;
|
|
95
|
+
let G2Element = _G2Element;
|
|
96
|
+
const _GTElement = class _GTElement {
|
|
97
|
+
constructor(element) {
|
|
98
|
+
this.element = element;
|
|
99
|
+
}
|
|
100
|
+
toBytes() {
|
|
101
|
+
const P = [0, 3, 1, 4, 2, 5];
|
|
102
|
+
const PAIR_SIZE = _GTElement.SIZE / P.length;
|
|
103
|
+
const bytes = import_bls12_381.bls12_381.fields.Fp12.toBytes(this.element);
|
|
104
|
+
const result = new Uint8Array(_GTElement.SIZE);
|
|
105
|
+
for (let i = 0; i < P.length; i++) {
|
|
106
|
+
const sourceStart = P[i] * PAIR_SIZE;
|
|
107
|
+
const sourceEnd = sourceStart + PAIR_SIZE;
|
|
108
|
+
const targetStart = i * PAIR_SIZE;
|
|
109
|
+
result.set(bytes.subarray(sourceStart, sourceEnd), targetStart);
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
equals(other) {
|
|
114
|
+
return import_bls12_381.bls12_381.fields.Fp12.eql(this.element, other.element);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
_GTElement.SIZE = 576;
|
|
118
|
+
let GTElement = _GTElement;
|
|
119
|
+
const _Scalar = class _Scalar {
|
|
120
|
+
constructor(scalar) {
|
|
121
|
+
this.scalar = scalar;
|
|
122
|
+
}
|
|
123
|
+
static fromBigint(scalar) {
|
|
124
|
+
if (scalar < 0n || scalar >= import_bls12_381.bls12_381.fields.Fr.ORDER) {
|
|
125
|
+
throw new Error("Scalar out of range");
|
|
126
|
+
}
|
|
127
|
+
return new _Scalar(scalar);
|
|
128
|
+
}
|
|
129
|
+
static random() {
|
|
130
|
+
const randomSecretKey = import_bls12_381.bls12_381.utils.randomSecretKey();
|
|
131
|
+
return _Scalar.fromBytes(randomSecretKey);
|
|
132
|
+
}
|
|
133
|
+
toBytes() {
|
|
134
|
+
return (0, import_utils.numberToBytesBE)(this.scalar, _Scalar.SIZE);
|
|
135
|
+
}
|
|
136
|
+
static fromBytes(bytes) {
|
|
137
|
+
if (bytes.length !== _Scalar.SIZE) {
|
|
138
|
+
throw new Error("Invalid scalar length");
|
|
139
|
+
}
|
|
140
|
+
return this.fromBigint((0, import_utils.bytesToNumberBE)(bytes));
|
|
141
|
+
}
|
|
142
|
+
static fromBytesLE(bytes) {
|
|
143
|
+
if (bytes.length !== _Scalar.SIZE) {
|
|
144
|
+
throw new Error("Invalid scalar length");
|
|
145
|
+
}
|
|
146
|
+
return this.fromBigint((0, import_utils.bytesToNumberLE)(bytes));
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
_Scalar.SIZE = 32;
|
|
150
|
+
let Scalar = _Scalar;
|
|
151
|
+
//# sourceMappingURL=bls12381.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/bls12381.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Fp2, Fp12 } from '@noble/curves/abstract/tower';\nimport type { WeierstrassPoint } from '@noble/curves/abstract/weierstrass';\nimport { bls12_381 } from '@noble/curves/bls12-381';\nimport { bytesToNumberBE, bytesToNumberLE, numberToBytesBE } from '@noble/curves/utils';\n\nexport class G1Element {\n\tpoint: WeierstrassPoint<bigint>;\n\n\tpublic static readonly SIZE = 48;\n\n\tconstructor(point: WeierstrassPoint<bigint>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G1Element {\n\t\treturn new G1Element(bls12_381.G1.Point.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G1Element {\n\t\ttry {\n\t\t\treturn new G1Element(bls12_381.G1.Point.fromBytes(bytes));\n\t\t} catch {\n\t\t\tthrow new Error('Invalid G1 point');\n\t\t}\n\t}\n\n\ttoBytes(): Uint8Array<ArrayBuffer> {\n\t\treturn this.point.toBytes() as Uint8Array<ArrayBuffer>;\n\t}\n\n\tmultiply(scalar: Scalar): G1Element {\n\t\treturn new G1Element(this.point.multiply(scalar.scalar));\n\t}\n\n\tadd(other: G1Element): G1Element {\n\t\treturn new G1Element(this.point.add(other.point));\n\t}\n\n\tsubtract(other: G1Element): G1Element {\n\t\treturn new G1Element(this.point.subtract(other.point));\n\t}\n\n\tstatic hashToCurve(data: Uint8Array): G1Element {\n\t\treturn new G1Element(bls12_381.G1.Point.fromAffine(bls12_381.G1.hashToCurve(data).toAffine()));\n\t}\n\n\tpairing(other: G2Element): GTElement {\n\t\treturn new GTElement(bls12_381.pairing(this.point, other.point));\n\t}\n}\n\nexport class G2Element {\n\tpoint: WeierstrassPoint<Fp2>;\n\n\tpublic static readonly SIZE = 96;\n\n\tconstructor(point: WeierstrassPoint<Fp2>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G2Element {\n\t\treturn new G2Element(bls12_381.G2.Point.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G2Element {\n\t\ttry {\n\t\t\treturn new G2Element(bls12_381.G2.Point.fromBytes(bytes));\n\t\t} catch {\n\t\t\tthrow new Error('Invalid G2 point');\n\t\t}\n\t}\n\n\ttoBytes(): Uint8Array<ArrayBuffer> {\n\t\treturn this.point.toBytes() as Uint8Array<ArrayBuffer>;\n\t}\n\n\tmultiply(scalar: Scalar): G2Element {\n\t\treturn new G2Element(this.point.multiply(scalar.scalar));\n\t}\n\n\tadd(other: G2Element): G2Element {\n\t\treturn new G2Element(this.point.add(other.point));\n\t}\n\n\tstatic hashToCurve(data: Uint8Array): G2Element {\n\t\treturn new G2Element(bls12_381.G2.Point.fromAffine(bls12_381.G2.hashToCurve(data).toAffine()));\n\t}\n\n\tequals(other: G2Element): boolean {\n\t\treturn this.point.equals(other.point);\n\t}\n}\n\nexport class GTElement {\n\telement: Fp12;\n\n\tpublic static readonly SIZE = 576;\n\n\tconstructor(element: Fp12) {\n\t\tthis.element = element;\n\t}\n\n\ttoBytes(): Uint8Array<ArrayBuffer> {\n\t\t// This permutation reorders the 6 pairs of coefficients of the GT element for compatability with the Rust and Move implementations.\n\t\t//\n\t\t// The permutation P may be computed as:\n\t\t// for i in 0..3 {\n\t\t// for j in 0..2 {\n\t\t// P[2 * i + j] = i + 3 * j;\n\t\t// }\n\t\t// }\n\t\tconst P = [0, 3, 1, 4, 2, 5];\n\t\tconst PAIR_SIZE = GTElement.SIZE / P.length;\n\n\t\tconst bytes = bls12_381.fields.Fp12.toBytes(this.element);\n\t\tconst result = new Uint8Array(GTElement.SIZE);\n\n\t\tfor (let i = 0; i < P.length; i++) {\n\t\t\tconst sourceStart = P[i] * PAIR_SIZE;\n\t\t\tconst sourceEnd = sourceStart + PAIR_SIZE;\n\t\t\tconst targetStart = i * PAIR_SIZE;\n\t\t\tresult.set(bytes.subarray(sourceStart, sourceEnd), targetStart);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tequals(other: GTElement): boolean {\n\t\treturn bls12_381.fields.Fp12.eql(this.element, other.element);\n\t}\n}\n\nexport class Scalar {\n\tscalar: bigint;\n\n\tpublic static readonly SIZE = 32;\n\n\tconstructor(scalar: bigint) {\n\t\tthis.scalar = scalar;\n\t}\n\n\tstatic fromBigint(scalar: bigint): Scalar {\n\t\tif (scalar < 0n || scalar >= bls12_381.fields.Fr.ORDER) {\n\t\t\tthrow new Error('Scalar out of range');\n\t\t}\n\t\treturn new Scalar(scalar);\n\t}\n\n\tstatic random(): Scalar {\n\t\tconst randomSecretKey = bls12_381.utils.randomSecretKey();\n\t\treturn Scalar.fromBytes(randomSecretKey)!;\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn numberToBytesBE(this.scalar, Scalar.SIZE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): Scalar {\n\t\tif (bytes.length !== Scalar.SIZE) {\n\t\t\tthrow new Error('Invalid scalar length');\n\t\t}\n\t\treturn this.fromBigint(bytesToNumberBE(bytes));\n\t}\n\n\tstatic fromBytesLE(bytes: Uint8Array): Scalar {\n\t\tif (bytes.length !== Scalar.SIZE) {\n\t\t\tthrow new Error('Invalid scalar length');\n\t\t}\n\t\treturn this.fromBigint(bytesToNumberLE(bytes));\n\t}\n}\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { G2Element } from './bls12381.js';
|
|
2
|
+
import type { DerivedKey, KeyServer } from './key-server.js';
|
|
3
|
+
import type { DecryptOptions, EncryptOptions, FetchKeysOptions, GetDerivedKeysOptions, SealClientExtensionOptions, SealClientOptions, SealCompatibleClient, SealOptions } from './types.js';
|
|
4
|
+
export declare function seal<Name = 'seal'>({ name, ...options }: SealOptions<Name>): {
|
|
5
|
+
name: Name;
|
|
6
|
+
register: (client: SealCompatibleClient) => SealClient;
|
|
7
|
+
};
|
|
8
|
+
export declare class SealClient {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(options: SealClientOptions);
|
|
11
|
+
/** @deprecated Use `seal()` instead */
|
|
12
|
+
static asClientExtension(options: SealClientExtensionOptions): {
|
|
13
|
+
name: "seal";
|
|
14
|
+
register: (client: SealCompatibleClient) => SealClient;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Return an encrypted message under the identity.
|
|
18
|
+
*
|
|
19
|
+
* @param kemType - The type of KEM to use.
|
|
20
|
+
* @param demType - The type of DEM to use.
|
|
21
|
+
* @param threshold - The threshold for the TSS encryption.
|
|
22
|
+
* @param packageId - the packageId namespace.
|
|
23
|
+
* @param id - the identity to use.
|
|
24
|
+
* @param data - the data to encrypt.
|
|
25
|
+
* @param aad - optional additional authenticated data.
|
|
26
|
+
* @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.
|
|
27
|
+
* Since the symmetric key can be used to decrypt, it should not be shared but can be used e.g. for backup.
|
|
28
|
+
*/
|
|
29
|
+
encrypt({ kemType, demType, threshold, packageId, id, data, aad, }: EncryptOptions): Promise<{
|
|
30
|
+
encryptedObject: Uint8Array<ArrayBuffer>;
|
|
31
|
+
key: Uint8Array<ArrayBuffer>;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Decrypt the given encrypted bytes using cached keys.
|
|
35
|
+
* Calls fetchKeys in case one or more of the required keys is not cached yet.
|
|
36
|
+
* The function throws an error if the client's key servers are not a subset of
|
|
37
|
+
* the encrypted object's key servers or if the threshold cannot be met.
|
|
38
|
+
*
|
|
39
|
+
* If checkShareConsistency is true, the decrypted shares are checked for consistency, meaning that
|
|
40
|
+
* any combination of at least threshold shares should either succesfully combine to the plaintext or fail.
|
|
41
|
+
* This is useful in case the encryptor is not trusted and the decryptor wants to ensure all decryptors
|
|
42
|
+
* receive the same output (e.g., for onchain encrypted voting).
|
|
43
|
+
*
|
|
44
|
+
* @param data - The encrypted bytes to decrypt.
|
|
45
|
+
* @param sessionKey - The session key to use.
|
|
46
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
47
|
+
* @param checkShareConsistency - If true, the shares are checked for consistency.
|
|
48
|
+
* @param checkLEEncoding - If true, the encryption is also checked using an LE encoded nonce.
|
|
49
|
+
* @returns - The decrypted plaintext corresponding to ciphertext.
|
|
50
|
+
*/
|
|
51
|
+
decrypt({ data, sessionKey, txBytes, checkShareConsistency, checkLEEncoding, }: DecryptOptions): Promise<Uint8Array<ArrayBufferLike>>;
|
|
52
|
+
getKeyServers(): Promise<Map<string, KeyServer>>;
|
|
53
|
+
/**
|
|
54
|
+
* Get the public keys for the given services.
|
|
55
|
+
* If all public keys are not in the cache, they are retrieved.
|
|
56
|
+
*
|
|
57
|
+
* @param services - The services to get the public keys for.
|
|
58
|
+
* @returns The public keys for the given services in the same order as the given services.
|
|
59
|
+
*/
|
|
60
|
+
getPublicKeys(services: string[]): Promise<G2Element[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Fetch keys from the key servers and update the cache.
|
|
63
|
+
*
|
|
64
|
+
* It is recommended to call this function once for all ids of all encrypted objects if
|
|
65
|
+
* there are multiple, then call decrypt for each object. This avoids calling fetchKey
|
|
66
|
+
* individually for each decrypt.
|
|
67
|
+
*
|
|
68
|
+
* @param ids - The ids of the encrypted objects.
|
|
69
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
70
|
+
* @param sessionKey - The session key to use.
|
|
71
|
+
* @param threshold - The threshold for the TSS encryptions. The function returns when a threshold of key servers had returned keys for all ids.
|
|
72
|
+
*/
|
|
73
|
+
fetchKeys({ ids, txBytes, sessionKey, threshold }: FetchKeysOptions): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Get derived keys from the given services.
|
|
76
|
+
*
|
|
77
|
+
* @param id - The id of the encrypted object.
|
|
78
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
79
|
+
* @param sessionKey - The session key to use.
|
|
80
|
+
* @param threshold - The threshold.
|
|
81
|
+
* @returns - Derived keys for the given services that are in the cache as a "service object ID" -> derived key map. If the call is succesful, exactly threshold keys will be returned.
|
|
82
|
+
*/
|
|
83
|
+
getDerivedKeys({ kemType, id, txBytes, sessionKey, threshold, }: GetDerivedKeysOptions): Promise<Map<string, DerivedKey>>;
|
|
84
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
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 __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
24
|
+
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);
|
|
25
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
26
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
27
|
+
var client_exports = {};
|
|
28
|
+
__export(client_exports, {
|
|
29
|
+
SealClient: () => SealClient,
|
|
30
|
+
seal: () => seal
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(client_exports);
|
|
33
|
+
var import_bcs = require("./bcs.js");
|
|
34
|
+
var import_bls12381 = require("./bls12381.js");
|
|
35
|
+
var import_decrypt = require("./decrypt.js");
|
|
36
|
+
var import_dem = require("./dem.js");
|
|
37
|
+
var import_encrypt = require("./encrypt.js");
|
|
38
|
+
var import_error = require("./error.js");
|
|
39
|
+
var import_ibe = require("./ibe.js");
|
|
40
|
+
var import_key_server = require("./key-server.js");
|
|
41
|
+
var import_utils = require("./utils.js");
|
|
42
|
+
var _haneulClient, _configs, _keyServers, _verifyKeyServers, _cachedKeys, _cachedPublicKeys, _timeout, _totalWeight, _SealClient_instances, createEncryptionInput_fn, weight_fn, validateEncryptionServices_fn, getWeightedKeyServers_fn, loadKeyServers_fn;
|
|
43
|
+
function seal({ name = "seal", ...options }) {
|
|
44
|
+
return {
|
|
45
|
+
name,
|
|
46
|
+
register: (client) => {
|
|
47
|
+
return new SealClient({
|
|
48
|
+
haneulClient: client,
|
|
49
|
+
...options
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const _SealClient = class _SealClient {
|
|
55
|
+
constructor(options) {
|
|
56
|
+
__privateAdd(this, _SealClient_instances);
|
|
57
|
+
__privateAdd(this, _haneulClient);
|
|
58
|
+
__privateAdd(this, _configs);
|
|
59
|
+
__privateAdd(this, _keyServers, null);
|
|
60
|
+
__privateAdd(this, _verifyKeyServers);
|
|
61
|
+
// A caching map for: fullId:object_id -> partial key.
|
|
62
|
+
__privateAdd(this, _cachedKeys, /* @__PURE__ */ new Map());
|
|
63
|
+
__privateAdd(this, _cachedPublicKeys, /* @__PURE__ */ new Map());
|
|
64
|
+
__privateAdd(this, _timeout);
|
|
65
|
+
__privateAdd(this, _totalWeight);
|
|
66
|
+
__privateSet(this, _haneulClient, options.haneulClient);
|
|
67
|
+
if (new Set(options.serverConfigs.map((s) => s.objectId)).size !== options.serverConfigs.length) {
|
|
68
|
+
throw new import_error.InvalidClientOptionsError("Duplicate object IDs");
|
|
69
|
+
}
|
|
70
|
+
if (options.serverConfigs.some((s) => s.apiKeyName && !s.apiKey || !s.apiKeyName && s.apiKey)) {
|
|
71
|
+
throw new import_error.InvalidClientOptionsError(
|
|
72
|
+
"Both apiKeyName and apiKey must be provided or not provided for all key servers"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
__privateSet(this, _configs, new Map(options.serverConfigs.map((server) => [server.objectId, server])));
|
|
76
|
+
__privateSet(this, _totalWeight, options.serverConfigs.map((server) => server.weight).reduce((sum, term) => sum + term, 0));
|
|
77
|
+
__privateSet(this, _verifyKeyServers, options.verifyKeyServers ?? true);
|
|
78
|
+
__privateSet(this, _timeout, options.timeout ?? 1e4);
|
|
79
|
+
}
|
|
80
|
+
/** @deprecated Use `seal()` instead */
|
|
81
|
+
static asClientExtension(options) {
|
|
82
|
+
return {
|
|
83
|
+
name: "seal",
|
|
84
|
+
register: (client) => {
|
|
85
|
+
return new _SealClient({
|
|
86
|
+
haneulClient: client,
|
|
87
|
+
...options
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Return an encrypted message under the identity.
|
|
94
|
+
*
|
|
95
|
+
* @param kemType - The type of KEM to use.
|
|
96
|
+
* @param demType - The type of DEM to use.
|
|
97
|
+
* @param threshold - The threshold for the TSS encryption.
|
|
98
|
+
* @param packageId - the packageId namespace.
|
|
99
|
+
* @param id - the identity to use.
|
|
100
|
+
* @param data - the data to encrypt.
|
|
101
|
+
* @param aad - optional additional authenticated data.
|
|
102
|
+
* @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.
|
|
103
|
+
* Since the symmetric key can be used to decrypt, it should not be shared but can be used e.g. for backup.
|
|
104
|
+
*/
|
|
105
|
+
async encrypt({
|
|
106
|
+
kemType = import_encrypt.KemType.BonehFranklinBLS12381DemCCA,
|
|
107
|
+
demType = import_encrypt.DemType.AesGcm256,
|
|
108
|
+
threshold,
|
|
109
|
+
packageId,
|
|
110
|
+
id,
|
|
111
|
+
data,
|
|
112
|
+
aad = new Uint8Array()
|
|
113
|
+
}) {
|
|
114
|
+
const packageObj = await __privateGet(this, _haneulClient).core.getObject({ objectId: packageId });
|
|
115
|
+
if (String(packageObj.object.version) !== "1") {
|
|
116
|
+
throw new import_error.InvalidPackageError(`Package ${packageId} is not the first version`);
|
|
117
|
+
}
|
|
118
|
+
return (0, import_encrypt.encrypt)({
|
|
119
|
+
keyServers: await __privateMethod(this, _SealClient_instances, getWeightedKeyServers_fn).call(this),
|
|
120
|
+
kemType,
|
|
121
|
+
threshold,
|
|
122
|
+
packageId,
|
|
123
|
+
id,
|
|
124
|
+
encryptionInput: __privateMethod(this, _SealClient_instances, createEncryptionInput_fn).call(this, demType, data, aad)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Decrypt the given encrypted bytes using cached keys.
|
|
129
|
+
* Calls fetchKeys in case one or more of the required keys is not cached yet.
|
|
130
|
+
* The function throws an error if the client's key servers are not a subset of
|
|
131
|
+
* the encrypted object's key servers or if the threshold cannot be met.
|
|
132
|
+
*
|
|
133
|
+
* If checkShareConsistency is true, the decrypted shares are checked for consistency, meaning that
|
|
134
|
+
* any combination of at least threshold shares should either succesfully combine to the plaintext or fail.
|
|
135
|
+
* This is useful in case the encryptor is not trusted and the decryptor wants to ensure all decryptors
|
|
136
|
+
* receive the same output (e.g., for onchain encrypted voting).
|
|
137
|
+
*
|
|
138
|
+
* @param data - The encrypted bytes to decrypt.
|
|
139
|
+
* @param sessionKey - The session key to use.
|
|
140
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
141
|
+
* @param checkShareConsistency - If true, the shares are checked for consistency.
|
|
142
|
+
* @param checkLEEncoding - If true, the encryption is also checked using an LE encoded nonce.
|
|
143
|
+
* @returns - The decrypted plaintext corresponding to ciphertext.
|
|
144
|
+
*/
|
|
145
|
+
async decrypt({
|
|
146
|
+
data,
|
|
147
|
+
sessionKey,
|
|
148
|
+
txBytes,
|
|
149
|
+
checkShareConsistency,
|
|
150
|
+
checkLEEncoding
|
|
151
|
+
}) {
|
|
152
|
+
const encryptedObject = import_bcs.EncryptedObject.parse(data);
|
|
153
|
+
__privateMethod(this, _SealClient_instances, validateEncryptionServices_fn).call(this, encryptedObject.services.map((s) => s[0]), encryptedObject.threshold);
|
|
154
|
+
await this.fetchKeys({
|
|
155
|
+
ids: [encryptedObject.id],
|
|
156
|
+
txBytes,
|
|
157
|
+
sessionKey,
|
|
158
|
+
threshold: encryptedObject.threshold
|
|
159
|
+
});
|
|
160
|
+
if (checkShareConsistency) {
|
|
161
|
+
const publicKeys = await this.getPublicKeys(
|
|
162
|
+
encryptedObject.services.map(([objectId, _]) => objectId)
|
|
163
|
+
);
|
|
164
|
+
return (0, import_decrypt.decrypt)({
|
|
165
|
+
encryptedObject,
|
|
166
|
+
keys: __privateGet(this, _cachedKeys),
|
|
167
|
+
publicKeys,
|
|
168
|
+
checkLEEncoding: false
|
|
169
|
+
// We intentionally do not support other encodings here
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return (0, import_decrypt.decrypt)({ encryptedObject, keys: __privateGet(this, _cachedKeys), checkLEEncoding });
|
|
173
|
+
}
|
|
174
|
+
async getKeyServers() {
|
|
175
|
+
if (!__privateGet(this, _keyServers)) {
|
|
176
|
+
__privateSet(this, _keyServers, __privateMethod(this, _SealClient_instances, loadKeyServers_fn).call(this).catch((error) => {
|
|
177
|
+
__privateSet(this, _keyServers, null);
|
|
178
|
+
throw error;
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
181
|
+
return __privateGet(this, _keyServers);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Get the public keys for the given services.
|
|
185
|
+
* If all public keys are not in the cache, they are retrieved.
|
|
186
|
+
*
|
|
187
|
+
* @param services - The services to get the public keys for.
|
|
188
|
+
* @returns The public keys for the given services in the same order as the given services.
|
|
189
|
+
*/
|
|
190
|
+
async getPublicKeys(services) {
|
|
191
|
+
const keyServers = await this.getKeyServers();
|
|
192
|
+
const missingKeyServers = services.filter(
|
|
193
|
+
(objectId) => !keyServers.has(objectId) && !__privateGet(this, _cachedPublicKeys).has(objectId)
|
|
194
|
+
);
|
|
195
|
+
if (missingKeyServers.length > 0) {
|
|
196
|
+
(await (0, import_key_server.retrieveKeyServers)({
|
|
197
|
+
objectIds: missingKeyServers,
|
|
198
|
+
client: __privateGet(this, _haneulClient)
|
|
199
|
+
})).forEach(
|
|
200
|
+
(keyServer) => __privateGet(this, _cachedPublicKeys).set(keyServer.objectId, import_bls12381.G2Element.fromBytes(keyServer.pk))
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
return services.map((objectId) => {
|
|
204
|
+
const keyServer = keyServers.get(objectId);
|
|
205
|
+
if (keyServer) {
|
|
206
|
+
return import_bls12381.G2Element.fromBytes(keyServer.pk);
|
|
207
|
+
}
|
|
208
|
+
return __privateGet(this, _cachedPublicKeys).get(objectId);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Fetch keys from the key servers and update the cache.
|
|
213
|
+
*
|
|
214
|
+
* It is recommended to call this function once for all ids of all encrypted objects if
|
|
215
|
+
* there are multiple, then call decrypt for each object. This avoids calling fetchKey
|
|
216
|
+
* individually for each decrypt.
|
|
217
|
+
*
|
|
218
|
+
* @param ids - The ids of the encrypted objects.
|
|
219
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
220
|
+
* @param sessionKey - The session key to use.
|
|
221
|
+
* @param threshold - The threshold for the TSS encryptions. The function returns when a threshold of key servers had returned keys for all ids.
|
|
222
|
+
*/
|
|
223
|
+
async fetchKeys({ ids, txBytes, sessionKey, threshold }) {
|
|
224
|
+
if (threshold > __privateGet(this, _totalWeight) || threshold < 1) {
|
|
225
|
+
throw new import_error.InvalidThresholdError(
|
|
226
|
+
`Invalid threshold ${threshold} servers with weights ${JSON.stringify(__privateGet(this, _configs))}`
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
const keyServers = await this.getKeyServers();
|
|
230
|
+
const fullIds = ids.map((id) => (0, import_utils.createFullId)(sessionKey.getPackageId(), id));
|
|
231
|
+
let completedWeight = 0;
|
|
232
|
+
const remainingKeyServers = [];
|
|
233
|
+
let remainingKeyServersWeight = 0;
|
|
234
|
+
for (const objectId of keyServers.keys()) {
|
|
235
|
+
if (fullIds.every((fullId) => __privateGet(this, _cachedKeys).has(`${fullId}:${objectId}`))) {
|
|
236
|
+
completedWeight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
|
|
237
|
+
} else {
|
|
238
|
+
remainingKeyServers.push(objectId);
|
|
239
|
+
remainingKeyServersWeight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (completedWeight >= threshold) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const certificate = await sessionKey.getCertificate();
|
|
246
|
+
const signedRequest = await sessionKey.createRequestParams(txBytes);
|
|
247
|
+
const controller = new AbortController();
|
|
248
|
+
const errors = [];
|
|
249
|
+
const keyFetches = remainingKeyServers.map(async (objectId) => {
|
|
250
|
+
const server = keyServers.get(objectId);
|
|
251
|
+
try {
|
|
252
|
+
const config = __privateGet(this, _configs).get(objectId);
|
|
253
|
+
const allKeys = await (0, import_key_server.fetchKeysForAllIds)({
|
|
254
|
+
url: server.url,
|
|
255
|
+
requestSignature: signedRequest.requestSignature,
|
|
256
|
+
transactionBytes: txBytes,
|
|
257
|
+
encKey: signedRequest.encKey,
|
|
258
|
+
encKeyPk: signedRequest.encKeyPk,
|
|
259
|
+
encVerificationKey: signedRequest.encVerificationKey,
|
|
260
|
+
certificate,
|
|
261
|
+
timeout: __privateGet(this, _timeout),
|
|
262
|
+
apiKeyName: config?.apiKeyName,
|
|
263
|
+
apiKey: config?.apiKey,
|
|
264
|
+
signal: controller.signal
|
|
265
|
+
});
|
|
266
|
+
for (const { fullId, key } of allKeys) {
|
|
267
|
+
const keyElement = import_bls12381.G1Element.fromBytes(key);
|
|
268
|
+
if (!import_ibe.BonehFranklinBLS12381Services.verifyUserSecretKey(
|
|
269
|
+
keyElement,
|
|
270
|
+
fullId,
|
|
271
|
+
import_bls12381.G2Element.fromBytes(server.pk)
|
|
272
|
+
)) {
|
|
273
|
+
console.warn("Received invalid key from key server " + server.objectId);
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
__privateGet(this, _cachedKeys).set(`${fullId}:${server.objectId}`, keyElement);
|
|
277
|
+
}
|
|
278
|
+
if (fullIds.every((fullId) => __privateGet(this, _cachedKeys).has(`${fullId}:${server.objectId}`))) {
|
|
279
|
+
completedWeight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
|
|
280
|
+
if (completedWeight >= threshold) {
|
|
281
|
+
controller.abort();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (!controller.signal.aborted) {
|
|
286
|
+
errors.push(error);
|
|
287
|
+
}
|
|
288
|
+
} finally {
|
|
289
|
+
remainingKeyServersWeight -= __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
|
|
290
|
+
if (remainingKeyServersWeight < threshold - completedWeight) {
|
|
291
|
+
controller.abort(new import_error.TooManyFailedFetchKeyRequestsError());
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
await Promise.allSettled(keyFetches);
|
|
296
|
+
if (completedWeight < threshold) {
|
|
297
|
+
throw (0, import_error.toMajorityError)(errors);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Get derived keys from the given services.
|
|
302
|
+
*
|
|
303
|
+
* @param id - The id of the encrypted object.
|
|
304
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
305
|
+
* @param sessionKey - The session key to use.
|
|
306
|
+
* @param threshold - The threshold.
|
|
307
|
+
* @returns - Derived keys for the given services that are in the cache as a "service object ID" -> derived key map. If the call is succesful, exactly threshold keys will be returned.
|
|
308
|
+
*/
|
|
309
|
+
async getDerivedKeys({
|
|
310
|
+
kemType = import_encrypt.KemType.BonehFranklinBLS12381DemCCA,
|
|
311
|
+
id,
|
|
312
|
+
txBytes,
|
|
313
|
+
sessionKey,
|
|
314
|
+
threshold
|
|
315
|
+
}) {
|
|
316
|
+
switch (kemType) {
|
|
317
|
+
case import_encrypt.KemType.BonehFranklinBLS12381DemCCA:
|
|
318
|
+
const keyServers = await this.getKeyServers();
|
|
319
|
+
if (threshold > __privateGet(this, _totalWeight)) {
|
|
320
|
+
throw new import_error.InvalidThresholdError(
|
|
321
|
+
`Invalid threshold ${threshold} for ${__privateGet(this, _totalWeight)} servers`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
await this.fetchKeys({
|
|
325
|
+
ids: [id],
|
|
326
|
+
txBytes,
|
|
327
|
+
sessionKey,
|
|
328
|
+
threshold
|
|
329
|
+
});
|
|
330
|
+
const fullId = (0, import_utils.createFullId)(sessionKey.getPackageId(), id);
|
|
331
|
+
const derivedKeys = /* @__PURE__ */ new Map();
|
|
332
|
+
let weight = 0;
|
|
333
|
+
for (const objectId of keyServers.keys()) {
|
|
334
|
+
const cachedKey = __privateGet(this, _cachedKeys).get(`${fullId}:${objectId}`);
|
|
335
|
+
if (cachedKey) {
|
|
336
|
+
derivedKeys.set(objectId, new import_key_server.BonehFranklinBLS12381DerivedKey(cachedKey));
|
|
337
|
+
weight += __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
|
|
338
|
+
if (weight >= threshold) {
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return derivedKeys;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
_haneulClient = new WeakMap();
|
|
348
|
+
_configs = new WeakMap();
|
|
349
|
+
_keyServers = new WeakMap();
|
|
350
|
+
_verifyKeyServers = new WeakMap();
|
|
351
|
+
_cachedKeys = new WeakMap();
|
|
352
|
+
_cachedPublicKeys = new WeakMap();
|
|
353
|
+
_timeout = new WeakMap();
|
|
354
|
+
_totalWeight = new WeakMap();
|
|
355
|
+
_SealClient_instances = new WeakSet();
|
|
356
|
+
createEncryptionInput_fn = function(type, data, aad) {
|
|
357
|
+
switch (type) {
|
|
358
|
+
case import_encrypt.DemType.AesGcm256:
|
|
359
|
+
return new import_dem.AesGcm256(data, aad);
|
|
360
|
+
case import_encrypt.DemType.Hmac256Ctr:
|
|
361
|
+
return new import_dem.Hmac256Ctr(data, aad);
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
weight_fn = function(objectId) {
|
|
365
|
+
return __privateGet(this, _configs).get(objectId)?.weight ?? 0;
|
|
366
|
+
};
|
|
367
|
+
validateEncryptionServices_fn = function(services, threshold) {
|
|
368
|
+
if (services.some((objectId) => {
|
|
369
|
+
const countInClient = __privateMethod(this, _SealClient_instances, weight_fn).call(this, objectId);
|
|
370
|
+
return countInClient > 0 && countInClient !== (0, import_utils.count)(services, objectId);
|
|
371
|
+
})) {
|
|
372
|
+
throw new import_error.InconsistentKeyServersError(
|
|
373
|
+
`Client's key servers must be a subset of the encrypted object's key servers`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
if (threshold > __privateGet(this, _totalWeight)) {
|
|
377
|
+
throw new import_error.InvalidThresholdError(
|
|
378
|
+
`Invalid threshold ${threshold} for ${__privateGet(this, _totalWeight)} servers`
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
getWeightedKeyServers_fn = async function() {
|
|
383
|
+
const keyServers = await this.getKeyServers();
|
|
384
|
+
const keyServersWithMultiplicity = [];
|
|
385
|
+
for (const [objectId, config] of __privateGet(this, _configs)) {
|
|
386
|
+
const keyServer = keyServers.get(objectId);
|
|
387
|
+
for (let i = 0; i < config.weight; i++) {
|
|
388
|
+
keyServersWithMultiplicity.push(keyServer);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return keyServersWithMultiplicity;
|
|
392
|
+
};
|
|
393
|
+
loadKeyServers_fn = async function() {
|
|
394
|
+
const keyServers = await (0, import_key_server.retrieveKeyServers)({
|
|
395
|
+
objectIds: [...__privateGet(this, _configs)].map(([objectId]) => objectId),
|
|
396
|
+
client: __privateGet(this, _haneulClient)
|
|
397
|
+
});
|
|
398
|
+
if (keyServers.length === 0) {
|
|
399
|
+
throw new import_error.InvalidKeyServerError("No key servers found");
|
|
400
|
+
}
|
|
401
|
+
if (__privateGet(this, _verifyKeyServers)) {
|
|
402
|
+
await Promise.all(
|
|
403
|
+
keyServers.map(async (server) => {
|
|
404
|
+
const config = __privateGet(this, _configs).get(server.objectId);
|
|
405
|
+
if (!await (0, import_key_server.verifyKeyServer)(server, __privateGet(this, _timeout), config?.apiKeyName, config?.apiKey)) {
|
|
406
|
+
throw new import_error.InvalidKeyServerError(`Key server ${server.objectId} is not valid`);
|
|
407
|
+
}
|
|
408
|
+
})
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
return new Map(keyServers.map((server) => [server.objectId, server]));
|
|
412
|
+
};
|
|
413
|
+
let SealClient = _SealClient;
|
|
414
|
+
//# sourceMappingURL=client.js.map
|