vialeys 0.0.1 → 0.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 (102) hide show
  1. package/README.md +11 -5
  2. package/WAProto/AICommon/AICommon.js +13991 -9169
  3. package/WAProto/AICommon/AICommon.proto +110 -3
  4. package/WAProto/CompanionReg/CompanionReg.js +114 -0
  5. package/WAProto/CompanionReg/CompanionReg.proto +3 -0
  6. package/WAProto/DeviceCapabilities/DeviceCapabilities.js +652 -0
  7. package/WAProto/DeviceCapabilities/DeviceCapabilities.proto +19 -0
  8. package/WAProto/E2E/E2E.js +77193 -51248
  9. package/WAProto/E2E/E2E.proto +68 -12
  10. package/WAProto/HistorySync/HistorySync.js +3375 -178
  11. package/WAProto/HistorySync/HistorySync.proto +3 -3
  12. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.js +98 -49
  13. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.proto +2 -2
  14. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +75226 -48422
  15. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.proto +6 -0
  16. package/WAProto/StatusAttributions/StatusAttributions.js +21 -0
  17. package/WAProto/StatusAttributions/StatusAttributions.proto +3 -0
  18. package/WAProto/SyncAction/SyncAction.js +9423 -2023
  19. package/WAProto/SyncAction/SyncAction.proto +208 -15
  20. package/WAProto/Wa6/Wa6.js +83 -0
  21. package/WAProto/Wa6/Wa6.proto +3 -0
  22. package/WAProto/Web/Web.js +92664 -63403
  23. package/WAProto/Web/Web.proto +31 -13
  24. package/engine-requirements.js +9 -7
  25. package/lib/Defaults/baileys-version.json +2 -2
  26. package/lib/Defaults/connection.js +51 -0
  27. package/lib/Defaults/constants.js +62 -0
  28. package/lib/Defaults/history.js +17 -0
  29. package/lib/Defaults/index.js +36 -142
  30. package/lib/Defaults/media.js +48 -0
  31. package/lib/Defaults/prefix.js +18 -0
  32. package/lib/Signal/Group/group-session-builder.js +10 -42
  33. package/lib/Signal/Group/group_cipher.js +9 -6
  34. package/lib/Signal/Group/index.js +39 -53
  35. package/lib/Signal/Group/keyhelper.js +8 -41
  36. package/lib/Signal/Group/sender-chain-key.js +4 -4
  37. package/lib/Signal/Group/sender-key-distribution-message.js +5 -5
  38. package/lib/Signal/Group/sender-key-message.js +12 -8
  39. package/lib/Signal/Group/sender-key-state.js +4 -4
  40. package/lib/Signal/Group/sender-message-key.js +2 -2
  41. package/lib/Signal/libsignal.js +45 -69
  42. package/lib/Signal/lid-mapping.js +15 -11
  43. package/lib/Socket/Client/types.js +2 -2
  44. package/lib/Socket/Client/websocket.js +16 -14
  45. package/lib/Socket/business.js +41 -32
  46. package/lib/Socket/chats.js +123 -98
  47. package/lib/Socket/community.js +50 -40
  48. package/lib/Socket/groups.js +59 -47
  49. package/lib/Socket/index.js +4 -4
  50. package/lib/Socket/messages-recv.js +226 -171
  51. package/lib/Socket/messages-send.js +187 -143
  52. package/lib/Socket/newsletter.js +61 -47
  53. package/lib/Socket/socket.js +133 -90
  54. package/lib/Socket/usync.js +6 -6
  55. package/lib/Store/index.js +27 -11
  56. package/lib/Store/make-cache-manager-store.js +14 -15
  57. package/lib/Store/make-in-memory-store.js +28 -24
  58. package/lib/Types/LabelAssociation.js +2 -2
  59. package/lib/Types/Message.js +6 -6
  60. package/lib/Types/MexUpdates.js +5 -4
  61. package/lib/Types/State.js +4 -4
  62. package/lib/Types/index.js +28 -12
  63. package/lib/Utils/auth-utils.js +28 -26
  64. package/lib/Utils/baileys-event-stream.js +68 -69
  65. package/lib/Utils/business.js +63 -53
  66. package/lib/Utils/chat-utils.js +81 -71
  67. package/lib/Utils/crypto.js +25 -45
  68. package/lib/Utils/decode-wa-message.js +319 -311
  69. package/lib/Utils/event-buffer.js +21 -22
  70. package/lib/Utils/generics.js +103 -73
  71. package/lib/Utils/history.js +21 -21
  72. package/lib/Utils/index.js +27 -13
  73. package/lib/Utils/link-preview.js +7 -30
  74. package/lib/Utils/logger.js +5 -5
  75. package/lib/Utils/lt-hash.js +3 -3
  76. package/lib/Utils/message-retry-manager.js +4 -4
  77. package/lib/Utils/messages-media.js +104 -109
  78. package/lib/Utils/messages.js +203 -171
  79. package/lib/Utils/noise-handler.js +28 -19
  80. package/lib/Utils/process-message.js +370 -136
  81. package/lib/Utils/signal.js +36 -25
  82. package/lib/Utils/use-multi-file-auth-state.js +18 -22
  83. package/lib/Utils/validate-connection.js +52 -45
  84. package/lib/WABinary/decode.js +6 -32
  85. package/lib/WABinary/encode.js +3 -29
  86. package/lib/WABinary/generic-utils.js +4 -4
  87. package/lib/WABinary/index.js +27 -11
  88. package/lib/WAM/encode.js +16 -8
  89. package/lib/WAM/index.js +27 -11
  90. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
  91. package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
  92. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
  93. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
  94. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
  95. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
  96. package/lib/WAUSync/Protocols/index.js +27 -11
  97. package/lib/WAUSync/USyncQuery.js +17 -10
  98. package/lib/WAUSync/index.js +27 -11
  99. package/lib/index.js +62 -37
  100. package/package.json +1 -1
  101. package/WAProto/index.d.ts +0 -55
  102. package/lib/index.d.ts +0 -13
@@ -2,33 +2,43 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true })
4
4
 
5
- const boom_1 = require("@hapi/boom")
6
- const crypto_1 = require("crypto")
7
- const path_1 = require("path")
8
- const os_1 = require("os")
9
- const fs_1 = require("fs")
10
- const WABinary_1 = require("../WABinary")
11
- const generics_1 = require("./generics")
12
- const messages_media_1 = require("./messages-media")
5
+ const { Boom } = require("@hapi/boom")
6
+ const { createHash } = require("crypto")
7
+ const { join } = require("path")
8
+ const { tmpdir } = require("os")
9
+ const {
10
+ promises,
11
+ createWriteStream
12
+ } = require("fs")
13
+ const {
14
+ getBinaryNodeChild,
15
+ getBinaryNodeChildren,
16
+ getBinaryNodeChildString
17
+ } = require("../WABinary")
18
+ const {
19
+ getStream,
20
+ getUrlFromDirectPath
21
+ } = require("./messages-media")
22
+ const { generateMessageID } = require("./generics")
13
23
 
14
24
  const parseCatalogNode = (node) => {
15
- const catalogNode = WABinary_1.getBinaryNodeChild(node, 'product_catalog')
16
- const products = WABinary_1.getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode)
17
- const paging = WABinary_1.getBinaryNodeChild(catalogNode, 'paging')
25
+ const catalogNode = getBinaryNodeChild(node, 'product_catalog')
26
+ const products = getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode)
27
+ const paging = getBinaryNodeChild(catalogNode, 'paging')
18
28
  return {
19
29
  products,
20
30
  nextPageCursor: paging
21
- ? WABinary_1.getBinaryNodeChildString(paging, 'after')
31
+ ? getBinaryNodeChildString(paging, 'after')
22
32
  : undefined
23
33
  }
24
34
  }
25
35
 
26
36
  const parseCollectionsNode = (node) => {
27
- const collectionsNode = WABinary_1.getBinaryNodeChild(node, 'collections')
28
- const collections = WABinary_1.getBinaryNodeChildren(collectionsNode, 'collection').map(collectionNode => {
29
- const id = WABinary_1.getBinaryNodeChildString(collectionNode, 'id')
30
- const name = WABinary_1.getBinaryNodeChildString(collectionNode, 'name')
31
- const products = WABinary_1.getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode)
37
+ const collectionsNode = getBinaryNodeChild(node, 'collections')
38
+ const collections = getBinaryNodeChildren(collectionsNode, 'collection').map(collectionNode => {
39
+ const id = getBinaryNodeChildString(collectionNode, 'id')
40
+ const name = getBinaryNodeChildString(collectionNode, 'name')
41
+ const products = getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode)
32
42
  return {
33
43
  id,
34
44
  name,
@@ -42,23 +52,23 @@ const parseCollectionsNode = (node) => {
42
52
  }
43
53
 
44
54
  const parseOrderDetailsNode = (node) => {
45
- const orderNode = WABinary_1.getBinaryNodeChild(node, 'order')
46
- const products = WABinary_1.getBinaryNodeChildren(orderNode, 'product').map(productNode => {
47
- const imageNode = WABinary_1.getBinaryNodeChild(productNode, 'image')
55
+ const orderNode = getBinaryNodeChild(node, 'order')
56
+ const products = getBinaryNodeChildren(orderNode, 'product').map(productNode => {
57
+ const imageNode = getBinaryNodeChild(productNode, 'image')
48
58
  return {
49
- id: WABinary_1.getBinaryNodeChildString(productNode, 'id'),
50
- name: WABinary_1.getBinaryNodeChildString(productNode, 'name'),
51
- imageUrl: WABinary_1.getBinaryNodeChildString(imageNode, 'url'),
52
- price: +WABinary_1.getBinaryNodeChildString(productNode, 'price'),
53
- currency: WABinary_1.getBinaryNodeChildString(productNode, 'currency'),
54
- quantity: +WABinary_1.getBinaryNodeChildString(productNode, 'quantity')
59
+ id: getBinaryNodeChildString(productNode, 'id'),
60
+ name: getBinaryNodeChildString(productNode, 'name'),
61
+ imageUrl: getBinaryNodeChildString(imageNode, 'url'),
62
+ price: +getBinaryNodeChildString(productNode, 'price'),
63
+ currency: getBinaryNodeChildString(productNode, 'currency'),
64
+ quantity: +getBinaryNodeChildString(productNode, 'quantity')
55
65
  }
56
66
  })
57
- const priceNode = WABinary_1.getBinaryNodeChild(orderNode, 'price')
67
+ const priceNode = getBinaryNodeChild(orderNode, 'price')
58
68
  const orderDetails = {
59
69
  price: {
60
- total: +WABinary_1.getBinaryNodeChildString(priceNode, 'total'),
61
- currency: WABinary_1.getBinaryNodeChildString(priceNode, 'currency'),
70
+ total: +getBinaryNodeChildString(priceNode, 'total'),
71
+ currency: getBinaryNodeChildString(priceNode, 'currency'),
62
72
  },
63
73
  products
64
74
  }
@@ -102,7 +112,7 @@ const toProductNode = (productId, product) => {
102
112
  attrs: {},
103
113
  content: product.images.map(img => {
104
114
  if (!('url' in img)) {
105
- throw new boom_1.Boom('Expected img for product to already be uploaded', { statusCode: 400 })
115
+ throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 })
106
116
  }
107
117
  return {
108
118
  tag: 'image',
@@ -163,23 +173,23 @@ const toProductNode = (productId, product) => {
163
173
 
164
174
  const parseProductNode = (productNode) => {
165
175
  const isHidden = productNode.attrs.is_hidden === 'true'
166
- const id = WABinary_1.getBinaryNodeChildString(productNode, 'id')
167
- const mediaNode = WABinary_1.getBinaryNodeChild(productNode, 'media')
168
- const statusInfoNode = WABinary_1.getBinaryNodeChild(productNode, 'status_info')
176
+ const id = getBinaryNodeChildString(productNode, 'id')
177
+ const mediaNode = getBinaryNodeChild(productNode, 'media')
178
+ const statusInfoNode = getBinaryNodeChild(productNode, 'status_info')
169
179
  const product = {
170
180
  id,
171
181
  imageUrls: parseImageUrls(mediaNode),
172
182
  reviewStatus: {
173
- whatsapp: WABinary_1.getBinaryNodeChildString(statusInfoNode, 'status'),
183
+ whatsapp: getBinaryNodeChildString(statusInfoNode, 'status'),
174
184
  },
175
185
  availability: 'in stock',
176
- name: WABinary_1.getBinaryNodeChildString(productNode, 'name'),
177
- retailerId: WABinary_1.getBinaryNodeChildString(productNode, 'retailer_id'),
178
- url: WABinary_1.getBinaryNodeChildString(productNode, 'url'),
179
- description: WABinary_1.getBinaryNodeChildString(productNode, 'description'),
180
- price: +WABinary_1.getBinaryNodeChildString(productNode, 'price'),
181
- currency: WABinary_1.getBinaryNodeChildString(productNode, 'currency'),
182
- isHidden,
186
+ name: getBinaryNodeChildString(productNode, 'name'),
187
+ retailerId: getBinaryNodeChildString(productNode, 'retailer_id'),
188
+ url: getBinaryNodeChildString(productNode, 'url'),
189
+ description: getBinaryNodeChildString(productNode, 'description'),
190
+ price: +getBinaryNodeChildString(productNode, 'price'),
191
+ currency: getBinaryNodeChildString(productNode, 'currency'),
192
+ isHidden
183
193
  }
184
194
  return product
185
195
  }
@@ -206,10 +216,10 @@ const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30
206
216
  return { url }
207
217
  }
208
218
  }
209
- const { stream } = await messages_media_1.getStream(img)
210
- const hasher = crypto_1.createHash('sha256')
211
- const filePath = path_1.join(os_1.tmpdir(), 'img' + generics_1.generateMessageID())
212
- const encFileWriteStream = fs_1.createWriteStream(filePath)
219
+ const { stream } = await getStream(img)
220
+ const hasher = createHash('sha256')
221
+ const filePath = join(tmpdir(), 'img' + generateMessageID())
222
+ const encFileWriteStream = createWriteStream(filePath)
213
223
  for await (const block of stream) {
214
224
  hasher.update(block)
215
225
  encFileWriteStream.write(block)
@@ -220,27 +230,27 @@ const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30
220
230
  fileEncSha256B64: sha,
221
231
  timeoutMs
222
232
  })
223
- await fs_1.promises
233
+ await promises
224
234
  .unlink(filePath)
225
235
  .catch(err => console.log('Error deleting temp file ', err))
226
- return { url: messages_media_1.getUrlFromDirectPath(directPath) }
236
+ return { url: getUrlFromDirectPath(directPath) }
227
237
  }))
228
238
  return results
229
239
  }
230
240
 
231
241
  const parseImageUrls = (mediaNode) => {
232
- const imgNode = WABinary_1.getBinaryNodeChild(mediaNode, 'image')
242
+ const imgNode = getBinaryNodeChild(mediaNode, 'image')
233
243
  return {
234
- requested: WABinary_1.getBinaryNodeChildString(imgNode, 'request_image_url'),
235
- original: WABinary_1.getBinaryNodeChildString(imgNode, 'original_image_url')
244
+ requested: getBinaryNodeChildString(imgNode, 'request_image_url'),
245
+ original: getBinaryNodeChildString(imgNode, 'original_image_url')
236
246
  }
237
247
  }
238
248
 
239
249
  const parseStatusInfo = (mediaNode) => {
240
- const node = WABinary_1.getBinaryNodeChild(mediaNode, 'status_info')
250
+ const node = getBinaryNodeChild(mediaNode, 'status_info')
241
251
  return {
242
- status: WABinary_1.getBinaryNodeChildString(node, 'status'),
243
- canAppeal: WABinary_1.getBinaryNodeChildString(node, 'can_appeal') === 'true',
252
+ status: getBinaryNodeChildString(node, 'status'),
253
+ canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true',
244
254
  }
245
255
  }
246
256
 
@@ -2,17 +2,27 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true })
4
4
 
5
- const boom_1 = require("@hapi/boom")
6
- const WAProto_1 = require("../../WAProto")
7
- const LabelAssociation_1 = require("../Types/LabelAssociation")
8
- const WABinary_1 = require("../WABinary")
9
- const crypto_1 = require("./crypto")
10
- const generics_1 = require("./generics")
11
- const lt_hash_1 = require("./lt-hash")
12
- const messages_media_1 = require("./messages-media")
5
+ const { Boom } = require("@hapi/boom")
6
+ const { proto } = require("../../WAProto")
7
+ const { LabelAssociationType } = require("../Types/LabelAssociation")
8
+ const {
9
+ getBinaryNodeChild,
10
+ getBinaryNodeChildren,
11
+ isJidGroup,
12
+ jidNormalizedUser
13
+ } = require("../WABinary")
14
+ const {
15
+ hkdf,
16
+ hmacSign,
17
+ aesEncrypt,
18
+ aesDecrypt
19
+ } = require("./crypto")
20
+ const { toNumber } = require("./generics")
21
+ const { LT_HASH_ANTI_TAMPERING } = require("./lt-hash")
22
+ const { downloadContentFromMessage } = require("./messages-media")
13
23
 
14
24
  const mutationKeys = async (keydata) => {
15
- const expanded = await crypto_1.hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' })
25
+ const expanded = await hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' })
16
26
  return {
17
27
  indexKey: expanded.slice(0, 32),
18
28
  valueEncryptionKey: expanded.slice(32, 64),
@@ -26,10 +36,10 @@ const generateMac = (operation, data, keyId, key) => {
26
36
  const getKeyData = () => {
27
37
  let r
28
38
  switch (operation) {
29
- case WAProto_1.proto.SyncdMutation.SyncdOperation.SET:
39
+ case proto.SyncdMutation.SyncdOperation.SET:
30
40
  r = 0x01
31
41
  break
32
- case WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE:
42
+ case proto.SyncdMutation.SyncdOperation.REMOVE:
33
43
  r = 0x02
34
44
  break
35
45
  }
@@ -40,7 +50,7 @@ const generateMac = (operation, data, keyId, key) => {
40
50
  const last = Buffer.alloc(8) // 8 bytes
41
51
  last.set([keyData.length], last.length - 1)
42
52
  const total = Buffer.concat([keyData, data, last])
43
- const hmac = crypto_1.hmacSign(total, key, 'sha512')
53
+ const hmac = hmacSign(total, key, 'sha512')
44
54
  return hmac.slice(0, 32)
45
55
  }
46
56
 
@@ -58,9 +68,9 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
58
68
  mix: ({ indexMac, valueMac, operation }) => {
59
69
  const indexMacBase64 = Buffer.from(indexMac).toString('base64')
60
70
  const prevOp = indexValueMap[indexMacBase64]
61
- if (operation === WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE) {
71
+ if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
62
72
  if (!prevOp) {
63
- throw new boom_1.Boom('tried remove, but no previous op', { data: { indexMac, valueMac } })
73
+ throw new Boom('tried remove, but no previous op', { data: { indexMac, valueMac } })
64
74
  }
65
75
  // remove from index value mac, since this mutation is erased
66
76
  delete indexValueMap[indexMacBase64]
@@ -76,7 +86,7 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
76
86
  },
77
87
  finish: async () => {
78
88
  const hashArrayBuffer = new Uint8Array(hash).buffer
79
- const result = await lt_hash_1.LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs)
89
+ const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs)
80
90
  const buffer = Buffer.from(result)
81
91
  return {
82
92
  hash: buffer,
@@ -92,7 +102,7 @@ const generateSnapshotMac = (lthash, version, name, key) => {
92
102
  to64BitNetworkOrder(version),
93
103
  Buffer.from(name, 'utf-8')
94
104
  ])
95
- return crypto_1.hmacSign(total, key, 'sha256')
105
+ return hmacSign(total, key, 'sha256')
96
106
  }
97
107
 
98
108
  const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
@@ -102,7 +112,7 @@ const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
102
112
  to64BitNetworkOrder(version),
103
113
  Buffer.from(type, 'utf-8')
104
114
  ])
105
- return crypto_1.hmacSign(total, key)
115
+ return hmacSign(total, key)
106
116
  }
107
117
 
108
118
  const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} })
@@ -110,22 +120,22 @@ const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueM
110
120
  const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
111
121
  const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined
112
122
  if (!key) {
113
- throw new boom_1.Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 })
123
+ throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 })
114
124
  }
115
125
  const encKeyId = Buffer.from(myAppStateKeyId, 'base64')
116
126
  state = { ...state, indexValueMap: { ...state.indexValueMap } }
117
127
  const indexBuffer = Buffer.from(JSON.stringify(index))
118
- const dataProto = WAProto_1.proto.SyncActionData.fromObject({
128
+ const dataProto = proto.SyncActionData.fromObject({
119
129
  index: indexBuffer,
120
130
  value: syncAction,
121
131
  padding: new Uint8Array(0),
122
132
  version: apiVersion
123
133
  })
124
- const encoded = WAProto_1.proto.SyncActionData.encode(dataProto).finish()
134
+ const encoded = proto.SyncActionData.encode(dataProto).finish()
125
135
  const keyValue = await mutationKeys(key.keyData)
126
- const encValue = crypto_1.aesEncrypt(encoded, keyValue.valueEncryptionKey)
136
+ const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey)
127
137
  const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey)
128
- const indexMac = crypto_1.hmacSign(indexBuffer, keyValue.indexKey)
138
+ const indexMac = hmacSign(indexBuffer, keyValue.indexKey)
129
139
  // update LT hash
130
140
  const generator = makeLtHashGenerator(state)
131
141
  generator.mix({ indexMac, valueMac, operation })
@@ -164,7 +174,7 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
164
174
  for (const msgMutation of msgMutations) {
165
175
  // if it's a syncdmutation, get the operation property
166
176
  // otherwise, if it's only a record -- it'll be a SET mutation
167
- const operation = 'operation' in msgMutation ? msgMutation.operation : WAProto_1.proto.SyncdMutation.SyncdOperation.SET
177
+ const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET
168
178
  const record = ('record' in msgMutation && !!msgMutation.record) ? msgMutation.record : msgMutation
169
179
  const key = await getKey(record.keyId.id)
170
180
  const content = Buffer.from(record.value.blob)
@@ -173,15 +183,15 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
173
183
  if (validateMacs) {
174
184
  const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey)
175
185
  if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
176
- throw new boom_1.Boom('HMAC content verification failed')
186
+ throw new Boom('HMAC content verification failed')
177
187
  }
178
188
  }
179
- const result = crypto_1.aesDecrypt(encContent, key.valueEncryptionKey)
180
- const syncAction = WAProto_1.proto.SyncActionData.decode(result)
189
+ const result = aesDecrypt(encContent, key.valueEncryptionKey)
190
+ const syncAction = proto.SyncActionData.decode(result)
181
191
  if (validateMacs) {
182
- const hmac = crypto_1.hmacSign(syncAction.index, key.indexKey)
192
+ const hmac = hmacSign(syncAction.index, key.indexKey)
183
193
  if (Buffer.compare(hmac, record.index.blob) !== 0) {
184
- throw new boom_1.Boom('HMAC index verification failed')
194
+ throw new Boom('HMAC index verification failed')
185
195
  }
186
196
  }
187
197
  const indexStr = Buffer.from(syncAction.index).toString()
@@ -197,7 +207,7 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
197
207
  const base64Key = Buffer.from(keyId).toString('base64')
198
208
  const keyEnc = await getAppStateSyncKey(base64Key)
199
209
  if (!keyEnc) {
200
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } })
210
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } })
201
211
  }
202
212
  return mutationKeys(keyEnc.keyData)
203
213
  }
@@ -208,13 +218,13 @@ const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onM
208
218
  const base64Key = Buffer.from(msg.keyId.id).toString('base64')
209
219
  const mainKeyObj = await getAppStateSyncKey(base64Key)
210
220
  if (!mainKeyObj) {
211
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } })
221
+ throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } })
212
222
  }
213
223
  const mainKey = await mutationKeys(mainKeyObj.keyData)
214
224
  const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32))
215
- const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, generics_1.toNumber(msg.version.version), name, mainKey.patchMacKey)
225
+ const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey)
216
226
  if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
217
- throw new boom_1.Boom('Invalid patch mac')
227
+ throw new Boom('Invalid patch mac')
218
228
  }
219
229
  }
220
230
  const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs)
@@ -222,13 +232,13 @@ const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onM
222
232
  }
223
233
 
224
234
  const extractSyncdPatches = async (result, options) => {
225
- const syncNode = WABinary_1.getBinaryNodeChild(result, 'sync')
226
- const collectionNodes = WABinary_1.getBinaryNodeChildren(syncNode, 'collection')
235
+ const syncNode = getBinaryNodeChild(result, 'sync')
236
+ const collectionNodes = getBinaryNodeChildren(syncNode, 'collection')
227
237
  const final = {}
228
238
  await Promise.all(collectionNodes.map(async (collectionNode) => {
229
- const patchesNode = WABinary_1.getBinaryNodeChild(collectionNode, 'patches')
230
- const patches = WABinary_1.getBinaryNodeChildren(patchesNode || collectionNode, 'patch')
231
- const snapshotNode = WABinary_1.getBinaryNodeChild(collectionNode, 'snapshot')
239
+ const patchesNode = getBinaryNodeChild(collectionNode, 'patches')
240
+ const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch')
241
+ const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot')
232
242
  const syncds = []
233
243
  const name = collectionNode.attrs.name
234
244
  const hasMorePatches = collectionNode.attrs.has_more_patches === 'true'
@@ -237,16 +247,16 @@ const extractSyncdPatches = async (result, options) => {
237
247
  if (!Buffer.isBuffer(snapshotNode)) {
238
248
  snapshotNode.content = Buffer.from(Object.values(snapshotNode.content))
239
249
  }
240
- const blobRef = WAProto_1.proto.ExternalBlobReference.decode(snapshotNode.content)
250
+ const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content)
241
251
  const data = await downloadExternalBlob(blobRef, options)
242
- snapshot = WAProto_1.proto.SyncdSnapshot.decode(data)
252
+ snapshot = proto.SyncdSnapshot.decode(data)
243
253
  }
244
254
  for (let { content } of patches) {
245
255
  if (content) {
246
256
  if (!Buffer.isBuffer(content)) {
247
257
  content = Buffer.from(Object.values(content))
248
258
  }
249
- const syncd = WAProto_1.proto.SyncdPatch.decode(content)
259
+ const syncd = proto.SyncdPatch.decode(content)
250
260
  if (!syncd.version) {
251
261
  syncd.version = { version: +collectionNode.attrs.version + 1 }
252
262
  }
@@ -259,7 +269,7 @@ const extractSyncdPatches = async (result, options) => {
259
269
  }
260
270
 
261
271
  const downloadExternalBlob = async (blob, options) => {
262
- const stream = await messages_media_1.downloadContentFromMessage(blob, 'md-app-state', { options })
272
+ const stream = await downloadContentFromMessage(blob, 'md-app-state', { options })
263
273
  const bufferArray = []
264
274
  for await (const chunk of stream) {
265
275
  bufferArray.push(chunk)
@@ -269,13 +279,13 @@ const downloadExternalBlob = async (blob, options) => {
269
279
 
270
280
  const downloadExternalPatch = async (blob, options) => {
271
281
  const buffer = await downloadExternalBlob(blob, options)
272
- const syncData = WAProto_1.proto.SyncdMutations.decode(buffer)
282
+ const syncData = proto.SyncdMutations.decode(buffer)
273
283
  return syncData
274
284
  }
275
285
 
276
286
  const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
277
287
  const newState = newLTHashState()
278
- newState.version = generics_1.toNumber(snapshot.version.version)
288
+ newState.version = toNumber(snapshot.version.version)
279
289
  const mutationMap = {}
280
290
  const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
281
291
  || newState.version > minimumVersionNumber
@@ -291,12 +301,12 @@ const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVe
291
301
  const base64Key = Buffer.from(snapshot.keyId.id).toString('base64')
292
302
  const keyEnc = await getAppStateSyncKey(base64Key)
293
303
  if (!keyEnc) {
294
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`)
304
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
295
305
  }
296
306
  const result = await mutationKeys(keyEnc.keyData)
297
307
  const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
298
308
  if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
299
- throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`)
309
+ throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`)
300
310
  }
301
311
  }
302
312
  return {
@@ -319,7 +329,7 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
319
329
  logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch')
320
330
  syncd.mutations?.push(...ref.mutations)
321
331
  }
322
- const patchVersion = generics_1.toNumber(version.version)
332
+ const patchVersion = toNumber(version.version)
323
333
  newState.version = patchVersion
324
334
  const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber
325
335
  const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
@@ -335,12 +345,12 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
335
345
  const base64Key = Buffer.from(keyId.id).toString('base64')
336
346
  const keyEnc = await getAppStateSyncKey(base64Key)
337
347
  if (!keyEnc) {
338
- throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`)
348
+ throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
339
349
  }
340
350
  const result = await mutationKeys(keyEnc.keyData)
341
351
  const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
342
352
  if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
343
- throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
353
+ throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
344
354
  }
345
355
  }
346
356
  // clear memory used up by the mutations
@@ -350,7 +360,7 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
350
360
  }
351
361
 
352
362
  const chatModificationToAppPatch = (mod, jid) => {
353
- const OP = WAProto_1.proto.SyncdMutation.SyncdOperation
363
+ const OP = proto.SyncdMutation.SyncdOperation
354
364
  const getMessageRange = (lastMessages) => {
355
365
  let messageRange
356
366
  if (Array.isArray(lastMessages)) {
@@ -359,16 +369,16 @@ const chatModificationToAppPatch = (mod, jid) => {
359
369
  lastMessageTimestamp: lastMsg?.messageTimestamp,
360
370
  messages: lastMessages?.length ? lastMessages.map(m => {
361
371
  if (!((m.key?.id) || (m.key?.remoteJid))) {
362
- throw new boom_1.Boom('Incomplete key', { statusCode: 400, data: m })
372
+ throw new Boom('Incomplete key', { statusCode: 400, data: m })
363
373
  }
364
- if (WABinary_1.isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
365
- throw new boom_1.Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
374
+ if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
375
+ throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
366
376
  }
367
- if (!m.messageTimestamp || !generics_1.toNumber(m.messageTimestamp)) {
368
- throw new boom_1.Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
377
+ if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
378
+ throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
369
379
  }
370
380
  if (m.key.participant) {
371
- m.key.participant = WABinary_1.jidNormalizedUser(m.key.participant)
381
+ m.key.participant = jidNormalizedUser(m.key.participant)
372
382
  }
373
383
  return m
374
384
  }) : undefined
@@ -522,7 +532,7 @@ const chatModificationToAppPatch = (mod, jid) => {
522
532
  index: ['setting_pushName'],
523
533
  type: 'critical_block',
524
534
  apiVersion: 1,
525
- operation: OP.SET,
535
+ operation: OP.SET
526
536
  }
527
537
  }
528
538
  else if ('quickReply' in mod) {
@@ -555,7 +565,7 @@ const chatModificationToAppPatch = (mod, jid) => {
555
565
  index: ['label_edit', mod.addLabel.id],
556
566
  type: 'regular',
557
567
  apiVersion: 3,
558
- operation: OP.SET,
568
+ operation: OP.SET
559
569
  }
560
570
  }
561
571
  else if ('addChatLabel' in mod) {
@@ -565,10 +575,10 @@ const chatModificationToAppPatch = (mod, jid) => {
565
575
  labeled: true,
566
576
  }
567
577
  },
568
- index: [LabelAssociation_1.LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
578
+ index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
569
579
  type: 'regular',
570
580
  apiVersion: 3,
571
- operation: OP.SET,
581
+ operation: OP.SET
572
582
  }
573
583
  }
574
584
  else if ('removeChatLabel' in mod) {
@@ -578,10 +588,10 @@ const chatModificationToAppPatch = (mod, jid) => {
578
588
  labeled: false,
579
589
  }
580
590
  },
581
- index: [LabelAssociation_1.LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
591
+ index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
582
592
  type: 'regular',
583
593
  apiVersion: 3,
584
- operation: OP.SET,
594
+ operation: OP.SET
585
595
  }
586
596
  }
587
597
  else if ('addMessageLabel' in mod) {
@@ -592,7 +602,7 @@ const chatModificationToAppPatch = (mod, jid) => {
592
602
  }
593
603
  },
594
604
  index: [
595
- LabelAssociation_1.LabelAssociationType.Message,
605
+ LabelAssociationType.Message,
596
606
  mod.addMessageLabel.labelId,
597
607
  jid,
598
608
  mod.addMessageLabel.messageId,
@@ -601,7 +611,7 @@ const chatModificationToAppPatch = (mod, jid) => {
601
611
  ],
602
612
  type: 'regular',
603
613
  apiVersion: 3,
604
- operation: OP.SET,
614
+ operation: OP.SET
605
615
  }
606
616
  }
607
617
  else if ('removeMessageLabel' in mod) {
@@ -612,7 +622,7 @@ const chatModificationToAppPatch = (mod, jid) => {
612
622
  }
613
623
  },
614
624
  index: [
615
- LabelAssociation_1.LabelAssociationType.Message,
625
+ LabelAssociationType.Message,
616
626
  mod.removeMessageLabel.labelId,
617
627
  jid,
618
628
  mod.removeMessageLabel.messageId,
@@ -621,11 +631,11 @@ const chatModificationToAppPatch = (mod, jid) => {
621
631
  ],
622
632
  type: 'regular',
623
633
  apiVersion: 3,
624
- operation: OP.SET,
634
+ operation: OP.SET
625
635
  }
626
636
  }
627
637
  else {
628
- throw new boom_1.Boom('not supported')
638
+ throw new Boom('not supported')
629
639
  }
630
640
  patch.syncAction.timestamp = Date.now()
631
641
  return patch
@@ -641,7 +651,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
641
651
  {
642
652
  id,
643
653
  muteEndTime: action.muteAction?.muted
644
- ? generics_1.toNumber(action.muteAction.muteEndTimestamp)
654
+ ? toNumber(action.muteAction.muteEndTimestamp)
645
655
  : null,
646
656
  conditional: getChatUpdateConditional(id, undefined)
647
657
  }
@@ -715,7 +725,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
715
725
  else if (action?.pinAction) {
716
726
  ev.emit('chats.update', [{
717
727
  id,
718
- pinned: action.pinAction?.pinned ? generics_1.toNumber(action.timestamp) : null,
728
+ pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
719
729
  conditional: getChatUpdateConditional(id, undefined)
720
730
  }])
721
731
  }
@@ -759,14 +769,14 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
759
769
  type: action.labelAssociationAction.labeled
760
770
  ? 'add'
761
771
  : 'remove',
762
- association: type === LabelAssociation_1.LabelAssociationType.Chat
772
+ association: type === LabelAssociationType.Chat
763
773
  ? {
764
- type: LabelAssociation_1.LabelAssociationType.Chat,
774
+ type: LabelAssociationType.Chat,
765
775
  chatId: syncAction.index[2],
766
776
  labelId: syncAction.index[1]
767
777
  }
768
778
  : {
769
- type: LabelAssociation_1.LabelAssociationType.Message,
779
+ type: LabelAssociationType.Message,
770
780
  chatId: syncAction.index[2],
771
781
  messageId: syncAction.index[3],
772
782
  labelId: syncAction.index[1]