@baileys-md/baileys 10.1.0 → 11.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 (122) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1303 -2
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +4633 -0
  5. package/WAProto/fix-imports.js +29 -0
  6. package/WAProto/index.js +13516 -4182
  7. package/lib/Defaults/baileys-version.js +1 -0
  8. package/lib/Defaults/index.js +51 -72
  9. package/lib/Signal/Group/ciphertext-message.js +12 -0
  10. package/lib/Signal/Group/group-session-builder.js +30 -0
  11. package/lib/Signal/Group/group_cipher.js +94 -0
  12. package/lib/Signal/Group/index.js +12 -0
  13. package/lib/Signal/Group/keyhelper.js +19 -0
  14. package/lib/Signal/Group/queue-job.js +54 -0
  15. package/lib/Signal/Group/sender-chain-key.js +32 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  17. package/lib/Signal/Group/sender-key-message.js +67 -0
  18. package/lib/Signal/Group/sender-key-name.js +48 -0
  19. package/lib/Signal/Group/sender-key-record.js +50 -0
  20. package/lib/Signal/Group/sender-key-state.js +96 -0
  21. package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +4 -16
  22. package/lib/Signal/libsignal.js +41 -61
  23. package/lib/Socket/Client/index.js +3 -19
  24. package/lib/Socket/Client/types.js +11 -0
  25. package/lib/Socket/Client/websocket.js +50 -0
  26. package/lib/Socket/business.js +37 -42
  27. package/lib/Socket/chats.js +194 -187
  28. package/lib/Socket/communities.js +351 -0
  29. package/lib/Socket/groups.js +87 -90
  30. package/lib/Socket/index.js +7 -8
  31. package/lib/Socket/messages-recv.js +360 -335
  32. package/lib/Socket/messages-send.js +156 -279
  33. package/lib/Socket/mex.js +42 -0
  34. package/lib/Socket/newsletter.js +144 -213
  35. package/lib/Socket/socket.js +128 -161
  36. package/lib/Socket/usync.js +19 -26
  37. package/lib/Types/Auth.js +2 -2
  38. package/lib/Types/Call.js +2 -2
  39. package/lib/Types/Chat.js +8 -4
  40. package/lib/Types/Contact.js +2 -2
  41. package/lib/Types/Events.js +2 -2
  42. package/lib/Types/GroupMetadata.js +2 -2
  43. package/lib/Types/Label.js +3 -5
  44. package/lib/Types/LabelAssociation.js +3 -5
  45. package/lib/Types/Message.js +7 -7
  46. package/lib/Types/Newsletter.js +30 -17
  47. package/lib/Types/Product.js +2 -2
  48. package/lib/Types/Signal.js +2 -2
  49. package/lib/Types/Socket.js +3 -2
  50. package/lib/Types/State.js +2 -2
  51. package/lib/Types/USync.js +2 -2
  52. package/lib/Types/index.js +15 -31
  53. package/lib/Utils/auth-utils.js +31 -47
  54. package/lib/Utils/baileys-event-stream.js +15 -22
  55. package/lib/Utils/business.js +66 -69
  56. package/lib/Utils/chat-utils.js +200 -195
  57. package/lib/Utils/crypto.js +70 -85
  58. package/lib/Utils/decode-wa-message.js +47 -51
  59. package/lib/Utils/event-buffer.js +36 -46
  60. package/lib/Utils/generics.js +116 -188
  61. package/lib/Utils/history.js +37 -46
  62. package/lib/Utils/index.js +19 -33
  63. package/lib/Utils/link-preview.js +14 -55
  64. package/lib/Utils/logger.js +3 -7
  65. package/lib/Utils/lt-hash.js +23 -26
  66. package/lib/{Store → Utils}/make-in-memory-store.js +19 -27
  67. package/lib/Utils/make-mutex.js +7 -10
  68. package/lib/{Store → Utils}/make-ordered-dictionary.js +1 -3
  69. package/lib/Utils/messages-media.js +236 -368
  70. package/lib/Utils/messages.js +278 -510
  71. package/lib/Utils/noise-handler.js +22 -31
  72. package/lib/{Store → Utils}/object-repository.js +1 -4
  73. package/lib/Utils/process-message.js +144 -148
  74. package/lib/Utils/signal.js +71 -64
  75. package/lib/Utils/use-multi-file-auth-state.js +112 -84
  76. package/lib/Utils/validate-connection.js +72 -115
  77. package/lib/WABinary/constants.js +1281 -20
  78. package/lib/WABinary/decode.js +15 -52
  79. package/lib/WABinary/encode.js +14 -48
  80. package/lib/WABinary/generic-utils.js +31 -39
  81. package/lib/WABinary/index.js +6 -21
  82. package/lib/WABinary/jid-utils.js +23 -40
  83. package/lib/WABinary/types.js +2 -2
  84. package/lib/WAM/BinaryInfo.js +2 -5
  85. package/lib/WAM/constants.js +2257 -2366
  86. package/lib/WAM/encode.js +17 -21
  87. package/lib/WAM/index.js +4 -19
  88. package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
  89. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
  90. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
  91. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
  92. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
  93. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +3 -6
  94. package/lib/WAUSync/Protocols/index.js +5 -20
  95. package/lib/WAUSync/USyncQuery.js +34 -32
  96. package/lib/WAUSync/USyncUser.js +2 -5
  97. package/lib/WAUSync/index.js +4 -19
  98. package/lib/index.js +11 -33
  99. package/package.json +25 -54
  100. package/WASignalGroup/GroupProtocol.js +0 -1697
  101. package/WASignalGroup/ciphertext_message.js +0 -16
  102. package/WASignalGroup/group_cipher.js +0 -120
  103. package/WASignalGroup/group_session_builder.js +0 -46
  104. package/WASignalGroup/index.js +0 -5
  105. package/WASignalGroup/keyhelper.js +0 -21
  106. package/WASignalGroup/protobufs.js +0 -3
  107. package/WASignalGroup/queue_job.js +0 -69
  108. package/WASignalGroup/sender_chain_key.js +0 -50
  109. package/WASignalGroup/sender_key_distribution_message.js +0 -78
  110. package/WASignalGroup/sender_key_message.js +0 -92
  111. package/WASignalGroup/sender_key_name.js +0 -70
  112. package/WASignalGroup/sender_key_record.js +0 -56
  113. package/WASignalGroup/sender_key_state.js +0 -129
  114. package/lib/Defaults/baileys-version.json +0 -3
  115. package/lib/Defaults/phonenumber-mcc.json +0 -223
  116. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  117. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  118. package/lib/Socket/Client/web-socket-client.js +0 -62
  119. package/lib/Socket/registration.js +0 -166
  120. package/lib/Store/index.js +0 -8
  121. package/lib/Store/make-cache-manager-store.js +0 -83
  122. package/lib/Store/make-mongo-store.js +0 -567
@@ -1,47 +1,35 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.encodeSignedDeviceIdentity = exports.configureSuccessfulPairing = exports.generateRegistrationNode = exports.generateLoginNode = exports.generateMobileNode = void 0;
4
- const boom_1 = require("@hapi/boom");
5
- const crypto_1 = require("crypto");
6
- const WAProto_1 = require("../../WAProto");
7
- const Defaults_1 = require("../Defaults");
8
- const WABinary_1 = require("../WABinary");
9
- const crypto_2 = require("./crypto");
10
- const generics_1 = require("./generics");
11
- const signal_1 = require("./signal");
1
+ import { Boom } from '@hapi/boom';
2
+ import { createHash } from 'crypto';
3
+ import { proto } from '../../WAProto/index.js';
4
+ import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
5
+ import { getBinaryNodeChild, jidDecode, S_WHATSAPP_NET } from '../WABinary/index.js';
6
+ import { Curve, hmacSign } from './crypto.js';
7
+ import { encodeBigEndian } from './generics.js';
8
+ import { createSignalIdentity } from './signal.js';
12
9
  const getUserAgent = (config) => {
13
- var _a, _b;
14
- const osVersion = config.mobile ? '15.3.1' : '0.1';
15
- const version = config.mobile ? [2, 24, 6] : config.version;
16
- const device = config.mobile ? 'iPhone_7' : 'Desktop';
17
- const manufacturer = config.mobile ? 'Apple' : '';
18
- const platform = config.mobile ? WAProto_1.proto.ClientPayload.UserAgent.Platform.IOS : WAProto_1.proto.ClientPayload.UserAgent.Platform.WEB;
19
- const phoneId = config.mobile ? { phoneId: config.auth.creds.phoneId } : {};
20
10
  return {
21
11
  appVersion: {
22
- primary: version[0],
23
- secondary: version[1],
24
- tertiary: version[2],
12
+ primary: config.version[0],
13
+ secondary: config.version[1],
14
+ tertiary: config.version[2]
25
15
  },
26
- platform,
27
- releaseChannel: WAProto_1.proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
28
- mcc: ((_a = config.auth.creds.registration) === null || _a === void 0 ? void 0 : _a.phoneNumberMobileCountryCode) || '000',
29
- mnc: ((_b = config.auth.creds.registration) === null || _b === void 0 ? void 0 : _b.phoneNumberMobileNetworkCode) || '000',
30
- osVersion: osVersion,
31
- manufacturer,
32
- device,
33
- osBuildNumber: osVersion,
16
+ platform: proto.ClientPayload.UserAgent.Platform.WEB,
17
+ releaseChannel: proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
18
+ osVersion: '0.1',
19
+ device: 'Desktop',
20
+ osBuildNumber: '0.1',
34
21
  localeLanguageIso6391: 'en',
35
- localeCountryIso31661Alpha2: config.countryCode,
36
- ...phoneId
22
+ mnc: '000',
23
+ mcc: '000',
24
+ localeCountryIso31661Alpha2: config.countryCode
37
25
  };
38
26
  };
39
27
  const PLATFORM_MAP = {
40
- 'Mac OS': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
41
- 'Windows': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
28
+ 'Mac OS': proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
29
+ Windows: proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
42
30
  };
43
31
  const getWebInfo = (config) => {
44
- let webSubPlatform = WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
32
+ let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
45
33
  if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
46
34
  webSubPlatform = PLATFORM_MAP[config.browser[0]];
47
35
  }
@@ -49,64 +37,41 @@ const getWebInfo = (config) => {
49
37
  };
50
38
  const getClientPayload = (config) => {
51
39
  const payload = {
52
- connectType: WAProto_1.proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
53
- connectReason: WAProto_1.proto.ClientPayload.ConnectReason.USER_ACTIVATED,
54
- userAgent: getUserAgent(config),
40
+ connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
41
+ connectReason: proto.ClientPayload.ConnectReason.USER_ACTIVATED,
42
+ userAgent: getUserAgent(config)
55
43
  };
56
- if (!config.mobile) {
57
- payload.webInfo = getWebInfo(config);
58
- }
44
+ payload.webInfo = getWebInfo(config);
59
45
  return payload;
60
46
  };
61
- const generateMobileNode = (config) => {
62
- if (!config.auth.creds) {
63
- throw new boom_1.Boom('No registration data found', { data: config });
64
- }
65
- const payload = {
66
- ...getClientPayload(config),
67
- sessionId: Math.floor(Math.random() * 999999999 + 1),
68
- shortConnect: true,
69
- connectAttemptCount: 0,
70
- device: 0,
71
- dnsSource: {
72
- appCached: false,
73
- dnsMethod: WAProto_1.proto.ClientPayload.DNSSource.DNSResolutionMethod.SYSTEM,
74
- },
75
- passive: false, // XMPP heartbeat setting (false: server actively pings) (true: client actively pings)
76
- pushName: 'test',
77
- username: Number(`${config.auth.creds.registration.phoneNumberCountryCode}${config.auth.creds.registration.phoneNumberNationalNumber}`),
78
- };
79
- return WAProto_1.proto.ClientPayload.fromObject(payload);
80
- };
81
- exports.generateMobileNode = generateMobileNode;
82
- const generateLoginNode = (userJid, config) => {
83
- const { user, device } = (0, WABinary_1.jidDecode)(userJid);
47
+ export const generateLoginNode = (userJid, config) => {
48
+ const { user, device } = jidDecode(userJid);
84
49
  const payload = {
85
50
  ...getClientPayload(config),
86
51
  passive: false,
87
52
  pull: true,
88
53
  username: +user,
89
- device: device,
54
+ device: device
90
55
  };
91
- return WAProto_1.proto.ClientPayload.fromObject(payload);
56
+ return proto.ClientPayload.fromObject(payload);
92
57
  };
93
- exports.generateLoginNode = generateLoginNode;
94
58
  const getPlatformType = (platform) => {
95
59
  const platformType = platform.toUpperCase();
96
- return WAProto_1.proto.DeviceProps.PlatformType[platformType] || WAProto_1.proto.DeviceProps.PlatformType.DESKTOP;
60
+ return (proto.DeviceProps.PlatformType[platformType] ||
61
+ proto.DeviceProps.PlatformType.DESKTOP);
97
62
  };
98
- const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
63
+ export const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
99
64
  // the app version needs to be md5 hashed
100
65
  // and passed in
101
- const appVersionBuf = (0, crypto_1.createHash)('md5')
66
+ const appVersionBuf = createHash('md5')
102
67
  .update(config.version.join('.')) // join as string
103
68
  .digest();
104
69
  const companion = {
105
70
  os: config.browser[0],
106
71
  platformType: getPlatformType(config.browser[1]),
107
- requireFullSync: config.syncFullHistory,
72
+ requireFullSync: config.syncFullHistory
108
73
  };
109
- const companionProto = WAProto_1.proto.DeviceProps.encode(companion).finish();
74
+ const companionProto = proto.DeviceProps.encode(companion).finish();
110
75
  const registerPayload = {
111
76
  ...getClientPayload(config),
112
77
  passive: false,
@@ -114,54 +79,53 @@ const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentity
114
79
  devicePairingData: {
115
80
  buildHash: appVersionBuf,
116
81
  deviceProps: companionProto,
117
- eRegid: (0, generics_1.encodeBigEndian)(registrationId),
118
- eKeytype: Defaults_1.KEY_BUNDLE_TYPE,
82
+ eRegid: encodeBigEndian(registrationId),
83
+ eKeytype: KEY_BUNDLE_TYPE,
119
84
  eIdent: signedIdentityKey.public,
120
- eSkeyId: (0, generics_1.encodeBigEndian)(signedPreKey.keyId, 3),
85
+ eSkeyId: encodeBigEndian(signedPreKey.keyId, 3),
121
86
  eSkeyVal: signedPreKey.keyPair.public,
122
- eSkeySig: signedPreKey.signature,
123
- },
87
+ eSkeySig: signedPreKey.signature
88
+ }
124
89
  };
125
- return WAProto_1.proto.ClientPayload.fromObject(registerPayload);
90
+ return proto.ClientPayload.fromObject(registerPayload);
126
91
  };
127
- exports.generateRegistrationNode = generateRegistrationNode;
128
- const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
92
+ export const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
129
93
  const msgId = stanza.attrs.id;
130
- const pairSuccessNode = (0, WABinary_1.getBinaryNodeChild)(stanza, 'pair-success');
131
- const deviceIdentityNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device-identity');
132
- const platformNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'platform');
133
- const deviceNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device');
134
- const businessNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'biz');
94
+ const pairSuccessNode = getBinaryNodeChild(stanza, 'pair-success');
95
+ const deviceIdentityNode = getBinaryNodeChild(pairSuccessNode, 'device-identity');
96
+ const platformNode = getBinaryNodeChild(pairSuccessNode, 'platform');
97
+ const deviceNode = getBinaryNodeChild(pairSuccessNode, 'device');
98
+ const businessNode = getBinaryNodeChild(pairSuccessNode, 'biz');
135
99
  if (!deviceIdentityNode || !deviceNode) {
136
- throw new boom_1.Boom('Missing device-identity or device in pair success node', { data: stanza });
100
+ throw new Boom('Missing device-identity or device in pair success node', { data: stanza });
137
101
  }
138
- const bizName = businessNode === null || businessNode === void 0 ? void 0 : businessNode.attrs.name;
102
+ const bizName = businessNode?.attrs.name;
139
103
  const jid = deviceNode.attrs.jid;
140
- const { details, hmac } = WAProto_1.proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
141
- // check HMAC matches
142
- const advSign = (0, crypto_2.hmacSign)(details, Buffer.from(advSecretKey, 'base64'));
104
+ const { details, hmac, accountType } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
105
+ const isHostedAccount = accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED;
106
+ const hmacPrefix = isHostedAccount ? Buffer.from([6, 5]) : Buffer.alloc(0);
107
+ const advSign = hmacSign(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
143
108
  if (Buffer.compare(hmac, advSign) !== 0) {
144
- throw new boom_1.Boom('Invalid account signature');
109
+ throw new Boom('Invalid account signature');
145
110
  }
146
- const account = WAProto_1.proto.ADVSignedDeviceIdentity.decode(details);
111
+ const account = proto.ADVSignedDeviceIdentity.decode(details);
147
112
  const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
148
- // verify the device signature matches
149
113
  const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails, signedIdentityKey.public]);
150
- if (!crypto_2.Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
151
- throw new boom_1.Boom('Failed to verify account signature');
114
+ if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
115
+ throw new Boom('Failed to verify account signature');
152
116
  }
153
- // sign the details with our identity key
154
- const deviceMsg = Buffer.concat([Buffer.from([6, 1]), deviceDetails, signedIdentityKey.public, accountSignatureKey]);
155
- account.deviceSignature = crypto_2.Curve.sign(signedIdentityKey.private, deviceMsg);
156
- const identity = (0, signal_1.createSignalIdentity)(jid, accountSignatureKey);
157
- const accountEnc = (0, exports.encodeSignedDeviceIdentity)(account, false);
158
- const deviceIdentity = WAProto_1.proto.ADVDeviceIdentity.decode(account.details);
117
+ const devicePrefix = isHostedAccount ? Buffer.from([6, 6]) : Buffer.from([6, 1]);
118
+ const deviceMsg = Buffer.concat([devicePrefix, deviceDetails, signedIdentityKey.public, accountSignatureKey]);
119
+ account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg);
120
+ const identity = createSignalIdentity(jid, accountSignatureKey);
121
+ const accountEnc = encodeSignedDeviceIdentity(account, false);
122
+ const deviceIdentity = proto.ADVDeviceIdentity.decode(account.details);
159
123
  const reply = {
160
124
  tag: 'iq',
161
125
  attrs: {
162
- to: WABinary_1.S_WHATSAPP_NET,
126
+ to: S_WHATSAPP_NET,
163
127
  type: 'result',
164
- id: msgId,
128
+ id: msgId
165
129
  },
166
130
  content: [
167
131
  {
@@ -180,28 +144,21 @@ const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, s
180
144
  const authUpdate = {
181
145
  account,
182
146
  me: { id: jid, name: bizName },
183
- signalIdentities: [
184
- ...(signalIdentities || []),
185
- identity
186
- ],
187
- platform: platformNode === null || platformNode === void 0 ? void 0 : platformNode.attrs.name
147
+ signalIdentities: [...(signalIdentities || []), identity],
148
+ platform: platformNode?.attrs.name
188
149
  };
189
150
  return {
190
151
  creds: authUpdate,
191
152
  reply
192
153
  };
193
154
  };
194
- exports.configureSuccessfulPairing = configureSuccessfulPairing;
195
- const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
196
- var _a;
155
+ export const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
197
156
  account = { ...account };
198
157
  // set to null if we are not to include the signature key
199
158
  // or if we are including the signature key but it is empty
200
- if (!includeSignatureKey || !((_a = account.accountSignatureKey) === null || _a === void 0 ? void 0 : _a.length)) {
159
+ if (!includeSignatureKey || !account.accountSignatureKey?.length) {
201
160
  account.accountSignatureKey = null;
202
161
  }
203
- return WAProto_1.proto.ADVSignedDeviceIdentity
204
- .encode(account)
205
- .finish();
162
+ return proto.ADVSignedDeviceIdentity.encode(account).finish();
206
163
  };
207
- exports.encodeSignedDeviceIdentity = encodeSignedDeviceIdentity;
164
+ //# sourceMappingURL=validate-connection.js.map