@mihiranga_mihiranga/mezuka-baileys 1.0.2

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 (114) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +651 -0
  3. package/WAProto/GenerateStatics.sh +4 -0
  4. package/WAProto/WAProto.proto +4775 -0
  5. package/WAProto/index.js +169661 -0
  6. package/WAProto//342/235/244/357/270/217 +1 -0
  7. package/engine-requirements.js +10 -0
  8. package/lib/Defaults/baileys-version.json +3 -0
  9. package/lib/Defaults/index.js +112 -0
  10. package/lib/Defaults/phonenumber-mcc.json +223 -0
  11. package/lib/Defaults//360/237/222/226 +1 -0
  12. package/lib/Signal/Group/ciphertext-message.js +15 -0
  13. package/lib/Signal/Group/group-session-builder.js +64 -0
  14. package/lib/Signal/Group/group_cipher.js +96 -0
  15. package/lib/Signal/Group/index.js +57 -0
  16. package/lib/Signal/Group/keyhelper.js +55 -0
  17. package/lib/Signal/Group/queue-job.js +57 -0
  18. package/lib/Signal/Group/sender-chain-key.js +34 -0
  19. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  20. package/lib/Signal/Group/sender-key-message.js +69 -0
  21. package/lib/Signal/Group/sender-key-name.js +51 -0
  22. package/lib/Signal/Group/sender-key-record.js +53 -0
  23. package/lib/Signal/Group/sender-key-state.js +99 -0
  24. package/lib/Signal/Group/sender-message-key.js +29 -0
  25. package/lib/Signal/Group//342/235/244/357/270/217 +1 -0
  26. package/lib/Signal/libsignal.js +174 -0
  27. package/lib/Signal//360/237/222/226 +1 -0
  28. package/lib/Socket/Client/index.js +18 -0
  29. package/lib/Socket/Client/types.js +13 -0
  30. package/lib/Socket/Client/websocket.js +72 -0
  31. package/lib/Socket/Client//342/235/244/357/270/217 +1 -0
  32. package/lib/Socket/business.js +260 -0
  33. package/lib/Socket/chats.js +890 -0
  34. package/lib/Socket/groups.js +332 -0
  35. package/lib/Socket/index.js +10 -0
  36. package/lib/Socket/messages-recv.js +1142 -0
  37. package/lib/Socket/messages-send.js +1068 -0
  38. package/lib/Socket/newsletter.js +250 -0
  39. package/lib/Socket/socket.js +654 -0
  40. package/lib/Socket/usync.js +70 -0
  41. package/lib/Socket//360/237/222/236 +1 -0
  42. package/lib/Store/index.js +8 -0
  43. package/lib/Store/make-in-memory-store.js +429 -0
  44. package/lib/Store/make-ordered-dictionary.js +81 -0
  45. package/lib/Store/object-repository.js +27 -0
  46. package/lib/Store//342/235/244/357/270/217 +1 -0
  47. package/lib/Types/Auth.js +2 -0
  48. package/lib/Types/Call.js +2 -0
  49. package/lib/Types/Chat.js +4 -0
  50. package/lib/Types/Contact.js +2 -0
  51. package/lib/Types/Events.js +2 -0
  52. package/lib/Types/GroupMetadata.js +2 -0
  53. package/lib/Types/Label.js +27 -0
  54. package/lib/Types/LabelAssociation.js +9 -0
  55. package/lib/Types/Message.js +7 -0
  56. package/lib/Types/Newsletter.js +18 -0
  57. package/lib/Types/Product.js +2 -0
  58. package/lib/Types/Signal.js +2 -0
  59. package/lib/Types/Socket.js +2 -0
  60. package/lib/Types/State.js +2 -0
  61. package/lib/Types/USync.js +2 -0
  62. package/lib/Types/index.js +42 -0
  63. package/lib/Types//360/237/222/236 +1 -0
  64. package/lib/Utils/auth-utils.js +199 -0
  65. package/lib/Utils/baileys-event-stream.js +63 -0
  66. package/lib/Utils/browser-utils.js +35 -0
  67. package/lib/Utils/business.js +234 -0
  68. package/lib/Utils/chat-utils.js +730 -0
  69. package/lib/Utils/crypto.js +193 -0
  70. package/lib/Utils/decode-wa-message.js +207 -0
  71. package/lib/Utils/event-buffer.js +518 -0
  72. package/lib/Utils/generics.js +473 -0
  73. package/lib/Utils/history.js +95 -0
  74. package/lib/Utils/index.js +33 -0
  75. package/lib/Utils/link-preview.js +126 -0
  76. package/lib/Utils/logger.js +7 -0
  77. package/lib/Utils/lt-hash.js +51 -0
  78. package/lib/Utils/make-mutex.js +43 -0
  79. package/lib/Utils/messages-media.js +880 -0
  80. package/lib/Utils/messages.js +1154 -0
  81. package/lib/Utils/noise-handler.js +150 -0
  82. package/lib/Utils/process-message.js +372 -0
  83. package/lib/Utils/signal.js +153 -0
  84. package/lib/Utils/use-multi-file-auth-state.js +125 -0
  85. package/lib/Utils/validate-connection.js +199 -0
  86. package/lib/Utils//342/235/244/357/270/217 +1 -0
  87. package/lib/WABinary/constants.js +1303 -0
  88. package/lib/WABinary/decode.js +265 -0
  89. package/lib/WABinary/encode.js +250 -0
  90. package/lib/WABinary/generic-utils.js +130 -0
  91. package/lib/WABinary/index.js +21 -0
  92. package/lib/WABinary/jid-utils.js +83 -0
  93. package/lib/WABinary/types.js +2 -0
  94. package/lib/WABinary//360/237/222/226 +1 -0
  95. package/lib/WAM/BinaryInfo.js +13 -0
  96. package/lib/WAM/constants.js +15350 -0
  97. package/lib/WAM/encode.js +155 -0
  98. package/lib/WAM/index.js +19 -0
  99. package/lib/WAM//360/237/222/226 +1 -0
  100. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  101. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  102. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  103. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  104. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  105. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  106. package/lib/WAUSync/Protocols/index.js +20 -0
  107. package/lib/WAUSync/Protocols//342/235/244/357/270/217 +1 -0
  108. package/lib/WAUSync/USyncQuery.js +89 -0
  109. package/lib/WAUSync/USyncUser.js +26 -0
  110. package/lib/WAUSync/index.js +19 -0
  111. package/lib/WAUSync//360/237/222/226 +1 -0
  112. package/lib/index.js +45 -0
  113. package/lib//360/237/222/226 +1 -0
  114. package/package.json +121 -0
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useMultiFileAuthState = void 0;
4
+ const async_mutex_1 = require("async-mutex");
5
+ const promises_1 = require("fs/promises");
6
+ const path_1 = require("path");
7
+ const WAProto_1 = require("../../WAProto");
8
+ const auth_utils_1 = require("./auth-utils");
9
+ const generics_1 = require("./generics");
10
+ // We need to lock files due to the fact that we are using async functions to read and write files
11
+ // https://github.com/WhiskeySockets/Baileys/issues/794
12
+ // https://github.com/nodejs/node/issues/26338
13
+ // Use a Map to store mutexes for each file path
14
+ const fileLocks = new Map();
15
+ // Get or create a mutex for a specific file path
16
+ const getFileLock = (path) => {
17
+ let mutex = fileLocks.get(path);
18
+ if (!mutex) {
19
+ mutex = new async_mutex_1.Mutex();
20
+ fileLocks.set(path, mutex);
21
+ }
22
+ return mutex;
23
+ };
24
+ /**
25
+ * stores the full authentication state in a single folder.
26
+ * Far more efficient than singlefileauthstate
27
+ *
28
+ * Again, I wouldn't endorse this for any production level use other than perhaps a bot.
29
+ * Would recommend writing an auth state for use with a proper SQL or No-SQL DB
30
+ * */
31
+ const useMultiFileAuthState = async (folder) => {
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ const writeData = async (data, file) => {
34
+ const filePath = (0, path_1.join)(folder, fixFileName(file));
35
+ const mutex = getFileLock(filePath);
36
+ return mutex.acquire().then(async (release) => {
37
+ try {
38
+ await (0, promises_1.writeFile)(filePath, JSON.stringify(data, generics_1.BufferJSON.replacer));
39
+ }
40
+ finally {
41
+ release();
42
+ }
43
+ });
44
+ };
45
+ const readData = async (file) => {
46
+ try {
47
+ const filePath = (0, path_1.join)(folder, fixFileName(file));
48
+ const mutex = getFileLock(filePath);
49
+ return await mutex.acquire().then(async (release) => {
50
+ try {
51
+ const data = await (0, promises_1.readFile)(filePath, { encoding: 'utf-8' });
52
+ return JSON.parse(data, generics_1.BufferJSON.reviver);
53
+ }
54
+ finally {
55
+ release();
56
+ }
57
+ });
58
+ }
59
+ catch (error) {
60
+ return null;
61
+ }
62
+ };
63
+ const removeData = async (file) => {
64
+ try {
65
+ const filePath = (0, path_1.join)(folder, fixFileName(file));
66
+ const mutex = getFileLock(filePath);
67
+ return mutex.acquire().then(async (release) => {
68
+ try {
69
+ await (0, promises_1.unlink)(filePath);
70
+ }
71
+ catch (_a) {
72
+ }
73
+ finally {
74
+ release();
75
+ }
76
+ });
77
+ }
78
+ catch (_a) {
79
+ }
80
+ };
81
+ const folderInfo = await (0, promises_1.stat)(folder).catch(() => { });
82
+ if (folderInfo) {
83
+ if (!folderInfo.isDirectory()) {
84
+ throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);
85
+ }
86
+ }
87
+ else {
88
+ await (0, promises_1.mkdir)(folder, { recursive: true });
89
+ }
90
+ const fixFileName = (file) => { var _a; return (_a = file === null || file === void 0 ? void 0 : file.replace(/\//g, '__')) === null || _a === void 0 ? void 0 : _a.replace(/:/g, '-'); };
91
+ const creds = await readData('creds.json') || (0, auth_utils_1.initAuthCreds)();
92
+ return {
93
+ state: {
94
+ creds,
95
+ keys: {
96
+ get: async (type, ids) => {
97
+ const data = {};
98
+ await Promise.all(ids.map(async (id) => {
99
+ let value = await readData(`${type}-${id}.json`);
100
+ if (type === 'app-state-sync-key' && value) {
101
+ value = WAProto_1.proto.Message.AppStateSyncKeyData.fromObject(value);
102
+ }
103
+ data[id] = value;
104
+ }));
105
+ return data;
106
+ },
107
+ set: async (data) => {
108
+ const tasks = [];
109
+ for (const category in data) {
110
+ for (const id in data[category]) {
111
+ const value = data[category][id];
112
+ const file = `${category}-${id}.json`;
113
+ tasks.push(value ? writeData(value, file) : removeData(file));
114
+ }
115
+ }
116
+ await Promise.all(tasks);
117
+ }
118
+ }
119
+ },
120
+ saveCreds: async () => {
121
+ return writeData(creds, 'creds.json');
122
+ }
123
+ };
124
+ };
125
+ exports.useMultiFileAuthState = useMultiFileAuthState;
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeSignedDeviceIdentity = exports.configureSuccessfulPairing = exports.generateRegistrationNode = exports.generateLoginNode = 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");
12
+ const getUserAgent = (config) => {
13
+ return {
14
+ appVersion: {
15
+ primary: config.version[0],
16
+ secondary: config.version[1],
17
+ tertiary: config.version[2],
18
+ },
19
+ platform: WAProto_1.proto.ClientPayload.UserAgent.Platform.WEB,
20
+ releaseChannel: WAProto_1.proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
21
+ osVersion: '0.1',
22
+ device: 'Desktop',
23
+ osBuildNumber: '0.1',
24
+ localeLanguageIso6391: 'en',
25
+ mnc: '000',
26
+ mcc: '000',
27
+ localeCountryIso31661Alpha2: 'US'
28
+ };
29
+ };
30
+ const PLATFORM_MAP = {
31
+ 'Mac OS': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
32
+ 'Windows': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
33
+ };
34
+ const getWebInfo = (config) => {
35
+ let webSubPlatform = WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
36
+ if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]] && config.browser[1] === 'Dekstop') {
37
+ webSubPlatform = PLATFORM_MAP[config.browser[0]];
38
+ }
39
+ return { webSubPlatform };
40
+ };
41
+ const getClientPayload = (config) => {
42
+ const payload = {
43
+ connectType: WAProto_1.proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
44
+ connectReason: WAProto_1.proto.ClientPayload.ConnectReason.USER_ACTIVATED,
45
+ userAgent: getUserAgent(config),
46
+ };
47
+ payload.webInfo = getWebInfo(config);
48
+ return payload;
49
+ };
50
+ const generateLoginNode = (userJid, config) => {
51
+ const { user, device } = (0, WABinary_1.jidDecode)(userJid);
52
+ const payload = {
53
+ ...getClientPayload(config),
54
+ passive: true,
55
+ pull: true,
56
+ username: +user,
57
+ device: device,
58
+ lidDbMigrated: false
59
+ };
60
+ return WAProto_1.proto.ClientPayload.fromObject(payload);
61
+ };
62
+ exports.generateLoginNode = generateLoginNode;
63
+ const getPlatformType = (platform) => {
64
+ const platformType = platform.toUpperCase();
65
+ return WAProto_1.proto.DeviceProps.PlatformType[platformType] || WAProto_1.proto.DeviceProps.PlatformType.CHROME;
66
+ };
67
+ const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
68
+ // the app version needs to be md5 hashed
69
+ // and passed in
70
+ const appVersionBuf = (0, crypto_1.createHash)('md5')
71
+ .update(config.version.join('.')) // join as string
72
+ .digest();
73
+ const companion = {
74
+ os: config.browser[0],
75
+ platformType: getPlatformType(config.browser[1]),
76
+ requireFullSync: config.syncFullHistory,
77
+ historySyncConfig: {
78
+ storageQuotaMb: 10240,
79
+ inlineInitialPayloadInE2EeMsg: true,
80
+ recentSyncDaysLimit: undefined,
81
+ supportCallLogHistory: false,
82
+ supportBotUserAgentChatHistory: true,
83
+ supportCagReactionsAndPolls: true,
84
+ supportBizHostedMsg: true,
85
+ supportRecentSyncChunkMessageCountTuning: true,
86
+ supportHostedGroupMsg: true,
87
+ supportFbidBotChatHistory: true,
88
+ supportAddOnHistorySyncMigration: undefined,
89
+ supportMessageAssociation: true,
90
+ supportGroupHistory: false,
91
+ onDemandReady: undefined,
92
+ supportGuestChat: undefined
93
+ },
94
+ version: {
95
+ primary: 10,
96
+ secondary: 15,
97
+ tertiary: 7
98
+ }
99
+ };
100
+ const companionProto = WAProto_1.proto.DeviceProps.encode(companion).finish();
101
+ const registerPayload = {
102
+ ...getClientPayload(config),
103
+ passive: false,
104
+ pull: false,
105
+ devicePairingData: {
106
+ buildHash: appVersionBuf,
107
+ deviceProps: companionProto,
108
+ eRegid: (0, generics_1.encodeBigEndian)(registrationId),
109
+ eKeytype: Defaults_1.KEY_BUNDLE_TYPE,
110
+ eIdent: signedIdentityKey.public,
111
+ eSkeyId: (0, generics_1.encodeBigEndian)(signedPreKey.keyId, 3),
112
+ eSkeyVal: signedPreKey.keyPair.public,
113
+ eSkeySig: signedPreKey.signature,
114
+ },
115
+ };
116
+ return WAProto_1.proto.ClientPayload.fromObject(registerPayload);
117
+ };
118
+ exports.generateRegistrationNode = generateRegistrationNode;
119
+ const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
120
+ const msgId = stanza.attrs.id;
121
+ const pairSuccessNode = (0, WABinary_1.getBinaryNodeChild)(stanza, 'pair-success');
122
+ const deviceIdentityNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device-identity');
123
+ const platformNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'platform');
124
+ const deviceNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device');
125
+ const businessNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'biz');
126
+ if (!deviceIdentityNode || !deviceNode) {
127
+ throw new boom_1.Boom('Missing device-identity or device in pair success node', { data: stanza });
128
+ }
129
+ const bizName = businessNode === null || businessNode === void 0 ? void 0 : businessNode.attrs.name;
130
+ const lid = deviceNode.attrs.lid;
131
+ const jid = deviceNode.attrs.jid;
132
+ const { details, hmac, accountType } = WAProto_1.proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
133
+ const isHostedAccount = accountType !== undefined && accountType === WAProto_1.proto.ADVEncryptionType.HOSTED;
134
+ const hmacPrefix = isHostedAccount ? Buffer.from([6, 5]) : Buffer.alloc(0);
135
+ const advSign = (0, crypto_2.hmacSign)(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
136
+ if (Buffer.compare(hmac, advSign) !== 0) {
137
+ throw new boom_1.Boom('Invalid account signature');
138
+ }
139
+ const account = WAProto_1.proto.ADVSignedDeviceIdentity.decode(details);
140
+ const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
141
+ const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails, signedIdentityKey.public]);
142
+ if (!crypto_2.Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
143
+ throw new boom_1.Boom('Failed to verify account signature');
144
+ }
145
+ const devicePrefix = isHostedAccount ? Buffer.from([6, 6]) : Buffer.from([6, 1]);
146
+ const deviceMsg = Buffer.concat([devicePrefix, deviceDetails, signedIdentityKey.public, accountSignatureKey]);
147
+ account.deviceSignature = crypto_2.Curve.sign(signedIdentityKey.private, deviceMsg);
148
+ const identity = (0, signal_1.createSignalIdentity)(jid, accountSignatureKey);
149
+ const accountEnc = (0, exports.encodeSignedDeviceIdentity)(account, false);
150
+ const deviceIdentity = WAProto_1.proto.ADVDeviceIdentity.decode(account.details);
151
+ const reply = {
152
+ tag: 'iq',
153
+ attrs: {
154
+ to: WABinary_1.S_WHATSAPP_NET,
155
+ type: 'result',
156
+ id: msgId,
157
+ },
158
+ content: [
159
+ {
160
+ tag: 'pair-device-sign',
161
+ attrs: {},
162
+ content: [
163
+ {
164
+ tag: 'device-identity',
165
+ attrs: { 'key-index': deviceIdentity.keyIndex.toString() },
166
+ content: accountEnc
167
+ }
168
+ ]
169
+ }
170
+ ]
171
+ };
172
+ const authUpdate = {
173
+ account,
174
+ me: { id: jid, name: bizName, lid },
175
+ signalIdentities: [
176
+ ...(signalIdentities || []),
177
+ identity
178
+ ],
179
+ platform: platformNode === null || platformNode === void 0 ? void 0 : platformNode.attrs.name
180
+ };
181
+ return {
182
+ creds: authUpdate,
183
+ reply
184
+ };
185
+ };
186
+ exports.configureSuccessfulPairing = configureSuccessfulPairing;
187
+ const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
188
+ var _a;
189
+ account = { ...account };
190
+ // set to null if we are not to include the signature key
191
+ // or if we are including the signature key but it is empty
192
+ if (!includeSignatureKey || !((_a = account.accountSignatureKey) === null || _a === void 0 ? void 0 : _a.length)) {
193
+ account.accountSignatureKey = null;
194
+ }
195
+ return WAProto_1.proto.ADVSignedDeviceIdentity
196
+ .encode(account)
197
+ .finish();
198
+ };
199
+ exports.encodeSignedDeviceIdentity = encodeSignedDeviceIdentity;
@@ -0,0 +1 @@
1
+