@digitaldefiance/node-ecies-lib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/LICENSE +21 -0
  2. package/dist/constants.d.ts +29 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/constants.js +129 -0
  5. package/dist/constants.js.map +1 -0
  6. package/dist/enumerations/index.d.ts +2 -0
  7. package/dist/enumerations/index.d.ts.map +1 -0
  8. package/dist/enumerations/index.js +2 -0
  9. package/dist/enumerations/index.js.map +1 -0
  10. package/dist/enumerations/pbkdf2-profile.d.ts +8 -0
  11. package/dist/enumerations/pbkdf2-profile.d.ts.map +1 -0
  12. package/dist/enumerations/pbkdf2-profile.js +9 -0
  13. package/dist/enumerations/pbkdf2-profile.js.map +1 -0
  14. package/dist/index.d.ts +6 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +6 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/interfaces/authenticated-cipher.d.ts +10 -0
  19. package/dist/interfaces/authenticated-cipher.d.ts.map +1 -0
  20. package/dist/interfaces/authenticated-cipher.js +2 -0
  21. package/dist/interfaces/authenticated-cipher.js.map +1 -0
  22. package/dist/interfaces/authenticated-decipher.d.ts +9 -0
  23. package/dist/interfaces/authenticated-decipher.d.ts.map +1 -0
  24. package/dist/interfaces/authenticated-decipher.js +2 -0
  25. package/dist/interfaces/authenticated-decipher.js.map +1 -0
  26. package/dist/interfaces/checksum-config.d.ts +5 -0
  27. package/dist/interfaces/checksum-config.d.ts.map +1 -0
  28. package/dist/interfaces/checksum-config.js +2 -0
  29. package/dist/interfaces/checksum-config.js.map +1 -0
  30. package/dist/interfaces/checksum-consts.d.ts +11 -0
  31. package/dist/interfaces/checksum-consts.d.ts.map +1 -0
  32. package/dist/interfaces/checksum-consts.js +2 -0
  33. package/dist/interfaces/checksum-consts.js.map +1 -0
  34. package/dist/interfaces/constants.d.ts +42 -0
  35. package/dist/interfaces/constants.d.ts.map +1 -0
  36. package/dist/interfaces/constants.js +2 -0
  37. package/dist/interfaces/constants.js.map +1 -0
  38. package/dist/interfaces/ecies-consts.d.ts +88 -0
  39. package/dist/interfaces/ecies-consts.d.ts.map +1 -0
  40. package/dist/interfaces/ecies-consts.js +2 -0
  41. package/dist/interfaces/ecies-consts.js.map +1 -0
  42. package/dist/interfaces/encryption-consts.d.ts +11 -0
  43. package/dist/interfaces/encryption-consts.d.ts.map +1 -0
  44. package/dist/interfaces/encryption-consts.js +2 -0
  45. package/dist/interfaces/encryption-consts.js.map +1 -0
  46. package/dist/interfaces/fec-consts.d.ts +5 -0
  47. package/dist/interfaces/fec-consts.d.ts.map +1 -0
  48. package/dist/interfaces/fec-consts.js +2 -0
  49. package/dist/interfaces/fec-consts.js.map +1 -0
  50. package/dist/interfaces/index.d.ts +18 -0
  51. package/dist/interfaces/index.d.ts.map +1 -0
  52. package/dist/interfaces/index.js +18 -0
  53. package/dist/interfaces/index.js.map +1 -0
  54. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.d.ts +6 -0
  55. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.d.ts.map +1 -0
  56. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.js +2 -0
  57. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.js.map +1 -0
  58. package/dist/interfaces/keyring-consts.d.ts +6 -0
  59. package/dist/interfaces/keyring-consts.d.ts.map +1 -0
  60. package/dist/interfaces/keyring-consts.js +2 -0
  61. package/dist/interfaces/keyring-consts.js.map +1 -0
  62. package/dist/interfaces/member-operational.d.ts +36 -0
  63. package/dist/interfaces/member-operational.d.ts.map +1 -0
  64. package/dist/interfaces/member-operational.js +2 -0
  65. package/dist/interfaces/member-operational.js.map +1 -0
  66. package/dist/interfaces/member-with-mnemonic.d.ts +7 -0
  67. package/dist/interfaces/member-with-mnemonic.d.ts.map +1 -0
  68. package/dist/interfaces/member-with-mnemonic.js +2 -0
  69. package/dist/interfaces/member-with-mnemonic.js.map +1 -0
  70. package/dist/interfaces/multi-encrypted-message.d.ts +8 -0
  71. package/dist/interfaces/multi-encrypted-message.d.ts.map +1 -0
  72. package/dist/interfaces/multi-encrypted-message.js +2 -0
  73. package/dist/interfaces/multi-encrypted-message.js.map +1 -0
  74. package/dist/interfaces/multi-encrypted-parsed-header.d.ts +24 -0
  75. package/dist/interfaces/multi-encrypted-parsed-header.d.ts.map +1 -0
  76. package/dist/interfaces/multi-encrypted-parsed-header.js +2 -0
  77. package/dist/interfaces/multi-encrypted-parsed-header.js.map +1 -0
  78. package/dist/interfaces/pbkdf-profiles.d.ts +6 -0
  79. package/dist/interfaces/pbkdf-profiles.d.ts.map +1 -0
  80. package/dist/interfaces/pbkdf-profiles.js +2 -0
  81. package/dist/interfaces/pbkdf-profiles.js.map +1 -0
  82. package/dist/interfaces/signing-key-private-key-info.d.ts +11 -0
  83. package/dist/interfaces/signing-key-private-key-info.d.ts.map +1 -0
  84. package/dist/interfaces/signing-key-private-key-info.js +2 -0
  85. package/dist/interfaces/signing-key-private-key-info.js.map +1 -0
  86. package/dist/interfaces/simple-keypair-buffer.d.ts +6 -0
  87. package/dist/interfaces/simple-keypair-buffer.d.ts.map +1 -0
  88. package/dist/interfaces/simple-keypair-buffer.js +2 -0
  89. package/dist/interfaces/simple-keypair-buffer.js.map +1 -0
  90. package/dist/interfaces/simple-keypair.d.ts +6 -0
  91. package/dist/interfaces/simple-keypair.d.ts.map +1 -0
  92. package/dist/interfaces/simple-keypair.js +2 -0
  93. package/dist/interfaces/simple-keypair.js.map +1 -0
  94. package/dist/interfaces/simple-public-key-only-buffer.d.ts +4 -0
  95. package/dist/interfaces/simple-public-key-only-buffer.d.ts.map +1 -0
  96. package/dist/interfaces/simple-public-key-only-buffer.js +2 -0
  97. package/dist/interfaces/simple-public-key-only-buffer.js.map +1 -0
  98. package/dist/interfaces/simple-public-key-only.d.ts +4 -0
  99. package/dist/interfaces/simple-public-key-only.d.ts.map +1 -0
  100. package/dist/interfaces/simple-public-key-only.js +2 -0
  101. package/dist/interfaces/simple-public-key-only.js.map +1 -0
  102. package/dist/interfaces/single-encrypted-parsed-header.d.ts +35 -0
  103. package/dist/interfaces/single-encrypted-parsed-header.d.ts.map +1 -0
  104. package/dist/interfaces/single-encrypted-parsed-header.js +2 -0
  105. package/dist/interfaces/single-encrypted-parsed-header.js.map +1 -0
  106. package/dist/interfaces/wallet-seed.d.ts +7 -0
  107. package/dist/interfaces/wallet-seed.d.ts.map +1 -0
  108. package/dist/interfaces/wallet-seed.js +2 -0
  109. package/dist/interfaces/wallet-seed.js.map +1 -0
  110. package/dist/interfaces/wrapped-key-consts.d.ts +7 -0
  111. package/dist/interfaces/wrapped-key-consts.d.ts.map +1 -0
  112. package/dist/interfaces/wrapped-key-consts.js +2 -0
  113. package/dist/interfaces/wrapped-key-consts.js.map +1 -0
  114. package/dist/member.d.ts +67 -0
  115. package/dist/member.d.ts.map +1 -0
  116. package/dist/member.js +256 -0
  117. package/dist/member.js.map +1 -0
  118. package/dist/services/ecies/crypto-core.d.ts +55 -0
  119. package/dist/services/ecies/crypto-core.d.ts.map +1 -0
  120. package/dist/services/ecies/crypto-core.debug.d.ts +13 -0
  121. package/dist/services/ecies/crypto-core.debug.d.ts.map +1 -0
  122. package/dist/services/ecies/crypto-core.debug.js +91 -0
  123. package/dist/services/ecies/crypto-core.debug.js.map +1 -0
  124. package/dist/services/ecies/crypto-core.js +115 -0
  125. package/dist/services/ecies/crypto-core.js.map +1 -0
  126. package/dist/services/ecies/debug-aes-gcm.d.ts +2 -0
  127. package/dist/services/ecies/debug-aes-gcm.d.ts.map +1 -0
  128. package/dist/services/ecies/debug-aes-gcm.js +106 -0
  129. package/dist/services/ecies/debug-aes-gcm.js.map +1 -0
  130. package/dist/services/ecies/debug-ecdh.d.ts +2 -0
  131. package/dist/services/ecies/debug-ecdh.d.ts.map +1 -0
  132. package/dist/services/ecies/debug-ecdh.js +123 -0
  133. package/dist/services/ecies/debug-ecdh.js.map +1 -0
  134. package/dist/services/ecies/debug-ecies-burnbag.d.ts +2 -0
  135. package/dist/services/ecies/debug-ecies-burnbag.d.ts.map +1 -0
  136. package/dist/services/ecies/debug-ecies-burnbag.js +134 -0
  137. package/dist/services/ecies/debug-ecies-burnbag.js.map +1 -0
  138. package/dist/services/ecies/debug-ecies.d.ts +2 -0
  139. package/dist/services/ecies/debug-ecies.d.ts.map +1 -0
  140. package/dist/services/ecies/debug-ecies.js +110 -0
  141. package/dist/services/ecies/debug-ecies.js.map +1 -0
  142. package/dist/services/ecies/debug-test.d.ts +2 -0
  143. package/dist/services/ecies/debug-test.d.ts.map +1 -0
  144. package/dist/services/ecies/debug-test.js +57 -0
  145. package/dist/services/ecies/debug-test.js.map +1 -0
  146. package/dist/services/ecies/file.d.ts +15 -0
  147. package/dist/services/ecies/file.d.ts.map +1 -0
  148. package/dist/services/ecies/file.js +104 -0
  149. package/dist/services/ecies/file.js.map +1 -0
  150. package/dist/services/ecies/index.d.ts +8 -0
  151. package/dist/services/ecies/index.d.ts.map +1 -0
  152. package/dist/services/ecies/index.js +8 -0
  153. package/dist/services/ecies/index.js.map +1 -0
  154. package/dist/services/ecies/multi-recipient.d.ts +83 -0
  155. package/dist/services/ecies/multi-recipient.d.ts.map +1 -0
  156. package/dist/services/ecies/multi-recipient.js +355 -0
  157. package/dist/services/ecies/multi-recipient.js.map +1 -0
  158. package/dist/services/ecies/service.d.ts +70 -0
  159. package/dist/services/ecies/service.d.ts.map +1 -0
  160. package/dist/services/ecies/service.js +145 -0
  161. package/dist/services/ecies/service.js.map +1 -0
  162. package/dist/services/ecies/signature.d.ts +38 -0
  163. package/dist/services/ecies/signature.d.ts.map +1 -0
  164. package/dist/services/ecies/signature.js +64 -0
  165. package/dist/services/ecies/signature.js.map +1 -0
  166. package/dist/services/ecies/single-recipient.d.ts +85 -0
  167. package/dist/services/ecies/single-recipient.d.ts.map +1 -0
  168. package/dist/services/ecies/single-recipient.js +390 -0
  169. package/dist/services/ecies/single-recipient.js.map +1 -0
  170. package/dist/services/ecies/utilities.d.ts +22 -0
  171. package/dist/services/ecies/utilities.d.ts.map +1 -0
  172. package/dist/services/ecies/utilities.js +65 -0
  173. package/dist/services/ecies/utilities.js.map +1 -0
  174. package/dist/services/index.d.ts +2 -0
  175. package/dist/services/index.d.ts.map +1 -0
  176. package/dist/services/index.js +2 -0
  177. package/dist/services/index.js.map +1 -0
  178. package/dist/types.d.ts +25 -0
  179. package/dist/types.d.ts.map +1 -0
  180. package/dist/types.js +2 -0
  181. package/dist/types.js.map +1 -0
  182. package/package.json +64 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/services/ecies/file.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AASzC,qBAAa,gBAAgB;IAKzB,OAAO,CAAC,YAAY;IACpB,OAAO,CAAC,cAAc;IALxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAe;IACjD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAM;gBAG/B,YAAY,EAAE,YAAY,EAC1B,cAAc,EAAE,MAAM;IAGhC,WAAW,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IAuB1C,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,GAAG,MAAM;IA4CzE,iBAAiB,CAAC,aAAa,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;IAwBlE,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,kBAAkB;CAoC3B"}
@@ -0,0 +1,104 @@
1
+ import * as fs from 'fs';
2
+ export class EciesFileService {
3
+ eciesService;
4
+ userPrivateKey;
5
+ static CHUNK_SIZE = 1024 * 1024; // 1MB chunks
6
+ static HEADER_SIZE = 20;
7
+ constructor(eciesService, userPrivateKey) {
8
+ this.eciesService = eciesService;
9
+ this.userPrivateKey = userPrivateKey;
10
+ }
11
+ decryptFile(encryptedData) {
12
+ const { header, chunks } = this.parseEncryptedFile(encryptedData);
13
+ const decryptedChunks = [];
14
+ for (const chunk of chunks) {
15
+ const decrypted = this.eciesService.decryptSimpleOrSingleWithHeader(false, this.userPrivateKey, chunk);
16
+ decryptedChunks.push(decrypted);
17
+ }
18
+ const result = Buffer.alloc(header.originalSize);
19
+ let offset = 0;
20
+ for (const chunk of decryptedChunks) {
21
+ const copyLength = Math.min(chunk.length, header.originalSize - offset);
22
+ chunk.copy(result, offset, 0, copyLength);
23
+ offset += copyLength;
24
+ }
25
+ return result;
26
+ }
27
+ encryptFileFromPath(filePath, recipientPublicKey) {
28
+ const stats = fs.statSync(filePath);
29
+ const totalChunks = Math.ceil(stats.size / EciesFileService.CHUNK_SIZE);
30
+ const header = {
31
+ version: 1,
32
+ chunkSize: EciesFileService.CHUNK_SIZE,
33
+ totalChunks,
34
+ originalSize: stats.size,
35
+ };
36
+ const headerBytes = this.serializeHeader(header);
37
+ const encryptedHeader = this.eciesService.encryptSimpleOrSingle(false, recipientPublicKey, headerBytes);
38
+ const chunks = [encryptedHeader];
39
+ const fd = fs.openSync(filePath, 'r');
40
+ try {
41
+ for (let i = 0; i < totalChunks; i++) {
42
+ const offset = i * EciesFileService.CHUNK_SIZE;
43
+ const chunkSize = Math.min(EciesFileService.CHUNK_SIZE, stats.size - offset);
44
+ const chunkData = Buffer.alloc(chunkSize);
45
+ fs.readSync(fd, chunkData, 0, chunkSize, offset);
46
+ const encryptedChunk = this.eciesService.encryptSimpleOrSingle(false, recipientPublicKey, chunkData);
47
+ chunks.push(encryptedChunk);
48
+ }
49
+ }
50
+ finally {
51
+ fs.closeSync(fd);
52
+ }
53
+ return Buffer.concat(chunks);
54
+ }
55
+ decryptFileToPath(encryptedData, outputPath) {
56
+ const { header, chunks } = this.parseEncryptedFile(encryptedData);
57
+ const fd = fs.openSync(outputPath, 'w');
58
+ let offset = 0;
59
+ try {
60
+ for (const chunk of chunks) {
61
+ const decrypted = this.eciesService.decryptSimpleOrSingleWithHeader(false, this.userPrivateKey, chunk);
62
+ const writeLength = Math.min(decrypted.length, header.originalSize - offset);
63
+ fs.writeSync(fd, decrypted, 0, writeLength, offset);
64
+ offset += writeLength;
65
+ }
66
+ }
67
+ finally {
68
+ fs.closeSync(fd);
69
+ }
70
+ }
71
+ serializeHeader(header) {
72
+ const buffer = Buffer.alloc(EciesFileService.HEADER_SIZE);
73
+ buffer.writeUInt32BE(header.version, 0);
74
+ buffer.writeUInt32BE(header.chunkSize, 4);
75
+ buffer.writeUInt32BE(header.totalChunks, 8);
76
+ buffer.writeUInt32BE(header.originalSize, 12);
77
+ return buffer;
78
+ }
79
+ deserializeHeader(data) {
80
+ return {
81
+ version: data.readUInt32BE(0),
82
+ chunkSize: data.readUInt32BE(4),
83
+ totalChunks: data.readUInt32BE(8),
84
+ originalSize: data.readUInt32BE(12),
85
+ };
86
+ }
87
+ parseEncryptedFile(encryptedData) {
88
+ const headerLength = this.eciesService.computeEncryptedLengthFromDataLength(EciesFileService.HEADER_SIZE, 'single');
89
+ const encryptedHeader = encryptedData.subarray(0, headerLength);
90
+ const decryptedHeaderBytes = this.eciesService.decryptSimpleOrSingleWithHeader(false, this.userPrivateKey, encryptedHeader);
91
+ const header = this.deserializeHeader(decryptedHeaderBytes);
92
+ const chunks = [];
93
+ let offset = headerLength;
94
+ for (let i = 0; i < header.totalChunks; i++) {
95
+ const chunkLength = this.eciesService.computeEncryptedLengthFromDataLength(i === header.totalChunks - 1
96
+ ? header.originalSize % header.chunkSize || header.chunkSize
97
+ : header.chunkSize, 'single');
98
+ chunks.push(encryptedData.subarray(offset, offset + chunkLength));
99
+ offset += chunkLength;
100
+ }
101
+ return { header, chunks };
102
+ }
103
+ }
104
+ //# sourceMappingURL=file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/services/ecies/file.ts"],"names":[],"mappings":"AAAA,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AAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC;YAClE,MAAM,IAAI,WAAW,CAAC;QACxB,CAAC;QAED,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC5B,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from './crypto-core';
2
+ export * from './file';
3
+ export * from './multi-recipient';
4
+ export * from './service';
5
+ export * from './signature';
6
+ export * from './single-recipient';
7
+ export * from './utilities';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/ecies/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from './crypto-core';
2
+ export * from './file';
3
+ export * from './multi-recipient';
4
+ export * from './service';
5
+ export * from './signature';
6
+ export * from './single-recipient';
7
+ export * from './utilities';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/ecies/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC"}
@@ -0,0 +1,83 @@
1
+ import { EciesStringKey } from '@digitaldefiance/ecies-lib';
2
+ import { I18nEngine, Language } from '@digitaldefiance/i18n-lib';
3
+ import { IMultiEncryptedMessage } from '../../interfaces/multi-encrypted-message';
4
+ import { IMultiEncryptedParsedHeader } from '../../interfaces/multi-encrypted-parsed-header';
5
+ import { Member } from '../../member';
6
+ import { EciesCryptoCore } from './crypto-core';
7
+ import { EciesSingleRecipientCore } from './single-recipient';
8
+ /**
9
+ * Multiple recipient encryption/decryption functions for ECIES
10
+ */
11
+ export declare class EciesMultiRecipient {
12
+ protected readonly cryptoCore: EciesCryptoCore;
13
+ protected readonly singleRecipientCore: EciesSingleRecipientCore;
14
+ protected readonly engine: I18nEngine<EciesStringKey, Language, any, any>;
15
+ constructor(cryptoCore: EciesCryptoCore, engine: I18nEngine<EciesStringKey, Language, any, any>);
16
+ /**
17
+ * Get the size of the header for a given encryption type
18
+ * @param encryptionType The encryption type (single, simple, etc.)
19
+ * @param options Optional encryption options
20
+ * @param options.recipientCount The number of recipients
21
+ * @returns
22
+ */
23
+ getHeaderSize(recipientCount: number): number;
24
+ /**
25
+ * Encrypt a message symmetric key with a public key
26
+ * @param receiverPublicKey The public key of the receiver
27
+ * @param messageSymmetricKey The message to encrypt
28
+ * @returns The encrypted message
29
+ */
30
+ encryptKey(receiverPublicKey: Buffer, messageSymmetricKey: Buffer): Buffer;
31
+ /**
32
+ * Decrypts symmetric key encrypted with ECIES using a header
33
+ * @param privateKey The private key to decrypt the data
34
+ * @param encryptedKey The data to decrypt
35
+ * @returns The decrypted data buffer
36
+ */
37
+ decryptKey(privateKey: Buffer, encryptedKey: Buffer): Buffer;
38
+ /**
39
+ * Encrypts a message for multiple recipients.
40
+ * @param recipients The recipients to encrypt the message for.
41
+ * @param message The message to encrypt.
42
+ * @param preamble Optional preamble to include in the encrypted message.
43
+ * @returns The encrypted message.
44
+ * @throws EciesError if the number of recipients is greater than 65535.
45
+ */
46
+ encryptMultiple(recipients: Member[], message: Buffer, preamble?: Buffer): IMultiEncryptedMessage;
47
+ /**
48
+ * Decrypts a message encrypted with multiple ECIE for a recipient.
49
+ * @param encryptedData The encrypted data.
50
+ * @param recipient The recipient.
51
+ * @returns The decrypted message.
52
+ */
53
+ decryptMultipleECIEForRecipient(encryptedData: IMultiEncryptedMessage, recipient: Member): Buffer;
54
+ /**
55
+ * Calculate the overhead for a message encrypted for multiple recipients
56
+ * @param recipientCount number of recipients
57
+ * @param includeMessageOverhead whether to include the overhead for the encrypted message
58
+ * @param encryptedKeys optional array of encrypted keys to calculate actual size
59
+ * @returns the overhead size in bytes
60
+ */
61
+ calculateECIESMultipleRecipientOverhead(recipientCount: number, includeMessageOverhead: boolean, encryptedKeys?: Buffer[]): number;
62
+ /**
63
+ * Builds the header for a message encrypted for multiple recipients
64
+ * @param data The encrypted message data including recipients and encrypted keys
65
+ * @returns The header buffer for the message
66
+ * @throws EciesError if the number of recipients is greater than the maximum allowed
67
+ * @throws EciesError if the number of encrypted keys does not match the number of recipients
68
+ */
69
+ buildECIESMultipleRecipientHeader(data: IMultiEncryptedMessage): Buffer;
70
+ /**
71
+ * Parses a multi-encrypted header.
72
+ * @param data - The data to parse.
73
+ * @returns The parsed header.
74
+ */
75
+ parseMultiEncryptedHeader(data: Buffer): IMultiEncryptedParsedHeader;
76
+ /**
77
+ * Parses a multi-encrypted buffer into its components.
78
+ * @param data - The multi-encrypted buffer to parse.
79
+ * @returns The parsed multi-encrypted buffer.
80
+ */
81
+ parseMultiEncryptedBuffer(data: Buffer): IMultiEncryptedMessage;
82
+ }
83
+ //# sourceMappingURL=multi-recipient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multi-recipient.d.ts","sourceRoot":"","sources":["../../../src/services/ecies/multi-recipient.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,cAAc,EACf,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAWjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AAClF,OAAO,EAAE,2BAA2B,EAAE,MAAM,gDAAgD,CAAC;AAC7F,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAA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CASvE"}
@@ -0,0 +1,355 @@
1
+ import { Constants as AppConstants, ECIES, EciesEncryptionTypeEnum, ECIESError, ECIESErrorTypeEnum, } from '@digitaldefiance/ecies-lib';
2
+ import { createCipheriv, createDecipheriv, createECDH, randomBytes, } from 'crypto';
3
+ import { ObjectId } from 'mongodb';
4
+ import { Constants } from '../../constants';
5
+ import { EciesSingleRecipientCore } from './single-recipient';
6
+ /**
7
+ * Multiple recipient encryption/decryption functions for ECIES
8
+ */
9
+ export class EciesMultiRecipient {
10
+ cryptoCore;
11
+ singleRecipientCore;
12
+ engine;
13
+ constructor(cryptoCore, engine) {
14
+ this.cryptoCore = cryptoCore;
15
+ this.singleRecipientCore = new EciesSingleRecipientCore(cryptoCore.config, engine);
16
+ this.engine = engine;
17
+ }
18
+ /**
19
+ * Get the size of the header for a given encryption type
20
+ * @param encryptionType The encryption type (single, simple, etc.)
21
+ * @param options Optional encryption options
22
+ * @param options.recipientCount The number of recipients
23
+ * @returns
24
+ */
25
+ getHeaderSize(recipientCount) {
26
+ return (ECIES.MULTIPLE.FIXED_OVERHEAD_SIZE +
27
+ recipientCount * ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE);
28
+ }
29
+ /**
30
+ * Encrypt a message symmetric key with a public key
31
+ * @param receiverPublicKey The public key of the receiver
32
+ * @param messageSymmetricKey The message to encrypt
33
+ * @returns The encrypted message
34
+ */
35
+ encryptKey(receiverPublicKey, messageSymmetricKey) {
36
+ // Generate ephemeral ECDH key pair
37
+ const ecdh = createECDH(this.cryptoCore.config.curveName);
38
+ ecdh.generateKeys();
39
+ // Compute shared secret
40
+ let sharedSecret;
41
+ try {
42
+ // Make sure we normalize the receiver's public key
43
+ const normalizedReceiverPublicKey = this.cryptoCore.normalizePublicKey(receiverPublicKey);
44
+ // Ensure we're using the properly formatted public key (with 0x04 prefix)
45
+ // Our debugging shows only the full format with prefix works correctly
46
+ sharedSecret = ecdh.computeSecret(normalizedReceiverPublicKey);
47
+ }
48
+ catch (error) {
49
+ console.error('[ERROR][encrypt] Failed to compute shared secret:', error);
50
+ if (error instanceof Error) {
51
+ if ('code' in error &&
52
+ error.code === 'ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY') {
53
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidRecipientPublicKey, this.engine, undefined, undefined, {
54
+ nodeError: error.code,
55
+ });
56
+ }
57
+ throw new ECIESError(ECIESErrorTypeEnum.SecretComputationFailed, this.engine, undefined, undefined, {
58
+ error: error.message,
59
+ });
60
+ }
61
+ throw new ECIESError(ECIESErrorTypeEnum.SecretComputationFailed, this.engine);
62
+ }
63
+ // Get the ephemeral public key and ensure it has the 0x04 prefix
64
+ let ephemeralPublicKey = ecdh.getPublicKey();
65
+ if (ephemeralPublicKey.length === ECIES.RAW_PUBLIC_KEY_LENGTH) {
66
+ ephemeralPublicKey = Buffer.concat([
67
+ Buffer.from([ECIES.PUBLIC_KEY_MAGIC]),
68
+ ephemeralPublicKey,
69
+ ]);
70
+ }
71
+ // Get the key from the shared secret (always use first 32 bytes)
72
+ const symKey = sharedSecret.subarray(0, ECIES.SYMMETRIC.KEY_SIZE);
73
+ const iv = randomBytes(ECIES.IV_SIZE);
74
+ // Create cipher with the derived symmetric key
75
+ const cipher = createCipheriv(ECIES.SYMMETRIC_ALGORITHM_CONFIGURATION, symKey, iv);
76
+ // Ensure auto padding is enabled
77
+ cipher.setAutoPadding(true);
78
+ // Encrypt the message
79
+ let encrypted = cipher.update(messageSymmetricKey);
80
+ encrypted = Buffer.concat([encrypted, cipher.final()]);
81
+ // Get and explicitly set the authentication tag to max tag length for consistency
82
+ const authTag = cipher.getAuthTag();
83
+ // Format:ephemeralPublicKey (65) | iv (16) | authTag (16) | encryptedData (ECIES.SYMMETRIC.KEY_SIZE = 32)
84
+ return Buffer.concat([ephemeralPublicKey, iv, authTag, encrypted]);
85
+ }
86
+ /**
87
+ * Decrypts symmetric key encrypted with ECIES using a header
88
+ * @param privateKey The private key to decrypt the data
89
+ * @param encryptedKey The data to decrypt
90
+ * @returns The decrypted data buffer
91
+ */
92
+ decryptKey(privateKey, encryptedKey) {
93
+ if (encryptedKey.length !== ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE) {
94
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidEncryptedKeyLength, this.engine, undefined, undefined, {
95
+ expected: String(ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE),
96
+ actual: String(encryptedKey.length),
97
+ });
98
+ }
99
+ const ephemeralPublicKey = encryptedKey.subarray(0, ECIES.PUBLIC_KEY_LENGTH);
100
+ const iv = encryptedKey.subarray(ECIES.PUBLIC_KEY_LENGTH, ECIES.PUBLIC_KEY_LENGTH + ECIES.IV_SIZE);
101
+ const authTag = encryptedKey.subarray(ECIES.PUBLIC_KEY_LENGTH + ECIES.IV_SIZE, ECIES.PUBLIC_KEY_LENGTH + ECIES.IV_SIZE + ECIES.AUTH_TAG_SIZE);
102
+ const encrypted = encryptedKey.subarray(ECIES.PUBLIC_KEY_LENGTH + ECIES.IV_SIZE + ECIES.AUTH_TAG_SIZE, ECIES.PUBLIC_KEY_LENGTH +
103
+ ECIES.IV_SIZE +
104
+ ECIES.AUTH_TAG_SIZE +
105
+ ECIES.SYMMETRIC.KEY_SIZE);
106
+ // Normalize the public key (ensuring 0x04 prefix)
107
+ const normalizedKey = this.cryptoCore.normalizePublicKey(ephemeralPublicKey);
108
+ // Decrypt using components with the normalized key
109
+ const decrypted = this.singleRecipientCore.decryptWithComponents(privateKey, normalizedKey, iv, authTag, encrypted);
110
+ if (decrypted.length !== ECIES.SYMMETRIC.KEY_SIZE) {
111
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine, undefined, undefined, {
112
+ expected: String(ECIES.SYMMETRIC.KEY_SIZE),
113
+ actual: String(decrypted.length),
114
+ });
115
+ }
116
+ return decrypted;
117
+ }
118
+ /**
119
+ * Encrypts a message for multiple recipients.
120
+ * @param recipients The recipients to encrypt the message for.
121
+ * @param message The message to encrypt.
122
+ * @param preamble Optional preamble to include in the encrypted message.
123
+ * @returns The encrypted message.
124
+ * @throws EciesError if the number of recipients is greater than 65535.
125
+ */
126
+ encryptMultiple(recipients, message, preamble) {
127
+ if (recipients.length > AppConstants.UINT16_MAX) {
128
+ throw new ECIESError(ECIESErrorTypeEnum.TooManyRecipients, this.engine);
129
+ }
130
+ const messageTypeBuffer = Buffer.alloc(1);
131
+ messageTypeBuffer.writeUint8(EciesEncryptionTypeEnum.Multiple);
132
+ // Generate a random symmetric key
133
+ const symmetricKey = randomBytes(ECIES.SYMMETRIC.KEY_SIZE);
134
+ const iv = randomBytes(ECIES.IV_SIZE);
135
+ // Encrypt the message with the symmetric key
136
+ const cipher = createCipheriv(ECIES.SYMMETRIC_ALGORITHM_CONFIGURATION, symmetricKey, iv);
137
+ const encrypted = cipher.update(message);
138
+ const final = cipher.final();
139
+ const authTag = cipher.getAuthTag();
140
+ const encryptedMessage = Buffer.concat([encrypted, final]);
141
+ const storedMessage = Buffer.concat([
142
+ preamble ?? Buffer.alloc(0),
143
+ iv,
144
+ authTag,
145
+ encryptedMessage,
146
+ ]);
147
+ const encryptionResults = recipients.map((member) => ({
148
+ id: member.id,
149
+ encryptedKey: this.encryptKey(member.publicKey, symmetricKey),
150
+ }));
151
+ const recipientIds = encryptionResults.map(({ id }) => id);
152
+ const recipientKeys = encryptionResults.map(({ encryptedKey }) => encryptedKey);
153
+ // Verify the encrypted message size (just the encrypted content)
154
+ if (encryptedMessage.length !== message.length) {
155
+ throw new ECIESError(ECIESErrorTypeEnum.MessageLengthMismatch, this.engine);
156
+ }
157
+ const headerSize = this.calculateECIESMultipleRecipientOverhead(recipients.length, false, recipientKeys);
158
+ return {
159
+ dataLength: message.length,
160
+ recipientCount: recipients.length,
161
+ recipientIds,
162
+ recipientKeys,
163
+ encryptedMessage: storedMessage,
164
+ headerSize,
165
+ };
166
+ }
167
+ /**
168
+ * Decrypts a message encrypted with multiple ECIE for a recipient.
169
+ * @param encryptedData The encrypted data.
170
+ * @param recipient The recipient.
171
+ * @returns The decrypted message.
172
+ */
173
+ decryptMultipleECIEForRecipient(encryptedData, recipient) {
174
+ if (recipient.privateKey === undefined) {
175
+ throw new ECIESError(ECIESErrorTypeEnum.PrivateKeyNotLoaded, this.engine);
176
+ }
177
+ // Find this recipient's encrypted key
178
+ const recipientIndex = encryptedData.recipientIds.findIndex((id) => id.equals(recipient.id));
179
+ if (recipientIndex === -1) {
180
+ throw new ECIESError(ECIESErrorTypeEnum.RecipientNotFound, this.engine);
181
+ }
182
+ const encryptedKey = encryptedData.recipientKeys[recipientIndex];
183
+ // Decrypt the symmetric key using the detected encryption type
184
+ const symmetricKey = this.decryptKey(Buffer.from(recipient.privateKey.value), encryptedKey);
185
+ // Extract the IV and auth tag from the encrypted message
186
+ const iv = encryptedData.encryptedMessage.subarray(0, ECIES.IV_SIZE);
187
+ const authTag = encryptedData.encryptedMessage.subarray(ECIES.IV_SIZE, ECIES.IV_SIZE + ECIES.AUTH_TAG_SIZE);
188
+ // Extract the encrypted content (no CRC, AES-GCM provides authentication)
189
+ const encrypted = encryptedData.encryptedMessage.subarray(ECIES.IV_SIZE + ECIES.AUTH_TAG_SIZE);
190
+ // Decrypt the content with the symmetric key
191
+ const decipher = createDecipheriv(ECIES.SYMMETRIC_ALGORITHM_CONFIGURATION, symmetricKey, iv);
192
+ decipher.setAuthTag(authTag);
193
+ const decrypted = decipher.update(encrypted);
194
+ const final = decipher.final();
195
+ const decryptedMessage = Buffer.concat([decrypted, final]);
196
+ // AES-GCM provides authentication via auth tag (no separate CRC needed)
197
+ // The decrypted message should match the original data length
198
+ if (decryptedMessage.length !== encryptedData.dataLength) {
199
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine);
200
+ }
201
+ return decryptedMessage;
202
+ }
203
+ /**
204
+ * Calculate the overhead for a message encrypted for multiple recipients
205
+ * @param recipientCount number of recipients
206
+ * @param includeMessageOverhead whether to include the overhead for the encrypted message
207
+ * @param encryptedKeys optional array of encrypted keys to calculate actual size
208
+ * @returns the overhead size in bytes
209
+ */
210
+ calculateECIESMultipleRecipientOverhead(recipientCount, includeMessageOverhead, encryptedKeys) {
211
+ if (recipientCount < 2) {
212
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidRecipientCount, this.engine);
213
+ }
214
+ // Calculate encrypted keys size
215
+ let encryptedKeysSize;
216
+ if (encryptedKeys) {
217
+ encryptedKeysSize = encryptedKeys.reduce((total, key) => total + key.length, 0);
218
+ }
219
+ else {
220
+ // Default assumption: all keys use Simple encryption type (more efficient)
221
+ encryptedKeysSize = recipientCount * ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE;
222
+ }
223
+ const baseOverhead = ECIES.MULTIPLE.DATA_LENGTH_SIZE +
224
+ ECIES.MULTIPLE.RECIPIENT_COUNT_SIZE +
225
+ recipientCount * Constants.OBJECT_ID_LENGTH + // recipient ids
226
+ encryptedKeysSize; // actual encrypted keys size
227
+ return includeMessageOverhead
228
+ ? baseOverhead + ECIES.MULTIPLE.FIXED_OVERHEAD_SIZE
229
+ : baseOverhead;
230
+ }
231
+ /**
232
+ * Builds the header for a message encrypted for multiple recipients
233
+ * @param data The encrypted message data including recipients and encrypted keys
234
+ * @returns The header buffer for the message
235
+ * @throws EciesError if the number of recipients is greater than the maximum allowed
236
+ * @throws EciesError if the number of encrypted keys does not match the number of recipients
237
+ */
238
+ buildECIESMultipleRecipientHeader(data) {
239
+ if (data.recipientIds.length > ECIES.MULTIPLE.MAX_RECIPIENTS) {
240
+ throw new ECIESError(ECIESErrorTypeEnum.TooManyRecipients, this.engine);
241
+ }
242
+ else if (data.recipientIds.length !== data.recipientKeys.length) {
243
+ throw new ECIESError(ECIESErrorTypeEnum.RecipientKeyCountMismatch, this.engine);
244
+ }
245
+ else if (data.dataLength < 0 ||
246
+ data.dataLength > ECIES.MAX_RAW_DATA_SIZE) {
247
+ throw new ECIESError(ECIESErrorTypeEnum.FileSizeTooLarge, this.engine);
248
+ }
249
+ // Create data length buffer
250
+ const dataLengthBuffer = Buffer.alloc(ECIES.MULTIPLE.DATA_LENGTH_SIZE);
251
+ dataLengthBuffer.writeBigUInt64BE(BigInt(data.dataLength));
252
+ // Create recipient count buffer
253
+ const recipientCountBuffer = Buffer.alloc(ECIES.MULTIPLE.RECIPIENT_COUNT_SIZE);
254
+ recipientCountBuffer.writeUInt16BE(data.recipientIds.length);
255
+ // Create recipients buffer
256
+ const recipientsBuffer = Buffer.alloc(data.recipientIds.length * Constants.OBJECT_ID_LENGTH);
257
+ data.recipientIds.forEach((recipientId, index) => {
258
+ recipientsBuffer.set(Buffer.from(recipientId.toHexString(), 'hex'), index * Constants.OBJECT_ID_LENGTH);
259
+ });
260
+ // Validate encrypted key lengths based on their encryption type
261
+ data.recipientKeys.forEach((encryptedKey) => {
262
+ if (encryptedKey.length === 0) {
263
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidEncryptedKeyLength, this.engine);
264
+ }
265
+ if (encryptedKey.length !== ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE) {
266
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidEncryptedKeyLength, this.engine, undefined, undefined, {
267
+ expected: String(ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE),
268
+ actual: String(encryptedKey.length),
269
+ });
270
+ }
271
+ });
272
+ // Create encrypted keys buffer with variable-length keys
273
+ const encryptedKeysBuffer = Buffer.concat(data.recipientKeys);
274
+ // Combine all buffers to form the header
275
+ return Buffer.concat([
276
+ dataLengthBuffer,
277
+ recipientCountBuffer,
278
+ recipientsBuffer,
279
+ encryptedKeysBuffer,
280
+ ]);
281
+ }
282
+ /**
283
+ * Parses a multi-encrypted header.
284
+ * @param data - The data to parse.
285
+ * @returns The parsed header.
286
+ */
287
+ parseMultiEncryptedHeader(data) {
288
+ // Ensure there's enough data to read headers
289
+ if (data.length < ECIES.MULTIPLE.FIXED_OVERHEAD_SIZE) {
290
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine);
291
+ }
292
+ let offset = 0;
293
+ // Read data length
294
+ const dataLength = Number(data.readBigUInt64BE(offset));
295
+ if (dataLength <= 0 || dataLength > ECIES.MAX_RAW_DATA_SIZE) {
296
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine);
297
+ }
298
+ offset += ECIES.MULTIPLE.DATA_LENGTH_SIZE; // 8 bytes
299
+ // Read recipient count
300
+ const recipientCount = data.readUInt16BE(offset);
301
+ if (recipientCount <= 0 || recipientCount > ECIES.MULTIPLE.MAX_RECIPIENTS) {
302
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidRecipientCount, this.engine);
303
+ }
304
+ offset += ECIES.MULTIPLE.RECIPIENT_COUNT_SIZE; // 2 bytes
305
+ // Ensure there's enough data for all recipients
306
+ const requiredLength = this.calculateECIESMultipleRecipientOverhead(recipientCount, false);
307
+ if (data.length < requiredLength) {
308
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine);
309
+ }
310
+ // Read recipient IDs
311
+ const recipientIds = [];
312
+ for (let i = 0; i < recipientCount; i++) {
313
+ recipientIds.push(new ObjectId(data
314
+ .subarray(offset, offset + Constants.OBJECT_ID_LENGTH)
315
+ .toString('hex')));
316
+ offset += Constants.OBJECT_ID_LENGTH;
317
+ }
318
+ // Read encrypted keys with variable lengths based on encryption type
319
+ const recipientKeys = [];
320
+ for (let i = 0; i < recipientCount; i++) {
321
+ if (offset >= data.length) {
322
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine);
323
+ }
324
+ if (offset + ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE > data.length) {
325
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidDataLength, this.engine, undefined, undefined, {
326
+ required: String(ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE),
327
+ available: String(data.length - offset),
328
+ });
329
+ }
330
+ recipientKeys.push(data.subarray(offset, offset + ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE));
331
+ offset += ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE;
332
+ }
333
+ return {
334
+ dataLength,
335
+ recipientCount,
336
+ recipientIds,
337
+ recipientKeys,
338
+ headerSize: offset,
339
+ };
340
+ }
341
+ /**
342
+ * Parses a multi-encrypted buffer into its components.
343
+ * @param data - The multi-encrypted buffer to parse.
344
+ * @returns The parsed multi-encrypted buffer.
345
+ */
346
+ parseMultiEncryptedBuffer(data) {
347
+ const header = this.parseMultiEncryptedHeader(data);
348
+ const encryptedMessage = data.subarray(header.headerSize);
349
+ return {
350
+ ...header,
351
+ encryptedMessage,
352
+ };
353
+ }
354
+ }
355
+ //# sourceMappingURL=multi-recipient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"multi-recipient.js","sourceRoot":"","sources":["../../../src/services/ecies/multi-recipient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,YAAY,EACzB,KAAK,EACL,uBAAuB,EACvB,UAAU,EACV,kBAAkB,GAEnB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,UAAU,EACV,WAAW,GACZ,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAM5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAE9D;;GAEG;AACH,MAAM,OAAO,mBAAmB;IACX,UAAU,CAAkB;IAC5B,mBAAmB,CAA2B;IAC9C,MAAM,CAAiD;IAE1E,YACE,UAA2B,EAC3B,MAAsD;QAEtD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,mBAAmB,GAAG,IAAI,wBAAwB,CACrD,UAAU,CAAC,MAAM,EACjB,MAAM,CACP,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;;;;;OAMG;IACI,aAAa,CAAC,cAAsB;QACzC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