gifted-baileys 1.5.0 → 1.5.5

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 (222) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1429 -684
  3. package/package.json +11 -26
  4. package/src/Defaults/baileys-version.json +3 -0
  5. package/{lib → src}/Defaults/index.js +16 -8
  6. package/src/Defaults/index.ts +131 -0
  7. package/src/Defaults/phonenumber-mcc.json +223 -0
  8. package/src/Signal/libsignal.js +180 -0
  9. package/src/Signal/libsignal.ts +141 -0
  10. package/src/Socket/Client/abstract-socket-client.ts +19 -0
  11. package/src/Socket/Client/index.ts +3 -0
  12. package/src/Socket/Client/mobile-socket-client.js +78 -0
  13. package/src/Socket/Client/mobile-socket-client.ts +66 -0
  14. package/src/Socket/Client/web-socket-client.js +75 -0
  15. package/src/Socket/Client/web-socket-client.ts +57 -0
  16. package/{lib → src}/Socket/business.js +33 -27
  17. package/src/Socket/business.ts +281 -0
  18. package/{lib → src}/Socket/chats.js +197 -178
  19. package/src/Socket/chats.ts +1030 -0
  20. package/{lib → src}/Socket/groups.js +69 -79
  21. package/src/Socket/groups.ts +356 -0
  22. package/{lib → src}/Socket/index.js +1 -4
  23. package/src/Socket/index.ts +13 -0
  24. package/{lib → src}/Socket/messages-recv.js +160 -108
  25. package/src/Socket/messages-recv.ts +985 -0
  26. package/{lib → src}/Socket/messages-send.js +183 -100
  27. package/src/Socket/messages-send.ts +871 -0
  28. package/src/Socket/newsletter.js +227 -0
  29. package/src/Socket/newsletter.ts +282 -0
  30. package/{lib → src}/Socket/registration.js +55 -63
  31. package/src/Socket/registration.ts +250 -0
  32. package/{lib → src}/Socket/socket.js +107 -66
  33. package/src/Socket/socket.ts +777 -0
  34. package/src/Store/index.ts +3 -0
  35. package/{lib → src}/Store/make-cache-manager-store.js +34 -25
  36. package/src/Store/make-cache-manager-store.ts +100 -0
  37. package/{lib → src}/Store/make-in-memory-store.js +51 -61
  38. package/src/Store/make-in-memory-store.ts +475 -0
  39. package/src/Store/make-ordered-dictionary.ts +86 -0
  40. package/{lib → src}/Store/object-repository.js +1 -1
  41. package/src/Store/object-repository.ts +32 -0
  42. package/src/Tests/test.app-state-sync.js +204 -0
  43. package/src/Tests/test.app-state-sync.ts +207 -0
  44. package/src/Tests/test.event-buffer.js +270 -0
  45. package/src/Tests/test.event-buffer.ts +319 -0
  46. package/src/Tests/test.key-store.js +76 -0
  47. package/src/Tests/test.key-store.ts +92 -0
  48. package/src/Tests/test.libsignal.js +141 -0
  49. package/src/Tests/test.libsignal.ts +186 -0
  50. package/src/Tests/test.media-download.js +93 -0
  51. package/src/Tests/test.media-download.ts +76 -0
  52. package/src/Tests/test.messages.js +33 -0
  53. package/src/Tests/test.messages.ts +37 -0
  54. package/src/Tests/utils.js +34 -0
  55. package/src/Tests/utils.ts +36 -0
  56. package/src/Types/Auth.ts +113 -0
  57. package/src/Types/Call.ts +15 -0
  58. package/src/Types/Chat.ts +106 -0
  59. package/{lib/Types/Contact.d.ts → src/Types/Contact.ts} +9 -8
  60. package/src/Types/Events.ts +93 -0
  61. package/src/Types/GroupMetadata.ts +53 -0
  62. package/{lib → src}/Types/Label.js +1 -1
  63. package/src/Types/Label.ts +36 -0
  64. package/{lib → src}/Types/LabelAssociation.js +1 -1
  65. package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
  66. package/src/Types/Message.ts +288 -0
  67. package/src/Types/Newsletter.js +32 -0
  68. package/src/Types/Newsletter.ts +98 -0
  69. package/src/Types/Product.ts +85 -0
  70. package/src/Types/Signal.ts +68 -0
  71. package/{lib/Types/Socket.d.ts → src/Types/Socket.ts} +68 -56
  72. package/src/Types/State.ts +29 -0
  73. package/{lib → src}/Types/index.js +2 -1
  74. package/src/Types/index.ts +59 -0
  75. package/{lib → src}/Utils/auth-utils.js +95 -76
  76. package/src/Utils/auth-utils.ts +222 -0
  77. package/src/Utils/baileys-event-stream.js +92 -0
  78. package/src/Utils/baileys-event-stream.ts +66 -0
  79. package/{lib → src}/Utils/business.js +45 -17
  80. package/src/Utils/business.ts +275 -0
  81. package/{lib → src}/Utils/chat-utils.js +74 -46
  82. package/src/Utils/chat-utils.ts +860 -0
  83. package/{lib → src}/Utils/crypto.js +31 -21
  84. package/src/Utils/crypto.ts +131 -0
  85. package/src/Utils/decode-wa-message.js +211 -0
  86. package/src/Utils/decode-wa-message.ts +228 -0
  87. package/{lib → src}/Utils/event-buffer.js +13 -4
  88. package/src/Utils/event-buffer.ts +613 -0
  89. package/{lib → src}/Utils/generics.js +98 -45
  90. package/src/Utils/generics.ts +434 -0
  91. package/{lib → src}/Utils/history.js +39 -10
  92. package/src/Utils/history.ts +112 -0
  93. package/src/Utils/index.ts +17 -0
  94. package/{lib → src}/Utils/link-preview.js +54 -17
  95. package/src/Utils/link-preview.ts +122 -0
  96. package/src/Utils/logger.ts +3 -0
  97. package/src/Utils/lt-hash.ts +61 -0
  98. package/{lib → src}/Utils/make-mutex.js +13 -4
  99. package/src/Utils/make-mutex.ts +44 -0
  100. package/{lib → src}/Utils/messages-media.js +296 -192
  101. package/src/Utils/messages-media.ts +847 -0
  102. package/{lib → src}/Utils/messages.js +124 -113
  103. package/src/Utils/messages.ts +956 -0
  104. package/{lib → src}/Utils/noise-handler.js +16 -3
  105. package/src/Utils/noise-handler.ts +197 -0
  106. package/{lib → src}/Utils/process-message.js +33 -29
  107. package/src/Utils/process-message.ts +414 -0
  108. package/{lib → src}/Utils/signal.js +23 -14
  109. package/src/Utils/signal.ts +177 -0
  110. package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -19
  111. package/src/Utils/use-multi-file-auth-state.ts +90 -0
  112. package/{lib → src}/Utils/validate-connection.js +25 -42
  113. package/src/Utils/validate-connection.ts +238 -0
  114. package/src/WABinary/constants.ts +42 -0
  115. package/{lib → src}/WABinary/decode.js +17 -7
  116. package/src/WABinary/decode.ts +265 -0
  117. package/{lib → src}/WABinary/encode.js +17 -7
  118. package/src/WABinary/encode.ts +236 -0
  119. package/{lib → src}/WABinary/generic-utils.js +2 -2
  120. package/src/WABinary/generic-utils.ts +121 -0
  121. package/src/WABinary/index.ts +5 -0
  122. package/{lib → src}/WABinary/jid-utils.js +4 -1
  123. package/src/WABinary/jid-utils.ts +68 -0
  124. package/src/WABinary/types.ts +17 -0
  125. package/src/WAM/BinaryInfo.js +13 -0
  126. package/src/WAM/BinaryInfo.ts +12 -0
  127. package/src/WAM/constants.js +15350 -0
  128. package/src/WAM/constants.ts +15382 -0
  129. package/src/WAM/encode.js +155 -0
  130. package/src/WAM/encode.ts +174 -0
  131. package/src/WAM/index.js +19 -0
  132. package/src/WAM/index.ts +3 -0
  133. package/src/gifted +1 -0
  134. package/{lib → src}/index.js +1 -0
  135. package/src/index.ts +13 -0
  136. package/lib/Defaults/baileys-version.json +0 -3
  137. package/lib/Defaults/index.d.ts +0 -284
  138. package/lib/Defaults/phonenumber-mcc.json +0 -223
  139. package/lib/Signal/libsignal.d.ts +0 -3
  140. package/lib/Signal/libsignal.js +0 -152
  141. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
  142. package/lib/Socket/Client/index.d.ts +0 -3
  143. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
  144. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  145. package/lib/Socket/Client/web-socket-client.d.ts +0 -12
  146. package/lib/Socket/Client/web-socket-client.js +0 -62
  147. package/lib/Socket/business.d.ts +0 -135
  148. package/lib/Socket/chats.d.ts +0 -79
  149. package/lib/Socket/groups.d.ts +0 -113
  150. package/lib/Socket/index.d.ts +0 -137
  151. package/lib/Socket/messages-recv.d.ts +0 -124
  152. package/lib/Socket/messages-send.d.ts +0 -119
  153. package/lib/Socket/registration.d.ts +0 -232
  154. package/lib/Socket/socket.d.ts +0 -42
  155. package/lib/Store/index.d.ts +0 -3
  156. package/lib/Store/make-cache-manager-store.d.ts +0 -13
  157. package/lib/Store/make-in-memory-store.d.ts +0 -117
  158. package/lib/Store/make-ordered-dictionary.d.ts +0 -13
  159. package/lib/Store/object-repository.d.ts +0 -10
  160. package/lib/Types/Auth.d.ts +0 -108
  161. package/lib/Types/Call.d.ts +0 -13
  162. package/lib/Types/Chat.d.ts +0 -102
  163. package/lib/Types/Events.d.ts +0 -157
  164. package/lib/Types/GroupMetadata.d.ts +0 -52
  165. package/lib/Types/Label.d.ts +0 -35
  166. package/lib/Types/Message.d.ts +0 -261
  167. package/lib/Types/Product.d.ts +0 -78
  168. package/lib/Types/Signal.d.ts +0 -57
  169. package/lib/Types/State.d.ts +0 -27
  170. package/lib/Types/index.d.ts +0 -56
  171. package/lib/Utils/auth-utils.d.ts +0 -18
  172. package/lib/Utils/baileys-event-stream.d.ts +0 -16
  173. package/lib/Utils/baileys-event-stream.js +0 -63
  174. package/lib/Utils/business.d.ts +0 -22
  175. package/lib/Utils/chat-utils.d.ts +0 -71
  176. package/lib/Utils/crypto.d.ts +0 -41
  177. package/lib/Utils/decode-wa-message.d.ts +0 -19
  178. package/lib/Utils/decode-wa-message.js +0 -174
  179. package/lib/Utils/event-buffer.d.ts +0 -35
  180. package/lib/Utils/generics.d.ts +0 -94
  181. package/lib/Utils/history.d.ts +0 -15
  182. package/lib/Utils/index.d.ts +0 -17
  183. package/lib/Utils/link-preview.d.ts +0 -21
  184. package/lib/Utils/logger.d.ts +0 -4
  185. package/lib/Utils/lt-hash.d.ts +0 -12
  186. package/lib/Utils/make-mutex.d.ts +0 -7
  187. package/lib/Utils/messages-media.d.ts +0 -107
  188. package/lib/Utils/messages.d.ts +0 -76
  189. package/lib/Utils/noise-handler.d.ts +0 -20
  190. package/lib/Utils/process-message.d.ts +0 -41
  191. package/lib/Utils/signal.d.ts +0 -32
  192. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -12
  193. package/lib/Utils/validate-connection.d.ts +0 -11
  194. package/lib/WABinary/constants.d.ts +0 -27
  195. package/lib/WABinary/decode.d.ts +0 -7
  196. package/lib/WABinary/encode.d.ts +0 -3
  197. package/lib/WABinary/generic-utils.d.ts +0 -15
  198. package/lib/WABinary/index.d.ts +0 -5
  199. package/lib/WABinary/jid-utils.d.ts +0 -29
  200. package/lib/WABinary/types.d.ts +0 -18
  201. package/lib/index.d.ts +0 -10
  202. /package/{lib → src}/Socket/Client/abstract-socket-client.js +0 -0
  203. /package/{lib → src}/Socket/Client/index.js +0 -0
  204. /package/{lib → src}/Store/index.js +0 -0
  205. /package/{lib → src}/Store/make-ordered-dictionary.js +0 -0
  206. /package/{lib → src}/Types/Auth.js +0 -0
  207. /package/{lib → src}/Types/Call.js +0 -0
  208. /package/{lib → src}/Types/Chat.js +0 -0
  209. /package/{lib → src}/Types/Contact.js +0 -0
  210. /package/{lib → src}/Types/Events.js +0 -0
  211. /package/{lib → src}/Types/GroupMetadata.js +0 -0
  212. /package/{lib → src}/Types/Message.js +0 -0
  213. /package/{lib → src}/Types/Product.js +0 -0
  214. /package/{lib → src}/Types/Signal.js +0 -0
  215. /package/{lib → src}/Types/Socket.js +0 -0
  216. /package/{lib → src}/Types/State.js +0 -0
  217. /package/{lib → src}/Utils/index.js +0 -0
  218. /package/{lib → src}/Utils/logger.js +0 -0
  219. /package/{lib → src}/Utils/lt-hash.js +0 -0
  220. /package/{lib → src}/WABinary/constants.js +0 -0
  221. /package/{lib → src}/WABinary/index.js +0 -0
  222. /package/{lib → src}/WABinary/types.js +0 -0
@@ -11,7 +11,7 @@ const generateIV = (counter) => {
11
11
  new DataView(iv).setUint32(8, counter);
12
12
  return new Uint8Array(iv);
13
13
  };
14
- const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, mobile, logger, }) => {
14
+ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, mobile, logger, routingInfo }) => {
15
15
  logger = logger.child({ class: 'ns' });
16
16
  const authenticate = (data) => {
17
17
  if (!isFinished) {
@@ -101,10 +101,23 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
101
101
  if (isFinished) {
102
102
  data = encrypt(data);
103
103
  }
104
- const introSize = sentIntro ? 0 : NOISE_HEADER.length;
104
+ let header;
105
+ if (routingInfo) {
106
+ header = Buffer.alloc(7);
107
+ header.write('ED', 0, 'utf8');
108
+ header.writeUint8(0, 2);
109
+ header.writeUint8(1, 3);
110
+ header.writeUint8(routingInfo.byteLength >> 16, 4);
111
+ header.writeUint16BE(routingInfo.byteLength & 65535, 5);
112
+ header = Buffer.concat([header, routingInfo, NOISE_HEADER]);
113
+ }
114
+ else {
115
+ header = Buffer.from(NOISE_HEADER);
116
+ }
117
+ const introSize = sentIntro ? 0 : header.length;
105
118
  const frame = Buffer.alloc(introSize + 3 + data.byteLength);
106
119
  if (!sentIntro) {
107
- frame.set(NOISE_HEADER);
120
+ frame.set(header);
108
121
  sentIntro = true;
109
122
  }
110
123
  frame.writeUInt8(data.byteLength >> 16, introSize);
@@ -0,0 +1,197 @@
1
+ import { Boom } from '@hapi/boom'
2
+ import { Logger } from 'pino'
3
+ import { proto } from '../../WAProto'
4
+ import { NOISE_MODE, WA_CERT_DETAILS } from '../Defaults'
5
+ import { KeyPair } from '../Types'
6
+ import { BinaryNode, decodeBinaryNode } from '../WABinary'
7
+ import { aesDecryptGCM, aesEncryptGCM, Curve, hkdf, sha256 } from './crypto'
8
+
9
+ const generateIV = (counter: number) => {
10
+ const iv = new ArrayBuffer(12)
11
+ new DataView(iv).setUint32(8, counter)
12
+
13
+ return new Uint8Array(iv)
14
+ }
15
+
16
+ export const makeNoiseHandler = ({
17
+ keyPair: { private: privateKey, public: publicKey },
18
+ NOISE_HEADER,
19
+ mobile,
20
+ logger,
21
+ routingInfo
22
+ }: {
23
+ keyPair: KeyPair
24
+ NOISE_HEADER: Uint8Array
25
+ mobile: boolean
26
+ logger: Logger
27
+ routingInfo?: Buffer | undefined
28
+ }) => {
29
+ logger = logger.child({ class: 'ns' })
30
+
31
+ const authenticate = (data: Uint8Array) => {
32
+ if(!isFinished) {
33
+ hash = sha256(Buffer.concat([hash, data]))
34
+ }
35
+ }
36
+
37
+ const encrypt = (plaintext: Uint8Array) => {
38
+ const result = aesEncryptGCM(plaintext, encKey, generateIV(writeCounter), hash)
39
+
40
+ writeCounter += 1
41
+
42
+ authenticate(result)
43
+ return result
44
+ }
45
+
46
+ const decrypt = (ciphertext: Uint8Array) => {
47
+ // before the handshake is finished, we use the same counter
48
+ // after handshake, the counters are different
49
+ const iv = generateIV(isFinished ? readCounter : writeCounter)
50
+ const result = aesDecryptGCM(ciphertext, decKey, iv, hash)
51
+
52
+ if(isFinished) {
53
+ readCounter += 1
54
+ } else {
55
+ writeCounter += 1
56
+ }
57
+
58
+ authenticate(ciphertext)
59
+ return result
60
+ }
61
+
62
+ const localHKDF = (data: Uint8Array) => {
63
+ const key = hkdf(Buffer.from(data), 64, { salt, info: '' })
64
+ return [key.slice(0, 32), key.slice(32)]
65
+ }
66
+
67
+ const mixIntoKey = (data: Uint8Array) => {
68
+ const [write, read] = localHKDF(data)
69
+ salt = write
70
+ encKey = read
71
+ decKey = read
72
+ readCounter = 0
73
+ writeCounter = 0
74
+ }
75
+
76
+ const finishInit = () => {
77
+ const [write, read] = localHKDF(new Uint8Array(0))
78
+ encKey = write
79
+ decKey = read
80
+ hash = Buffer.from([])
81
+ readCounter = 0
82
+ writeCounter = 0
83
+ isFinished = true
84
+ }
85
+
86
+ const data = Buffer.from(NOISE_MODE)
87
+ let hash = Buffer.from(data.byteLength === 32 ? data : sha256(data))
88
+ let salt = hash
89
+ let encKey = hash
90
+ let decKey = hash
91
+ let readCounter = 0
92
+ let writeCounter = 0
93
+ let isFinished = false
94
+ let sentIntro = false
95
+
96
+ let inBytes = Buffer.alloc(0)
97
+
98
+ authenticate(NOISE_HEADER)
99
+ authenticate(publicKey)
100
+
101
+ return {
102
+ encrypt,
103
+ decrypt,
104
+ authenticate,
105
+ mixIntoKey,
106
+ finishInit,
107
+ processHandshake: ({ serverHello }: proto.HandshakeMessage, noiseKey: KeyPair) => {
108
+ authenticate(serverHello!.ephemeral!)
109
+ mixIntoKey(Curve.sharedKey(privateKey, serverHello!.ephemeral!))
110
+
111
+ const decStaticContent = decrypt(serverHello!.static!)
112
+ mixIntoKey(Curve.sharedKey(privateKey, decStaticContent))
113
+
114
+ const certDecoded = decrypt(serverHello!.payload!)
115
+
116
+ if(mobile) {
117
+ proto.CertChain.NoiseCertificate.decode(certDecoded)
118
+ } else {
119
+ const { intermediate: certIntermediate } = proto.CertChain.decode(certDecoded)
120
+
121
+ const { issuerSerial } = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate!.details!)
122
+
123
+ if(issuerSerial !== WA_CERT_DETAILS.SERIAL) {
124
+ throw new Boom('certification match failed', { statusCode: 400 })
125
+ }
126
+ }
127
+
128
+ const keyEnc = encrypt(noiseKey.public)
129
+ mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello!.ephemeral!))
130
+
131
+ return keyEnc
132
+ },
133
+ encodeFrame: (data: Buffer | Uint8Array) => {
134
+ if(isFinished) {
135
+ data = encrypt(data)
136
+ }
137
+
138
+ let header: Buffer
139
+
140
+ if(routingInfo) {
141
+ header = Buffer.alloc(7)
142
+ header.write('ED', 0, 'utf8')
143
+ header.writeUint8(0, 2)
144
+ header.writeUint8(1, 3)
145
+ header.writeUint8(routingInfo.byteLength >> 16, 4)
146
+ header.writeUint16BE(routingInfo.byteLength & 65535, 5)
147
+ header = Buffer.concat([header, routingInfo, NOISE_HEADER])
148
+ } else {
149
+ header = Buffer.from(NOISE_HEADER)
150
+ }
151
+
152
+ const introSize = sentIntro ? 0 : header.length
153
+ const frame = Buffer.alloc(introSize + 3 + data.byteLength)
154
+
155
+ if(!sentIntro) {
156
+ frame.set(header)
157
+ sentIntro = true
158
+ }
159
+
160
+ frame.writeUInt8(data.byteLength >> 16, introSize)
161
+ frame.writeUInt16BE(65535 & data.byteLength, introSize + 1)
162
+ frame.set(data, introSize + 3)
163
+
164
+ return frame
165
+ },
166
+ decodeFrame: (newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
167
+ // the binary protocol uses its own framing mechanism
168
+ // on top of the WS frames
169
+ // so we get this data and separate out the frames
170
+ const getBytesSize = () => {
171
+ if(inBytes.length >= 3) {
172
+ return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1)
173
+ }
174
+ }
175
+
176
+ inBytes = Buffer.concat([ inBytes, newData ])
177
+
178
+ logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`)
179
+
180
+ let size = getBytesSize()
181
+ while(size && inBytes.length >= size + 3) {
182
+ let frame: Uint8Array | BinaryNode = inBytes.slice(3, size + 3)
183
+ inBytes = inBytes.slice(size + 3)
184
+
185
+ if(isFinished) {
186
+ const result = decrypt(frame as Uint8Array)
187
+ frame = decodeBinaryNode(result)
188
+ }
189
+
190
+ logger.trace({ msg: (frame as any)?.attrs?.id }, 'recv frame')
191
+
192
+ onFrame(frame)
193
+ size = getBytesSize()
194
+ }
195
+ }
196
+ }
197
+ }
@@ -1,6 +1,16 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decryptPollVote = exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;
12
+ exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;
13
+ exports.decryptPollVote = decryptPollVote;
4
14
  const WAProto_1 = require("../../WAProto");
5
15
  const Types_1 = require("../Types");
6
16
  const messages_1 = require("../Utils/messages");
@@ -101,9 +111,8 @@ function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pol
101
111
  return Buffer.from(txt);
102
112
  }
103
113
  }
104
- exports.decryptPollVote = decryptPollVote;
105
- const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, keyStore, logger, options, getMessage }) => {
106
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
114
+ const processMessage = (message_1, _a) => __awaiter(void 0, [message_1, _a], void 0, function* (message, { shouldProcessHistoryMsg, ev, creds, keyStore, logger, options, getMessage }) {
115
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
107
116
  const meId = creds.me.id;
108
117
  const { accountSettings } = creds;
109
118
  const chat = { id: (0, WABinary_1.jidNormalizedUser)((0, exports.getChatId)(message.key)) };
@@ -118,7 +127,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
118
127
  const content = (0, messages_1.normalizeMessageContent)(message.message);
119
128
  // unarchive chat if it's a real message, or someone reacted to our message
120
129
  // and we've the unarchive chats setting on
121
- if ((isRealMsg || ((_b = (_a = content === null || content === void 0 ? void 0 : content.reactionMessage) === null || _a === void 0 ? void 0 : _a.key) === null || _b === void 0 ? void 0 : _b.fromMe))
130
+ if ((isRealMsg || ((_c = (_b = content === null || content === void 0 ? void 0 : content.reactionMessage) === null || _b === void 0 ? void 0 : _b.key) === null || _c === void 0 ? void 0 : _c.fromMe))
122
131
  && (accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats)) {
123
132
  chat.archived = false;
124
133
  chat.readOnly = false;
@@ -129,7 +138,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
129
138
  case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
130
139
  const histNotification = protocolMsg.historySyncNotification;
131
140
  const process = shouldProcessHistoryMsg;
132
- const isLatest = !((_c = creds.processedHistoryMessages) === null || _c === void 0 ? void 0 : _c.length);
141
+ const isLatest = !((_d = creds.processedHistoryMessages) === null || _d === void 0 ? void 0 : _d.length);
133
142
  logger === null || logger === void 0 ? void 0 : logger.info({
134
143
  histNotification,
135
144
  process,
@@ -143,24 +152,24 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
143
152
  { key: message.key, messageTimestamp: message.messageTimestamp }
144
153
  ]
145
154
  });
146
- const data = await (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options);
147
- ev.emit('messaging-history.set', { ...data, isLatest });
155
+ const data = yield (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options);
156
+ ev.emit('messaging-history.set', Object.assign(Object.assign({}, data), { isLatest }));
148
157
  }
149
158
  break;
150
159
  case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
151
160
  const keys = protocolMsg.appStateSyncKeyShare.keys;
152
161
  if (keys === null || keys === void 0 ? void 0 : keys.length) {
153
162
  let newAppStateSyncKeyId = '';
154
- await keyStore.transaction(async () => {
163
+ yield keyStore.transaction(() => __awaiter(void 0, void 0, void 0, function* () {
155
164
  const newKeys = [];
156
165
  for (const { keyData, keyId } of keys) {
157
166
  const strKeyId = Buffer.from(keyId.keyId).toString('base64');
158
167
  newKeys.push(strKeyId);
159
- await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
168
+ yield keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
160
169
  newAppStateSyncKeyId = strKeyId;
161
170
  }
162
171
  logger === null || logger === void 0 ? void 0 : logger.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
163
- });
172
+ }));
164
173
  ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
165
174
  }
166
175
  else {
@@ -170,10 +179,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
170
179
  case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
171
180
  ev.emit('messages.update', [
172
181
  {
173
- key: {
174
- ...message.key,
175
- id: protocolMsg.key.id
176
- },
182
+ key: Object.assign(Object.assign({}, message.key), { id: protocolMsg.key.id }),
177
183
  update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
178
184
  }
179
185
  ]);
@@ -202,10 +208,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
202
208
  }
203
209
  }
204
210
  else if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
205
- const reaction = {
206
- ...content.reactionMessage,
207
- key: message.key,
208
- };
211
+ const reaction = Object.assign(Object.assign({}, content.reactionMessage), { key: message.key });
209
212
  ev.emit('messages.reaction', [{
210
213
  reaction,
211
214
  key: content.reactionMessage.key,
@@ -217,7 +220,8 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
217
220
  let participants;
218
221
  const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: message.participant, participants, action }));
219
222
  const emitGroupUpdate = (update) => {
220
- ev.emit('groups.update', [{ id: jid, ...update }]);
223
+ var _a;
224
+ ev.emit('groups.update', [Object.assign(Object.assign({ id: jid }, update), { author: (_a = message.participant) !== null && _a !== void 0 ? _a : undefined })]);
221
225
  };
222
226
  const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));
223
227
  switch (message.messageStubType) {
@@ -248,28 +252,28 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
248
252
  emitParticipantsUpdate('promote');
249
253
  break;
250
254
  case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
251
- const announceValue = (_d = message.messageStubParameters) === null || _d === void 0 ? void 0 : _d[0];
255
+ const announceValue = (_e = message.messageStubParameters) === null || _e === void 0 ? void 0 : _e[0];
252
256
  emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
253
257
  break;
254
258
  case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
255
- const restrictValue = (_e = message.messageStubParameters) === null || _e === void 0 ? void 0 : _e[0];
259
+ const restrictValue = (_f = message.messageStubParameters) === null || _f === void 0 ? void 0 : _f[0];
256
260
  emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
257
261
  break;
258
262
  case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
259
- const name = (_f = message.messageStubParameters) === null || _f === void 0 ? void 0 : _f[0];
263
+ const name = (_g = message.messageStubParameters) === null || _g === void 0 ? void 0 : _g[0];
260
264
  chat.name = name;
261
265
  emitGroupUpdate({ subject: name });
262
266
  break;
263
267
  case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
264
- const code = (_g = message.messageStubParameters) === null || _g === void 0 ? void 0 : _g[0];
268
+ const code = (_h = message.messageStubParameters) === null || _h === void 0 ? void 0 : _h[0];
265
269
  emitGroupUpdate({ inviteCode: code });
266
270
  break;
267
271
  case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
268
- const memberAddValue = (_h = message.messageStubParameters) === null || _h === void 0 ? void 0 : _h[0];
272
+ const memberAddValue = (_j = message.messageStubParameters) === null || _j === void 0 ? void 0 : _j[0];
269
273
  emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
270
274
  break;
271
275
  case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
272
- const approvalMode = (_j = message.messageStubParameters) === null || _j === void 0 ? void 0 : _j[0];
276
+ const approvalMode = (_k = message.messageStubParameters) === null || _k === void 0 ? void 0 : _k[0];
273
277
  emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
274
278
  break;
275
279
  }
@@ -277,12 +281,12 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
277
281
  else if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
278
282
  const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey;
279
283
  // we need to fetch the poll creation message to get the poll enc key
280
- const pollMsg = await getMessage(creationMsgKey);
284
+ const pollMsg = yield getMessage(creationMsgKey);
281
285
  if (pollMsg) {
282
286
  const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(meId);
283
287
  const pollCreatorJid = (0, generics_1.getKeyAuthor)(creationMsgKey, meIdNormalised);
284
288
  const voterJid = (0, generics_1.getKeyAuthor)(message.key, meIdNormalised);
285
- const pollEncKey = (_k = pollMsg.messageContextInfo) === null || _k === void 0 ? void 0 : _k.messageSecret;
289
+ const pollEncKey = (_l = pollMsg.messageContextInfo) === null || _l === void 0 ? void 0 : _l.messageSecret;
286
290
  try {
287
291
  const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
288
292
  pollEncKey,
@@ -316,5 +320,5 @@ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, key
316
320
  if (Object.keys(chat).length > 1) {
317
321
  ev.emit('chats.update', [chat]);
318
322
  }
319
- };
323
+ });
320
324
  exports.default = processMessage;