@aseppxyzz12/baileys 1.1.8

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 (185) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +2579 -0
  3. package/WAProto/AICommon/AICommon.js +19396 -0
  4. package/WAProto/AICommon/AICommon.proto +820 -0
  5. package/WAProto/Adv/Adv.js +1137 -0
  6. package/WAProto/Adv/Adv.proto +42 -0
  7. package/WAProto/BotMetadata/BotMetadata.js +8975 -0
  8. package/WAProto/BotMetadata/BotMetadata.proto +484 -0
  9. package/WAProto/Cert/Cert.js +893 -0
  10. package/WAProto/Cert/Cert.proto +30 -0
  11. package/WAProto/ChatLockSettings/ChatLockSettings.js +673 -0
  12. package/WAProto/ChatLockSettings/ChatLockSettings.proto +9 -0
  13. package/WAProto/CompanionReg/CompanionReg.js +2457 -0
  14. package/WAProto/CompanionReg/CompanionReg.proto +103 -0
  15. package/WAProto/DeviceCapabilities/DeviceCapabilities.js +846 -0
  16. package/WAProto/DeviceCapabilities/DeviceCapabilities.proto +38 -0
  17. package/WAProto/E2E/E2E.js +78728 -0
  18. package/WAProto/E2E/E2E.proto +1970 -0
  19. package/WAProto/Ephemeral/Ephemeral.js +143 -0
  20. package/WAProto/Ephemeral/Ephemeral.proto +7 -0
  21. package/WAProto/HistorySync/HistorySync.js +95931 -0
  22. package/WAProto/HistorySync/HistorySync.proto +229 -0
  23. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.js +414 -0
  24. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.proto +13 -0
  25. package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.js +980 -0
  26. package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.proto +38 -0
  27. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +81185 -0
  28. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.proto +88 -0
  29. package/WAProto/MmsRetry/MmsRetry.js +310 -0
  30. package/WAProto/MmsRetry/MmsRetry.proto +19 -0
  31. package/WAProto/Protocol/Protocol.js +399 -0
  32. package/WAProto/Protocol/Protocol.proto +22 -0
  33. package/WAProto/Reporting/Reporting.js +535 -0
  34. package/WAProto/Reporting/Reporting.proto +22 -0
  35. package/WAProto/ServerSync/ServerSync.js +1830 -0
  36. package/WAProto/ServerSync/ServerSync.proto +70 -0
  37. package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.js +3267 -0
  38. package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.proto +95 -0
  39. package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.js +1140 -0
  40. package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.proto +44 -0
  41. package/WAProto/StatusAttributions/StatusAttributions.js +1604 -0
  42. package/WAProto/StatusAttributions/StatusAttributions.proto +89 -0
  43. package/WAProto/SyncAction/SyncAction.js +17424 -0
  44. package/WAProto/SyncAction/SyncAction.proto +663 -0
  45. package/WAProto/UserPassword/UserPassword.js +544 -0
  46. package/WAProto/UserPassword/UserPassword.proto +28 -0
  47. package/WAProto/VnameCert/VnameCert.js +1466 -0
  48. package/WAProto/VnameCert/VnameCert.proto +65 -0
  49. package/WAProto/Wa6/Wa6.js +4601 -0
  50. package/WAProto/Wa6/Wa6.proto +241 -0
  51. package/WAProto/Web/Web.js +90819 -0
  52. package/WAProto/Web/Web.proto +605 -0
  53. package/WAProto/index.js +30 -0
  54. package/engine-requirements.js +9 -0
  55. package/lib/Defaults/baileys-version.json +3 -0
  56. package/lib/Defaults/connection.js +39 -0
  57. package/lib/Defaults/constants.js +50 -0
  58. package/lib/Defaults/history.js +13 -0
  59. package/lib/Defaults/index.js +36 -0
  60. package/lib/Defaults/media.js +43 -0
  61. package/lib/Defaults/phonenumber-mcc.json +223 -0
  62. package/lib/Defaults/prefix.js +12 -0
  63. package/lib/Function/Download/tiktok.js +19 -0
  64. package/lib/Function/Tools/bypass.js +19 -0
  65. package/lib/Function/index.js +13 -0
  66. package/lib/Signal/Group/ciphertext-message.js +14 -0
  67. package/lib/Signal/Group/group-session-builder.js +46 -0
  68. package/lib/Signal/Group/group_cipher.js +104 -0
  69. package/lib/Signal/Group/index.js +42 -0
  70. package/lib/Signal/Group/keyhelper.js +21 -0
  71. package/lib/Signal/Group/queue-job.js +57 -0
  72. package/lib/Signal/Group/sender-chain-key.js +34 -0
  73. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  74. package/lib/Signal/Group/sender-key-message.js +78 -0
  75. package/lib/Signal/Group/sender-key-name.js +49 -0
  76. package/lib/Signal/Group/sender-key-record.js +45 -0
  77. package/lib/Signal/Group/sender-key-state.js +100 -0
  78. package/lib/Signal/Group/sender-message-key.js +28 -0
  79. package/lib/Signal/libsignal.js +364 -0
  80. package/lib/Signal/lid-mapping.js +164 -0
  81. package/lib/Socket/Client/abstract-socket-client.js +13 -0
  82. package/lib/Socket/Client/index.js +31 -0
  83. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  84. package/lib/Socket/Client/types.js +12 -0
  85. package/lib/Socket/Client/web-socket-client.js +62 -0
  86. package/lib/Socket/Client/websocket.js +67 -0
  87. package/lib/Socket/business.js +291 -0
  88. package/lib/Socket/chats.js +874 -0
  89. package/lib/Socket/community.js +454 -0
  90. package/lib/Socket/dugong.js +658 -0
  91. package/lib/Socket/groups.js +357 -0
  92. package/lib/Socket/index.js +13 -0
  93. package/lib/Socket/messages-recv.js +1509 -0
  94. package/lib/Socket/messages-send.js +1505 -0
  95. package/lib/Socket/mex.js +54 -0
  96. package/lib/Socket/newsletter.js +171 -0
  97. package/lib/Socket/registration.js +167 -0
  98. package/lib/Socket/rich-content.js +279 -0
  99. package/lib/Socket/socket.js +905 -0
  100. package/lib/Socket/username.js +157 -0
  101. package/lib/Socket/usync.js +70 -0
  102. package/lib/Store/index.js +37 -0
  103. package/lib/Store/keyed-db.js +114 -0
  104. package/lib/Store/make-cache-manager-store.js +77 -0
  105. package/lib/Store/make-in-memory-store.js +423 -0
  106. package/lib/Store/make-ordered-dictionary.js +78 -0
  107. package/lib/Store/object-repository.js +26 -0
  108. package/lib/Types/Auth.js +2 -0
  109. package/lib/Types/Bussines.js +2 -0
  110. package/lib/Types/Call.js +2 -0
  111. package/lib/Types/Chat.js +10 -0
  112. package/lib/Types/Contact.js +2 -0
  113. package/lib/Types/Events.js +2 -0
  114. package/lib/Types/GroupMetadata.js +2 -0
  115. package/lib/Types/Label.js +25 -0
  116. package/lib/Types/LabelAssociation.js +4 -0
  117. package/lib/Types/Message.js +11 -0
  118. package/lib/Types/Mex.js +40 -0
  119. package/lib/Types/MexUpdates.js +15 -0
  120. package/lib/Types/Newsletter.js +32 -0
  121. package/lib/Types/Product.js +2 -0
  122. package/lib/Types/Signal.js +2 -0
  123. package/lib/Types/Socket.js +2 -0
  124. package/lib/Types/State.js +9 -0
  125. package/lib/Types/USync.js +2 -0
  126. package/lib/Types/index.js +60 -0
  127. package/lib/Utils/auth-utils.js +256 -0
  128. package/lib/Utils/baileys-event-stream.js +63 -0
  129. package/lib/Utils/bot-toolkit.js +455 -0
  130. package/lib/Utils/browser-utils.js +29 -0
  131. package/lib/Utils/business.js +233 -0
  132. package/lib/Utils/chat-utils.js +904 -0
  133. package/lib/Utils/companion-reg-client-utils.js +41 -0
  134. package/lib/Utils/crypto.js +144 -0
  135. package/lib/Utils/decode-wa-message.js +305 -0
  136. package/lib/Utils/event-buffer.js +555 -0
  137. package/lib/Utils/generics.js +411 -0
  138. package/lib/Utils/history.js +100 -0
  139. package/lib/Utils/identity-change-handler.js +53 -0
  140. package/lib/Utils/index.js +57 -0
  141. package/lib/Utils/link-preview.js +76 -0
  142. package/lib/Utils/logger.js +4 -0
  143. package/lib/Utils/lt-hash.js +45 -0
  144. package/lib/Utils/make-mutex.js +33 -0
  145. package/lib/Utils/message-composer.js +289 -0
  146. package/lib/Utils/message-retry-manager.js +134 -0
  147. package/lib/Utils/messages-media.js +806 -0
  148. package/lib/Utils/messages.js +1908 -0
  149. package/lib/Utils/noise-handler.js +157 -0
  150. package/lib/Utils/offline-node-processor.js +42 -0
  151. package/lib/Utils/pre-key-manager.js +86 -0
  152. package/lib/Utils/process-message.js +814 -0
  153. package/lib/Utils/reporting-utils.js +264 -0
  154. package/lib/Utils/signal.js +183 -0
  155. package/lib/Utils/stanza-ack.js +36 -0
  156. package/lib/Utils/sync-action-utils.js +52 -0
  157. package/lib/Utils/tc-token-utils.js +167 -0
  158. package/lib/Utils/use-multi-file-auth-state.js +104 -0
  159. package/lib/Utils/validate-connection.js +229 -0
  160. package/lib/Utils/vialeys-event-stream.js +41 -0
  161. package/lib/WABinary/constants.js +1308 -0
  162. package/lib/WABinary/decode.js +233 -0
  163. package/lib/WABinary/encode.js +212 -0
  164. package/lib/WABinary/generic-utils.js +124 -0
  165. package/lib/WABinary/index.js +36 -0
  166. package/lib/WABinary/jid-utils.js +101 -0
  167. package/lib/WABinary/types.js +2 -0
  168. package/lib/WAM/BinaryInfo.js +12 -0
  169. package/lib/WAM/constants.js +20491 -0
  170. package/lib/WAM/encode.js +148 -0
  171. package/lib/WAM/index.js +34 -0
  172. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -0
  173. package/lib/WAUSync/Protocols/USyncContactProtocol.js +38 -0
  174. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
  175. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +25 -0
  176. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -0
  177. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +32 -0
  178. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  179. package/lib/WAUSync/Protocols/index.js +38 -0
  180. package/lib/WAUSync/USyncQuery.js +97 -0
  181. package/lib/WAUSync/USyncUser.js +33 -0
  182. package/lib/WAUSync/index.js +34 -0
  183. package/lib/index.js +113 -0
  184. package/lib/messages-send.js +1496 -0
  185. package/package.json +100 -0
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = queueJob;
4
+ const _queueAsyncBuckets = new Map();
5
+ const _gcLimit = 10000;
6
+ async function _asyncQueueExecutor(queue, cleanup) {
7
+ let offt = 0;
8
+ // eslint-disable-next-line no-constant-condition
9
+ while (true) {
10
+ const limit = Math.min(queue.length, _gcLimit);
11
+ for (let i = offt; i < limit; i++) {
12
+ const job = queue[i];
13
+ try {
14
+ job.resolve(await job.awaitable());
15
+ }
16
+ catch (e) {
17
+ job.reject(e);
18
+ }
19
+ }
20
+ if (limit < queue.length) {
21
+ if (limit >= _gcLimit) {
22
+ queue.splice(0, limit);
23
+ offt = 0;
24
+ }
25
+ else {
26
+ offt = limit;
27
+ }
28
+ }
29
+ else {
30
+ break;
31
+ }
32
+ }
33
+ cleanup();
34
+ }
35
+ function queueJob(bucket, awaitable) {
36
+ // Skip name assignment since it's readonly in strict mode
37
+ if (typeof bucket !== 'string') {
38
+ console.warn('Unhandled bucket type (for naming):', typeof bucket, bucket);
39
+ }
40
+ let inactive = false;
41
+ if (!_queueAsyncBuckets.has(bucket)) {
42
+ _queueAsyncBuckets.set(bucket, []);
43
+ inactive = true;
44
+ }
45
+ const queue = _queueAsyncBuckets.get(bucket);
46
+ const job = new Promise((resolve, reject) => {
47
+ queue.push({
48
+ awaitable,
49
+ resolve: resolve,
50
+ reject
51
+ });
52
+ });
53
+ if (inactive) {
54
+ _asyncQueueExecutor(queue, () => _queueAsyncBuckets.delete(bucket));
55
+ }
56
+ return job;
57
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { calculateMAC: calculateMAC } = require("libsignal/src/crypto");
4
+ const { SenderMessageKey: SenderMessageKey } = require("./sender-message-key");
5
+ class SenderChainKey {
6
+ constructor(iteration, chainKey) {
7
+ this.MESSAGE_KEY_SEED = Buffer.from([1]);
8
+ this.CHAIN_KEY_SEED = Buffer.from([2]);
9
+ this.iteration = iteration;
10
+ this.chainKey = Buffer.from(chainKey);
11
+ }
12
+ getIteration() {
13
+ return this.iteration;
14
+ }
15
+ getSenderMessageKey() {
16
+ return new SenderMessageKey(
17
+ this.iteration,
18
+ this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey)
19
+ );
20
+ }
21
+ getNext() {
22
+ return new SenderChainKey(
23
+ this.iteration + 1,
24
+ this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey)
25
+ );
26
+ }
27
+ getSeed() {
28
+ return this.chainKey;
29
+ }
30
+ getDerivative(seed, key) {
31
+ return calculateMAC(key, seed);
32
+ }
33
+ }
34
+ module.exports = { SenderChainKey: SenderChainKey };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { proto: proto } = require("../../../WAProto");
4
+ const { CiphertextMessage: CiphertextMessage } = require("./ciphertext-message");
5
+ class SenderKeyDistributionMessage extends CiphertextMessage {
6
+ constructor(id, iteration, chainKey, signatureKey, serialized) {
7
+ super();
8
+ if (serialized) {
9
+ try {
10
+ const message = serialized.slice(1);
11
+ const distributionMessage =
12
+ proto.SenderKeyDistributionMessage.decode(message).toJSON();
13
+ this.serialized = serialized;
14
+ this.id = distributionMessage.id;
15
+ this.iteration = distributionMessage.iteration;
16
+ this.chainKey =
17
+ typeof distributionMessage.chainKey === "string"
18
+ ? Buffer.from(distributionMessage.chainKey, "base64")
19
+ : distributionMessage.chainKey;
20
+ this.signatureKey =
21
+ typeof distributionMessage.signingKey === "string"
22
+ ? Buffer.from(distributionMessage.signingKey, "base64")
23
+ : distributionMessage.signingKey;
24
+ } catch (e) {
25
+ throw new Error(String(e));
26
+ }
27
+ } else {
28
+ const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
29
+ this.id = id;
30
+ this.iteration = iteration;
31
+ this.chainKey = chainKey;
32
+ this.signatureKey = signatureKey;
33
+ const message = proto.SenderKeyDistributionMessage.encode(
34
+ proto.SenderKeyDistributionMessage.create({
35
+ id: id,
36
+ iteration: iteration,
37
+ chainKey: chainKey,
38
+ signingKey: this.signatureKey,
39
+ })
40
+ ).finish();
41
+ this.serialized = Buffer.concat([Buffer.from([version]), message]);
42
+ }
43
+ }
44
+ intsToByteHighAndLow(highValue, lowValue) {
45
+ return (((highValue << 4) | lowValue) & 255) % 256;
46
+ }
47
+ serialize() {
48
+ return this.serialized;
49
+ }
50
+ getType() {
51
+ return this.SENDERKEY_DISTRIBUTION_TYPE;
52
+ }
53
+ getIteration() {
54
+ return this.iteration;
55
+ }
56
+ getChainKey() {
57
+ return this.chainKey;
58
+ }
59
+ getSignatureKey() {
60
+ return this.signatureKey;
61
+ }
62
+ getId() {
63
+ return this.id;
64
+ }
65
+ }
66
+ module.exports = { SenderKeyDistributionMessage: SenderKeyDistributionMessage };
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const {
4
+ verifySignature: verifySignature,
5
+ calculateSignature: calculateSignature,
6
+ } = require("libsignal/src/curve");
7
+ const { proto: proto } = require("../../../WAProto");
8
+ const { CiphertextMessage: CiphertextMessage } = require("./ciphertext-message");
9
+ class SenderKeyMessage extends CiphertextMessage {
10
+ constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
11
+ super();
12
+ this.SIGNATURE_LENGTH = 64;
13
+ if (serialized) {
14
+ const version = serialized[0];
15
+ const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
16
+ const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
17
+ const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
18
+ this.serialized = serialized;
19
+ this.messageVersion = (version & 255) >> 4;
20
+ this.keyId = senderKeyMessage.id;
21
+ this.iteration = senderKeyMessage.iteration;
22
+ this.ciphertext =
23
+ typeof senderKeyMessage.ciphertext === "string"
24
+ ? Buffer.from(senderKeyMessage.ciphertext, "base64")
25
+ : senderKeyMessage.ciphertext;
26
+ this.signature = signature;
27
+ } else {
28
+ const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 255) % 256;
29
+ const ciphertextBuffer = Buffer.from(ciphertext);
30
+ const message = proto.SenderKeyMessage.encode(
31
+ proto.SenderKeyMessage.create({
32
+ id: keyId,
33
+ iteration: iteration,
34
+ ciphertext: ciphertextBuffer,
35
+ })
36
+ ).finish();
37
+ const signature = this.getSignature(
38
+ signatureKey,
39
+ Buffer.concat([Buffer.from([version]), message])
40
+ );
41
+ this.serialized = Buffer.concat([
42
+ Buffer.from([version]),
43
+ message,
44
+ Buffer.from(signature),
45
+ ]);
46
+ this.messageVersion = this.CURRENT_VERSION;
47
+ this.keyId = keyId;
48
+ this.iteration = iteration;
49
+ this.ciphertext = ciphertextBuffer;
50
+ this.signature = signature;
51
+ }
52
+ }
53
+ getKeyId() {
54
+ return this.keyId;
55
+ }
56
+ getIteration() {
57
+ return this.iteration;
58
+ }
59
+ getCipherText() {
60
+ return this.ciphertext;
61
+ }
62
+ verifySignature(signatureKey) {
63
+ const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
64
+ const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
65
+ const res = verifySignature(signatureKey, part1, part2);
66
+ if (!res) throw new Error("Invalid signature!");
67
+ }
68
+ getSignature(signatureKey, serialized) {
69
+ return Buffer.from(calculateSignature(signatureKey, serialized));
70
+ }
71
+ serialize() {
72
+ return this.serialized;
73
+ }
74
+ getType() {
75
+ return 4;
76
+ }
77
+ }
78
+ module.exports = { SenderKeyMessage: SenderKeyMessage };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function isNull(str) {
4
+ return str === null || str === "";
5
+ }
6
+ function intValue(num) {
7
+ const MAX_VALUE = 2147483647;
8
+ const MIN_VALUE = -2147483648;
9
+ if (num > MAX_VALUE || num < MIN_VALUE) {
10
+ return num & 4294967295;
11
+ }
12
+ return num;
13
+ }
14
+ function hashCode(strKey) {
15
+ let hash = 0;
16
+ if (!isNull(strKey)) {
17
+ for (let i = 0; i < strKey.length; i++) {
18
+ hash = hash * 31 + strKey.charCodeAt(i);
19
+ hash = intValue(hash);
20
+ }
21
+ }
22
+ return hash;
23
+ }
24
+ class SenderKeyName {
25
+ constructor(groupId, sender) {
26
+ this.groupId = groupId;
27
+ this.sender = sender;
28
+ }
29
+ getGroupId() {
30
+ return this.groupId;
31
+ }
32
+ getSender() {
33
+ return this.sender;
34
+ }
35
+ serialize() {
36
+ return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
37
+ }
38
+ toString() {
39
+ return this.serialize();
40
+ }
41
+ equals(other) {
42
+ if (other === null) return false;
43
+ return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
44
+ }
45
+ hashCode() {
46
+ return hashCode(this.groupId) ^ hashCode(this.sender.toString());
47
+ }
48
+ }
49
+ module.exports = { SenderKeyName: SenderKeyName };
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { BufferJSON: BufferJSON } = require("../../Utils/generics");
4
+ const { SenderKeyState: SenderKeyState } = require("./sender-key-state");
5
+ class SenderKeyRecord {
6
+ constructor(serialized) {
7
+ this.MAX_STATES = 5;
8
+ this.senderKeyStates = [];
9
+ if (serialized) {
10
+ for (const structure of serialized) {
11
+ this.senderKeyStates.push(
12
+ new SenderKeyState(null, null, null, null, null, null, structure)
13
+ );
14
+ }
15
+ }
16
+ }
17
+ isEmpty() {
18
+ return this.senderKeyStates.length === 0;
19
+ }
20
+ getSenderKeyState(keyId) {
21
+ if (keyId === undefined && this.senderKeyStates.length) {
22
+ return this.senderKeyStates[this.senderKeyStates.length - 1];
23
+ }
24
+ return this.senderKeyStates.find((state) => state.getKeyId() === keyId);
25
+ }
26
+ addSenderKeyState(id, iteration, chainKey, signatureKey) {
27
+ this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
28
+ if (this.senderKeyStates.length > this.MAX_STATES) {
29
+ this.senderKeyStates.shift();
30
+ }
31
+ }
32
+ setSenderKeyState(id, iteration, chainKey, keyPair) {
33
+ this.senderKeyStates.length = 0;
34
+ this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
35
+ }
36
+ serialize() {
37
+ return this.senderKeyStates.map((state) => state.getStructure());
38
+ }
39
+ static deserialize(data) {
40
+ const str = Buffer.from(data).toString("utf-8");
41
+ const parsed = JSON.parse(str, BufferJSON.reviver);
42
+ return new SenderKeyRecord(parsed);
43
+ }
44
+ }
45
+ module.exports = { SenderKeyRecord: SenderKeyRecord };
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { SenderChainKey: SenderChainKey } = require("./sender-chain-key");
4
+ const { SenderMessageKey: SenderMessageKey } = require("./sender-message-key");
5
+ class SenderKeyState {
6
+ constructor(
7
+ id,
8
+ iteration,
9
+ chainKey,
10
+ signatureKeyPair,
11
+ signatureKeyPublic,
12
+ signatureKeyPrivate,
13
+ senderKeyStateStructure
14
+ ) {
15
+ this.MAX_MESSAGE_KEYS = 2e3;
16
+ if (senderKeyStateStructure) {
17
+ this.senderKeyStateStructure = {
18
+ ...senderKeyStateStructure,
19
+ senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
20
+ ? senderKeyStateStructure.senderMessageKeys
21
+ : [],
22
+ };
23
+ } else {
24
+ if (signatureKeyPair) {
25
+ signatureKeyPublic = signatureKeyPair.public;
26
+ signatureKeyPrivate = signatureKeyPair.private;
27
+ }
28
+ this.senderKeyStateStructure = {
29
+ senderKeyId: id || 0,
30
+ senderChainKey: {
31
+ iteration: iteration || 0,
32
+ seed: Buffer.from(chainKey || []),
33
+ },
34
+ senderSigningKey: {
35
+ public: Buffer.from(signatureKeyPublic || []),
36
+ private: Buffer.from(signatureKeyPrivate || []),
37
+ },
38
+ senderMessageKeys: [],
39
+ };
40
+ }
41
+ }
42
+ getKeyId() {
43
+ return this.senderKeyStateStructure.senderKeyId;
44
+ }
45
+ getSenderChainKey() {
46
+ return new SenderChainKey(
47
+ this.senderKeyStateStructure.senderChainKey.iteration,
48
+ this.senderKeyStateStructure.senderChainKey.seed
49
+ );
50
+ }
51
+ setSenderChainKey(chainKey) {
52
+ this.senderKeyStateStructure.senderChainKey = {
53
+ iteration: chainKey.getIteration(),
54
+ seed: chainKey.getSeed(),
55
+ };
56
+ }
57
+ getSigningKeyPublic() {
58
+ const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public);
59
+ if (publicKey.length === 32) {
60
+ const fixed = Buffer.alloc(33);
61
+ fixed[0] = 5;
62
+ publicKey.copy(fixed, 1);
63
+ return fixed;
64
+ }
65
+ return publicKey;
66
+ }
67
+ getSigningKeyPrivate() {
68
+ const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
69
+ return Buffer.from(privateKey || []);
70
+ }
71
+ hasSenderMessageKey(iteration) {
72
+ return this.senderKeyStateStructure.senderMessageKeys.some(
73
+ (key) => key.iteration === iteration
74
+ );
75
+ }
76
+ addSenderMessageKey(senderMessageKey) {
77
+ this.senderKeyStateStructure.senderMessageKeys.push({
78
+ iteration: senderMessageKey.getIteration(),
79
+ seed: senderMessageKey.getSeed(),
80
+ });
81
+ if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
82
+ this.senderKeyStateStructure.senderMessageKeys.shift();
83
+ }
84
+ }
85
+ removeSenderMessageKey(iteration) {
86
+ const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(
87
+ (key) => key.iteration === iteration
88
+ );
89
+ if (index !== -1) {
90
+ const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
91
+ this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
92
+ return new SenderMessageKey(messageKey.iteration, messageKey.seed);
93
+ }
94
+ return null;
95
+ }
96
+ getStructure() {
97
+ return this.senderKeyStateStructure;
98
+ }
99
+ }
100
+ module.exports = { SenderKeyState: SenderKeyState };
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { deriveSecrets: deriveSecrets } = require("libsignal/src/crypto");
4
+ class SenderMessageKey {
5
+ constructor(iteration, seed) {
6
+ const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from("WhisperGroup"));
7
+ const keys = new Uint8Array(32);
8
+ keys.set(new Uint8Array(derivative[0].slice(16)));
9
+ keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16);
10
+ this.iv = Buffer.from(derivative[0].slice(0, 16));
11
+ this.cipherKey = Buffer.from(keys.buffer);
12
+ this.iteration = iteration;
13
+ this.seed = seed;
14
+ }
15
+ getIteration() {
16
+ return this.iteration;
17
+ }
18
+ getIv() {
19
+ return this.iv;
20
+ }
21
+ getCipherKey() {
22
+ return this.cipherKey;
23
+ }
24
+ getSeed() {
25
+ return this.seed;
26
+ }
27
+ }
28
+ module.exports = { SenderMessageKey: SenderMessageKey };