@mysten/seal 0.4.1 → 0.4.3
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 +14 -0
- package/dist/cjs/bls12381.d.ts +5 -0
- package/dist/cjs/bls12381.js +38 -23
- package/dist/cjs/bls12381.js.map +2 -2
- package/dist/cjs/client.d.ts +18 -2
- package/dist/cjs/client.js +46 -1
- package/dist/cjs/client.js.map +2 -2
- package/dist/cjs/dem.js +1 -1
- package/dist/cjs/dem.js.map +2 -2
- package/dist/cjs/encrypt.js +12 -15
- package/dist/cjs/encrypt.js.map +2 -2
- package/dist/cjs/ibe.d.ts +3 -1
- package/dist/cjs/ibe.js +4 -4
- package/dist/cjs/ibe.js.map +2 -2
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/kdf.js +6 -16
- package/dist/cjs/kdf.js.map +2 -2
- package/dist/cjs/key-server.d.ts +14 -0
- package/dist/cjs/key-server.js +10 -0
- package/dist/cjs/key-server.js.map +2 -2
- package/dist/cjs/utils.d.ts +7 -0
- package/dist/cjs/utils.js +12 -8
- package/dist/cjs/utils.js.map +2 -2
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/bls12381.d.ts +5 -0
- package/dist/esm/bls12381.js +38 -23
- package/dist/esm/bls12381.js.map +2 -2
- package/dist/esm/client.d.ts +18 -2
- package/dist/esm/client.js +52 -2
- package/dist/esm/client.js.map +2 -2
- package/dist/esm/dem.js +1 -1
- package/dist/esm/dem.js.map +2 -2
- package/dist/esm/encrypt.js +12 -15
- package/dist/esm/encrypt.js.map +2 -2
- package/dist/esm/ibe.d.ts +3 -1
- package/dist/esm/ibe.js +4 -4
- package/dist/esm/ibe.js.map +2 -2
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/kdf.js +6 -16
- package/dist/esm/kdf.js.map +2 -2
- package/dist/esm/key-server.d.ts +14 -0
- package/dist/esm/key-server.js +11 -1
- package/dist/esm/key-server.js.map +2 -2
- package/dist/esm/utils.d.ts +7 -0
- package/dist/esm/utils.js +12 -8
- package/dist/esm/utils.js.map +2 -2
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
package/dist/cjs/ibe.js.map
CHANGED
|
@@ -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 { 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 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\tmsgAndIndices: { msg: Uint8Array; index: number }[],\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\tmsgAndIndices: { msg: Uint8Array; index: number }[],\n\t\trandomnessKey: Uint8Array,\n\t): typeof IBEEncryptions.$inferType {\n\t\tif (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndIndices.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 = msgAndIndices.map((
|
|
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;AAKnF,MAAe,WAAW;AAAA,EAGhC,YAAY,WAAqB;AAChC,SAAK,YAAY;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAe;AACd,WAAO,KAAK,UAAU;AAAA,EACvB;AAcD;
|
|
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 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\tmsgAndIndices: { msg: Uint8Array; index: number }[],\n\t\trandomnessKey: Uint8Array,\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\tmsgAndIndices: { msg: Uint8Array; index: number }[],\n\t\trandomnessKey: Uint8Array,\n\t): typeof IBEEncryptions.$inferType {\n\t\tif (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndIndices.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 = msgAndIndices.map(({ msg, index }, i) =>\n\t\t\txor(msg, kdf(keys[i], nonce, id, this.objectIds[i], index)),\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 = G1Element.hashToCurve(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 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\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\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;AAKnF,MAAe,WAAW;AAAA,EAGhC,YAAY,WAAqB;AAChC,SAAK,YAAY;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAe;AACd,WAAO,KAAK,UAAU;AAAA,EACvB;AAcD;AAQO,MAAM,sCAAsC,WAAW;AAAA,EAG7D,YAAY,UAAuB;AAClC,UAAM,SAAS,IAAI,CAAC,YAAY,QAAQ,QAAQ,CAAC;AACjD,SAAK,aAAa,SAAS,IAAI,CAAC,YAAY,0BAAU,UAAU,QAAQ,EAAE,CAAC;AAAA,EAC5E;AAAA,EAEA,eACC,IACA,eACA,eACmC;AACnC,QAAI,KAAK,WAAW,WAAW,KAAK,KAAK,WAAW,WAAW,cAAc,QAAQ;AACpF,YAAM,IAAI,MAAM,qBAAqB;AAAA,IACtC;AACA,UAAM,CAAC,GAAG,OAAO,IAAI,IAAI,aAAa,KAAK,YAAY,EAAE;AACzD,UAAM,kBAAkB,cAAc;AAAA,MAAI,CAAC,EAAE,KAAK,MAAM,GAAG,UAC1D,kBAAI,SAAK,gBAAI,KAAK,CAAC,GAAG,OAAO,I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owlistedKeyServers } from './key-server.js';
|
|
2
2
|
export { EncryptedObject } from './bcs.js';
|
|
3
3
|
export { SealClient, type SealClientOptions } from './client.js';
|
|
4
|
-
export { SessionKey } from './session-key.js';
|
|
4
|
+
export { SessionKey, type SessionKeyType } from './session-key.js';
|
|
5
5
|
export * from './error.js';
|
|
6
6
|
export type { SealCompatibleClient } from './types.js';
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -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 { getAllowlistedKeyServers } from './key-server.js';\nexport { EncryptedObject } from './bcs.js';\nexport { SealClient, type SealClientOptions } from './client.js';\nexport { SessionKey } from './session-key.js';\nexport * from './error.js';\nexport type { SealCompatibleClient } from './types.js';\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAAyC;AACzC,iBAAgC;AAChC,oBAAmD;AACnD,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { getAllowlistedKeyServers } from './key-server.js';\nexport { EncryptedObject } from './bcs.js';\nexport { SealClient, type SealClientOptions } from './client.js';\nexport { SessionKey, type SessionKeyType } from './session-key.js';\nexport * from './error.js';\nexport type { SealCompatibleClient } from './types.js';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAAyC;AACzC,iBAAgC;AAChC,oBAAmD;AACnD,yBAAgD;AAChD,0BAAc,uBAPd;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/kdf.js
CHANGED
|
@@ -28,24 +28,14 @@ var import_hkdf = require("@noble/hashes/hkdf");
|
|
|
28
28
|
var import_hmac = require("@noble/hashes/hmac");
|
|
29
29
|
var import_sha3 = require("@noble/hashes/sha3");
|
|
30
30
|
var import_bls12381 = require("./bls12381.js");
|
|
31
|
+
var import_utils = require("./utils.js");
|
|
31
32
|
function kdf(element, nonce, id, objectId, index) {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const permutedBytes = new Uint8Array(GT_ELEMENT_BYTE_LENGTH);
|
|
37
|
-
PERMUTATION.forEach((pi, i) => {
|
|
38
|
-
permutedBytes.set(
|
|
39
|
-
bytes.slice(i * COEFFICIENT_SIZE, (i + 1) * COEFFICIENT_SIZE),
|
|
40
|
-
pi * COEFFICIENT_SIZE
|
|
41
|
-
);
|
|
42
|
-
});
|
|
43
|
-
const inputBytes = new Uint8Array([
|
|
44
|
-
...permutedBytes,
|
|
45
|
-
...nonce.toBytes(),
|
|
46
|
-
...import_bls12381.G1Element.hashToCurve(id).toBytes()
|
|
33
|
+
const inputBytes = (0, import_utils.flatten)([
|
|
34
|
+
element.toBytes(),
|
|
35
|
+
nonce.toBytes(),
|
|
36
|
+
import_bls12381.G1Element.hashToCurve(id).toBytes()
|
|
47
37
|
]);
|
|
48
|
-
const info =
|
|
38
|
+
const info = (0, import_utils.flatten)([(0, import_bcs.fromHex)(objectId), new Uint8Array([index])]);
|
|
49
39
|
return (0, import_hkdf.hkdf)(import_sha3.sha3_256, inputBytes, "", info, 32);
|
|
50
40
|
}
|
|
51
41
|
var KeyPurpose = /* @__PURE__ */ ((KeyPurpose2) => {
|
package/dist/cjs/kdf.js.map
CHANGED
|
@@ -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 { fromHex } from '@mysten/bcs';\nimport { hkdf } from '@noble/hashes/hkdf';\nimport { hmac } from '@noble/hashes/hmac';\nimport { sha3_256 } from '@noble/hashes/sha3';\n\nimport { G1Element } from './bls12381.js';\nimport type { G2Element, 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(\n\telement: GTElement,\n\tnonce: G2Element,\n\tid: Uint8Array,\n\tobjectId: string,\n\tindex: number,\n): Uint8Array {\n\
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAwB;AACxB,kBAAqB;AACrB,kBAAqB;AACrB,kBAAyB;AAEzB,sBAA0B;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@mysten/bcs';\nimport { hkdf } from '@noble/hashes/hkdf';\nimport { hmac } from '@noble/hashes/hmac';\nimport { sha3_256 } from '@noble/hashes/sha3';\n\nimport { G1Element } from './bls12381.js';\nimport type { G2Element, GTElement } from './bls12381.js';\nimport { flatten } from './utils.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(\n\telement: GTElement,\n\tnonce: G2Element,\n\tid: Uint8Array,\n\tobjectId: string,\n\tindex: number,\n): Uint8Array {\n\tconst inputBytes = flatten([\n\t\telement.toBytes(),\n\t\tnonce.toBytes(),\n\t\tG1Element.hashToCurve(id).toBytes(),\n\t]);\n\n\tconst info = flatten([fromHex(objectId), new Uint8Array([index])]);\n\n\treturn hkdf(sha3_256, inputBytes, '', 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,iBAAwB;AACxB,kBAAqB;AACrB,kBAAqB;AACrB,kBAAyB;AAEzB,sBAA0B;AAE1B,mBAAwB;AASjB,SAAS,IACf,SACA,OACA,IACA,UACA,OACa;AACb,QAAM,iBAAa,sBAAQ;AAAA,IAC1B,QAAQ,QAAQ;AAAA,IAChB,MAAM,QAAQ;AAAA,IACd,0BAAU,YAAY,EAAE,EAAE,QAAQ;AAAA,EACnC,CAAC;AAED,QAAM,WAAO,sBAAQ,KAAC,oBAAQ,QAAQ,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC;AAEjE,aAAO,kBAAK,sBAAU,YAAY,IAAI,MAAM,EAAE;AAC/C;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
6
|
"names": ["KeyPurpose"]
|
|
7
7
|
}
|
package/dist/cjs/key-server.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SealCompatibleClient } from './types.js';
|
|
2
2
|
import { Version } from './utils.js';
|
|
3
|
+
import type { G1Element } from './bls12381.js';
|
|
3
4
|
export type KeyServer = {
|
|
4
5
|
objectId: string;
|
|
5
6
|
name: string;
|
|
@@ -44,3 +45,16 @@ export declare function verifyKeyServer(server: KeyServer, timeout: number): Pro
|
|
|
44
45
|
* @param response - The response from the key server.
|
|
45
46
|
*/
|
|
46
47
|
export declare function verifyKeyServerVersion(response: Response): void;
|
|
48
|
+
export interface DerivedKey {
|
|
49
|
+
toString(): string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A user secret key for the Boneh-Franklin BLS12381 scheme.
|
|
53
|
+
* This is a wrapper around the G1Element type.
|
|
54
|
+
*/
|
|
55
|
+
export declare class BonehFranklinBLS12381DerivedKey implements DerivedKey {
|
|
56
|
+
key: G1Element;
|
|
57
|
+
representation: string;
|
|
58
|
+
constructor(key: G1Element);
|
|
59
|
+
toString(): string;
|
|
60
|
+
}
|
package/dist/cjs/key-server.js
CHANGED
|
@@ -18,6 +18,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var key_server_exports = {};
|
|
20
20
|
__export(key_server_exports, {
|
|
21
|
+
BonehFranklinBLS12381DerivedKey: () => BonehFranklinBLS12381DerivedKey,
|
|
21
22
|
KeyServerType: () => KeyServerType,
|
|
22
23
|
SERVER_VERSION_REQUIREMENT: () => SERVER_VERSION_REQUIREMENT,
|
|
23
24
|
getAllowlistedKeyServers: () => getAllowlistedKeyServers,
|
|
@@ -108,4 +109,13 @@ function verifyKeyServerVersion(response) {
|
|
|
108
109
|
);
|
|
109
110
|
}
|
|
110
111
|
}
|
|
112
|
+
class BonehFranklinBLS12381DerivedKey {
|
|
113
|
+
constructor(key) {
|
|
114
|
+
this.key = key;
|
|
115
|
+
this.representation = (0, import_bcs.toHex)(key.toBytes());
|
|
116
|
+
}
|
|
117
|
+
toString() {
|
|
118
|
+
return this.representation;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
111
121
|
//# sourceMappingURL=key-server.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/key-server.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport { fromBase64, fromHex } from '@mysten/bcs';\nimport { bls12_381 } from '@noble/curves/bls12-381';\n\nimport { KeyServerMove } from './bcs.js';\nimport {\n\tInvalidGetObjectError,\n\tInvalidKeyServerVersionError,\n\tSealAPIError,\n\tUnsupportedFeatureError,\n\tUnsupportedNetworkError,\n} from './error.js';\nimport { DST_POP } from './ibe.js';\nimport { PACKAGE_VERSION } from './version.js';\nimport type { SealCompatibleClient } from './types.js';\nimport { Version } from './utils.js';\n\nexport type KeyServer = {\n\tobjectId: string;\n\tname: string;\n\turl: string;\n\tkeyType: KeyServerType;\n\tpk: Uint8Array;\n};\n\nexport enum KeyServerType {\n\tBonehFranklinBLS12381 = 0,\n}\n\nexport const SERVER_VERSION_REQUIREMENT = new Version('0.2.0');\n\n/**\n * Returns a static list of Seal key server object ids that the dapp can choose to use.\n * @param network - The network to use.\n * @returns The object id's of the key servers.\n */\nexport function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): string[] {\n\tif (network === 'testnet') {\n\t\treturn [\n\t\t\t'0xb35a7228d8cf224ad1e828c0217c95a5153bafc2906d6f9c178197dce26fbcf8',\n\t\t\t'0x2d6cde8a9d9a65bde3b0a346566945a63b4bfb70e9a06c41bdb70807e2502b06',\n\t\t];\n\t} else {\n\t\tthrow new UnsupportedNetworkError(`Unsupported network ${network}`);\n\t}\n}\n\n/**\n * Given a list of key server object IDs, returns a list of SealKeyServer\n * from onchain state containing name, objectId, URL and pk.\n *\n * @param objectIds - The key server object IDs.\n * @param client - The SuiClient to use.\n * @returns - An array of SealKeyServer.\n */\nexport async function retrieveKeyServers({\n\tobjectIds,\n\tclient,\n}: {\n\tobjectIds: string[];\n\tclient: SealCompatibleClient;\n}): Promise<KeyServer[]> {\n\t// todo: do not fetch the same object ID if this is fetched before.\n\treturn await Promise.all(\n\t\tobjectIds.map(async (objectId) => {\n\t\t\tlet res;\n\t\t\ttry {\n\t\t\t\tres = await client.core.getObject({\n\t\t\t\t\tobjectId,\n\t\t\t\t});\n\t\t\t} catch (e) {\n\t\t\t\tthrow new InvalidGetObjectError(`KeyServer ${objectId} not found; ${(e as Error).message}`);\n\t\t\t}\n\n\t\t\tconst ks = KeyServerMove.parse(res.object.content);\n\t\t\tif (ks.keyType !== 0) {\n\t\t\t\tthrow new UnsupportedFeatureError(`Unsupported key type ${ks.keyType}`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tobjectId,\n\t\t\t\tname: ks.name,\n\t\t\t\turl: ks.url,\n\t\t\t\tkeyType: KeyServerType.BonehFranklinBLS12381,\n\t\t\t\tpk: new Uint8Array(ks.pk),\n\t\t\t};\n\t\t}),\n\t);\n}\n\n/**\n * Given a KeyServer, fetch the proof of possession (PoP) from the URL and verify it\n * against the pubkey. This should be used only rarely when the dapp uses a dynamic\n * set of key servers.\n *\n * @param server - The KeyServer to verify.\n * @returns - True if the key server is valid, false otherwise.\n */\nexport async function verifyKeyServer(server: KeyServer, timeout: number): Promise<boolean> {\n\tconst requestId = crypto.randomUUID();\n\tconst response = await fetch(server.url! + '/v1/service', {\n\t\tmethod: 'GET',\n\t\theaders: {\n\t\t\t'Content-Type': 'application/json',\n\t\t\t'Request-Id': requestId,\n\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t},\n\t\tsignal: AbortSignal.timeout(timeout),\n\t});\n\n\tawait SealAPIError.assertResponse(response, requestId);\n\tverifyKeyServerVersion(response);\n\tconst serviceResponse = await response.json();\n\n\tif (serviceResponse.service_id !== server.objectId) {\n\t\treturn false;\n\t}\n\tconst fullMsg = new Uint8Array([...DST_POP, ...server.pk, ...fromHex(server.objectId)]);\n\treturn bls12_381.verifyShortSignature(fromBase64(serviceResponse.pop), fullMsg, server.pk);\n}\n\n/**\n * Verify the key server version. Throws an `InvalidKeyServerError` if the version is not supported.\n *\n * @param response - The response from the key server.\n */\nexport function verifyKeyServerVersion(response: Response) {\n\tconst keyServerVersion = response.headers.get('X-KeyServer-Version');\n\tif (keyServerVersion == null) {\n\t\tthrow new InvalidKeyServerVersionError('Key server version not found');\n\t}\n\tif (new Version(keyServerVersion).older_than(SERVER_VERSION_REQUIREMENT)) {\n\t\tthrow new InvalidKeyServerVersionError(\n\t\t\t`Key server version ${keyServerVersion} is not supported`,\n\t\t);\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport { fromBase64, fromHex, toHex } from '@mysten/bcs';\nimport { bls12_381 } from '@noble/curves/bls12-381';\n\nimport { KeyServerMove } from './bcs.js';\nimport {\n\tInvalidGetObjectError,\n\tInvalidKeyServerVersionError,\n\tSealAPIError,\n\tUnsupportedFeatureError,\n\tUnsupportedNetworkError,\n} from './error.js';\nimport { DST_POP } from './ibe.js';\nimport { PACKAGE_VERSION } from './version.js';\nimport type { SealCompatibleClient } from './types.js';\nimport { Version } from './utils.js';\nimport type { G1Element } from './bls12381.js';\n\nexport type KeyServer = {\n\tobjectId: string;\n\tname: string;\n\turl: string;\n\tkeyType: KeyServerType;\n\tpk: Uint8Array;\n};\n\nexport enum KeyServerType {\n\tBonehFranklinBLS12381 = 0,\n}\n\nexport const SERVER_VERSION_REQUIREMENT = new Version('0.2.0');\n\n/**\n * Returns a static list of Seal key server object ids that the dapp can choose to use.\n * @param network - The network to use.\n * @returns The object id's of the key servers.\n */\nexport function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): string[] {\n\tif (network === 'testnet') {\n\t\treturn [\n\t\t\t'0xb35a7228d8cf224ad1e828c0217c95a5153bafc2906d6f9c178197dce26fbcf8',\n\t\t\t'0x2d6cde8a9d9a65bde3b0a346566945a63b4bfb70e9a06c41bdb70807e2502b06',\n\t\t];\n\t} else {\n\t\tthrow new UnsupportedNetworkError(`Unsupported network ${network}`);\n\t}\n}\n\n/**\n * Given a list of key server object IDs, returns a list of SealKeyServer\n * from onchain state containing name, objectId, URL and pk.\n *\n * @param objectIds - The key server object IDs.\n * @param client - The SuiClient to use.\n * @returns - An array of SealKeyServer.\n */\nexport async function retrieveKeyServers({\n\tobjectIds,\n\tclient,\n}: {\n\tobjectIds: string[];\n\tclient: SealCompatibleClient;\n}): Promise<KeyServer[]> {\n\t// todo: do not fetch the same object ID if this is fetched before.\n\treturn await Promise.all(\n\t\tobjectIds.map(async (objectId) => {\n\t\t\tlet res;\n\t\t\ttry {\n\t\t\t\tres = await client.core.getObject({\n\t\t\t\t\tobjectId,\n\t\t\t\t});\n\t\t\t} catch (e) {\n\t\t\t\tthrow new InvalidGetObjectError(`KeyServer ${objectId} not found; ${(e as Error).message}`);\n\t\t\t}\n\n\t\t\tconst ks = KeyServerMove.parse(res.object.content);\n\t\t\tif (ks.keyType !== 0) {\n\t\t\t\tthrow new UnsupportedFeatureError(`Unsupported key type ${ks.keyType}`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tobjectId,\n\t\t\t\tname: ks.name,\n\t\t\t\turl: ks.url,\n\t\t\t\tkeyType: KeyServerType.BonehFranklinBLS12381,\n\t\t\t\tpk: new Uint8Array(ks.pk),\n\t\t\t};\n\t\t}),\n\t);\n}\n\n/**\n * Given a KeyServer, fetch the proof of possession (PoP) from the URL and verify it\n * against the pubkey. This should be used only rarely when the dapp uses a dynamic\n * set of key servers.\n *\n * @param server - The KeyServer to verify.\n * @returns - True if the key server is valid, false otherwise.\n */\nexport async function verifyKeyServer(server: KeyServer, timeout: number): Promise<boolean> {\n\tconst requestId = crypto.randomUUID();\n\tconst response = await fetch(server.url! + '/v1/service', {\n\t\tmethod: 'GET',\n\t\theaders: {\n\t\t\t'Content-Type': 'application/json',\n\t\t\t'Request-Id': requestId,\n\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t},\n\t\tsignal: AbortSignal.timeout(timeout),\n\t});\n\n\tawait SealAPIError.assertResponse(response, requestId);\n\tverifyKeyServerVersion(response);\n\tconst serviceResponse = await response.json();\n\n\tif (serviceResponse.service_id !== server.objectId) {\n\t\treturn false;\n\t}\n\tconst fullMsg = new Uint8Array([...DST_POP, ...server.pk, ...fromHex(server.objectId)]);\n\treturn bls12_381.verifyShortSignature(fromBase64(serviceResponse.pop), fullMsg, server.pk);\n}\n\n/**\n * Verify the key server version. Throws an `InvalidKeyServerError` if the version is not supported.\n *\n * @param response - The response from the key server.\n */\nexport function verifyKeyServerVersion(response: Response) {\n\tconst keyServerVersion = response.headers.get('X-KeyServer-Version');\n\tif (keyServerVersion == null) {\n\t\tthrow new InvalidKeyServerVersionError('Key server version not found');\n\t}\n\tif (new Version(keyServerVersion).older_than(SERVER_VERSION_REQUIREMENT)) {\n\t\tthrow new InvalidKeyServerVersionError(\n\t\t\t`Key server version ${keyServerVersion} is not supported`,\n\t\t);\n\t}\n}\n\nexport interface DerivedKey {\n\ttoString(): string;\n}\n\n/**\n * A user secret key for the Boneh-Franklin BLS12381 scheme.\n * This is a wrapper around the G1Element type.\n */\nexport class BonehFranklinBLS12381DerivedKey implements DerivedKey {\n\trepresentation: string;\n\n\tconstructor(public key: G1Element) {\n\t\tthis.representation = toHex(key.toBytes());\n\t}\n\n\ttoString(): string {\n\t\treturn this.representation;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,iBAA2C;AAC3C,uBAA0B;AAE1B,IAAAA,cAA8B;AAC9B,mBAMO;AACP,iBAAwB;AACxB,qBAAgC;AAEhC,mBAAwB;AAWjB,IAAK,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Aa,eAAe,UAAU,SAAS;AACrD,yBAAuB,QAAQ;AAC/B,QAAM,kBAAkB,MAAM,SAAS,KAAK;AAE5C,MAAI,gBAAgB,eAAe,OAAO,UAAU;AACnD,WAAO;AAAA,EACR;AACA,QAAM,UAAU,IAAI,WAAW,CAAC,GAAG,oBAAS,GAAG,OAAO,IAAI,OAAG,oBAAQ,OAAO,QAAQ,CAAC,CAAC;AACtF,SAAO,2BAAU,yBAAqB,uBAAW,gBAAgB,GAAG,GAAG,SAAS,OAAO,EAAE;AAC1F;AAOO,SAAS,uBAAuB,UAAoB;AAC1D,QAAM,mBAAmB,SAAS,QAAQ,IAAI,qBAAqB;AACnE,MAAI,oBAAoB,MAAM;AAC7B,UAAM,IAAI,0CAA6B,8BAA8B;AAAA,EACtE;AACA,MAAI,IAAI,qBAAQ,gBAAgB,EAAE,WAAW,0BAA0B,GAAG;AACzE,UAAM,IAAI;AAAA,MACT,sBAAsB,gBAAgB;AAAA,IACvC;AAAA,EACD;AACD;AAUO,MAAM,gCAAsD;AAAA,EAGlE,YAAmB,KAAgB;AAAhB;AAClB,SAAK,qBAAiB,kBAAM,IAAI,QAAQ,CAAC;AAAA,EAC1C;AAAA,EAEA,WAAmB;AAClB,WAAO,KAAK;AAAA,EACb;AACD;",
|
|
6
6
|
"names": ["import_bcs", "KeyServerType"]
|
|
7
7
|
}
|
package/dist/cjs/utils.d.ts
CHANGED
|
@@ -8,6 +8,13 @@ export declare function xorUnchecked(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
|
8
8
|
* @returns The full ID.
|
|
9
9
|
*/
|
|
10
10
|
export declare function createFullId(dst: Uint8Array, packageId: string, innerId: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Flatten an array of Uint8Arrays into a single Uint8Array.
|
|
13
|
+
*
|
|
14
|
+
* @param arrays - An array of Uint8Arrays to flatten.
|
|
15
|
+
* @returns A single Uint8Array containing all the elements of the input arrays in the given order.
|
|
16
|
+
*/
|
|
17
|
+
export declare function flatten(arrays: Uint8Array[]): Uint8Array;
|
|
11
18
|
/**
|
|
12
19
|
* A simple class to represent a version number of the form x.y.z.
|
|
13
20
|
*/
|
package/dist/cjs/utils.js
CHANGED
|
@@ -20,6 +20,7 @@ var utils_exports = {};
|
|
|
20
20
|
__export(utils_exports, {
|
|
21
21
|
Version: () => Version,
|
|
22
22
|
createFullId: () => createFullId,
|
|
23
|
+
flatten: () => flatten,
|
|
23
24
|
xor: () => xor,
|
|
24
25
|
xorUnchecked: () => xorUnchecked
|
|
25
26
|
});
|
|
@@ -40,19 +41,22 @@ function createFullId(dst, packageId, innerId) {
|
|
|
40
41
|
if (!(0, import_utils.isValidSuiObjectId)(packageId)) {
|
|
41
42
|
throw new import_error.UserError(`Invalid package ID ${packageId}`);
|
|
42
43
|
}
|
|
43
|
-
const
|
|
44
|
-
const innerIdBytes = (0, import_bcs.fromHex)(innerId);
|
|
45
|
-
const fullId = new Uint8Array(1 + dst.length + packageIdBytes.length + innerIdBytes.length);
|
|
46
|
-
fullId.set([dst.length], 0);
|
|
47
|
-
fullId.set(dst, 1);
|
|
48
|
-
fullId.set(packageIdBytes, 1 + dst.length);
|
|
49
|
-
fullId.set(innerIdBytes, 1 + dst.length + packageIdBytes.length);
|
|
44
|
+
const fullId = flatten([new Uint8Array([dst.length]), dst, (0, import_bcs.fromHex)(packageId), (0, import_bcs.fromHex)(innerId)]);
|
|
50
45
|
return (0, import_bcs.toHex)(fullId);
|
|
51
46
|
}
|
|
47
|
+
function flatten(arrays) {
|
|
48
|
+
const length = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
49
|
+
const result = new Uint8Array(length);
|
|
50
|
+
arrays.reduce((offset, array) => {
|
|
51
|
+
result.set(array, offset);
|
|
52
|
+
return offset + array.length;
|
|
53
|
+
}, 0);
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
52
56
|
class Version {
|
|
53
57
|
constructor(version) {
|
|
54
58
|
const parts = version.split(".").map(Number);
|
|
55
|
-
if (parts.length !== 3 || parts.some((part) => isNaN(part) || part < 0)) {
|
|
59
|
+
if (parts.length !== 3 || parts.some((part) => isNaN(part) || !Number.isInteger(part) || part < 0)) {
|
|
56
60
|
throw new import_error.UserError(`Invalid version format: ${version}`);
|
|
57
61
|
}
|
|
58
62
|
this.major = parts[0];
|
package/dist/cjs/utils.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex, toHex } from '@mysten/bcs';\nimport { isValidSuiObjectId } from '@mysten/sui/utils';\n\nimport { UserError } from './error.js';\n\nexport function xor(a: Uint8Array, b: Uint8Array): Uint8Array {\n\tif (a.length !== b.length) {\n\t\tthrow new Error('Invalid input');\n\t}\n\treturn xorUnchecked(a, b);\n}\n\nexport function xorUnchecked(a: Uint8Array, b: Uint8Array): Uint8Array {\n\treturn a.map((ai, i) => ai ^ b[i]);\n}\n\n/**\n * Create a full ID concatenating DST || package ID || inner ID.\n * @param dst - The domain separation tag.\n * @param packageId - The package ID.\n * @param innerId - The inner ID.\n * @returns The full ID.\n */\nexport function createFullId(dst: Uint8Array, packageId: string, innerId: string): string {\n\tif (!isValidSuiObjectId(packageId)) {\n\t\tthrow new UserError(`Invalid package ID ${packageId}`);\n\t}\n\tconst
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA+B;AAC/B,mBAAmC;AAEnC,mBAA0B;AAEnB,SAAS,IAAI,GAAe,GAA2B;AAC7D,MAAI,EAAE,WAAW,EAAE,QAAQ;AAC1B,UAAM,IAAI,MAAM,eAAe;AAAA,EAChC;AACA,SAAO,aAAa,GAAG,CAAC;AACzB;AAEO,SAAS,aAAa,GAAe,GAA2B;AACtE,SAAO,EAAE,IAAI,CAAC,IAAI,MAAM,KAAK,EAAE,CAAC,CAAC;AAClC;AASO,SAAS,aAAa,KAAiB,WAAmB,SAAyB;AACzF,MAAI,KAAC,iCAAmB,SAAS,GAAG;AACnC,UAAM,IAAI,uBAAU,sBAAsB,SAAS,EAAE;AAAA,EACtD;AACA,QAAM,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex, toHex } from '@mysten/bcs';\nimport { isValidSuiObjectId } from '@mysten/sui/utils';\n\nimport { UserError } from './error.js';\n\nexport function xor(a: Uint8Array, b: Uint8Array): Uint8Array {\n\tif (a.length !== b.length) {\n\t\tthrow new Error('Invalid input');\n\t}\n\treturn xorUnchecked(a, b);\n}\n\nexport function xorUnchecked(a: Uint8Array, b: Uint8Array): Uint8Array {\n\treturn a.map((ai, i) => ai ^ b[i]);\n}\n\n/**\n * Create a full ID concatenating DST || package ID || inner ID.\n * @param dst - The domain separation tag.\n * @param packageId - The package ID.\n * @param innerId - The inner ID.\n * @returns The full ID.\n */\nexport function createFullId(dst: Uint8Array, packageId: string, innerId: string): string {\n\tif (!isValidSuiObjectId(packageId)) {\n\t\tthrow new UserError(`Invalid package ID ${packageId}`);\n\t}\n\tconst fullId = flatten([new Uint8Array([dst.length]), dst, fromHex(packageId), fromHex(innerId)]);\n\treturn toHex(fullId);\n}\n\n/**\n * Flatten an array of Uint8Arrays into a single Uint8Array.\n *\n * @param arrays - An array of Uint8Arrays to flatten.\n * @returns A single Uint8Array containing all the elements of the input arrays in the given order.\n */\nexport function flatten(arrays: Uint8Array[]): Uint8Array {\n\tconst length = arrays.reduce((sum, arr) => sum + arr.length, 0);\n\tconst result = new Uint8Array(length);\n\tarrays.reduce((offset, array) => {\n\t\tresult.set(array, offset);\n\t\treturn offset + array.length;\n\t}, 0);\n\treturn result;\n}\n\n/**\n * A simple class to represent a version number of the form x.y.z.\n */\nexport class Version {\n\tmajor: number;\n\tminor: number;\n\tpatch: number;\n\n\tconstructor(version: string) {\n\t\t// Very basic version parsing. Assumes version is in the format x.y.z where x, y, and z are non-negative integers.\n\t\tconst parts = version.split('.').map(Number);\n\t\tif (\n\t\t\tparts.length !== 3 ||\n\t\t\tparts.some((part) => isNaN(part) || !Number.isInteger(part) || part < 0)\n\t\t) {\n\t\t\tthrow new UserError(`Invalid version format: ${version}`);\n\t\t}\n\t\tthis.major = parts[0];\n\t\tthis.minor = parts[1];\n\t\tthis.patch = parts[2];\n\t}\n\n\t// Compare this version with another version. True if this version is older than the other version.\n\tolder_than(other: Version): boolean {\n\t\tif (this.major !== other.major) {\n\t\t\treturn this.major < other.major;\n\t\t} else if (this.minor !== other.minor) {\n\t\t\treturn this.minor < other.minor;\n\t\t}\n\t\treturn this.patch < other.patch;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA+B;AAC/B,mBAAmC;AAEnC,mBAA0B;AAEnB,SAAS,IAAI,GAAe,GAA2B;AAC7D,MAAI,EAAE,WAAW,EAAE,QAAQ;AAC1B,UAAM,IAAI,MAAM,eAAe;AAAA,EAChC;AACA,SAAO,aAAa,GAAG,CAAC;AACzB;AAEO,SAAS,aAAa,GAAe,GAA2B;AACtE,SAAO,EAAE,IAAI,CAAC,IAAI,MAAM,KAAK,EAAE,CAAC,CAAC;AAClC;AASO,SAAS,aAAa,KAAiB,WAAmB,SAAyB;AACzF,MAAI,KAAC,iCAAmB,SAAS,GAAG;AACnC,UAAM,IAAI,uBAAU,sBAAsB,SAAS,EAAE;AAAA,EACtD;AACA,QAAM,SAAS,QAAQ,CAAC,IAAI,WAAW,CAAC,IAAI,MAAM,CAAC,GAAG,SAAK,oBAAQ,SAAS,OAAG,oBAAQ,OAAO,CAAC,CAAC;AAChG,aAAO,kBAAM,MAAM;AACpB;AAQO,SAAS,QAAQ,QAAkC;AACzD,QAAM,SAAS,OAAO,OAAO,CAAC,KAAK,QAAQ,MAAM,IAAI,QAAQ,CAAC;AAC9D,QAAM,SAAS,IAAI,WAAW,MAAM;AACpC,SAAO,OAAO,CAAC,QAAQ,UAAU;AAChC,WAAO,IAAI,OAAO,MAAM;AACxB,WAAO,SAAS,MAAM;AAAA,EACvB,GAAG,CAAC;AACJ,SAAO;AACR;AAKO,MAAM,QAAQ;AAAA,EAKpB,YAAY,SAAiB;AAE5B,UAAM,QAAQ,QAAQ,MAAM,GAAG,EAAE,IAAI,MAAM;AAC3C,QACC,MAAM,WAAW,KACjB,MAAM,KAAK,CAAC,SAAS,MAAM,IAAI,KAAK,CAAC,OAAO,UAAU,IAAI,KAAK,OAAO,CAAC,GACtE;AACD,YAAM,IAAI,uBAAU,2BAA2B,OAAO,EAAE;AAAA,IACzD;AACA,SAAK,QAAQ,MAAM,CAAC;AACpB,SAAK,QAAQ,MAAM,CAAC;AACpB,SAAK,QAAQ,MAAM,CAAC;AAAA,EACrB;AAAA;AAAA,EAGA,WAAW,OAAyB;AACnC,QAAI,KAAK,UAAU,MAAM,OAAO;AAC/B,aAAO,KAAK,QAAQ,MAAM;AAAA,IAC3B,WAAW,KAAK,UAAU,MAAM,OAAO;AACtC,aAAO,KAAK,QAAQ,MAAM;AAAA,IAC3B;AACA,WAAO,KAAK,QAAQ,MAAM;AAAA,EAC3B;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const PACKAGE_VERSION = "0.4.
|
|
1
|
+
export declare const PACKAGE_VERSION = "0.4.3";
|
package/dist/cjs/version.js
CHANGED
package/dist/cjs/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/version.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '0.4.
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '0.4.3';\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,MAAM,kBAAkB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/bls12381.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Fp2, Fp12 } from '@noble/curves/abstract/tower';
|
|
|
2
2
|
import type { ProjPointType } from '@noble/curves/abstract/weierstrass';
|
|
3
3
|
export declare class G1Element {
|
|
4
4
|
point: ProjPointType<bigint>;
|
|
5
|
+
static readonly SIZE = 48;
|
|
5
6
|
constructor(point: ProjPointType<bigint>);
|
|
6
7
|
static generator(): G1Element;
|
|
7
8
|
static fromBytes(bytes: Uint8Array): G1Element;
|
|
@@ -14,6 +15,7 @@ export declare class G1Element {
|
|
|
14
15
|
}
|
|
15
16
|
export declare class G2Element {
|
|
16
17
|
point: ProjPointType<Fp2>;
|
|
18
|
+
static readonly SIZE = 96;
|
|
17
19
|
constructor(point: ProjPointType<Fp2>);
|
|
18
20
|
static generator(): G2Element;
|
|
19
21
|
static fromBytes(bytes: Uint8Array): G2Element;
|
|
@@ -24,11 +26,14 @@ export declare class G2Element {
|
|
|
24
26
|
}
|
|
25
27
|
export declare class GTElement {
|
|
26
28
|
element: Fp12;
|
|
29
|
+
static readonly SIZE = 576;
|
|
27
30
|
constructor(element: Fp12);
|
|
28
31
|
toBytes(): Uint8Array;
|
|
32
|
+
equals(other: GTElement): boolean;
|
|
29
33
|
}
|
|
30
34
|
export declare class Scalar {
|
|
31
35
|
scalar: bigint;
|
|
36
|
+
static readonly SIZE = 32;
|
|
32
37
|
constructor(scalar: bigint);
|
|
33
38
|
static random(): Scalar;
|
|
34
39
|
toBytes(): Uint8Array;
|
package/dist/esm/bls12381.js
CHANGED
|
@@ -1,86 +1,101 @@
|
|
|
1
1
|
import { toHex } from "@mysten/bcs";
|
|
2
2
|
import { bls12_381 } from "@noble/curves/bls12-381";
|
|
3
|
-
|
|
3
|
+
import { flatten } from "./utils.js";
|
|
4
|
+
const _G1Element = class _G1Element {
|
|
4
5
|
constructor(point) {
|
|
5
6
|
this.point = point;
|
|
6
7
|
}
|
|
7
8
|
static generator() {
|
|
8
|
-
return new
|
|
9
|
+
return new _G1Element(bls12_381.G1.ProjectivePoint.BASE);
|
|
9
10
|
}
|
|
10
11
|
static fromBytes(bytes) {
|
|
11
|
-
return new
|
|
12
|
+
return new _G1Element(bls12_381.G1.ProjectivePoint.fromHex(toHex(bytes)));
|
|
12
13
|
}
|
|
13
14
|
toBytes() {
|
|
14
15
|
return this.point.toRawBytes();
|
|
15
16
|
}
|
|
16
17
|
multiply(scalar) {
|
|
17
|
-
return new
|
|
18
|
+
return new _G1Element(this.point.multiply(scalar.scalar));
|
|
18
19
|
}
|
|
19
20
|
add(other) {
|
|
20
|
-
return new
|
|
21
|
+
return new _G1Element(this.point.add(other.point));
|
|
21
22
|
}
|
|
22
23
|
subtract(other) {
|
|
23
|
-
return new
|
|
24
|
+
return new _G1Element(this.point.subtract(other.point));
|
|
24
25
|
}
|
|
25
26
|
static hashToCurve(data) {
|
|
26
|
-
return new
|
|
27
|
+
return new _G1Element(
|
|
27
28
|
bls12_381.G1.ProjectivePoint.fromAffine(bls12_381.G1.hashToCurve(data).toAffine())
|
|
28
29
|
);
|
|
29
30
|
}
|
|
30
31
|
pairing(other) {
|
|
31
32
|
return new GTElement(bls12_381.pairing(this.point, other.point));
|
|
32
33
|
}
|
|
33
|
-
}
|
|
34
|
-
|
|
34
|
+
};
|
|
35
|
+
_G1Element.SIZE = 48;
|
|
36
|
+
let G1Element = _G1Element;
|
|
37
|
+
const _G2Element = class _G2Element {
|
|
35
38
|
constructor(point) {
|
|
36
39
|
this.point = point;
|
|
37
40
|
}
|
|
38
41
|
static generator() {
|
|
39
|
-
return new
|
|
42
|
+
return new _G2Element(bls12_381.G2.ProjectivePoint.BASE);
|
|
40
43
|
}
|
|
41
44
|
static fromBytes(bytes) {
|
|
42
|
-
return new
|
|
45
|
+
return new _G2Element(bls12_381.G2.ProjectivePoint.fromHex(toHex(bytes)));
|
|
43
46
|
}
|
|
44
47
|
toBytes() {
|
|
45
48
|
return this.point.toRawBytes();
|
|
46
49
|
}
|
|
47
50
|
multiply(scalar) {
|
|
48
|
-
return new
|
|
51
|
+
return new _G2Element(this.point.multiply(scalar.scalar));
|
|
49
52
|
}
|
|
50
53
|
add(other) {
|
|
51
|
-
return new
|
|
54
|
+
return new _G2Element(this.point.add(other.point));
|
|
52
55
|
}
|
|
53
56
|
hashToCurve(data) {
|
|
54
|
-
return new
|
|
57
|
+
return new _G2Element(
|
|
55
58
|
bls12_381.G2.ProjectivePoint.fromAffine(bls12_381.G2.hashToCurve(data).toAffine())
|
|
56
59
|
);
|
|
57
60
|
}
|
|
58
|
-
}
|
|
59
|
-
|
|
61
|
+
};
|
|
62
|
+
_G2Element.SIZE = 96;
|
|
63
|
+
let G2Element = _G2Element;
|
|
64
|
+
const _GTElement = class _GTElement {
|
|
60
65
|
constructor(element) {
|
|
61
66
|
this.element = element;
|
|
62
67
|
}
|
|
63
68
|
toBytes() {
|
|
64
|
-
|
|
69
|
+
const P = [0, 3, 1, 4, 2, 5];
|
|
70
|
+
const PAIR_SIZE = _GTElement.SIZE / P.length;
|
|
71
|
+
const bytes = bls12_381.fields.Fp12.toBytes(this.element);
|
|
72
|
+
return flatten(P.map((p) => bytes.subarray(p * PAIR_SIZE, (p + 1) * PAIR_SIZE)));
|
|
73
|
+
}
|
|
74
|
+
equals(other) {
|
|
75
|
+
return bls12_381.fields.Fp12.eql(this.element, other.element);
|
|
65
76
|
}
|
|
66
|
-
}
|
|
67
|
-
|
|
77
|
+
};
|
|
78
|
+
_GTElement.SIZE = 576;
|
|
79
|
+
let GTElement = _GTElement;
|
|
80
|
+
const _Scalar = class _Scalar {
|
|
68
81
|
constructor(scalar) {
|
|
69
82
|
this.scalar = scalar;
|
|
70
83
|
}
|
|
71
84
|
static random() {
|
|
72
|
-
return
|
|
85
|
+
return _Scalar.fromBytes(bls12_381.utils.randomPrivateKey());
|
|
73
86
|
}
|
|
74
87
|
toBytes() {
|
|
75
88
|
return new Uint8Array(bls12_381.fields.Fr.toBytes(this.scalar));
|
|
76
89
|
}
|
|
77
90
|
static fromBytes(bytes) {
|
|
78
|
-
return new
|
|
91
|
+
return new _Scalar(bls12_381.fields.Fr.fromBytes(bytes));
|
|
79
92
|
}
|
|
80
93
|
static fromNumber(num) {
|
|
81
|
-
return new
|
|
94
|
+
return new _Scalar(BigInt(num));
|
|
82
95
|
}
|
|
83
|
-
}
|
|
96
|
+
};
|
|
97
|
+
_Scalar.SIZE = 32;
|
|
98
|
+
let Scalar = _Scalar;
|
|
84
99
|
export {
|
|
85
100
|
G1Element,
|
|
86
101
|
G2Element,
|
package/dist/esm/bls12381.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/bls12381.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { toHex } from '@mysten/bcs';\nimport type { Fp2, Fp12 } from '@noble/curves/abstract/tower';\nimport type { ProjPointType } from '@noble/curves/abstract/weierstrass';\nimport { bls12_381 } from '@noble/curves/bls12-381';\n\nexport class G1Element {\n\tpoint: ProjPointType<bigint>;\n\n\tconstructor(point: ProjPointType<bigint>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G1Element {\n\t\treturn new G1Element(bls12_381.G1.ProjectivePoint.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G1Element {\n\t\treturn new G1Element(bls12_381.G1.ProjectivePoint.fromHex(toHex(bytes)));\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn this.point.toRawBytes();\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(\n\t\t\tbls12_381.G1.ProjectivePoint.fromAffine(bls12_381.G1.hashToCurve(data).toAffine()),\n\t\t);\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: ProjPointType<Fp2>;\n\n\tconstructor(point: ProjPointType<Fp2>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G2Element {\n\t\treturn new G2Element(bls12_381.G2.ProjectivePoint.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G2Element {\n\t\treturn new G2Element(bls12_381.G2.ProjectivePoint.fromHex(toHex(bytes)));\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn this.point.toRawBytes();\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\thashToCurve(data: Uint8Array): G2Element {\n\t\treturn new G2Element(\n\t\t\tbls12_381.G2.ProjectivePoint.fromAffine(bls12_381.G2.hashToCurve(data).toAffine()),\n\t\t);\n\t}\n}\n\nexport class GTElement {\n\telement: Fp12;\n\n\tconstructor(element: Fp12) {\n\t\tthis.element = element;\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\
|
|
5
|
-
"mappings": "AAGA,SAAS,aAAa;AAGtB,SAAS,iBAAiB;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { toHex } from '@mysten/bcs';\nimport type { Fp2, Fp12 } from '@noble/curves/abstract/tower';\nimport type { ProjPointType } from '@noble/curves/abstract/weierstrass';\nimport { bls12_381 } from '@noble/curves/bls12-381';\nimport { flatten } from './utils.js';\n\nexport class G1Element {\n\tpoint: ProjPointType<bigint>;\n\n\tpublic static readonly SIZE = 48;\n\n\tconstructor(point: ProjPointType<bigint>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G1Element {\n\t\treturn new G1Element(bls12_381.G1.ProjectivePoint.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G1Element {\n\t\treturn new G1Element(bls12_381.G1.ProjectivePoint.fromHex(toHex(bytes)));\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn this.point.toRawBytes();\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(\n\t\t\tbls12_381.G1.ProjectivePoint.fromAffine(bls12_381.G1.hashToCurve(data).toAffine()),\n\t\t);\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: ProjPointType<Fp2>;\n\n\tpublic static readonly SIZE = 96;\n\n\tconstructor(point: ProjPointType<Fp2>) {\n\t\tthis.point = point;\n\t}\n\n\tstatic generator(): G2Element {\n\t\treturn new G2Element(bls12_381.G2.ProjectivePoint.BASE);\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): G2Element {\n\t\treturn new G2Element(bls12_381.G2.ProjectivePoint.fromHex(toHex(bytes)));\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn this.point.toRawBytes();\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\thashToCurve(data: Uint8Array): G2Element {\n\t\treturn new G2Element(\n\t\t\tbls12_381.G2.ProjectivePoint.fromAffine(bls12_381.G2.hashToCurve(data).toAffine()),\n\t\t);\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 {\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\treturn flatten(P.map((p) => bytes.subarray(p * PAIR_SIZE, (p + 1) * PAIR_SIZE)));\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 random(): Scalar {\n\t\treturn Scalar.fromBytes(bls12_381.utils.randomPrivateKey());\n\t}\n\n\ttoBytes(): Uint8Array {\n\t\treturn new Uint8Array(bls12_381.fields.Fr.toBytes(this.scalar));\n\t}\n\n\tstatic fromBytes(bytes: Uint8Array): Scalar {\n\t\treturn new Scalar(bls12_381.fields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AA,EACf;AAAA,EAEA,OAAO,SAAiB;AACvB,WAAO,QAAO,UAAU,UAAU,MAAM,iBAAiB,CAAC;AAAA,EAC3D;AAAA,EAEA,UAAsB;AACrB,WAAO,IAAI,WAAW,UAAU,OAAO,GAAG,QAAQ,KAAK,MAAM,CAAC;AAAA,EAC/D;AAAA,EAEA,OAAO,UAAU,OAA2B;AAC3C,WAAO,IAAI,QAAO,UAAU,OAAO,GAAG,UAAU,KAAK,CAAC;AAAA,EACvD;AAAA,EAEA,OAAO,WAAW,KAAqB;AACtC,WAAO,IAAI,QAAO,OAAO,GAAG,CAAC;AAAA,EAC9B;AACD;AAxBa,QAGW,OAAO;AAHxB,IAAM,SAAN;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DemType, KemType } from './encrypt.js';
|
|
2
|
-
import type { KeyServer } from './key-server.js';
|
|
2
|
+
import type { DerivedKey, KeyServer } from './key-server.js';
|
|
3
3
|
import type { SessionKey } from './session-key.js';
|
|
4
4
|
import type { SealCompatibleClient } from './types.js';
|
|
5
5
|
/**
|
|
@@ -71,7 +71,7 @@ export declare class SealClient {
|
|
|
71
71
|
/**
|
|
72
72
|
* Fetch keys from the key servers and update the cache.
|
|
73
73
|
*
|
|
74
|
-
* It is recommended to call this function once for all ids of all encrypted
|
|
74
|
+
* It is recommended to call this function once for all ids of all encrypted objects if
|
|
75
75
|
* there are multiple, then call decrypt for each object. This avoids calling fetchKey
|
|
76
76
|
* individually for each decrypt.
|
|
77
77
|
*
|
|
@@ -86,4 +86,20 @@ export declare class SealClient {
|
|
|
86
86
|
sessionKey: SessionKey;
|
|
87
87
|
threshold: number;
|
|
88
88
|
}): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Get derived keys from the given services.
|
|
91
|
+
*
|
|
92
|
+
* @param id - The id of the encrypted object.
|
|
93
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
94
|
+
* @param sessionKey - The session key to use.
|
|
95
|
+
* @param threshold - The threshold.
|
|
96
|
+
* @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.
|
|
97
|
+
*/
|
|
98
|
+
getDerivedKeys({ kemType, id, txBytes, sessionKey, threshold, }: {
|
|
99
|
+
kemType?: KemType;
|
|
100
|
+
id: string;
|
|
101
|
+
txBytes: Uint8Array;
|
|
102
|
+
sessionKey: SessionKey;
|
|
103
|
+
threshold: number;
|
|
104
|
+
}): Promise<Map<string, DerivedKey>>;
|
|
89
105
|
}
|
package/dist/esm/client.js
CHANGED
|
@@ -19,7 +19,12 @@ import {
|
|
|
19
19
|
toMajorityError
|
|
20
20
|
} from "./error.js";
|
|
21
21
|
import { BonehFranklinBLS12381Services, DST } from "./ibe.js";
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
BonehFranklinBLS12381DerivedKey,
|
|
24
|
+
KeyServerType,
|
|
25
|
+
retrieveKeyServers,
|
|
26
|
+
verifyKeyServer
|
|
27
|
+
} from "./key-server.js";
|
|
23
28
|
import { fetchKeysForAllIds } from "./keys.js";
|
|
24
29
|
import { createFullId } from "./utils.js";
|
|
25
30
|
const _SealClient = class _SealClient {
|
|
@@ -118,7 +123,7 @@ const _SealClient = class _SealClient {
|
|
|
118
123
|
/**
|
|
119
124
|
* Fetch keys from the key servers and update the cache.
|
|
120
125
|
*
|
|
121
|
-
* It is recommended to call this function once for all ids of all encrypted
|
|
126
|
+
* It is recommended to call this function once for all ids of all encrypted objects if
|
|
122
127
|
* there are multiple, then call decrypt for each object. This avoids calling fetchKey
|
|
123
128
|
* individually for each decrypt.
|
|
124
129
|
*
|
|
@@ -216,6 +221,51 @@ const _SealClient = class _SealClient {
|
|
|
216
221
|
throw toMajorityError(errors);
|
|
217
222
|
}
|
|
218
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* Get derived keys from the given services.
|
|
226
|
+
*
|
|
227
|
+
* @param id - The id of the encrypted object.
|
|
228
|
+
* @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
|
|
229
|
+
* @param sessionKey - The session key to use.
|
|
230
|
+
* @param threshold - The threshold.
|
|
231
|
+
* @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.
|
|
232
|
+
*/
|
|
233
|
+
async getDerivedKeys({
|
|
234
|
+
kemType = KemType.BonehFranklinBLS12381DemCCA,
|
|
235
|
+
id,
|
|
236
|
+
txBytes,
|
|
237
|
+
sessionKey,
|
|
238
|
+
threshold
|
|
239
|
+
}) {
|
|
240
|
+
switch (kemType) {
|
|
241
|
+
case KemType.BonehFranklinBLS12381DemCCA:
|
|
242
|
+
const keyServers = await this.getKeyServers();
|
|
243
|
+
if (threshold > __privateGet(this, _serverObjectIds).length) {
|
|
244
|
+
throw new InvalidThresholdError(
|
|
245
|
+
`Invalid threshold ${threshold} for ${__privateGet(this, _serverObjectIds).length} servers`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
await this.fetchKeys({
|
|
249
|
+
ids: [id],
|
|
250
|
+
txBytes,
|
|
251
|
+
sessionKey,
|
|
252
|
+
threshold
|
|
253
|
+
});
|
|
254
|
+
const fullId = createFullId(DST, sessionKey.getPackageId(), id);
|
|
255
|
+
const derivedKeys = /* @__PURE__ */ new Map();
|
|
256
|
+
let servicesAdded = 0;
|
|
257
|
+
for (const keyServer of keyServers) {
|
|
258
|
+
const cachedKey = __privateGet(this, _cachedKeys).get(`${fullId}:${keyServer.objectId}`);
|
|
259
|
+
if (cachedKey) {
|
|
260
|
+
derivedKeys.set(keyServer.objectId, new BonehFranklinBLS12381DerivedKey(cachedKey));
|
|
261
|
+
if (++servicesAdded === threshold) {
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return derivedKeys;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
219
269
|
};
|
|
220
270
|
_suiClient = new WeakMap();
|
|
221
271
|
_serverObjectIds = new WeakMap();
|