@mysten/seal 0.0.0-experimental-20250330082435
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 +31 -0
- package/README.md +4 -0
- package/dist/cjs/bcs.d.ts +132 -0
- package/dist/cjs/bcs.js +67 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/bls12381.d.ts +37 -0
- package/dist/cjs/bls12381.js +110 -0
- package/dist/cjs/bls12381.js.map +7 -0
- package/dist/cjs/client.d.ts +83 -0
- package/dist/cjs/client.js +283 -0
- package/dist/cjs/client.js.map +7 -0
- package/dist/cjs/decrypt.d.ts +15 -0
- package/dist/cjs/decrypt.js +94 -0
- package/dist/cjs/decrypt.js.map +7 -0
- package/dist/cjs/dem.d.ts +36 -0
- package/dist/cjs/dem.js +173 -0
- package/dist/cjs/dem.js.map +7 -0
- package/dist/cjs/elgamal.d.ts +11 -0
- package/dist/cjs/elgamal.js +46 -0
- package/dist/cjs/elgamal.js.map +7 -0
- package/dist/cjs/encrypt.d.ts +33 -0
- package/dist/cjs/encrypt.js +118 -0
- package/dist/cjs/encrypt.js.map +7 -0
- package/dist/cjs/error.d.ts +58 -0
- package/dist/cjs/error.js +175 -0
- package/dist/cjs/error.js.map +7 -0
- package/dist/cjs/ibe.d.ts +63 -0
- package/dist/cjs/ibe.js +104 -0
- package/dist/cjs/ibe.js.map +7 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/kdf.d.ts +14 -0
- package/dist/cjs/kdf.js +64 -0
- package/dist/cjs/kdf.js.map +7 -0
- package/dist/cjs/key-server.d.ts +38 -0
- package/dist/cjs/key-server.js +101 -0
- package/dist/cjs/key-server.js.map +7 -0
- package/dist/cjs/keys.d.ts +17 -0
- package/dist/cjs/keys.js +61 -0
- package/dist/cjs/keys.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/session-key.d.ts +42 -0
- package/dist/cjs/session-key.js +140 -0
- package/dist/cjs/session-key.js.map +7 -0
- package/dist/cjs/types.d.ts +1 -0
- package/dist/cjs/types.js +17 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/utils.d.ts +10 -0
- package/dist/cjs/utils.js +51 -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 +132 -0
- package/dist/esm/bcs.js +47 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/bls12381.d.ts +37 -0
- package/dist/esm/bls12381.js +90 -0
- package/dist/esm/bls12381.js.map +7 -0
- package/dist/esm/client.d.ts +83 -0
- package/dist/esm/client.js +268 -0
- package/dist/esm/client.js.map +7 -0
- package/dist/esm/decrypt.d.ts +15 -0
- package/dist/esm/decrypt.js +74 -0
- package/dist/esm/decrypt.js.map +7 -0
- package/dist/esm/dem.d.ts +36 -0
- package/dist/esm/dem.js +153 -0
- package/dist/esm/dem.js.map +7 -0
- package/dist/esm/elgamal.d.ts +11 -0
- package/dist/esm/elgamal.js +26 -0
- package/dist/esm/elgamal.js.map +7 -0
- package/dist/esm/encrypt.d.ts +33 -0
- package/dist/esm/encrypt.js +98 -0
- package/dist/esm/encrypt.js.map +7 -0
- package/dist/esm/error.d.ts +58 -0
- package/dist/esm/error.js +155 -0
- package/dist/esm/error.js.map +7 -0
- package/dist/esm/ibe.d.ts +63 -0
- package/dist/esm/ibe.js +84 -0
- package/dist/esm/ibe.js.map +7 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/kdf.d.ts +14 -0
- package/dist/esm/kdf.js +44 -0
- package/dist/esm/kdf.js.map +7 -0
- package/dist/esm/key-server.d.ts +38 -0
- package/dist/esm/key-server.js +86 -0
- package/dist/esm/key-server.js.map +7 -0
- package/dist/esm/keys.d.ts +17 -0
- package/dist/esm/keys.js +41 -0
- package/dist/esm/keys.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/session-key.d.ts +42 -0
- package/dist/esm/session-key.js +124 -0
- package/dist/esm/session-key.js.map +7 -0
- package/dist/esm/types.d.ts +1 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/utils.d.ts +10 -0
- package/dist/esm/utils.js +31 -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 +59 -0
package/dist/cjs/ibe.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
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 ibe_exports = {};
|
|
20
|
+
__export(ibe_exports, {
|
|
21
|
+
BonehFranklinBLS12381Services: () => BonehFranklinBLS12381Services,
|
|
22
|
+
DST: () => DST,
|
|
23
|
+
DST_POP: () => DST_POP,
|
|
24
|
+
IBEServers: () => IBEServers
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(ibe_exports);
|
|
27
|
+
var import_bcs = require("@mysten/bcs");
|
|
28
|
+
var import_bls12381 = require("./bls12381.js");
|
|
29
|
+
var import_kdf = require("./kdf.js");
|
|
30
|
+
var import_utils = require("./utils.js");
|
|
31
|
+
const DST = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-00");
|
|
32
|
+
const DST_POP = new TextEncoder().encode("SUI-SEAL-IBE-BLS12381-POP-00");
|
|
33
|
+
class IBEServers {
|
|
34
|
+
constructor(objectIds) {
|
|
35
|
+
this.objectIds = objectIds;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The number of key servers.
|
|
39
|
+
*/
|
|
40
|
+
size() {
|
|
41
|
+
return this.objectIds.length;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class BonehFranklinBLS12381Services extends IBEServers {
|
|
45
|
+
constructor(services) {
|
|
46
|
+
super(services.map((service) => service.objectId));
|
|
47
|
+
this.publicKeys = services.map((service) => import_bls12381.G2Element.fromBytes(service.pk));
|
|
48
|
+
}
|
|
49
|
+
encryptBatched(id, msgAndIndices, randomnessKey) {
|
|
50
|
+
if (this.publicKeys.length === 0 || this.publicKeys.length !== msgAndIndices.length) {
|
|
51
|
+
throw new Error("Invalid public keys");
|
|
52
|
+
}
|
|
53
|
+
const [r, nonce, keys] = encapBatched(this.publicKeys, id);
|
|
54
|
+
const encryptedShares = msgAndIndices.map(
|
|
55
|
+
(msgAndIndex, i) => (0, import_utils.xor)(msgAndIndex.msg, (0, import_kdf.kdf)(keys[i], nonce, id, this.objectIds[i], msgAndIndex.index))
|
|
56
|
+
);
|
|
57
|
+
const encryptedRandomness = (0, import_utils.xor)(randomnessKey, r.toBytes());
|
|
58
|
+
return {
|
|
59
|
+
BonehFranklinBLS12381: {
|
|
60
|
+
nonce: nonce.toBytes(),
|
|
61
|
+
encryptedShares,
|
|
62
|
+
encryptedRandomness
|
|
63
|
+
},
|
|
64
|
+
$kind: "BonehFranklinBLS12381"
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Returns true if the user secret key is valid for the given public key and id.
|
|
69
|
+
* @param user_secret_key - The user secret key.
|
|
70
|
+
* @param id - The identity.
|
|
71
|
+
* @param public_key - The public key.
|
|
72
|
+
* @returns True if the user secret key is valid for the given public key and id.
|
|
73
|
+
*/
|
|
74
|
+
static verifyUserSecretKey(userSecretKey, id, publicKey) {
|
|
75
|
+
const lhs = userSecretKey.pairing(import_bls12381.G2Element.generator()).toBytes();
|
|
76
|
+
const rhs = import_bls12381.G1Element.hashToCurve((0, import_bcs.fromHex)(id)).pairing(publicKey).toBytes();
|
|
77
|
+
return lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Identity-based decryption.
|
|
81
|
+
*
|
|
82
|
+
* @param nonce The encryption nonce.
|
|
83
|
+
* @param sk The user secret key.
|
|
84
|
+
* @param ciphertext The encrypted message.
|
|
85
|
+
* @param info An info parameter also included in the KDF.
|
|
86
|
+
* @returns The decrypted message.
|
|
87
|
+
*/
|
|
88
|
+
static decrypt(nonce, sk, ciphertext, id, [objectId, index]) {
|
|
89
|
+
return (0, import_utils.xor)(ciphertext, (0, import_kdf.kdf)(decap(nonce, sk), nonce, id, objectId, index));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function encapBatched(publicKeys, id) {
|
|
93
|
+
if (publicKeys.length === 0) {
|
|
94
|
+
throw new Error("No public keys provided");
|
|
95
|
+
}
|
|
96
|
+
const r = import_bls12381.Scalar.random();
|
|
97
|
+
const nonce = import_bls12381.G2Element.generator().multiply(r);
|
|
98
|
+
const gid = import_bls12381.G1Element.hashToCurve(id).multiply(r);
|
|
99
|
+
return [r, nonce, publicKeys.map((public_key) => gid.pairing(public_key))];
|
|
100
|
+
}
|
|
101
|
+
function decap(nonce, usk) {
|
|
102
|
+
return usk.pairing(nonce);
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=ibe.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/ibe.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex } from '@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((msgAndIndex, i) =>\n\t\t\txor(msgAndIndex.msg, kdf(keys[i], nonce, id, this.objectIds[i], msgAndIndex.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()).toBytes();\n\t\tconst rhs = G1Element.hashToCurve(fromHex(id)).pairing(publicKey).toBytes();\n\t\treturn lhs.length === rhs.length && lhs.every((value, index) => value === rhs[index]);\n\t}\n\n\t/**\n\t * Identity-based decryption.\n\t *\n\t * @param nonce The encryption nonce.\n\t * @param sk The user secret key.\n\t * @param ciphertext The encrypted message.\n\t * @param info An info parameter also included in the KDF.\n\t * @returns The decrypted message.\n\t */\n\tstatic decrypt(\n\t\tnonce: G2Element,\n\t\tsk: G1Element,\n\t\tciphertext: Uint8Array,\n\t\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;AAMO,MAAM,sCAAsC,WAAW;AAAA,EAG7D,YAAY,UAAuB;AAClC,UAAM,SAAS,IAAI,CAAC,YAAY,QAAQ,QAAQ,CA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AAa,YAAyB,IAAkD;AAChG,MAAI,WAAW,WAAW,GAAG;AAC5B,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC1C;AACA,QAAM,IAAI,uBAAO,OAAO;AACxB,QAAM,QAAQ,0BAAU,UAAU,EAAE,SAAS,CAAC;AAC9C,QAAM,MAAM,0BAAU,YAAY,EAAE,EAAE,SAAS,CAAC;AAChD,SAAO,CAAC,GAAG,OAAO,WAAW,IAAI,CAAC,eAAe,IAAI,QAAQ,UAAU,CAAC,CAAC;AAC1E;AASA,SAAS,MAAM,OAAkB,KAA2B;AAC3D,SAAO,IAAI,QAAQ,KAAK;AACzB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
SealClient: () => import_client.SealClient,
|
|
23
|
+
SessionKey: () => import_session_key.SessionKey,
|
|
24
|
+
getAllowlistedKeyServers: () => import_key_server.getAllowlistedKeyServers
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_key_server = require("./key-server.js");
|
|
28
|
+
var import_client = require("./client.js");
|
|
29
|
+
var import_session_key = require("./session-key.js");
|
|
30
|
+
__reExport(index_exports, require("./error.js"), module.exports);
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { getAllowlistedKeyServers } from './key-server.js';\nexport { SealClient, type SealClientOptions } from './client.js';\nexport { SessionKey } from './session-key.js';\nexport * from './error.js';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAAyC;AACzC,oBAAmD;AACnD,yBAA2B;AAC3B,0BAAc,uBANd;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { G2Element, GTElement } from './bls12381.js';
|
|
2
|
+
/**
|
|
3
|
+
* The default key derivation function.
|
|
4
|
+
*
|
|
5
|
+
* @param element The GTElement to derive the key from.
|
|
6
|
+
* @param info Optional context and application specific information.
|
|
7
|
+
* @returns The derived key.
|
|
8
|
+
*/
|
|
9
|
+
export declare function kdf(element: GTElement, nonce: G2Element, id: Uint8Array, objectId: string, index: number): Uint8Array;
|
|
10
|
+
export declare enum KeyPurpose {
|
|
11
|
+
EncryptedRandomness = 0,
|
|
12
|
+
DEM = 1
|
|
13
|
+
}
|
|
14
|
+
export declare function deriveKey(purpose: KeyPurpose, baseKey: Uint8Array): Uint8Array;
|
package/dist/cjs/kdf.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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 kdf_exports = {};
|
|
20
|
+
__export(kdf_exports, {
|
|
21
|
+
KeyPurpose: () => KeyPurpose,
|
|
22
|
+
deriveKey: () => deriveKey,
|
|
23
|
+
kdf: () => kdf
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(kdf_exports);
|
|
26
|
+
var import_bcs = require("@mysten/bcs");
|
|
27
|
+
var import_hkdf = require("@noble/hashes/hkdf");
|
|
28
|
+
var import_hmac = require("@noble/hashes/hmac");
|
|
29
|
+
var import_sha3 = require("@noble/hashes/sha3");
|
|
30
|
+
var import_bls12381 = require("./bls12381.js");
|
|
31
|
+
function kdf(element, nonce, id, objectId, index) {
|
|
32
|
+
const GT_ELEMENT_BYTE_LENGTH = 576;
|
|
33
|
+
const PERMUTATION = [0, 2, 4, 1, 3, 5];
|
|
34
|
+
const COEFFICIENT_SIZE = GT_ELEMENT_BYTE_LENGTH / PERMUTATION.length;
|
|
35
|
+
const bytes = element.toBytes();
|
|
36
|
+
let 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()
|
|
47
|
+
]);
|
|
48
|
+
const info = new Uint8Array([...(0, import_bcs.fromHex)(objectId), index]);
|
|
49
|
+
return (0, import_hkdf.hkdf)(import_sha3.sha3_256, inputBytes, "", info, 32);
|
|
50
|
+
}
|
|
51
|
+
var KeyPurpose = /* @__PURE__ */ ((KeyPurpose2) => {
|
|
52
|
+
KeyPurpose2[KeyPurpose2["EncryptedRandomness"] = 0] = "EncryptedRandomness";
|
|
53
|
+
KeyPurpose2[KeyPurpose2["DEM"] = 1] = "DEM";
|
|
54
|
+
return KeyPurpose2;
|
|
55
|
+
})(KeyPurpose || {});
|
|
56
|
+
function deriveKey(purpose, baseKey) {
|
|
57
|
+
switch (purpose) {
|
|
58
|
+
case 0 /* EncryptedRandomness */:
|
|
59
|
+
return (0, import_hmac.hmac)(import_sha3.sha3_256, baseKey, new Uint8Array([0]));
|
|
60
|
+
case 1 /* DEM */:
|
|
61
|
+
return (0, import_hmac.hmac)(import_sha3.sha3_256, baseKey, new Uint8Array([1]));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=kdf.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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\t// This permutation flips the order of 6 pairs of coefficients of the GT element.\n\t// The permutation may be computed as:\n\t// for i in 0..3 {\n\t// for j in 0..2 {\n\t// PERMUTATION[i + j * 3] = i * 2 + j;\n\t// }\n\t// }\n\tconst GT_ELEMENT_BYTE_LENGTH = 576;\n\tconst PERMUTATION = [0, 2, 4, 1, 3, 5];\n\tconst COEFFICIENT_SIZE = GT_ELEMENT_BYTE_LENGTH / PERMUTATION.length;\n\n\tconst bytes = element.toBytes();\n\tlet permutedBytes = new Uint8Array(GT_ELEMENT_BYTE_LENGTH);\n\tPERMUTATION.forEach((pi, i) => {\n\t\tpermutedBytes.set(\n\t\t\tbytes.slice(i * COEFFICIENT_SIZE, (i + 1) * COEFFICIENT_SIZE),\n\t\t\tpi * COEFFICIENT_SIZE,\n\t\t);\n\t});\n\tconst inputBytes = new Uint8Array([\n\t\t...permutedBytes,\n\t\t...nonce.toBytes(),\n\t\t...G1Element.hashToCurve(id).toBytes(),\n\t]);\n\tconst info = new Uint8Array([...fromHex(objectId), index]);\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 h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AI,WAAW,CAAC,CAAC,CAAC,CAAC;AAAA,EACpD;AACD;",
|
|
6
|
+
"names": ["KeyPurpose"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SuiClient } from '@mysten/sui/client';
|
|
2
|
+
export type KeyServer = {
|
|
3
|
+
objectId: string;
|
|
4
|
+
name: string;
|
|
5
|
+
url: string;
|
|
6
|
+
keyType: KeyServerType;
|
|
7
|
+
pk: Uint8Array;
|
|
8
|
+
};
|
|
9
|
+
export declare enum KeyServerType {
|
|
10
|
+
BonehFranklinBLS12381 = 0
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Returns a static list of Seal key server object ids that the dapp can choose to use.
|
|
14
|
+
* @param network - The network to use.
|
|
15
|
+
* @returns The object id's of the key servers.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getAllowlistedKeyServers(network: 'testnet' | 'mainnet'): string[];
|
|
18
|
+
/**
|
|
19
|
+
* Given a list of key server object IDs, returns a list of SealKeyServer
|
|
20
|
+
* from onchain state containing name, objectId, URL and pk.
|
|
21
|
+
*
|
|
22
|
+
* @param objectIds - The key server object IDs.
|
|
23
|
+
* @param client - The SuiClient to use.
|
|
24
|
+
* @returns - An array of SealKeyServer.
|
|
25
|
+
*/
|
|
26
|
+
export declare function retrieveKeyServers({ objectIds, client, }: {
|
|
27
|
+
objectIds: string[];
|
|
28
|
+
client: SuiClient;
|
|
29
|
+
}): Promise<KeyServer[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Given a KeyServer, fetch the proof of possession (PoP) from the URL and verify it
|
|
32
|
+
* against the pubkey. This should be used only rarely when the dapp uses a dynamic
|
|
33
|
+
* set of key servers.
|
|
34
|
+
*
|
|
35
|
+
* @param server - The KeyServer to verify.
|
|
36
|
+
* @returns - True if the key server is valid, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export declare function verifyKeyServer(server: KeyServer, timeout: number): Promise<boolean>;
|
|
@@ -0,0 +1,101 @@
|
|
|
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 key_server_exports = {};
|
|
20
|
+
__export(key_server_exports, {
|
|
21
|
+
KeyServerType: () => KeyServerType,
|
|
22
|
+
getAllowlistedKeyServers: () => getAllowlistedKeyServers,
|
|
23
|
+
retrieveKeyServers: () => retrieveKeyServers,
|
|
24
|
+
verifyKeyServer: () => verifyKeyServer
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(key_server_exports);
|
|
27
|
+
var import_bcs = require("@mysten/bcs");
|
|
28
|
+
var import_bls12_381 = require("@noble/curves/bls12-381");
|
|
29
|
+
var import_bcs2 = require("./bcs.js");
|
|
30
|
+
var import_error = require("./error.js");
|
|
31
|
+
var import_ibe = require("./ibe.js");
|
|
32
|
+
var import_version = require("./version.js");
|
|
33
|
+
var KeyServerType = /* @__PURE__ */ ((KeyServerType2) => {
|
|
34
|
+
KeyServerType2[KeyServerType2["BonehFranklinBLS12381"] = 0] = "BonehFranklinBLS12381";
|
|
35
|
+
return KeyServerType2;
|
|
36
|
+
})(KeyServerType || {});
|
|
37
|
+
function getAllowlistedKeyServers(network) {
|
|
38
|
+
if (network === "testnet") {
|
|
39
|
+
return [
|
|
40
|
+
"0xb35a7228d8cf224ad1e828c0217c95a5153bafc2906d6f9c178197dce26fbcf8",
|
|
41
|
+
"0x2d6cde8a9d9a65bde3b0a346566945a63b4bfb70e9a06c41bdb70807e2502b06"
|
|
42
|
+
];
|
|
43
|
+
} else {
|
|
44
|
+
throw new import_error.UnsupportedNetworkError(`Unsupported network ${network}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function retrieveKeyServers({
|
|
48
|
+
objectIds,
|
|
49
|
+
client
|
|
50
|
+
}) {
|
|
51
|
+
return await Promise.all(
|
|
52
|
+
objectIds.map(async (objectId) => {
|
|
53
|
+
const res = await client.getObject({
|
|
54
|
+
id: objectId,
|
|
55
|
+
options: {
|
|
56
|
+
showBcs: true
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
if (!res || res.error || !res.data) {
|
|
60
|
+
throw new import_error.InvalidGetObjectError(`KeyServer ${objectId} not found; ${res.error}`);
|
|
61
|
+
}
|
|
62
|
+
if (!res.data.bcs || !("bcsBytes" in res.data.bcs)) {
|
|
63
|
+
throw new import_error.InvalidGetObjectError(
|
|
64
|
+
`Invalid KeyServer query: ${objectId}, expected object, got package`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
let ks = import_bcs2.KeyServerMove.parse((0, import_bcs.fromBase64)(res.data.bcs.bcsBytes));
|
|
68
|
+
if (ks.keyType !== 0) {
|
|
69
|
+
throw new import_error.UnsupportedFeatureError(`Unsupported key type ${ks.keyType}`);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
objectId,
|
|
73
|
+
name: ks.name,
|
|
74
|
+
url: ks.url,
|
|
75
|
+
keyType: 0 /* BonehFranklinBLS12381 */,
|
|
76
|
+
pk: new Uint8Array(ks.pk)
|
|
77
|
+
};
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
async function verifyKeyServer(server, timeout) {
|
|
82
|
+
const requestId = crypto.randomUUID();
|
|
83
|
+
const response = await fetch(server.url + "/v1/service", {
|
|
84
|
+
method: "GET",
|
|
85
|
+
headers: {
|
|
86
|
+
"Content-Type": "application/json",
|
|
87
|
+
"Request-Id": requestId,
|
|
88
|
+
"Client-Sdk-Type": "typescript",
|
|
89
|
+
"Client-Sdk-Version": import_version.PACKAGE_VERSION
|
|
90
|
+
},
|
|
91
|
+
signal: AbortSignal.timeout(timeout)
|
|
92
|
+
});
|
|
93
|
+
await import_error.SealAPIError.assertResponse(response, requestId);
|
|
94
|
+
const serviceResponse = await response.json();
|
|
95
|
+
if (serviceResponse.service_id !== server.objectId) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const fullMsg = new Uint8Array([...import_ibe.DST_POP, ...server.pk, ...(0, import_bcs.fromHex)(server.objectId)]);
|
|
99
|
+
return import_bls12_381.bls12_381.verifyShortSignature((0, import_bcs.fromBase64)(serviceResponse.pop), fullMsg, server.pk);
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=key-server.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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 type { SuiClient } from '@mysten/sui/client';\nimport { bls12_381 } from '@noble/curves/bls12-381';\n\nimport { KeyServerMove } from './bcs.js';\nimport {\n\tInvalidGetObjectError,\n\tSealAPIError,\n\tUnsupportedFeatureError,\n\tUnsupportedNetworkError,\n} from './error.js';\nimport { DST_POP } from './ibe.js';\nimport { PACKAGE_VERSION } from './version.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\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: SuiClient;\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\tconst res = await client.getObject({\n\t\t\t\tid: objectId,\n\t\t\t\toptions: {\n\t\t\t\t\tshowBcs: true,\n\t\t\t\t},\n\t\t\t});\n\t\t\tif (!res || res.error || !res.data) {\n\t\t\t\tthrow new InvalidGetObjectError(`KeyServer ${objectId} not found; ${res.error}`);\n\t\t\t}\n\n\t\t\tif (!res.data.bcs || !('bcsBytes' in res.data.bcs)) {\n\t\t\t\tthrow new InvalidGetObjectError(\n\t\t\t\t\t`Invalid KeyServer query: ${objectId}, expected object, got package`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlet ks = KeyServerMove.parse(fromBase64(res.data.bcs!.bcsBytes));\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\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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,iBAAoC;AAEpC,uBAA0B;AAE1B,IAAAA,cAA8B;AAC9B,mBAKO;AACP,iBAAwB;AACxB,qBAAgC;AAUzB,IAAK,gBAAL,kBAAKC,mBAAL;AACN,EAAAA,8BAAA,2BAAwB,KAAxB;AADW,SAAAA;AAAA,GAAA;AASL,SAAS,yBAAyB,SAA0C;AAClF,MAAI,YAAY,WAAW;AAC1B,WAAO;AAAA,MACN;AAAA,MACA;AAAA,IACD;AAAA,E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SAAS;AACrD,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;",
|
|
6
|
+
"names": ["import_bcs", "KeyServerType"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Certificate } from './session-key.js';
|
|
2
|
+
/**
|
|
3
|
+
* Helper function to request all keys from URL with requestSig, txBytes, ephemeral pubkey.
|
|
4
|
+
* Then decrypt the Seal key with ephemeral secret key. Returns a list decryption keys with
|
|
5
|
+
* their full IDs.
|
|
6
|
+
*
|
|
7
|
+
* @param url - The URL of the key server.
|
|
8
|
+
* @param requestSig - The Base64 string of request signature.
|
|
9
|
+
* @param txBytes - The transaction bytes.
|
|
10
|
+
* @param encKey - The ephemeral secret key.
|
|
11
|
+
* @param certificate - The certificate.
|
|
12
|
+
* @returns - A list of full ID and the decrypted key.
|
|
13
|
+
*/
|
|
14
|
+
export declare function fetchKeysForAllIds(url: string, requestSig: string, txBytes: Uint8Array, encKey: Uint8Array, certificate: Certificate, timeout: number, signal?: AbortSignal): Promise<{
|
|
15
|
+
fullId: string;
|
|
16
|
+
key: Uint8Array;
|
|
17
|
+
}[]>;
|
package/dist/cjs/keys.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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 keys_exports = {};
|
|
20
|
+
__export(keys_exports, {
|
|
21
|
+
fetchKeysForAllIds: () => fetchKeysForAllIds
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(keys_exports);
|
|
24
|
+
var import_bcs = require("@mysten/bcs");
|
|
25
|
+
var import_elgamal = require("./elgamal.js");
|
|
26
|
+
var import_error = require("./error.js");
|
|
27
|
+
var import_version = require("./version.js");
|
|
28
|
+
async function fetchKeysForAllIds(url, requestSig, txBytes, encKey, certificate, timeout, signal) {
|
|
29
|
+
const encKeyPk = (0, import_elgamal.toPublicKey)(encKey);
|
|
30
|
+
const encVerificationKey = (0, import_elgamal.toVerificationKey)(encKey);
|
|
31
|
+
const body = {
|
|
32
|
+
ptb: (0, import_bcs.toBase64)(txBytes.slice(1)),
|
|
33
|
+
// removes the byte of the transaction type version
|
|
34
|
+
enc_key: (0, import_bcs.toBase64)(encKeyPk),
|
|
35
|
+
enc_verification_key: (0, import_bcs.toBase64)(encVerificationKey),
|
|
36
|
+
request_signature: requestSig,
|
|
37
|
+
// already b64
|
|
38
|
+
certificate
|
|
39
|
+
};
|
|
40
|
+
const timeoutSignal = AbortSignal.timeout(timeout);
|
|
41
|
+
const combinedSignal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;
|
|
42
|
+
const requestId = crypto.randomUUID();
|
|
43
|
+
const response = await fetch(url + "/v1/fetch_key", {
|
|
44
|
+
method: "POST",
|
|
45
|
+
headers: {
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
"Request-Id": requestId,
|
|
48
|
+
"Client-Sdk-Type": "typescript",
|
|
49
|
+
"Client-Sdk-Version": import_version.PACKAGE_VERSION
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(body),
|
|
52
|
+
signal: combinedSignal
|
|
53
|
+
});
|
|
54
|
+
await import_error.SealAPIError.assertResponse(response, requestId);
|
|
55
|
+
const resp = await response.json();
|
|
56
|
+
return resp.decryption_keys.map((dk) => ({
|
|
57
|
+
fullId: (0, import_bcs.toHex)(new Uint8Array(dk.id)),
|
|
58
|
+
key: (0, import_elgamal.elgamalDecrypt)(encKey, dk.encrypted_key.map(import_bcs.fromBase64))
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=keys.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/keys.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase64, toBase64, toHex } from '@mysten/bcs';\n\nimport { elgamalDecrypt, toPublicKey, toVerificationKey } from './elgamal.js';\nimport { SealAPIError } from './error.js';\nimport type { Certificate } from './session-key.js';\nimport { PACKAGE_VERSION } from './version.js';\n\n/**\n * Helper function to request all keys from URL with requestSig, txBytes, ephemeral pubkey.\n * Then decrypt the Seal key with ephemeral secret key. Returns a list decryption keys with\n * their full IDs.\n *\n * @param url - The URL of the key server.\n * @param requestSig - The Base64 string of request signature.\n * @param txBytes - The transaction bytes.\n * @param encKey - The ephemeral secret key.\n * @param certificate - The certificate.\n * @returns - A list of full ID and the decrypted key.\n */\nexport async function fetchKeysForAllIds(\n\turl: string,\n\trequestSig: string,\n\ttxBytes: Uint8Array,\n\tencKey: Uint8Array,\n\tcertificate: Certificate,\n\ttimeout: number,\n\tsignal?: AbortSignal,\n): Promise<{ fullId: string; key: Uint8Array }[]> {\n\tconst encKeyPk = toPublicKey(encKey);\n\tconst encVerificationKey = toVerificationKey(encKey);\n\tconst body = {\n\t\tptb: toBase64(txBytes.slice(1)), // removes the byte of the transaction type version\n\t\tenc_key: toBase64(encKeyPk),\n\t\tenc_verification_key: toBase64(encVerificationKey),\n\t\trequest_signature: requestSig, // already b64\n\t\tcertificate,\n\t};\n\n\tconst timeoutSignal = AbortSignal.timeout(timeout);\n\tconst combinedSignal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;\n\n\tconst requestId = crypto.randomUUID();\n\tconst response = await fetch(url + '/v1/fetch_key', {\n\t\tmethod: 'POST',\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\tbody: JSON.stringify(body),\n\t\tsignal: combinedSignal,\n\t});\n\tawait SealAPIError.assertResponse(response, requestId);\n\n\tconst resp = await response.json();\n\treturn resp.decryption_keys.map((dk: { id: Uint8Array; encrypted_key: [string, string] }) => ({\n\t\tfullId: toHex(new Uint8Array(dk.id)),\n\t\tkey: elgamalDecrypt(encKey, dk.encrypted_key.map(fromBase64) as [Uint8Array, Uint8Array]),\n\t}));\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA4C;AAE5C,qBAA+D;AAC/D,mBAA6B;AAE7B,qBAAgC;AAchC,eAAsB,mBACrB,KACA,YACA,SACA,QACA,aACA,SACA,QACiD;AACjD,QAAM,eAAW,4BAAY,MAAM;AACnC,QAAM,yBAAqB,kCAAkB,MAAM;AACnD,QAAM,OAAO;AAAA,IACZ,SAAK,qBAAS,QAAQ,MAAM,CAAC,CAAC;AAAA;AAAA,IAC9B,aAAS,qBAAS,QAAQ;AAAA,IAC1B,0BAAsB,qBAAS,kBAAkB;AAAA,IACjD,mBAAmB;AAAA;AAAA,IACnB;AAAA,EACD;AAEA,QAAM,gBAAgB,YAAY,QAAQ,OAAO;AACjD,QAAM,iBAAiB,SAAS,YAAY,IAAI,CAAC,QAAQ,aAAa,CAAC,IAAI;AAE3E,QAAM,YAAY,OAAO,WAAW;AACpC,QAAM,WAAW,MAAM,MAAM,MAAM,iBAAiB;AAAA,IACnD,QAAQ;AAAA,IACR,SAAS;AAAA,MACR,gBAAgB;AAAA,MAChB,cAAc;AAAA,MACd,mBAAmB;AAAA,MACnB,sBAAsB;AAAA,IACvB;AAAA,IACA,MAAM,KAAK,UAAU,IAAI;AAAA,IACzB,QAAQ;AAAA,EACT,CAAC;AACD,QAAM,0BAAa,eAAe,UAAU,SAAS;AAErD,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,SAAO,KAAK,gBAAgB,IAAI,CAAC,QAA6D;AAAA,IAC7F,YAAQ,kBAAM,IAAI,WAAW,GAAG,EAAE,CAAC;AAAA,IACnC,SAAK,+BAAe,QAAQ,GAAG,cAAc,IAAI,qBAAU,CAA6B;AAAA,EACzF,EAAE;AACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Signer } from '@mysten/sui/cryptography';
|
|
2
|
+
export declare const RequestFormat: import("@mysten/bcs").BcsType<{
|
|
3
|
+
ptb: number[];
|
|
4
|
+
encKey: number[];
|
|
5
|
+
encVerificationKey: number[];
|
|
6
|
+
}, {
|
|
7
|
+
ptb: Iterable<number> & {
|
|
8
|
+
length: number;
|
|
9
|
+
};
|
|
10
|
+
encKey: Iterable<number> & {
|
|
11
|
+
length: number;
|
|
12
|
+
};
|
|
13
|
+
encVerificationKey: Iterable<number> & {
|
|
14
|
+
length: number;
|
|
15
|
+
};
|
|
16
|
+
}>;
|
|
17
|
+
export type Certificate = {
|
|
18
|
+
user: string;
|
|
19
|
+
session_vk: string;
|
|
20
|
+
creation_time: number;
|
|
21
|
+
ttl_min: number;
|
|
22
|
+
signature: string;
|
|
23
|
+
};
|
|
24
|
+
export declare class SessionKey {
|
|
25
|
+
#private;
|
|
26
|
+
constructor({ address, packageId, ttlMin, signer, }: {
|
|
27
|
+
address: string;
|
|
28
|
+
packageId: string;
|
|
29
|
+
ttlMin: number;
|
|
30
|
+
signer?: Signer;
|
|
31
|
+
});
|
|
32
|
+
isExpired(): boolean;
|
|
33
|
+
getAddress(): string;
|
|
34
|
+
getPackageId(): string;
|
|
35
|
+
getPersonalMessage(): Uint8Array;
|
|
36
|
+
setPersonalMessageSignature(personalMessageSignature: string): Promise<void>;
|
|
37
|
+
getCertificate(): Promise<Certificate>;
|
|
38
|
+
createRequestParams(txBytes: Uint8Array): Promise<{
|
|
39
|
+
decryptionKey: Uint8Array;
|
|
40
|
+
requestSignature: string;
|
|
41
|
+
}>;
|
|
42
|
+
}
|