@baileys-md/baileys 11.2.4 → 12.0.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 (126) hide show
  1. package/LICENSE +1 -1
  2. package/WAProto/WAProto.proto +5311 -0
  3. package/WAProto/index.js +70084 -131686
  4. package/lib/Defaults/index.js +118 -117
  5. package/lib/KeyDB/BinarySearch.js +20 -0
  6. package/lib/KeyDB/KeyedDB.js +167 -0
  7. package/lib/KeyDB/index.js +4 -0
  8. package/lib/Signal/Group/ciphertext-message.js +13 -0
  9. package/lib/Signal/Group/group-session-builder.js +32 -0
  10. package/lib/Signal/Group/group_cipher.js +84 -0
  11. package/lib/Signal/Group/index.js +13 -0
  12. package/lib/Signal/Group/keyhelper.js +20 -0
  13. package/lib/Signal/Group/sender-chain-key.js +28 -0
  14. package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
  15. package/lib/Signal/Group/sender-key-message.js +68 -0
  16. package/{WASignalGroup/sender_key_name.js → lib/Signal/Group/sender-key-name.js} +10 -28
  17. package/lib/Signal/Group/sender-key-record.js +43 -0
  18. package/lib/Signal/Group/sender-key-state.js +86 -0
  19. package/lib/Signal/Group/sender-message-key.js +28 -0
  20. package/lib/Signal/libsignal.js +314 -151
  21. package/lib/Signal/lid-mapping.js +155 -0
  22. package/lib/Socket/Client/index.js +4 -19
  23. package/lib/Socket/Client/types.js +13 -0
  24. package/lib/Socket/Client/websocket.js +52 -0
  25. package/lib/Socket/business.js +359 -242
  26. package/lib/Socket/chats.js +847 -844
  27. package/lib/Socket/communities.js +413 -0
  28. package/lib/Socket/groups.js +304 -319
  29. package/lib/Socket/index.js +15 -9
  30. package/lib/Socket/messages-recv.js +1109 -989
  31. package/lib/Socket/messages-send.js +611 -347
  32. package/lib/Socket/mex.js +45 -0
  33. package/lib/Socket/newsletter.js +230 -231
  34. package/lib/Socket/socket.js +795 -616
  35. package/lib/Store/index.js +6 -8
  36. package/lib/Store/make-cache-manager-store.js +73 -81
  37. package/lib/Store/make-in-memory-store.js +286 -427
  38. package/lib/Store/make-ordered-dictionary.js +77 -79
  39. package/lib/Store/object-repository.js +24 -26
  40. package/lib/Types/Auth.js +3 -2
  41. package/lib/Types/Bussines.js +3 -0
  42. package/lib/Types/Call.js +3 -2
  43. package/lib/Types/Chat.js +9 -4
  44. package/lib/Types/Contact.js +3 -2
  45. package/lib/Types/Events.js +3 -2
  46. package/lib/Types/GroupMetadata.js +3 -2
  47. package/lib/Types/Label.js +24 -26
  48. package/lib/Types/LabelAssociation.js +6 -8
  49. package/lib/Types/Message.js +12 -7
  50. package/lib/Types/Newsletter.js +32 -17
  51. package/lib/Types/Product.js +3 -2
  52. package/lib/Types/Signal.js +3 -2
  53. package/lib/Types/Socket.js +4 -2
  54. package/lib/Types/State.js +11 -2
  55. package/lib/Types/USync.js +3 -2
  56. package/lib/Types/index.js +27 -41
  57. package/lib/Utils/auth-utils.js +211 -198
  58. package/lib/Utils/baileys-event-stream.js +42 -61
  59. package/lib/Utils/browser-utils.js +25 -0
  60. package/lib/Utils/business.js +213 -214
  61. package/lib/Utils/chat-utils.js +711 -689
  62. package/lib/Utils/crypto.js +112 -133
  63. package/lib/Utils/decode-wa-message.js +254 -186
  64. package/lib/Utils/event-buffer.js +510 -502
  65. package/lib/Utils/generics.js +318 -408
  66. package/lib/Utils/history.js +83 -90
  67. package/lib/Utils/index.js +21 -33
  68. package/lib/Utils/link-preview.js +71 -116
  69. package/lib/Utils/logger.js +5 -7
  70. package/lib/Utils/lt-hash.js +40 -46
  71. package/lib/Utils/make-mutex.js +34 -41
  72. package/lib/Utils/message-retry-manager.js +113 -0
  73. package/lib/Utils/messages-media.js +575 -671
  74. package/lib/Utils/messages.js +354 -462
  75. package/lib/Utils/noise-handler.js +138 -149
  76. package/lib/Utils/pre-key-manager.js +85 -0
  77. package/lib/Utils/process-message.js +323 -354
  78. package/lib/Utils/signal.js +148 -130
  79. package/lib/Utils/use-multi-file-auth-state.js +109 -91
  80. package/lib/Utils/validate-connection.js +183 -190
  81. package/lib/WABinary/constants.js +1298 -35
  82. package/lib/WABinary/decode.js +231 -256
  83. package/lib/WABinary/encode.js +207 -239
  84. package/lib/WABinary/generic-utils.js +119 -40
  85. package/lib/WABinary/index.js +7 -21
  86. package/lib/WABinary/jid-utils.js +88 -64
  87. package/lib/WABinary/types.js +3 -2
  88. package/lib/WAM/BinaryInfo.js +10 -12
  89. package/lib/WAM/constants.js +22851 -15348
  90. package/lib/WAM/encode.js +135 -136
  91. package/lib/WAM/index.js +5 -19
  92. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -30
  93. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +49 -53
  94. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -28
  95. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -39
  96. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  97. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +26 -20
  98. package/lib/WAUSync/Protocols/index.js +6 -20
  99. package/lib/WAUSync/USyncQuery.js +86 -85
  100. package/lib/WAUSync/USyncUser.js +23 -25
  101. package/lib/WAUSync/index.js +5 -19
  102. package/lib/index.js +18 -33
  103. package/package.json +52 -57
  104. package/README.md +0 -2
  105. package/WASignalGroup/GroupProtocol.js +0 -1697
  106. package/WASignalGroup/ciphertext_message.js +0 -16
  107. package/WASignalGroup/group_cipher.js +0 -120
  108. package/WASignalGroup/group_session_builder.js +0 -46
  109. package/WASignalGroup/index.js +0 -5
  110. package/WASignalGroup/keyhelper.js +0 -21
  111. package/WASignalGroup/protobufs.js +0 -3
  112. package/WASignalGroup/queue_job.js +0 -69
  113. package/WASignalGroup/sender_chain_key.js +0 -50
  114. package/WASignalGroup/sender_key_distribution_message.js +0 -78
  115. package/WASignalGroup/sender_key_message.js +0 -92
  116. package/WASignalGroup/sender_key_record.js +0 -56
  117. package/WASignalGroup/sender_key_state.js +0 -129
  118. package/WASignalGroup/sender_message_key.js +0 -39
  119. package/lib/Defaults/baileys-version.json +0 -3
  120. package/lib/Defaults/phonenumber-mcc.json +0 -223
  121. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  122. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  123. package/lib/Socket/Client/web-socket-client.js +0 -62
  124. package/lib/Socket/registration.js +0 -166
  125. package/lib/Socket/usync.js +0 -70
  126. package/lib/Store/make-mongo-store.js +0 -567
@@ -0,0 +1,65 @@
1
+ //=======================================================//
2
+ import { CiphertextMessage } from "./ciphertext-message.js";
3
+ import { proto } from "../../../WAProto/index.js";
4
+ //=======================================================//
5
+ export 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 = proto.SenderKeyDistributionMessage.decode(message).toJSON();
12
+ this.serialized = serialized;
13
+ this.id = distributionMessage.id;
14
+ this.iteration = distributionMessage.iteration;
15
+ this.chainKey =
16
+ typeof distributionMessage.chainKey === "string"
17
+ ? Buffer.from(distributionMessage.chainKey, "base64")
18
+ : distributionMessage.chainKey;
19
+ this.signatureKey =
20
+ typeof distributionMessage.signingKey === "string"
21
+ ? Buffer.from(distributionMessage.signingKey, "base64")
22
+ : distributionMessage.signingKey;
23
+ }
24
+ catch (e) {
25
+ throw new Error(String(e));
26
+ }
27
+ }
28
+ else {
29
+ const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
30
+ this.id = id;
31
+ this.iteration = iteration;
32
+ this.chainKey = chainKey;
33
+ this.signatureKey = signatureKey;
34
+ const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
35
+ id,
36
+ iteration,
37
+ chainKey,
38
+ signingKey: this.signatureKey
39
+ })).finish();
40
+ this.serialized = Buffer.concat([Buffer.from([version]), message]);
41
+ }
42
+ }
43
+ intsToByteHighAndLow(highValue, lowValue) {
44
+ return (((highValue << 4) | lowValue) & 0xff) % 256;
45
+ }
46
+ serialize() {
47
+ return this.serialized;
48
+ }
49
+ getType() {
50
+ return this.SENDERKEY_DISTRIBUTION_TYPE;
51
+ }
52
+ getIteration() {
53
+ return this.iteration;
54
+ }
55
+ getChainKey() {
56
+ return this.chainKey;
57
+ }
58
+ getSignatureKey() {
59
+ return this.signatureKey;
60
+ }
61
+ getId() {
62
+ return this.id;
63
+ }
64
+ }
65
+ //=======================================================//
@@ -0,0 +1,68 @@
1
+ //=======================================================//
2
+ import { calculateSignature, verifySignature } from "@dyyxyzz/libsignal-dyysilence/src/curve.js";
3
+ import { CiphertextMessage } from "./ciphertext-message.js";
4
+ import { proto } from "../../../WAProto/index.js";
5
+ //=======================================================//
6
+ export class SenderKeyMessage extends CiphertextMessage {
7
+ constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
8
+ super();
9
+ this.SIGNATURE_LENGTH = 64;
10
+ if (serialized) {
11
+ const version = serialized[0];
12
+ const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
13
+ const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
14
+ const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
15
+ this.serialized = serialized;
16
+ this.messageVersion = (version & 0xff) >> 4;
17
+ this.keyId = senderKeyMessage.id;
18
+ this.iteration = senderKeyMessage.iteration;
19
+ this.ciphertext =
20
+ typeof senderKeyMessage.ciphertext === "string"
21
+ ? Buffer.from(senderKeyMessage.ciphertext, "base64")
22
+ : senderKeyMessage.ciphertext;
23
+ this.signature = signature;
24
+ }
25
+ else {
26
+ const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
27
+ const ciphertextBuffer = Buffer.from(ciphertext);
28
+ const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
29
+ id: keyId,
30
+ iteration: iteration,
31
+ ciphertext: ciphertextBuffer
32
+ })).finish();
33
+ const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
34
+ this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
35
+ this.messageVersion = this.CURRENT_VERSION;
36
+ this.keyId = keyId;
37
+ this.iteration = iteration;
38
+ this.ciphertext = ciphertextBuffer;
39
+ this.signature = signature;
40
+ }
41
+ }
42
+ getKeyId() {
43
+ return this.keyId;
44
+ }
45
+ getIteration() {
46
+ return this.iteration;
47
+ }
48
+ getCipherText() {
49
+ return this.ciphertext;
50
+ }
51
+ verifySignature(signatureKey) {
52
+ const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
53
+ const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
54
+ const res = verifySignature(signatureKey, part1, part2);
55
+ if (!res)
56
+ throw new Error("Invalid signature!");
57
+ }
58
+ getSignature(signatureKey, serialized) {
59
+ return Buffer.from(calculateSignature(signatureKey, serialized));
60
+ }
61
+ serialize() {
62
+ return this.serialized;
63
+ }
64
+ getType() {
65
+ return 4;
66
+ }
67
+ }
68
+ //=======================================================//
@@ -1,22 +1,17 @@
1
+ //=======================================================//
1
2
  function isNull(str) {
2
- return str === null || str.value === '';
3
+ return str === null || str === "";
3
4
  }
4
-
5
- /**
6
- * java String hashCode 的实现
7
- * @param strKey
8
- * @return intValue
9
- */
5
+ //=======================================================//
10
6
  function intValue(num) {
11
7
  const MAX_VALUE = 0x7fffffff;
12
8
  const MIN_VALUE = -0x80000000;
13
9
  if (num > MAX_VALUE || num < MIN_VALUE) {
14
- // eslint-disable-next-line
15
- return (num &= 0xffffffff);
10
+ return num & 0xffffffff;
16
11
  }
17
12
  return num;
18
13
  }
19
-
14
+ //=======================================================//
20
15
  function hashCode(strKey) {
21
16
  let hash = 0;
22
17
  if (!isNull(strKey)) {
@@ -27,44 +22,31 @@ function hashCode(strKey) {
27
22
  }
28
23
  return hash;
29
24
  }
30
-
31
- /**
32
- * 将js页面的number类型转换为java的int类型
33
- * @param num
34
- * @return intValue
35
- */
36
-
37
- class SenderKeyName {
25
+ //=======================================================//
26
+ export class SenderKeyName {
38
27
  constructor(groupId, sender) {
39
28
  this.groupId = groupId;
40
29
  this.sender = sender;
41
30
  }
42
-
43
31
  getGroupId() {
44
32
  return this.groupId;
45
33
  }
46
-
47
34
  getSender() {
48
35
  return this.sender;
49
36
  }
50
-
51
37
  serialize() {
52
38
  return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
53
39
  }
54
-
55
40
  toString() {
56
41
  return this.serialize();
57
42
  }
58
-
59
43
  equals(other) {
60
- if (other === null) return false;
61
- if (!(other instanceof SenderKeyName)) return false;
44
+ if (other === null)
45
+ return false;
62
46
  return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
63
47
  }
64
-
65
48
  hashCode() {
66
49
  return hashCode(this.groupId) ^ hashCode(this.sender.toString());
67
50
  }
68
51
  }
69
-
70
- module.exports = SenderKeyName;
52
+ //=======================================================//
@@ -0,0 +1,43 @@
1
+ //=======================================================//
2
+ import { SenderKeyState } from "./sender-key-state.js";
3
+ import { BufferJSON } from "../../Utils/generics.js";
4
+ //=======================================================//
5
+ export 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(new SenderKeyState(null, null, null, null, null, null, structure));
12
+ }
13
+ }
14
+ }
15
+ isEmpty() {
16
+ return this.senderKeyStates.length === 0;
17
+ }
18
+ getSenderKeyState(keyId) {
19
+ if (keyId === undefined && this.senderKeyStates.length) {
20
+ return this.senderKeyStates[this.senderKeyStates.length - 1];
21
+ }
22
+ return this.senderKeyStates.find(state => state.getKeyId() === keyId);
23
+ }
24
+ addSenderKeyState(id, iteration, chainKey, signatureKey) {
25
+ this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
26
+ if (this.senderKeyStates.length > this.MAX_STATES) {
27
+ this.senderKeyStates.shift();
28
+ }
29
+ }
30
+ setSenderKeyState(id, iteration, chainKey, keyPair) {
31
+ this.senderKeyStates.length = 0;
32
+ this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
33
+ }
34
+ serialize() {
35
+ return this.senderKeyStates.map(state => state.getStructure());
36
+ }
37
+ static deserialize(data) {
38
+ const str = Buffer.from(data).toString("utf-8");
39
+ const parsed = JSON.parse(str, BufferJSON.reviver);
40
+ return new SenderKeyRecord(parsed);
41
+ }
42
+ }
43
+ //=======================================================//
@@ -0,0 +1,86 @@
1
+ //=======================================================//
2
+ import { SenderMessageKey } from "./sender-message-key.js";
3
+ import { SenderChainKey } from "./sender-chain-key.js";
4
+ //=======================================================//
5
+ export class SenderKeyState {
6
+ constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
7
+ this.MAX_MESSAGE_KEYS = 2000;
8
+ if (senderKeyStateStructure) {
9
+ this.senderKeyStateStructure = {
10
+ ...senderKeyStateStructure,
11
+ senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
12
+ ? senderKeyStateStructure.senderMessageKeys
13
+ : []
14
+ };
15
+ }
16
+ else {
17
+ if (signatureKeyPair) {
18
+ signatureKeyPublic = signatureKeyPair.public;
19
+ signatureKeyPrivate = signatureKeyPair.private;
20
+ }
21
+ this.senderKeyStateStructure = {
22
+ senderKeyId: id || 0,
23
+ senderChainKey: {
24
+ iteration: iteration || 0,
25
+ seed: Buffer.from(chainKey || [])
26
+ },
27
+ senderSigningKey: {
28
+ public: Buffer.from(signatureKeyPublic || []),
29
+ private: Buffer.from(signatureKeyPrivate || [])
30
+ },
31
+ senderMessageKeys: []
32
+ };
33
+ }
34
+ }
35
+ getKeyId() {
36
+ return this.senderKeyStateStructure.senderKeyId;
37
+ }
38
+ getSenderChainKey() {
39
+ return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
40
+ }
41
+ setSenderChainKey(chainKey) {
42
+ this.senderKeyStateStructure.senderChainKey = {
43
+ iteration: chainKey.getIteration(),
44
+ seed: chainKey.getSeed()
45
+ };
46
+ }
47
+ getSigningKeyPublic() {
48
+ const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public);
49
+ if (publicKey.length === 32) {
50
+ const fixed = Buffer.alloc(33);
51
+ fixed[0] = 0x05;
52
+ publicKey.copy(fixed, 1);
53
+ return fixed;
54
+ }
55
+ return publicKey;
56
+ }
57
+ getSigningKeyPrivate() {
58
+ const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
59
+ return Buffer.from(privateKey || []);
60
+ }
61
+ hasSenderMessageKey(iteration) {
62
+ return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
63
+ }
64
+ addSenderMessageKey(senderMessageKey) {
65
+ this.senderKeyStateStructure.senderMessageKeys.push({
66
+ iteration: senderMessageKey.getIteration(),
67
+ seed: senderMessageKey.getSeed()
68
+ });
69
+ if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
70
+ this.senderKeyStateStructure.senderMessageKeys.shift();
71
+ }
72
+ }
73
+ removeSenderMessageKey(iteration) {
74
+ const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
75
+ if (index !== -1) {
76
+ const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
77
+ this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
78
+ return new SenderMessageKey(messageKey.iteration, messageKey.seed);
79
+ }
80
+ return null;
81
+ }
82
+ getStructure() {
83
+ return this.senderKeyStateStructure;
84
+ }
85
+ }
86
+ //=======================================================//
@@ -0,0 +1,28 @@
1
+ //=======================================================//
2
+ import { deriveSecrets } from "@dyyxyzz/libsignal-dyysilence/src/crypto.js";
3
+ //=======================================================//
4
+ export 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
+ //=======================================================//