gifted-baileys 1.5.0 → 1.5.4

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 (120) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1429 -684
  3. package/WAProto/WAProto.proto +969 -88
  4. package/WAProto/index.d.ts +13199 -1260
  5. package/WAProto/index.js +124901 -74525
  6. package/lib/Defaults/baileys-version.json +1 -1
  7. package/lib/Defaults/index.d.ts +2 -2
  8. package/lib/Defaults/index.js +6 -5
  9. package/lib/Defaults/phonenumber-mcc.json +221 -221
  10. package/lib/Signal/libsignal.js +18 -9
  11. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -2
  12. package/lib/Socket/Client/index.d.ts +2 -3
  13. package/lib/Socket/Client/index.js +2 -3
  14. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -1
  15. package/lib/Socket/Client/types.d.ts +17 -0
  16. package/lib/Socket/Client/types.js +13 -0
  17. package/lib/Socket/Client/{web-socket-client.d.ts → websocket.d.ts} +1 -1
  18. package/lib/Socket/Client/{web-socket-client.js → websocket.js} +2 -2
  19. package/lib/Socket/business.d.ts +64 -29
  20. package/lib/Socket/business.js +1 -0
  21. package/lib/Socket/chats.d.ts +10 -8
  22. package/lib/Socket/chats.js +114 -97
  23. package/lib/Socket/groups.d.ts +10 -8
  24. package/lib/Socket/groups.js +4 -2
  25. package/lib/Socket/index.d.ts +70 -35
  26. package/lib/Socket/messages-recv.d.ts +63 -29
  27. package/lib/Socket/messages-recv.js +374 -155
  28. package/lib/Socket/messages-send.d.ts +48 -12
  29. package/lib/Socket/messages-send.js +445 -87
  30. package/lib/Socket/newsletter.d.ts +132 -0
  31. package/lib/Socket/newsletter.js +236 -0
  32. package/lib/Socket/registration.d.ts +73 -41
  33. package/lib/Socket/registration.js +7 -7
  34. package/lib/Socket/socket.d.ts +2 -0
  35. package/lib/Socket/socket.js +56 -15
  36. package/lib/Socket/usync.d.ts +37 -0
  37. package/lib/Socket/usync.js +70 -0
  38. package/lib/Store/make-cache-manager-store.d.ts +2 -1
  39. package/lib/Store/make-in-memory-store.d.ts +2 -1
  40. package/lib/Store/make-in-memory-store.js +40 -46
  41. package/lib/Store/make-ordered-dictionary.d.ts +1 -1
  42. package/lib/Types/Auth.d.ts +2 -1
  43. package/lib/Types/Call.d.ts +1 -1
  44. package/lib/Types/Chat.d.ts +12 -7
  45. package/lib/Types/Events.d.ts +17 -2
  46. package/lib/Types/GroupMetadata.d.ts +6 -2
  47. package/lib/Types/Label.d.ts +11 -0
  48. package/lib/Types/Label.js +1 -1
  49. package/lib/Types/LabelAssociation.js +1 -1
  50. package/lib/Types/Message.d.ts +184 -12
  51. package/lib/Types/Newsletter.d.ts +92 -0
  52. package/lib/Types/Newsletter.js +32 -0
  53. package/lib/Types/Socket.d.ts +7 -2
  54. package/lib/Types/USync.d.ts +25 -0
  55. package/lib/Types/USync.js +2 -0
  56. package/lib/Types/index.d.ts +10 -0
  57. package/lib/Types/index.js +2 -1
  58. package/lib/Utils/auth-utils.js +5 -3
  59. package/lib/Utils/business.d.ts +1 -1
  60. package/lib/Utils/business.js +2 -2
  61. package/lib/Utils/chat-utils.d.ts +11 -12
  62. package/lib/Utils/chat-utils.js +41 -20
  63. package/lib/Utils/crypto.d.ts +15 -16
  64. package/lib/Utils/crypto.js +35 -23
  65. package/lib/Utils/decode-wa-message.d.ts +17 -0
  66. package/lib/Utils/decode-wa-message.js +65 -13
  67. package/lib/Utils/generics.d.ts +11 -17
  68. package/lib/Utils/generics.js +47 -13
  69. package/lib/Utils/history.d.ts +6 -2
  70. package/lib/Utils/history.js +3 -0
  71. package/lib/Utils/link-preview.js +1 -1
  72. package/lib/Utils/logger.d.ts +1 -3
  73. package/lib/Utils/make-mutex.d.ts +2 -2
  74. package/lib/Utils/messages-media.d.ts +31 -25
  75. package/lib/Utils/messages-media.js +95 -53
  76. package/lib/Utils/messages.d.ts +2 -1
  77. package/lib/Utils/messages.js +515 -34
  78. package/lib/Utils/noise-handler.d.ts +6 -6
  79. package/lib/Utils/noise-handler.js +16 -3
  80. package/lib/Utils/process-message.js +4 -3
  81. package/lib/Utils/signal.d.ts +2 -1
  82. package/lib/Utils/signal.js +11 -19
  83. package/lib/Utils/use-multi-file-auth-state.js +11 -3
  84. package/lib/Utils/validate-connection.js +16 -2
  85. package/lib/WABinary/decode.d.ts +1 -2
  86. package/lib/WABinary/decode.js +17 -7
  87. package/lib/WABinary/encode.d.ts +1 -2
  88. package/lib/WABinary/encode.js +33 -17
  89. package/lib/WABinary/generic-utils.d.ts +2 -3
  90. package/lib/WABinary/generic-utils.js +2 -2
  91. package/lib/WABinary/jid-utils.d.ts +4 -2
  92. package/lib/WABinary/jid-utils.js +4 -1
  93. package/lib/WAM/BinaryInfo.d.ts +8 -0
  94. package/lib/WAM/BinaryInfo.js +13 -0
  95. package/lib/WAM/constants.d.ts +38 -0
  96. package/lib/WAM/constants.js +15350 -0
  97. package/lib/WAM/encode.d.ts +2 -0
  98. package/lib/WAM/encode.js +155 -0
  99. package/lib/WAM/index.d.ts +3 -0
  100. package/lib/WAM/index.js +19 -0
  101. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  102. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  103. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  104. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  105. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  106. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  107. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  108. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  109. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  110. package/lib/WAUSync/Protocols/index.js +20 -0
  111. package/lib/WAUSync/USyncQuery.d.ts +26 -0
  112. package/lib/WAUSync/USyncQuery.js +79 -0
  113. package/lib/WAUSync/USyncUser.d.ts +10 -0
  114. package/lib/WAUSync/USyncUser.js +22 -0
  115. package/lib/WAUSync/index.d.ts +3 -0
  116. package/lib/WAUSync/index.js +19 -0
  117. package/lib/gifted +1 -0
  118. package/lib/index.js +2 -0
  119. package/package.json +10 -7
  120. package/lib/index.d.ts +0 -10
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { AxiosRequestConfig } from 'axios';
3
2
  import type { Logger } from 'pino';
4
3
  import { proto } from '../../WAProto';
@@ -14,7 +13,7 @@ export declare const encodeSyncdPatch: ({ type, index, syncAction, apiVersion, o
14
13
  state: LTHashState;
15
14
  }>;
16
15
  export declare const decodeSyncdMutations: (msgMutations: (proto.ISyncdMutation | proto.ISyncdRecord)[], initialState: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, onMutation: (mutation: ChatMutation) => void, validateMacs: boolean) => Promise<{
17
- hash: Buffer;
16
+ hash: any;
18
17
  indexValueMap: {
19
18
  [indexMacBase64: string]: {
20
19
  valueMac: Uint8Array | Buffer;
@@ -22,47 +21,47 @@ export declare const decodeSyncdMutations: (msgMutations: (proto.ISyncdMutation
22
21
  };
23
22
  }>;
24
23
  export declare const decodeSyncdPatch: (msg: proto.ISyncdPatch, name: WAPatchName, initialState: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, onMutation: (mutation: ChatMutation) => void, validateMacs: boolean) => Promise<{
25
- hash: Buffer;
24
+ hash: any;
26
25
  indexValueMap: {
27
26
  [indexMacBase64: string]: {
28
27
  valueMac: Uint8Array | Buffer;
29
28
  };
30
29
  };
31
30
  }>;
32
- export declare const extractSyncdPatches: (result: BinaryNode, options: AxiosRequestConfig<any>) => Promise<{
31
+ export declare const extractSyncdPatches: (result: BinaryNode, options: AxiosRequestConfig<{}>) => Promise<{
33
32
  critical_block: {
34
33
  patches: proto.ISyncdPatch[];
35
34
  hasMorePatches: boolean;
36
- snapshot?: proto.ISyncdSnapshot | undefined;
35
+ snapshot?: proto.ISyncdSnapshot;
37
36
  };
38
37
  critical_unblock_low: {
39
38
  patches: proto.ISyncdPatch[];
40
39
  hasMorePatches: boolean;
41
- snapshot?: proto.ISyncdSnapshot | undefined;
40
+ snapshot?: proto.ISyncdSnapshot;
42
41
  };
43
42
  regular_high: {
44
43
  patches: proto.ISyncdPatch[];
45
44
  hasMorePatches: boolean;
46
- snapshot?: proto.ISyncdSnapshot | undefined;
45
+ snapshot?: proto.ISyncdSnapshot;
47
46
  };
48
47
  regular_low: {
49
48
  patches: proto.ISyncdPatch[];
50
49
  hasMorePatches: boolean;
51
- snapshot?: proto.ISyncdSnapshot | undefined;
50
+ snapshot?: proto.ISyncdSnapshot;
52
51
  };
53
52
  regular: {
54
53
  patches: proto.ISyncdPatch[];
55
54
  hasMorePatches: boolean;
56
- snapshot?: proto.ISyncdSnapshot | undefined;
55
+ snapshot?: proto.ISyncdSnapshot;
57
56
  };
58
57
  }>;
59
- export declare const downloadExternalBlob: (blob: proto.IExternalBlobReference, options: AxiosRequestConfig<any>) => Promise<Buffer>;
60
- export declare const downloadExternalPatch: (blob: proto.IExternalBlobReference, options: AxiosRequestConfig<any>) => Promise<proto.SyncdMutations>;
58
+ export declare const downloadExternalBlob: (blob: proto.IExternalBlobReference, options: AxiosRequestConfig<{}>) => Promise<any>;
59
+ export declare const downloadExternalPatch: (blob: proto.IExternalBlobReference, options: AxiosRequestConfig<{}>) => Promise<proto.SyncdMutations>;
61
60
  export declare const decodeSyncdSnapshot: (name: WAPatchName, snapshot: proto.ISyncdSnapshot, getAppStateSyncKey: FetchAppStateSyncKey, minimumVersionNumber: number | undefined, validateMacs?: boolean) => Promise<{
62
61
  state: LTHashState;
63
62
  mutationMap: ChatMutationMap;
64
63
  }>;
65
- export declare const decodePatches: (name: WAPatchName, syncds: proto.ISyncdPatch[], initial: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, options: AxiosRequestConfig<any>, minimumVersionNumber?: number, logger?: Logger, validateMacs?: boolean) => Promise<{
64
+ export declare const decodePatches: (name: WAPatchName, syncds: proto.ISyncdPatch[], initial: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, options: AxiosRequestConfig<{}>, minimumVersionNumber?: number, logger?: Logger, validateMacs?: boolean) => Promise<{
66
65
  state: LTHashState;
67
66
  mutationMap: ChatMutationMap;
68
67
  }>;
@@ -305,8 +305,7 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
305
305
  indexValueMap: { ...initial.indexValueMap }
306
306
  };
307
307
  const mutationMap = {};
308
- for (let i = 0; i < syncds.length; i++) {
309
- const syncd = syncds[i];
308
+ for (const syncd of syncds) {
310
309
  const { version, keyId, snapshotMac } = syncd;
311
310
  if (syncd.externalMutations) {
312
311
  logger === null || logger === void 0 ? void 0 : logger.trace({ name, version }, 'downloading external patch');
@@ -418,25 +417,31 @@ const chatModificationToAppPatch = (mod, jid) => {
418
417
  operation: OP.SET
419
418
  };
420
419
  }
420
+ else if ('deleteForMe' in mod) {
421
+ const { timestamp, key, deleteMedia } = mod.deleteForMe;
422
+ patch = {
423
+ syncAction: {
424
+ deleteMessageForMeAction: {
425
+ deleteMedia,
426
+ messageTimestamp: timestamp
427
+ }
428
+ },
429
+ index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
430
+ type: 'regular_high',
431
+ apiVersion: 3,
432
+ operation: OP.SET
433
+ };
434
+ }
421
435
  else if ('clear' in mod) {
422
- if (mod.clear === 'all') {
423
- throw new boom_1.Boom('not supported');
424
- }
425
- else {
426
- const key = mod.clear.messages[0];
427
- patch = {
428
- syncAction: {
429
- deleteMessageForMeAction: {
430
- deleteMedia: false,
431
- messageTimestamp: key.timestamp
432
- }
433
- },
434
- index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
435
- type: 'regular_high',
436
- apiVersion: 3,
437
- operation: OP.SET
438
- };
439
- }
436
+ patch = {
437
+ syncAction: {
438
+ clearChatAction: {} // add message range later
439
+ },
440
+ index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
441
+ type: 'regular_high',
442
+ apiVersion: 6,
443
+ operation: OP.SET
444
+ };
440
445
  }
441
446
  else if ('pin' in mod) {
442
447
  patch = {
@@ -491,6 +496,22 @@ const chatModificationToAppPatch = (mod, jid) => {
491
496
  operation: OP.SET,
492
497
  };
493
498
  }
499
+ else if ('addLabel' in mod) {
500
+ patch = {
501
+ syncAction: {
502
+ labelEditAction: {
503
+ name: mod.addLabel.name,
504
+ color: mod.addLabel.color,
505
+ predefinedId: mod.addLabel.predefinedId,
506
+ deleted: mod.addLabel.deleted
507
+ }
508
+ },
509
+ index: ['label_edit', mod.addLabel.id],
510
+ type: 'regular',
511
+ apiVersion: 3,
512
+ operation: OP.SET,
513
+ };
514
+ }
494
515
  else if ('addChatLabel' in mod) {
495
516
  patch = {
496
517
  syncAction: {
@@ -1,10 +1,9 @@
1
- /// <reference types="node" />
2
1
  import { KeyPair } from '../Types';
3
2
  /** prefix version byte to the pub keys, required for some curve crypto functions */
4
- export declare const generateSignalPubKey: (pubKey: Uint8Array | Buffer) => Uint8Array | Buffer;
3
+ export declare const generateSignalPubKey: (pubKey: Uint8Array | Buffer) => any;
5
4
  export declare const Curve: {
6
5
  generateKeyPair: () => KeyPair;
7
- sharedKey: (privateKey: Uint8Array, publicKey: Uint8Array) => Buffer;
6
+ sharedKey: (privateKey: Uint8Array, publicKey: Uint8Array) => any;
8
7
  sign: (privateKey: Uint8Array, buf: Uint8Array) => any;
9
8
  verify: (pubKey: Uint8Array, message: Uint8Array, signature: Uint8Array) => boolean;
10
9
  };
@@ -17,25 +16,25 @@ export declare const signedKeyPair: (identityKeyPair: KeyPair, keyId: number) =>
17
16
  * encrypt AES 256 GCM;
18
17
  * where the tag tag is suffixed to the ciphertext
19
18
  * */
20
- export declare function aesEncryptGCM(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer;
19
+ export declare function aesEncryptGCM(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): any;
21
20
  /**
22
21
  * decrypt AES 256 GCM;
23
22
  * where the auth tag is suffixed to the ciphertext
24
23
  * */
25
- export declare function aesDecryptGCM(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): Buffer;
26
- export declare function aesEncryptCTR(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer;
27
- export declare function aesDecryptCTR(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer;
24
+ export declare function aesDecryptGCM(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array, additionalData: Uint8Array): any;
25
+ export declare function aesEncryptCTR(plaintext: Uint8Array, key: Uint8Array, iv: Uint8Array): any;
26
+ export declare function aesDecryptCTR(ciphertext: Uint8Array, key: Uint8Array, iv: Uint8Array): any;
28
27
  /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
29
- export declare function aesDecrypt(buffer: Buffer, key: Buffer): Buffer;
28
+ export declare function aesDecrypt(buffer: Buffer, key: Buffer): any;
30
29
  /** decrypt AES 256 CBC */
31
- export declare function aesDecryptWithIV(buffer: Buffer, key: Buffer, IV: Buffer): Buffer;
32
- export declare function aesEncrypt(buffer: Buffer | Uint8Array, key: Buffer): Buffer;
33
- export declare function aesEncrypWithIV(buffer: Buffer, key: Buffer, IV: Buffer): Buffer;
34
- export declare function hmacSign(buffer: Buffer | Uint8Array, key: Buffer | Uint8Array, variant?: 'sha256' | 'sha512'): Buffer;
35
- export declare function sha256(buffer: Buffer): Buffer;
36
- export declare function md5(buffer: Buffer): Buffer;
30
+ export declare function aesDecryptWithIV(buffer: Buffer, key: Buffer, IV: Buffer): any;
31
+ export declare function aesEncrypt(buffer: Buffer | Uint8Array, key: Buffer): any;
32
+ export declare function aesEncrypWithIV(buffer: Buffer, key: Buffer, IV: Buffer): any;
33
+ export declare function hmacSign(buffer: Buffer | Uint8Array, key: Buffer | Uint8Array, variant?: 'sha256' | 'sha512'): any;
34
+ export declare function sha256(buffer: Buffer): any;
35
+ export declare function md5(buffer: Buffer): any;
37
36
  export declare function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info: {
38
37
  salt?: Buffer;
39
38
  info?: string;
40
- }): Buffer;
41
- export declare function derivePairingCodeKey(pairingCode: string, salt: Buffer): Buffer;
39
+ }): any;
40
+ export declare function derivePairingCodeKey(pairingCode: string, salt: Buffer): Promise<any>;
@@ -15,22 +15,47 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
28
38
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.derivePairingCodeKey = exports.hkdf = exports.md5 = exports.sha256 = exports.hmacSign = exports.aesEncrypWithIV = exports.aesEncrypt = exports.aesDecryptWithIV = exports.aesDecrypt = exports.aesDecryptCTR = exports.aesEncryptCTR = exports.aesDecryptGCM = exports.aesEncryptGCM = exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
39
+ exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
40
+ exports.aesEncryptGCM = aesEncryptGCM;
41
+ exports.aesDecryptGCM = aesDecryptGCM;
42
+ exports.aesEncryptCTR = aesEncryptCTR;
43
+ exports.aesDecryptCTR = aesDecryptCTR;
44
+ exports.aesDecrypt = aesDecrypt;
45
+ exports.aesDecryptWithIV = aesDecryptWithIV;
46
+ exports.aesEncrypt = aesEncrypt;
47
+ exports.aesEncrypWithIV = aesEncrypWithIV;
48
+ exports.hmacSign = hmacSign;
49
+ exports.sha256 = sha256;
50
+ exports.md5 = md5;
51
+ exports.hkdf = hkdf;
52
+ exports.derivePairingCodeKey = derivePairingCodeKey;
30
53
  const crypto_1 = require("crypto");
31
54
  const futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));
32
55
  const libsignal = __importStar(require("libsignal"));
56
+ const util_1 = require("util");
33
57
  const Defaults_1 = require("../Defaults");
58
+ const pbkdf2Promise = (0, util_1.promisify)(crypto_1.pbkdf2);
34
59
  /** prefix version byte to the pub keys, required for some curve crypto functions */
35
60
  const generateSignalPubKey = (pubKey) => (pubKey.length === 33
36
61
  ? pubKey
@@ -77,7 +102,6 @@ function aesEncryptGCM(plaintext, key, iv, additionalData) {
77
102
  cipher.setAAD(additionalData);
78
103
  return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
79
104
  }
80
- exports.aesEncryptGCM = aesEncryptGCM;
81
105
  /**
82
106
  * decrypt AES 256 GCM;
83
107
  * where the auth tag is suffixed to the ciphertext
@@ -92,60 +116,48 @@ function aesDecryptGCM(ciphertext, key, iv, additionalData) {
92
116
  decipher.setAuthTag(tag);
93
117
  return Buffer.concat([decipher.update(enc), decipher.final()]);
94
118
  }
95
- exports.aesDecryptGCM = aesDecryptGCM;
96
119
  function aesEncryptCTR(plaintext, key, iv) {
97
120
  const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', key, iv);
98
121
  return Buffer.concat([cipher.update(plaintext), cipher.final()]);
99
122
  }
100
- exports.aesEncryptCTR = aesEncryptCTR;
101
123
  function aesDecryptCTR(ciphertext, key, iv) {
102
124
  const decipher = (0, crypto_1.createDecipheriv)('aes-256-ctr', key, iv);
103
125
  return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
104
126
  }
105
- exports.aesDecryptCTR = aesDecryptCTR;
106
127
  /** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
107
128
  function aesDecrypt(buffer, key) {
108
129
  return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
109
130
  }
110
- exports.aesDecrypt = aesDecrypt;
111
131
  /** decrypt AES 256 CBC */
112
132
  function aesDecryptWithIV(buffer, key, IV) {
113
133
  const aes = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, IV);
114
134
  return Buffer.concat([aes.update(buffer), aes.final()]);
115
135
  }
116
- exports.aesDecryptWithIV = aesDecryptWithIV;
117
136
  // encrypt AES 256 CBC; where a random IV is prefixed to the buffer
118
137
  function aesEncrypt(buffer, key) {
119
138
  const IV = (0, crypto_1.randomBytes)(16);
120
139
  const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
121
140
  return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
122
141
  }
123
- exports.aesEncrypt = aesEncrypt;
124
142
  // encrypt AES 256 CBC with a given IV
125
143
  function aesEncrypWithIV(buffer, key, IV) {
126
144
  const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
127
145
  return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
128
146
  }
129
- exports.aesEncrypWithIV = aesEncrypWithIV;
130
147
  // sign HMAC using SHA 256
131
148
  function hmacSign(buffer, key, variant = 'sha256') {
132
149
  return (0, crypto_1.createHmac)(variant, key).update(buffer).digest();
133
150
  }
134
- exports.hmacSign = hmacSign;
135
151
  function sha256(buffer) {
136
152
  return (0, crypto_1.createHash)('sha256').update(buffer).digest();
137
153
  }
138
- exports.sha256 = sha256;
139
154
  function md5(buffer) {
140
155
  return (0, crypto_1.createHash)('md5').update(buffer).digest();
141
156
  }
142
- exports.md5 = md5;
143
157
  // HKDF key expansion
144
158
  function hkdf(buffer, expandedLength, info) {
145
159
  return (0, futoin_hkdf_1.default)(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info);
146
160
  }
147
- exports.hkdf = hkdf;
148
- function derivePairingCodeKey(pairingCode, salt) {
149
- return (0, crypto_1.pbkdf2Sync)(pairingCode, salt, 2 << 16, 32, 'sha256');
161
+ async function derivePairingCodeKey(pairingCode, salt) {
162
+ return await pbkdf2Promise(pairingCode, salt, 2 << 16, 32, 'sha256');
150
163
  }
151
- exports.derivePairingCodeKey = derivePairingCodeKey;
@@ -2,6 +2,23 @@ import { Logger } from 'pino';
2
2
  import { proto } from '../../WAProto';
3
3
  import { SignalRepository } from '../Types';
4
4
  import { BinaryNode } from '../WABinary';
5
+ export declare const NO_MESSAGE_FOUND_ERROR_TEXT = "Message absent from node";
6
+ export declare const MISSING_KEYS_ERROR_TEXT = "Key used already or never filled";
7
+ export declare const NACK_REASONS: {
8
+ ParsingError: number;
9
+ UnrecognizedStanza: number;
10
+ UnrecognizedStanzaClass: number;
11
+ UnrecognizedStanzaType: number;
12
+ InvalidProtobuf: number;
13
+ InvalidHostedCompanionStanza: number;
14
+ MissingMessageSecret: number;
15
+ SignalErrorOldCounter: number;
16
+ MessageDeletedOnPeer: number;
17
+ UnhandledError: number;
18
+ UnsupportedAdminRevoke: number;
19
+ UnsupportedLIDGroup: number;
20
+ DBOperationFailed: number;
21
+ };
5
22
  /**
6
23
  * Decode the received node as a message.
7
24
  * @note this will only parse the message, not decrypt it
@@ -1,16 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decryptMessageNode = exports.decodeMessageNode = void 0;
3
+ exports.decryptMessageNode = exports.NACK_REASONS = exports.MISSING_KEYS_ERROR_TEXT = exports.NO_MESSAGE_FOUND_ERROR_TEXT = void 0;
4
+ exports.decodeMessageNode = decodeMessageNode;
4
5
  const boom_1 = require("@hapi/boom");
5
6
  const WAProto_1 = require("../../WAProto");
6
7
  const WABinary_1 = require("../WABinary");
7
8
  const generics_1 = require("./generics");
8
- const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
9
+ exports.NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';
10
+ exports.MISSING_KEYS_ERROR_TEXT = 'Key used already or never filled';
11
+ exports.NACK_REASONS = {
12
+ ParsingError: 487,
13
+ UnrecognizedStanza: 488,
14
+ UnrecognizedStanzaClass: 489,
15
+ UnrecognizedStanzaType: 490,
16
+ InvalidProtobuf: 491,
17
+ InvalidHostedCompanionStanza: 493,
18
+ MissingMessageSecret: 495,
19
+ SignalErrorOldCounter: 496,
20
+ MessageDeletedOnPeer: 499,
21
+ UnhandledError: 500,
22
+ UnsupportedAdminRevoke: 550,
23
+ UnsupportedLIDGroup: 551,
24
+ DBOperationFailed: 552
25
+ };
9
26
  /**
10
27
  * Decode the received node as a message.
11
28
  * @note this will only parse the message, not decrypt it
12
29
  */
13
30
  function decodeMessageNode(stanza, meId, meLid) {
31
+ var _a, _b;
14
32
  let msgType;
15
33
  let chatId;
16
34
  let author;
@@ -68,10 +86,15 @@ function decodeMessageNode(stanza, meId, meLid) {
68
86
  chatId = from;
69
87
  author = participant;
70
88
  }
89
+ else if ((0, WABinary_1.isJidNewsLetter)(from)) {
90
+ msgType = 'newsletter';
91
+ author = from;
92
+ chatId = from;
93
+ }
71
94
  else {
72
95
  throw new boom_1.Boom('Unknown message type', { data: stanza });
73
96
  }
74
- const fromMe = ((0, WABinary_1.isLidUser)(from) ? isMeLid : isMe)(stanza.attrs.participant || stanza.attrs.from);
97
+ const fromMe = (0, WABinary_1.isJidNewsLetter)(from) ? !!((_a = stanza.attrs) === null || _a === void 0 ? void 0 : _a.is_sender) : ((0, WABinary_1.isLidUser)(from) ? isMeLid : isMe)(stanza.attrs.participant || stanza.attrs.from);
75
98
  const pushname = stanza.attrs.notify;
76
99
  const key = {
77
100
  remoteJid: chatId,
@@ -85,6 +108,9 @@ function decodeMessageNode(stanza, meId, meLid) {
85
108
  pushName: pushname,
86
109
  broadcast: (0, WABinary_1.isJidBroadcast)(from)
87
110
  };
111
+ if (msgType === 'newsletter') {
112
+ fullMessage.newsletterServerId = +((_b = stanza.attrs) === null || _b === void 0 ? void 0 : _b.server_id);
113
+ }
88
114
  if (key.fromMe) {
89
115
  fullMessage.status = WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK;
90
116
  }
@@ -94,7 +120,6 @@ function decodeMessageNode(stanza, meId, meLid) {
94
120
  sender: msgType === 'chat' ? author : chatId
95
121
  };
96
122
  }
97
- exports.decodeMessageNode = decodeMessageNode;
98
123
  const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
99
124
  const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid);
100
125
  return {
@@ -104,14 +129,32 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
104
129
  async decrypt() {
105
130
  var _a;
106
131
  let decryptables = 0;
107
- if (Array.isArray(stanza.content)) {
132
+ if ((0, WABinary_1.isJidNewsLetter)(fullMessage.key.remoteJid)) {
133
+ const node = (0, WABinary_1.getBinaryNodeChild)(stanza, 'plaintext');
134
+ const msg = WAProto_1.proto.Message.decode(node === null || node === void 0 ? void 0 : node.content);
135
+ if (msg.senderKeyDistributionMessage) {
136
+ //eslint-disable-next-line max-depth
137
+ try {
138
+ await repository.processSenderKeyDistributionMessage({
139
+ authorJid: author,
140
+ item: msg.senderKeyDistributionMessage
141
+ });
142
+ }
143
+ catch (err) {
144
+ logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
145
+ }
146
+ }
147
+ fullMessage.message = msg;
148
+ decryptables += 1;
149
+ }
150
+ else if (Array.isArray(stanza.content)) {
108
151
  for (const { tag, attrs, content } of stanza.content) {
109
152
  if (tag === 'verified_name' && content instanceof Uint8Array) {
110
153
  const cert = WAProto_1.proto.VerifiedNameCertificate.decode(content);
111
154
  const details = WAProto_1.proto.VerifiedNameCertificate.Details.decode(cert.details);
112
155
  fullMessage.verifiedBizName = details.verifiedName;
113
156
  }
114
- if (tag !== 'enc') {
157
+ if (tag !== 'enc' && tag !== 'plaintext') {
115
158
  continue;
116
159
  }
117
160
  if (!(content instanceof Uint8Array)) {
@@ -120,7 +163,7 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
120
163
  decryptables += 1;
121
164
  let msgBuffer;
122
165
  try {
123
- const e2eType = attrs.type;
166
+ const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type;
124
167
  switch (e2eType) {
125
168
  case 'skmsg':
126
169
  msgBuffer = await repository.decryptGroupMessage({
@@ -138,16 +181,25 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
138
181
  ciphertext: content
139
182
  });
140
183
  break;
184
+ case 'plaintext':
185
+ msgBuffer = content;
186
+ break;
141
187
  default:
142
188
  throw new Error(`Unknown e2e type: ${e2eType}`);
143
189
  }
144
- let msg = WAProto_1.proto.Message.decode((0, generics_1.unpadRandomMax16)(msgBuffer));
190
+ let msg = WAProto_1.proto.Message.decode(e2eType !== 'plaintext' ? (0, generics_1.unpadRandomMax16)(msgBuffer) : msgBuffer);
145
191
  msg = ((_a = msg.deviceSentMessage) === null || _a === void 0 ? void 0 : _a.message) || msg;
146
192
  if (msg.senderKeyDistributionMessage) {
147
- await repository.processSenderKeyDistributionMessage({
148
- authorJid: author,
149
- item: msg.senderKeyDistributionMessage
150
- });
193
+ //eslint-disable-next-line max-depth
194
+ try {
195
+ await repository.processSenderKeyDistributionMessage({
196
+ authorJid: author,
197
+ item: msg.senderKeyDistributionMessage
198
+ });
199
+ }
200
+ catch (err) {
201
+ logger.error({ key: fullMessage.key, err }, 'failed to decrypt message');
202
+ }
151
203
  }
152
204
  if (fullMessage.message) {
153
205
  Object.assign(fullMessage.message, msg);
@@ -166,7 +218,7 @@ const decryptMessageNode = (stanza, meId, meLid, repository, logger) => {
166
218
  // if nothing was found to decrypt
167
219
  if (!decryptables) {
168
220
  fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;
169
- fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];
221
+ fullMessage.messageStubParameters = [exports.NO_MESSAGE_FOUND_ERROR_TEXT];
170
222
  }
171
223
  }
172
224
  };
@@ -1,27 +1,20 @@
1
- /// <reference types="node" />
2
1
  import { AxiosRequestConfig } from 'axios';
3
2
  import { Logger } from 'pino';
4
3
  import { proto } from '../../WAProto';
5
- import { BaileysEventEmitter, BaileysEventMap, WACallUpdateType, WAVersion } from '../Types';
4
+ import { BaileysEventEmitter, BaileysEventMap, BrowsersMap, ConnectionState, WACallUpdateType, WAVersion } from '../Types';
6
5
  import { BinaryNode } from '../WABinary';
7
- export declare const Browsers: {
8
- ubuntu: (browser: any) => [string, string, string];
9
- macOS: (browser: any) => [string, string, string];
10
- baileys: (browser: any) => [string, string, string];
11
- windows: (browser: any) => [string, string, string];
12
- /** The appropriate browser based on your OS & release */
13
- appropriate: (browser: any) => [string, string, string];
14
- };
6
+ export declare const Browsers: BrowsersMap;
7
+ export declare const getPlatformId: (browser: string) => any;
15
8
  export declare const BufferJSON: {
16
9
  replacer: (k: any, value: any) => any;
17
10
  reviver: (_: any, value: any) => any;
18
11
  };
19
12
  export declare const getKeyAuthor: (key: proto.IMessageKey | undefined | null, meId?: string) => string;
20
- export declare const writeRandomPadMax16: (msg: Uint8Array) => Buffer;
21
- export declare const unpadRandomMax16: (e: Uint8Array | Buffer) => Uint8Array;
22
- export declare const encodeWAMessage: (message: proto.IMessage) => Buffer;
13
+ export declare const writeRandomPadMax16: (msg: Uint8Array) => any;
14
+ export declare const unpadRandomMax16: (e: Uint8Array | Buffer) => Uint8Array<any>;
15
+ export declare const encodeWAMessage: (message: proto.IMessage) => any;
23
16
  export declare const generateRegistrationId: () => number;
24
- export declare const encodeBigEndian: (e: number, t?: number) => Uint8Array;
17
+ export declare const encodeBigEndian: (e: number, t?: number) => Uint8Array<ArrayBuffer>;
25
18
  export declare const toNumber: (t: Long | number | null | undefined) => number;
26
19
  /** unix timestamp of a date in seconds */
27
20
  export declare const unixTimestampSeconds: (date?: Date) => number;
@@ -38,16 +31,17 @@ export declare const delayCancellable: (ms: number) => {
38
31
  cancel: () => void;
39
32
  };
40
33
  export declare function promiseTimeout<T>(ms: number | undefined, promise: (resolve: (v: T) => void, reject: (error: any) => void) => void): Promise<T>;
34
+ export declare const generateMessageIDV2: (userId?: string) => string;
41
35
  export declare const generateMessageID: () => string;
42
36
  export declare function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T): (check: (u: BaileysEventMap[T]) => boolean | undefined, timeoutMs?: number) => Promise<void>;
43
- export declare const bindWaitForConnectionUpdate: (ev: BaileysEventEmitter) => (check: (u: Partial<import("../Types").ConnectionState>) => boolean | undefined, timeoutMs?: number) => Promise<void>;
37
+ export declare const bindWaitForConnectionUpdate: (ev: BaileysEventEmitter) => (check: (u: Partial<ConnectionState>) => boolean | undefined, timeoutMs?: number) => Promise<void>;
44
38
  export declare const printQRIfNecessaryListener: (ev: BaileysEventEmitter, logger: Logger) => void;
45
39
  /**
46
40
  * utility that fetches latest baileys version from the master branch.
47
41
  * Use to ensure your WA connection is always on the latest version
48
42
  */
49
43
  export declare const fetchLatestBaileysVersion: (options?: AxiosRequestConfig<any>) => Promise<{
50
- version: WAVersion;
44
+ version: any;
51
45
  isLatest: boolean;
52
46
  error?: undefined;
53
47
  } | {
@@ -89,6 +83,6 @@ export declare const getCodeFromWSError: (error: Error) => number;
89
83
  * Is the given platform WA business
90
84
  * @param platform AuthenticationCreds.platform
91
85
  */
92
- export declare const isWABusinessPlatform: (platform: string) => boolean;
86
+ export declare const isWABusinessPlatform: (platform: string) => platform is "smbi" | "smba";
93
87
  export declare function trimUndefined(obj: any): any;
94
88
  export declare function bytesToCrockford(buffer: Buffer): string;