@mysten/seal 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/bcs.d.ts +132 -0
  3. package/dist/cjs/bcs.js +67 -0
  4. package/dist/cjs/bcs.js.map +7 -0
  5. package/dist/cjs/client.d.ts +83 -0
  6. package/dist/cjs/client.js +283 -0
  7. package/dist/cjs/client.js.map +7 -0
  8. package/dist/cjs/decrypt.d.ts +15 -0
  9. package/dist/cjs/decrypt.js +94 -0
  10. package/dist/cjs/decrypt.js.map +7 -0
  11. package/dist/cjs/dem.d.ts +36 -0
  12. package/dist/cjs/dem.js +173 -0
  13. package/dist/cjs/dem.js.map +7 -0
  14. package/dist/cjs/elgamal.js.map +2 -2
  15. package/dist/cjs/encrypt.d.ts +14 -5
  16. package/dist/cjs/encrypt.js +52 -22
  17. package/dist/cjs/encrypt.js.map +3 -3
  18. package/dist/cjs/error.d.ts +58 -0
  19. package/dist/cjs/error.js +175 -0
  20. package/dist/cjs/error.js.map +7 -0
  21. package/dist/cjs/ibe.d.ts +13 -17
  22. package/dist/cjs/ibe.js +25 -28
  23. package/dist/cjs/ibe.js.map +2 -2
  24. package/dist/cjs/index.d.ts +3 -5
  25. package/dist/cjs/index.js +5 -11
  26. package/dist/cjs/index.js.map +2 -2
  27. package/dist/cjs/kdf.d.ts +7 -2
  28. package/dist/cjs/kdf.js +37 -2
  29. package/dist/cjs/kdf.js.map +3 -3
  30. package/dist/cjs/key-server.d.ts +5 -5
  31. package/dist/cjs/key-server.js +24 -21
  32. package/dist/cjs/key-server.js.map +2 -2
  33. package/dist/cjs/keys.d.ts +17 -0
  34. package/dist/cjs/keys.js +61 -0
  35. package/dist/cjs/keys.js.map +7 -0
  36. package/dist/cjs/session-key.d.ts +20 -14
  37. package/dist/cjs/session-key.js +90 -23
  38. package/dist/cjs/session-key.js.map +2 -2
  39. package/dist/cjs/types.d.ts +1 -86
  40. package/dist/cjs/types.js +0 -32
  41. package/dist/cjs/types.js.map +2 -2
  42. package/dist/cjs/utils.d.ts +2 -1
  43. package/dist/cjs/utils.js +17 -5
  44. package/dist/cjs/utils.js.map +2 -2
  45. package/dist/cjs/version.d.ts +1 -0
  46. package/dist/cjs/version.js +25 -0
  47. package/dist/cjs/version.js.map +7 -0
  48. package/dist/esm/bcs.d.ts +132 -0
  49. package/dist/esm/bcs.js +47 -0
  50. package/dist/esm/bcs.js.map +7 -0
  51. package/dist/esm/client.d.ts +83 -0
  52. package/dist/esm/client.js +268 -0
  53. package/dist/esm/client.js.map +7 -0
  54. package/dist/esm/decrypt.d.ts +15 -0
  55. package/dist/esm/decrypt.js +74 -0
  56. package/dist/esm/decrypt.js.map +7 -0
  57. package/dist/esm/dem.d.ts +36 -0
  58. package/dist/esm/dem.js +153 -0
  59. package/dist/esm/dem.js.map +7 -0
  60. package/dist/esm/elgamal.js.map +2 -2
  61. package/dist/esm/encrypt.d.ts +14 -5
  62. package/dist/esm/encrypt.js +49 -19
  63. package/dist/esm/encrypt.js.map +3 -3
  64. package/dist/esm/error.d.ts +58 -0
  65. package/dist/esm/error.js +155 -0
  66. package/dist/esm/error.js.map +7 -0
  67. package/dist/esm/ibe.d.ts +13 -17
  68. package/dist/esm/ibe.js +25 -28
  69. package/dist/esm/ibe.js.map +2 -2
  70. package/dist/esm/index.d.ts +3 -5
  71. package/dist/esm/index.js +5 -16
  72. package/dist/esm/index.js.map +2 -2
  73. package/dist/esm/kdf.d.ts +7 -2
  74. package/dist/esm/kdf.js +37 -2
  75. package/dist/esm/kdf.js.map +3 -3
  76. package/dist/esm/key-server.d.ts +5 -5
  77. package/dist/esm/key-server.js +29 -21
  78. package/dist/esm/key-server.js.map +2 -2
  79. package/dist/esm/keys.d.ts +17 -0
  80. package/dist/esm/keys.js +41 -0
  81. package/dist/esm/keys.js.map +7 -0
  82. package/dist/esm/session-key.d.ts +20 -14
  83. package/dist/esm/session-key.js +95 -24
  84. package/dist/esm/session-key.js.map +2 -2
  85. package/dist/esm/types.d.ts +1 -86
  86. package/dist/esm/types.js +0 -28
  87. package/dist/esm/types.js.map +3 -3
  88. package/dist/esm/utils.d.ts +2 -1
  89. package/dist/esm/utils.js +17 -5
  90. package/dist/esm/utils.js.map +2 -2
  91. package/dist/esm/version.d.ts +1 -0
  92. package/dist/esm/version.js +5 -0
  93. package/dist/esm/version.js.map +7 -0
  94. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  95. package/dist/tsconfig.tsbuildinfo +1 -1
  96. package/package.json +6 -5
  97. package/dist/cjs/aes.d.ts +0 -18
  98. package/dist/cjs/aes.js +0 -111
  99. package/dist/cjs/aes.js.map +0 -7
  100. package/dist/cjs/key-store.d.ts +0 -49
  101. package/dist/cjs/key-store.js +0 -203
  102. package/dist/cjs/key-store.js.map +0 -7
  103. package/dist/esm/aes.d.ts +0 -18
  104. package/dist/esm/aes.js +0 -91
  105. package/dist/esm/aes.js.map +0 -7
  106. package/dist/esm/key-store.d.ts +0 -49
  107. package/dist/esm/key-store.js +0 -183
  108. package/dist/esm/key-store.js.map +0 -7
package/dist/cjs/utils.js CHANGED
@@ -19,21 +19,33 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var utils_exports = {};
20
20
  __export(utils_exports, {
21
21
  createFullId: () => createFullId,
22
- xor: () => xor
22
+ xor: () => xor,
23
+ xorUnchecked: () => xorUnchecked
23
24
  });
24
25
  module.exports = __toCommonJS(utils_exports);
26
+ var import_bcs = require("@mysten/bcs");
27
+ var import_utils = require("@mysten/sui/utils");
28
+ var import_error = require("./error.js");
25
29
  function xor(a, b) {
26
30
  if (a.length !== b.length) {
27
31
  throw new Error("Invalid input");
28
32
  }
33
+ return xorUnchecked(a, b);
34
+ }
35
+ function xorUnchecked(a, b) {
29
36
  return a.map((ai, i) => ai ^ b[i]);
30
37
  }
31
38
  function createFullId(dst, packageId, innerId) {
32
- const fullId = new Uint8Array(1 + dst.length + packageId.length + innerId.length);
39
+ if (!(0, import_utils.isValidSuiObjectId)(packageId)) {
40
+ throw new import_error.UserError(`Invalid package ID ${packageId}`);
41
+ }
42
+ const packageIdBytes = (0, import_bcs.fromHex)(packageId);
43
+ const innerIdBytes = (0, import_bcs.fromHex)(innerId);
44
+ const fullId = new Uint8Array(1 + dst.length + packageIdBytes.length + innerIdBytes.length);
33
45
  fullId.set([dst.length], 0);
34
46
  fullId.set(dst, 1);
35
- fullId.set(packageId, 1 + dst.length);
36
- fullId.set(innerId, 1 + dst.length + packageId.length);
37
- return fullId;
47
+ fullId.set(packageIdBytes, 1 + dst.length);
48
+ fullId.set(innerIdBytes, 1 + dst.length + packageIdBytes.length);
49
+ return (0, import_bcs.toHex)(fullId);
38
50
  }
39
51
  //# sourceMappingURL=utils.js.map
@@ -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\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 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(\n\tdst: Uint8Array,\n\tpackageId: Uint8Array,\n\tinnerId: Uint8Array,\n): Uint8Array {\n\tconst fullId = new Uint8Array(1 + dst.length + packageId.length + innerId.length);\n\tfullId.set([dst.length], 0);\n\tfullId.set(dst, 1);\n\tfullId.set(packageId, 1 + dst.length);\n\tfullId.set(innerId, 1 + dst.length + packageId.length);\n\treturn fullId;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,IAAI,GAAe,GAA2B;AAC7D,MAAI,EAAE,WAAW,EAAE,QAAQ;AAC1B,UAAM,IAAI,MAAM,eAAe;AAAA,EAChC;AACA,SAAO,EAAE,IAAI,CAAC,IAAI,MAAM,KAAK,EAAE,CAAC,CAAC;AAClC;AASO,SAAS,aACf,KACA,WACA,SACa;AACb,QAAM,SAAS,IAAI,WAAW,IAAI,IAAI,SAAS,UAAU,SAAS,QAAQ,MAAM;AAChF,SAAO,IAAI,CAAC,IAAI,MAAM,GAAG,CAAC;AAC1B,SAAO,IAAI,KAAK,CAAC;AACjB,SAAO,IAAI,WAAW,IAAI,IAAI,MAAM;AACpC,SAAO,IAAI,SAAS,IAAI,IAAI,SAAS,UAAU,MAAM;AACrD,SAAO;AACR;",
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 packageIdBytes = fromHex(packageId);\n\tconst innerIdBytes = fromHex(innerId);\n\tconst fullId = new Uint8Array(1 + dst.length + packageIdBytes.length + innerIdBytes.length);\n\tfullId.set([dst.length], 0);\n\tfullId.set(dst, 1);\n\tfullId.set(packageIdBytes, 1 + dst.length);\n\tfullId.set(innerIdBytes, 1 + dst.length + packageIdBytes.length);\n\treturn toHex(fullId);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;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,qBAAiB,oBAAQ,SAAS;AACxC,QAAM,mBAAe,oBAAQ,OAAO;AACpC,QAAM,SAAS,IAAI,WAAW,IAAI,IAAI,SAAS,eAAe,SAAS,aAAa,MAAM;AAC1F,SAAO,IAAI,CAAC,IAAI,MAAM,GAAG,CAAC;AAC1B,SAAO,IAAI,KAAK,CAAC;AACjB,SAAO,IAAI,gBAAgB,IAAI,IAAI,MAAM;AACzC,SAAO,IAAI,cAAc,IAAI,IAAI,SAAS,eAAe,MAAM;AAC/D,aAAO,kBAAM,MAAM;AACpB;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1 @@
1
+ export declare const PACKAGE_VERSION = "0.3.1";
@@ -0,0 +1,25 @@
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 version_exports = {};
20
+ __export(version_exports, {
21
+ PACKAGE_VERSION: () => PACKAGE_VERSION
22
+ });
23
+ module.exports = __toCommonJS(version_exports);
24
+ const PACKAGE_VERSION = "0.3.1";
25
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 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.3.1';\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,MAAM,kBAAkB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,132 @@
1
+ export declare const IBEEncryptions: import("@mysten/bcs").BcsType<{
2
+ BonehFranklinBLS12381: {
3
+ nonce: Uint8Array<ArrayBufferLike>;
4
+ encryptedShares: Uint8Array<ArrayBufferLike>[];
5
+ encryptedRandomness: Uint8Array<ArrayBufferLike>;
6
+ };
7
+ $kind: "BonehFranklinBLS12381";
8
+ }, {
9
+ BonehFranklinBLS12381: {
10
+ nonce: Iterable<number>;
11
+ encryptedShares: Iterable<Iterable<number>> & {
12
+ length: number;
13
+ };
14
+ encryptedRandomness: Iterable<number>;
15
+ };
16
+ }>;
17
+ export declare const Ciphertext: import("@mysten/bcs").BcsType<import("@mysten/bcs").EnumOutputShapeWithKeys<{
18
+ Aes256Gcm: {
19
+ blob: number[];
20
+ aad: number[] | null;
21
+ };
22
+ Hmac256Ctr: {
23
+ blob: number[];
24
+ aad: number[] | null;
25
+ mac: Uint8Array<ArrayBufferLike>;
26
+ };
27
+ Plain: {};
28
+ }, "Aes256Gcm" | "Hmac256Ctr" | "Plain">, import("@mysten/bcs").EnumInputShape<{
29
+ Aes256Gcm: {
30
+ blob: Iterable<number> & {
31
+ length: number;
32
+ };
33
+ aad: (Iterable<number> & {
34
+ length: number;
35
+ }) | null | undefined;
36
+ };
37
+ Hmac256Ctr: {
38
+ blob: Iterable<number> & {
39
+ length: number;
40
+ };
41
+ aad: (Iterable<number> & {
42
+ length: number;
43
+ }) | null | undefined;
44
+ mac: Iterable<number>;
45
+ };
46
+ Plain: {};
47
+ }>>;
48
+ /**
49
+ * The encrypted object format. Should be aligned with the Rust implementation.
50
+ */
51
+ export declare const EncryptedObject: import("@mysten/bcs").BcsType<{
52
+ version: number;
53
+ packageId: string;
54
+ id: string;
55
+ services: [string, number][];
56
+ threshold: number;
57
+ encryptedShares: {
58
+ BonehFranklinBLS12381: {
59
+ nonce: Uint8Array<ArrayBufferLike>;
60
+ encryptedShares: Uint8Array<ArrayBufferLike>[];
61
+ encryptedRandomness: Uint8Array<ArrayBufferLike>;
62
+ };
63
+ $kind: "BonehFranklinBLS12381";
64
+ };
65
+ ciphertext: import("@mysten/bcs").EnumOutputShapeWithKeys<{
66
+ Aes256Gcm: {
67
+ blob: number[];
68
+ aad: number[] | null;
69
+ };
70
+ Hmac256Ctr: {
71
+ blob: number[];
72
+ aad: number[] | null;
73
+ mac: Uint8Array<ArrayBufferLike>;
74
+ };
75
+ Plain: {};
76
+ }, "Aes256Gcm" | "Hmac256Ctr" | "Plain">;
77
+ }, {
78
+ version: number;
79
+ packageId: string | Uint8Array<ArrayBufferLike>;
80
+ id: string;
81
+ services: Iterable<readonly [string | Uint8Array<ArrayBufferLike>, number]> & {
82
+ length: number;
83
+ };
84
+ threshold: number;
85
+ encryptedShares: {
86
+ BonehFranklinBLS12381: {
87
+ nonce: Iterable<number>;
88
+ encryptedShares: Iterable<Iterable<number>> & {
89
+ length: number;
90
+ };
91
+ encryptedRandomness: Iterable<number>;
92
+ };
93
+ };
94
+ ciphertext: import("@mysten/bcs").EnumInputShape<{
95
+ Aes256Gcm: {
96
+ blob: Iterable<number> & {
97
+ length: number;
98
+ };
99
+ aad: (Iterable<number> & {
100
+ length: number;
101
+ }) | null | undefined;
102
+ };
103
+ Hmac256Ctr: {
104
+ blob: Iterable<number> & {
105
+ length: number;
106
+ };
107
+ aad: (Iterable<number> & {
108
+ length: number;
109
+ }) | null | undefined;
110
+ mac: Iterable<number>;
111
+ };
112
+ Plain: {};
113
+ }>;
114
+ }>;
115
+ /**
116
+ * The Move struct for the KeyServer object.
117
+ */
118
+ export declare const KeyServerMove: import("@mysten/bcs").BcsType<{
119
+ id: string;
120
+ name: string;
121
+ url: string;
122
+ keyType: number;
123
+ pk: number[];
124
+ }, {
125
+ id: string | Uint8Array<ArrayBufferLike>;
126
+ name: string;
127
+ url: string;
128
+ keyType: number;
129
+ pk: Iterable<number> & {
130
+ length: number;
131
+ };
132
+ }>;
@@ -0,0 +1,47 @@
1
+ import { fromHex, toHex } from "@mysten/bcs";
2
+ import { bcs } from "@mysten/sui/bcs";
3
+ const IBEEncryptions = bcs.enum("IBEEncryptions", {
4
+ BonehFranklinBLS12381: bcs.struct("BonehFranklinBLS12381", {
5
+ nonce: bcs.bytes(96),
6
+ encryptedShares: bcs.vector(bcs.bytes(32)),
7
+ encryptedRandomness: bcs.bytes(32)
8
+ })
9
+ });
10
+ const Ciphertext = bcs.enum("Ciphertext", {
11
+ Aes256Gcm: bcs.struct("Aes256Gcm", {
12
+ blob: bcs.vector(bcs.U8),
13
+ aad: bcs.option(bcs.vector(bcs.U8))
14
+ }),
15
+ Hmac256Ctr: bcs.struct("Hmac256Ctr", {
16
+ blob: bcs.vector(bcs.U8),
17
+ aad: bcs.option(bcs.vector(bcs.U8)),
18
+ mac: bcs.bytes(32)
19
+ }),
20
+ Plain: bcs.struct("Plain", {})
21
+ });
22
+ const EncryptedObject = bcs.struct("EncryptedObject", {
23
+ version: bcs.U8,
24
+ packageId: bcs.Address,
25
+ id: bcs.vector(bcs.U8).transform({
26
+ output: (val) => toHex(new Uint8Array(val)),
27
+ input: (val) => fromHex(val)
28
+ }),
29
+ services: bcs.vector(bcs.tuple([bcs.Address, bcs.U8])),
30
+ threshold: bcs.U8,
31
+ encryptedShares: IBEEncryptions,
32
+ ciphertext: Ciphertext
33
+ });
34
+ const KeyServerMove = bcs.struct("KeyServer", {
35
+ id: bcs.Address,
36
+ name: bcs.string(),
37
+ url: bcs.string(),
38
+ keyType: bcs.u8(),
39
+ pk: bcs.vector(bcs.u8())
40
+ });
41
+ export {
42
+ Ciphertext,
43
+ EncryptedObject,
44
+ IBEEncryptions,
45
+ KeyServerMove
46
+ };
47
+ //# sourceMappingURL=bcs.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/bcs.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromHex, toHex } from '@mysten/bcs';\nimport { bcs } from '@mysten/sui/bcs';\n\nexport const IBEEncryptions = bcs.enum('IBEEncryptions', {\n\tBonehFranklinBLS12381: bcs.struct('BonehFranklinBLS12381', {\n\t\tnonce: bcs.bytes(96),\n\t\tencryptedShares: bcs.vector(bcs.bytes(32)),\n\t\tencryptedRandomness: bcs.bytes(32),\n\t}),\n});\n\nexport const Ciphertext = bcs.enum('Ciphertext', {\n\tAes256Gcm: bcs.struct('Aes256Gcm', {\n\t\tblob: bcs.vector(bcs.U8),\n\t\taad: bcs.option(bcs.vector(bcs.U8)),\n\t}),\n\tHmac256Ctr: bcs.struct('Hmac256Ctr', {\n\t\tblob: bcs.vector(bcs.U8),\n\t\taad: bcs.option(bcs.vector(bcs.U8)),\n\t\tmac: bcs.bytes(32),\n\t}),\n\tPlain: bcs.struct('Plain', {}),\n});\n\n/**\n * The encrypted object format. Should be aligned with the Rust implementation.\n */\nexport const EncryptedObject = bcs.struct('EncryptedObject', {\n\tversion: bcs.U8,\n\tpackageId: bcs.Address,\n\tid: bcs.vector(bcs.U8).transform({\n\t\toutput: (val) => toHex(new Uint8Array(val)),\n\t\tinput: (val: string) => fromHex(val),\n\t}),\n\tservices: bcs.vector(bcs.tuple([bcs.Address, bcs.U8])),\n\tthreshold: bcs.U8,\n\tencryptedShares: IBEEncryptions,\n\tciphertext: Ciphertext,\n});\n\n/**\n * The Move struct for the KeyServer object.\n */\nexport const KeyServerMove = bcs.struct('KeyServer', {\n\tid: bcs.Address,\n\tname: bcs.string(),\n\turl: bcs.string(),\n\tkeyType: bcs.u8(),\n\tpk: bcs.vector(bcs.u8()),\n});\n"],
5
+ "mappings": "AAGA,SAAS,SAAS,aAAa;AAC/B,SAAS,WAAW;AAEb,MAAM,iBAAiB,IAAI,KAAK,kBAAkB;AAAA,EACxD,uBAAuB,IAAI,OAAO,yBAAyB;AAAA,IAC1D,OAAO,IAAI,MAAM,EAAE;AAAA,IACnB,iBAAiB,IAAI,OAAO,IAAI,MAAM,EAAE,CAAC;AAAA,IACzC,qBAAqB,IAAI,MAAM,EAAE;AAAA,EAClC,CAAC;AACF,CAAC;AAEM,MAAM,aAAa,IAAI,KAAK,cAAc;AAAA,EAChD,WAAW,IAAI,OAAO,aAAa;AAAA,IAClC,MAAM,IAAI,OAAO,IAAI,EAAE;AAAA,IACvB,KAAK,IAAI,OAAO,IAAI,OAAO,IAAI,EAAE,CAAC;AAAA,EACnC,CAAC;AAAA,EACD,YAAY,IAAI,OAAO,cAAc;AAAA,IACpC,MAAM,IAAI,OAAO,IAAI,EAAE;AAAA,IACvB,KAAK,IAAI,OAAO,IAAI,OAAO,IAAI,EAAE,CAAC;AAAA,IAClC,KAAK,IAAI,MAAM,EAAE;AAAA,EAClB,CAAC;AAAA,EACD,OAAO,IAAI,OAAO,SAAS,CAAC,CAAC;AAC9B,CAAC;AAKM,MAAM,kBAAkB,IAAI,OAAO,mBAAmB;AAAA,EAC5D,SAAS,IAAI;AAAA,EACb,WAAW,IAAI;AAAA,EACf,IAAI,IAAI,OAAO,IAAI,EAAE,EAAE,UAAU;AAAA,IAChC,QAAQ,CAAC,QAAQ,MAAM,IAAI,WAAW,GAAG,CAAC;AAAA,IAC1C,OAAO,CAAC,QAAgB,QAAQ,GAAG;AAAA,EACpC,CAAC;AAAA,EACD,UAAU,IAAI,OAAO,IAAI,MAAM,CAAC,IAAI,SAAS,IAAI,EAAE,CAAC,CAAC;AAAA,EACrD,WAAW,IAAI;AAAA,EACf,iBAAiB;AAAA,EACjB,YAAY;AACb,CAAC;AAKM,MAAM,gBAAgB,IAAI,OAAO,aAAa;AAAA,EACpD,IAAI,IAAI;AAAA,EACR,MAAM,IAAI,OAAO;AAAA,EACjB,KAAK,IAAI,OAAO;AAAA,EAChB,SAAS,IAAI,GAAG;AAAA,EAChB,IAAI,IAAI,OAAO,IAAI,GAAG,CAAC;AACxB,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,83 @@
1
+ import type { SuiClient } from '@mysten/sui/client';
2
+ import { DemType, KemType } from './encrypt.js';
3
+ import type { KeyServer } from './key-server.js';
4
+ import type { SessionKey } from './session-key.js';
5
+ /**
6
+ * Configuration options for initializing a SealClient
7
+ * @property serverObjectIds: Array of object IDs for the key servers to use.
8
+ * @property verifyKeyServers: Whether to verify the key servers' authenticity.
9
+ * Should be false if servers are pre-verified (e.g., getAllowlistedKeyServers).
10
+ * Defaults to true.
11
+ * @property timeout: Timeout in milliseconds for network requests. Defaults to 10 seconds.
12
+ */
13
+ export interface SealClientOptions {
14
+ suiClient: SuiClient;
15
+ serverObjectIds: string[];
16
+ verifyKeyServers?: boolean;
17
+ timeout?: number;
18
+ }
19
+ export declare class SealClient {
20
+ #private;
21
+ constructor(options: SealClientOptions);
22
+ /**
23
+ * Return an encrypted message under the identity.
24
+ *
25
+ * @param kemType - The type of KEM to use.
26
+ * @param demType - The type of DEM to use.
27
+ * @param threshold - The threshold for the TSS encryption.
28
+ * @param packageId - the packageId namespace.
29
+ * @param id - the identity to use.
30
+ * @param data - the data to encrypt.
31
+ * @param aad - optional additional authenticated data.
32
+ * @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.
33
+ * Since the symmetric key can be used to decrypt, it should not be shared but can be used e.g. for backup.
34
+ */
35
+ encrypt({ kemType, demType, threshold, packageId, id, data, aad, }: {
36
+ kemType?: KemType;
37
+ demType?: DemType;
38
+ threshold: number;
39
+ packageId: string;
40
+ id: string;
41
+ data: Uint8Array;
42
+ aad?: Uint8Array;
43
+ }): Promise<{
44
+ encryptedObject: Uint8Array;
45
+ key: Uint8Array;
46
+ }>;
47
+ /**
48
+ * Decrypt the given encrypted bytes using cached keys.
49
+ * Calls fetchKeys in case one or more of the required keys is not cached yet.
50
+ * The function throws an error if the client's key servers are not a subset of
51
+ * the encrypted object's key servers (including the same weights) or if the
52
+ * threshold cannot be met.
53
+ *
54
+ * @param data - The encrypted bytes to decrypt.
55
+ * @param sessionKey - The session key to use.
56
+ * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
57
+ * @returns - The decrypted plaintext corresponding to ciphertext.
58
+ */
59
+ decrypt({ data, sessionKey, txBytes, }: {
60
+ data: Uint8Array;
61
+ sessionKey: SessionKey;
62
+ txBytes: Uint8Array;
63
+ }): Promise<Uint8Array<ArrayBufferLike>>;
64
+ getKeyServers(): Promise<KeyServer[]>;
65
+ /**
66
+ * Fetch keys from the key servers and update the cache.
67
+ *
68
+ * It is recommended to call this function once for all ids of all encrypted obejcts if
69
+ * there are multiple, then call decrypt for each object. This avoids calling fetchKey
70
+ * individually for each decrypt.
71
+ *
72
+ * @param ids - The ids of the encrypted objects.
73
+ * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
74
+ * @param sessionKey - The session key to use.
75
+ * @param threshold - The threshold for the TSS encryptions. The function returns when a threshold of key servers had returned keys for all ids.
76
+ */
77
+ fetchKeys({ ids, txBytes, sessionKey, threshold, }: {
78
+ ids: string[];
79
+ txBytes: Uint8Array;
80
+ sessionKey: SessionKey;
81
+ threshold: number;
82
+ }): Promise<void>;
83
+ }
@@ -0,0 +1,268 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+ var _suiClient, _serverObjectIds, _verifyKeyServers, _keyServers, _cachedKeys, _timeout, _SealClient_instances, createEncryptionInput_fn, validateEncryptionServices_fn, loadKeyServers_fn;
10
+ import { EncryptedObject } from "./bcs.js";
11
+ import { G1Element, G2Element } from "./bls12381.js";
12
+ import { decrypt } from "./decrypt.js";
13
+ import { AesGcm256, Hmac256Ctr } from "./dem.js";
14
+ import { DemType, encrypt, KemType } from "./encrypt.js";
15
+ import {
16
+ InconsistentKeyServersError,
17
+ InvalidKeyServerError,
18
+ InvalidThresholdError,
19
+ toMajorityError
20
+ } from "./error.js";
21
+ import { BonehFranklinBLS12381Services, DST } from "./ibe.js";
22
+ import { KeyServerType, retrieveKeyServers, verifyKeyServer } from "./key-server.js";
23
+ import { fetchKeysForAllIds } from "./keys.js";
24
+ import { createFullId } from "./utils.js";
25
+ class SealClient {
26
+ constructor(options) {
27
+ __privateAdd(this, _SealClient_instances);
28
+ __privateAdd(this, _suiClient);
29
+ __privateAdd(this, _serverObjectIds);
30
+ __privateAdd(this, _verifyKeyServers);
31
+ __privateAdd(this, _keyServers, null);
32
+ // A caching map for: fullId:object_id -> partial key.
33
+ __privateAdd(this, _cachedKeys, /* @__PURE__ */ new Map());
34
+ __privateAdd(this, _timeout);
35
+ __privateSet(this, _suiClient, options.suiClient);
36
+ __privateSet(this, _serverObjectIds, options.serverObjectIds);
37
+ __privateSet(this, _verifyKeyServers, options.verifyKeyServers ?? true);
38
+ __privateSet(this, _timeout, options.timeout ?? 1e4);
39
+ }
40
+ /**
41
+ * Return an encrypted message under the identity.
42
+ *
43
+ * @param kemType - The type of KEM to use.
44
+ * @param demType - The type of DEM to use.
45
+ * @param threshold - The threshold for the TSS encryption.
46
+ * @param packageId - the packageId namespace.
47
+ * @param id - the identity to use.
48
+ * @param data - the data to encrypt.
49
+ * @param aad - optional additional authenticated data.
50
+ * @returns The bcs bytes of the encrypted object containing all metadata and the 256-bit symmetric key that was used to encrypt the object.
51
+ * Since the symmetric key can be used to decrypt, it should not be shared but can be used e.g. for backup.
52
+ */
53
+ async encrypt({
54
+ kemType = KemType.BonehFranklinBLS12381DemCCA,
55
+ demType = DemType.AesGcm256,
56
+ threshold,
57
+ packageId,
58
+ id,
59
+ data,
60
+ aad = new Uint8Array()
61
+ }) {
62
+ return encrypt({
63
+ keyServers: await this.getKeyServers(),
64
+ kemType,
65
+ threshold,
66
+ packageId,
67
+ id,
68
+ encryptionInput: __privateMethod(this, _SealClient_instances, createEncryptionInput_fn).call(this, demType, data, aad)
69
+ });
70
+ }
71
+ /**
72
+ * Decrypt the given encrypted bytes using cached keys.
73
+ * Calls fetchKeys in case one or more of the required keys is not cached yet.
74
+ * The function throws an error if the client's key servers are not a subset of
75
+ * the encrypted object's key servers (including the same weights) or if the
76
+ * threshold cannot be met.
77
+ *
78
+ * @param data - The encrypted bytes to decrypt.
79
+ * @param sessionKey - The session key to use.
80
+ * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
81
+ * @returns - The decrypted plaintext corresponding to ciphertext.
82
+ */
83
+ async decrypt({
84
+ data,
85
+ sessionKey,
86
+ txBytes
87
+ }) {
88
+ const encryptedObject = EncryptedObject.parse(data);
89
+ __privateMethod(this, _SealClient_instances, validateEncryptionServices_fn).call(this, encryptedObject.services.map((s) => s[0]), encryptedObject.threshold);
90
+ await this.fetchKeys({
91
+ ids: [encryptedObject.id],
92
+ txBytes,
93
+ sessionKey,
94
+ threshold: encryptedObject.threshold
95
+ });
96
+ return decrypt({ encryptedObject, keys: __privateGet(this, _cachedKeys) });
97
+ }
98
+ async getKeyServers() {
99
+ if (!__privateGet(this, _keyServers)) {
100
+ __privateSet(this, _keyServers, __privateMethod(this, _SealClient_instances, loadKeyServers_fn).call(this).catch((error) => {
101
+ __privateSet(this, _keyServers, null);
102
+ throw error;
103
+ }));
104
+ }
105
+ return __privateGet(this, _keyServers);
106
+ }
107
+ /**
108
+ * Fetch keys from the key servers and update the cache.
109
+ *
110
+ * It is recommended to call this function once for all ids of all encrypted obejcts if
111
+ * there are multiple, then call decrypt for each object. This avoids calling fetchKey
112
+ * individually for each decrypt.
113
+ *
114
+ * @param ids - The ids of the encrypted objects.
115
+ * @param txBytes - The transaction bytes to use (that calls seal_approve* functions).
116
+ * @param sessionKey - The session key to use.
117
+ * @param threshold - The threshold for the TSS encryptions. The function returns when a threshold of key servers had returned keys for all ids.
118
+ */
119
+ async fetchKeys({
120
+ ids,
121
+ txBytes,
122
+ sessionKey,
123
+ threshold
124
+ }) {
125
+ const keyServers = await this.getKeyServers();
126
+ if (threshold > keyServers.length || threshold < 1 || keyServers.length < 1) {
127
+ throw new InvalidThresholdError(
128
+ `Invalid threshold ${threshold} for ${keyServers.length} servers`
129
+ );
130
+ }
131
+ let completedServerCount = 0;
132
+ const remainingKeyServers = /* @__PURE__ */ new Set();
133
+ const fullIds = ids.map((id) => createFullId(DST, sessionKey.getPackageId(), id));
134
+ for (const server of keyServers) {
135
+ let hasAllKeys = true;
136
+ for (const fullId of fullIds) {
137
+ if (!__privateGet(this, _cachedKeys).has(`${fullId}:${server.objectId}`)) {
138
+ hasAllKeys = false;
139
+ remainingKeyServers.add(server);
140
+ break;
141
+ }
142
+ }
143
+ if (hasAllKeys) {
144
+ completedServerCount++;
145
+ }
146
+ }
147
+ if (completedServerCount >= threshold) {
148
+ return;
149
+ }
150
+ for (const server of remainingKeyServers) {
151
+ if (server.keyType !== KeyServerType.BonehFranklinBLS12381) {
152
+ throw new InvalidKeyServerError(
153
+ `Server ${server.objectId} has invalid key type: ${server.keyType}`
154
+ );
155
+ }
156
+ }
157
+ const cert = await sessionKey.getCertificate();
158
+ const signedRequest = await sessionKey.createRequestParams(txBytes);
159
+ const controller = new AbortController();
160
+ const errors = [];
161
+ const keyFetches = [...remainingKeyServers].map(async (server) => {
162
+ try {
163
+ const allKeys = await fetchKeysForAllIds(
164
+ server.url,
165
+ signedRequest.requestSignature,
166
+ txBytes,
167
+ signedRequest.decryptionKey,
168
+ cert,
169
+ __privateGet(this, _timeout),
170
+ controller.signal
171
+ );
172
+ let receivedIds = /* @__PURE__ */ new Set();
173
+ for (const { fullId, key } of allKeys) {
174
+ const keyElement = G1Element.fromBytes(key);
175
+ if (!BonehFranklinBLS12381Services.verifyUserSecretKey(
176
+ keyElement,
177
+ fullId,
178
+ G2Element.fromBytes(server.pk)
179
+ )) {
180
+ console.warn("Received invalid key from key server " + server.objectId);
181
+ continue;
182
+ }
183
+ __privateGet(this, _cachedKeys).set(`${fullId}:${server.objectId}`, keyElement);
184
+ receivedIds.add(fullId);
185
+ }
186
+ const expectedIds = new Set(fullIds);
187
+ const hasAllKeys = receivedIds.size === expectedIds.size && [...receivedIds].every((id) => expectedIds.has(id));
188
+ if (hasAllKeys) {
189
+ completedServerCount++;
190
+ if (completedServerCount >= threshold) {
191
+ controller.abort();
192
+ }
193
+ }
194
+ } catch (error) {
195
+ if (!controller.signal.aborted) {
196
+ errors.push(error);
197
+ }
198
+ if (remainingKeyServers.size - errors.length < threshold - completedServerCount) {
199
+ controller.abort(error);
200
+ }
201
+ }
202
+ });
203
+ await Promise.allSettled(keyFetches);
204
+ if (completedServerCount < threshold) {
205
+ throw toMajorityError(errors);
206
+ }
207
+ }
208
+ }
209
+ _suiClient = new WeakMap();
210
+ _serverObjectIds = new WeakMap();
211
+ _verifyKeyServers = new WeakMap();
212
+ _keyServers = new WeakMap();
213
+ _cachedKeys = new WeakMap();
214
+ _timeout = new WeakMap();
215
+ _SealClient_instances = new WeakSet();
216
+ createEncryptionInput_fn = function(type, data, aad) {
217
+ switch (type) {
218
+ case DemType.AesGcm256:
219
+ return new AesGcm256(data, aad);
220
+ case DemType.Hmac256Ctr:
221
+ return new Hmac256Ctr(data, aad);
222
+ }
223
+ };
224
+ validateEncryptionServices_fn = function(services, threshold) {
225
+ const serverObjectIdsMap = /* @__PURE__ */ new Map();
226
+ for (const objectId of __privateGet(this, _serverObjectIds)) {
227
+ serverObjectIdsMap.set(objectId, (serverObjectIdsMap.get(objectId) ?? 0) + 1);
228
+ }
229
+ const servicesMap = /* @__PURE__ */ new Map();
230
+ for (const service of services) {
231
+ servicesMap.set(service, (servicesMap.get(service) ?? 0) + 1);
232
+ }
233
+ for (const [objectId, count] of serverObjectIdsMap) {
234
+ if (servicesMap.get(objectId) !== count) {
235
+ throw new InconsistentKeyServersError(
236
+ `Client's key servers must be a subset of the encrypted object's key servers`
237
+ );
238
+ }
239
+ }
240
+ if (threshold > __privateGet(this, _serverObjectIds).length) {
241
+ throw new InvalidThresholdError(
242
+ `Invalid threshold ${threshold} for ${__privateGet(this, _serverObjectIds).length} servers`
243
+ );
244
+ }
245
+ };
246
+ loadKeyServers_fn = async function() {
247
+ const keyServers = await retrieveKeyServers({
248
+ objectIds: __privateGet(this, _serverObjectIds),
249
+ client: __privateGet(this, _suiClient)
250
+ });
251
+ if (keyServers.length === 0) {
252
+ throw new InvalidKeyServerError("No key servers found");
253
+ }
254
+ if (__privateGet(this, _verifyKeyServers)) {
255
+ await Promise.all(
256
+ keyServers.map(async (server) => {
257
+ if (!await verifyKeyServer(server, __privateGet(this, _timeout))) {
258
+ throw new InvalidKeyServerError(`Key server ${server.objectId} is not valid`);
259
+ }
260
+ })
261
+ );
262
+ }
263
+ return keyServers;
264
+ };
265
+ export {
266
+ SealClient
267
+ };
268
+ //# sourceMappingURL=client.js.map