@hansaka02/baileys 7.3.2 → 7.3.6

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 (210) hide show
  1. package/lib/Defaults/baileys-version.json +2 -2
  2. package/lib/Defaults/connection.js +51 -0
  3. package/lib/Defaults/constants.js +74 -0
  4. package/lib/Defaults/history.js +19 -0
  5. package/lib/Defaults/index.js +36 -142
  6. package/lib/Defaults/media.js +48 -0
  7. package/lib/Defaults/prefix.js +18 -0
  8. package/lib/Signal/Group/group-session-builder.js +10 -42
  9. package/lib/Signal/Group/group_cipher.js +9 -6
  10. package/lib/Signal/Group/index.js +39 -53
  11. package/lib/Signal/Group/keyhelper.js +8 -41
  12. package/lib/Signal/Group/sender-chain-key.js +5 -18
  13. package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
  14. package/lib/Signal/Group/sender-key-message.js +12 -8
  15. package/lib/Signal/Group/sender-key-record.js +7 -16
  16. package/lib/Signal/Group/sender-key-state.js +15 -61
  17. package/lib/Signal/Group/sender-message-key.js +2 -2
  18. package/lib/Signal/libsignal.js +237 -177
  19. package/lib/Signal/lid-mapping.js +128 -71
  20. package/lib/Socket/Client/types.js +2 -2
  21. package/lib/Socket/Client/websocket.js +25 -16
  22. package/lib/Socket/business.js +46 -33
  23. package/lib/Socket/chats.js +286 -170
  24. package/lib/Socket/community.js +215 -77
  25. package/lib/Socket/groups.js +77 -61
  26. package/lib/Socket/index.js +4 -4
  27. package/lib/Socket/messages-recv.js +629 -457
  28. package/lib/Socket/messages-send.js +645 -656
  29. package/lib/Socket/mex.js +61 -0
  30. package/lib/Socket/newsletter.js +166 -245
  31. package/lib/Socket/socket.js +396 -170
  32. package/lib/Store/index.js +27 -11
  33. package/lib/Store/make-cache-manager-store.js +14 -15
  34. package/lib/Store/make-in-memory-store.js +28 -24
  35. package/lib/Types/LabelAssociation.js +2 -2
  36. package/lib/Types/Message.js +6 -6
  37. package/lib/Types/MexUpdates.js +5 -5
  38. package/lib/Types/Newsletter.js +32 -25
  39. package/lib/Types/State.js +4 -4
  40. package/lib/Types/index.js +28 -12
  41. package/lib/Utils/auth-utils.js +212 -375
  42. package/lib/Utils/baileys-event-stream.js +68 -69
  43. package/lib/Utils/browser-utils.js +43 -0
  44. package/lib/Utils/business.js +63 -53
  45. package/lib/Utils/chat-utils.js +241 -106
  46. package/lib/Utils/crypto.js +25 -45
  47. package/lib/Utils/decode-wa-message.js +361 -311
  48. package/lib/Utils/event-buffer.js +97 -42
  49. package/lib/Utils/generics.js +90 -207
  50. package/lib/Utils/history.js +29 -27
  51. package/lib/Utils/index.js +28 -14
  52. package/lib/Utils/link-preview.js +24 -62
  53. package/lib/Utils/logger.js +5 -5
  54. package/lib/Utils/lt-hash.js +29 -23
  55. package/lib/Utils/make-mutex.js +26 -28
  56. package/lib/Utils/message-retry-manager.js +55 -7
  57. package/lib/Utils/messages-media.js +434 -247
  58. package/lib/Utils/messages.js +963 -917
  59. package/lib/Utils/noise-handler.js +60 -20
  60. package/lib/Utils/pre-key-manager.js +126 -0
  61. package/lib/Utils/process-message.js +216 -141
  62. package/lib/Utils/signal.js +75 -37
  63. package/lib/Utils/use-multi-file-auth-state.js +18 -22
  64. package/lib/Utils/validate-connection.js +96 -66
  65. package/lib/WABinary/constants.js +1268 -1268
  66. package/lib/WABinary/decode.js +62 -34
  67. package/lib/WABinary/encode.js +57 -36
  68. package/lib/WABinary/generic-utils.js +4 -4
  69. package/lib/WABinary/index.js +27 -11
  70. package/lib/WABinary/jid-utils.js +58 -11
  71. package/lib/WAM/constants.js +19064 -11563
  72. package/lib/WAM/encode.js +71 -14
  73. package/lib/WAM/index.js +27 -11
  74. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
  75. package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
  76. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
  77. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
  78. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
  79. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
  80. package/lib/WAUSync/Protocols/index.js +27 -11
  81. package/lib/WAUSync/USyncQuery.js +51 -28
  82. package/lib/WAUSync/index.js +27 -11
  83. package/lib/index.js +60 -31
  84. package/package.json +12 -17
  85. package/WAProto/AICommon/AICommon.d.ts +0 -11702
  86. package/WAProto/Adv/Adv.d.ts +0 -643
  87. package/WAProto/BotMetadata/BotMetadata.d.ts +0 -5654
  88. package/WAProto/Cert/Cert.d.ts +0 -613
  89. package/WAProto/ChatLockSettings/ChatLockSettings.d.ts +0 -476
  90. package/WAProto/CompanionReg/CompanionReg.d.ts +0 -1361
  91. package/WAProto/DeviceCapabilities/DeviceCapabilities.d.ts +0 -577
  92. package/WAProto/E2E/E2E.d.ts +0 -41724
  93. package/WAProto/Ephemeral/Ephemeral.d.ts +0 -114
  94. package/WAProto/HistorySync/HistorySync.d.ts +0 -51700
  95. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.d.ts +0 -229
  96. package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.d.ts +0 -583
  97. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.d.ts +0 -42897
  98. package/WAProto/MmsRetry/MmsRetry.d.ts +0 -243
  99. package/WAProto/Protocol/Protocol.d.ts +0 -270
  100. package/WAProto/Reporting/Reporting.d.ts +0 -371
  101. package/WAProto/ServerSync/ServerSync.d.ts +0 -1285
  102. package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.d.ts +0 -1868
  103. package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.d.ts +0 -767
  104. package/WAProto/StatusAttributions/StatusAttributions.d.ts +0 -1027
  105. package/WAProto/SyncAction/SyncAction.d.ts +0 -11193
  106. package/WAProto/UserPassword/UserPassword.d.ts +0 -363
  107. package/WAProto/VnameCert/VnameCert.d.ts +0 -821
  108. package/WAProto/Wa6/Wa6.d.ts +0 -2128
  109. package/WAProto/Web/Web.d.ts +0 -46383
  110. package/WAProto/index.d.ts +0 -55
  111. package/lib/Defaults/index.d.ts +0 -77
  112. package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
  113. package/lib/Signal/Group/group-session-builder.d.ts +0 -17
  114. package/lib/Signal/Group/group_cipher.d.ts +0 -19
  115. package/lib/Signal/Group/index.d.ts +0 -11
  116. package/lib/Signal/Group/keyhelper.d.ts +0 -16
  117. package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
  118. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
  119. package/lib/Signal/Group/sender-key-message.d.ts +0 -19
  120. package/lib/Signal/Group/sender-key-name.d.ts +0 -19
  121. package/lib/Signal/Group/sender-key-record.d.ts +0 -32
  122. package/lib/Signal/Group/sender-key-state.d.ts +0 -44
  123. package/lib/Signal/Group/sender-message-key.d.ts +0 -11
  124. package/lib/Signal/libsignal.d.ts +0 -8
  125. package/lib/Signal/lid-mapping.d.ts +0 -28
  126. package/lib/Socket/Client/index.d.ts +0 -2
  127. package/lib/Socket/Client/types.d.ts +0 -16
  128. package/lib/Socket/Client/websocket.d.ts +0 -13
  129. package/lib/Socket/business.d.ts +0 -187
  130. package/lib/Socket/chats.d.ts +0 -97
  131. package/lib/Socket/community.d.ts +0 -129
  132. package/lib/Socket/groups.d.ts +0 -129
  133. package/lib/Socket/index.d.ts +0 -191
  134. package/lib/Socket/messages-recv.d.ts +0 -174
  135. package/lib/Socket/messages-send.d.ts +0 -165
  136. package/lib/Socket/newsletter.d.ts +0 -145
  137. package/lib/Socket/socket.d.ts +0 -45
  138. package/lib/Socket/usync.d.ts +0 -37
  139. package/lib/Socket/usync.js +0 -83
  140. package/lib/Store/index.d.ts +0 -4
  141. package/lib/Store/make-cache-manager-store.d.ts +0 -14
  142. package/lib/Store/make-in-memory-store.d.ts +0 -123
  143. package/lib/Store/make-ordered-dictionary.d.ts +0 -12
  144. package/lib/Store/object-repository.d.ts +0 -10
  145. package/lib/Types/Auth.d.ts +0 -121
  146. package/lib/Types/Bussiness.d.ts +0 -28
  147. package/lib/Types/Call.d.ts +0 -14
  148. package/lib/Types/Chat.d.ts +0 -143
  149. package/lib/Types/Contact.d.ts +0 -23
  150. package/lib/Types/Events.d.ts +0 -226
  151. package/lib/Types/GroupMetadata.d.ts +0 -66
  152. package/lib/Types/Label.d.ts +0 -48
  153. package/lib/Types/LabelAssociation.d.ts +0 -35
  154. package/lib/Types/Message.d.ts +0 -484
  155. package/lib/Types/MexUpdates.d.ts +0 -9
  156. package/lib/Types/Newsletter.d.ts +0 -109
  157. package/lib/Types/Product.d.ts +0 -92
  158. package/lib/Types/Signal.d.ts +0 -98
  159. package/lib/Types/Socket.d.ts +0 -141
  160. package/lib/Types/State.d.ts +0 -41
  161. package/lib/Types/USync.d.ts +0 -26
  162. package/lib/Types/index.d.ts +0 -80
  163. package/lib/Utils/auth-utils.d.ts +0 -21
  164. package/lib/Utils/baileys-event-stream.d.ts +0 -18
  165. package/lib/Utils/business.d.ts +0 -29
  166. package/lib/Utils/chat-utils.d.ts +0 -82
  167. package/lib/Utils/crypto.d.ts +0 -56
  168. package/lib/Utils/decode-wa-message.d.ts +0 -53
  169. package/lib/Utils/event-buffer.d.ts +0 -39
  170. package/lib/Utils/generics.d.ts +0 -117
  171. package/lib/Utils/history.d.ts +0 -23
  172. package/lib/Utils/index.d.ts +0 -20
  173. package/lib/Utils/link-preview.d.ts +0 -23
  174. package/lib/Utils/logger.d.ts +0 -13
  175. package/lib/Utils/lt-hash.d.ts +0 -14
  176. package/lib/Utils/make-mutex.d.ts +0 -9
  177. package/lib/Utils/message-retry-manager.d.ts +0 -88
  178. package/lib/Utils/messages-media.d.ts +0 -135
  179. package/lib/Utils/messages.d.ts +0 -105
  180. package/lib/Utils/noise-handler.d.ts +0 -20
  181. package/lib/Utils/process-message.d.ts +0 -49
  182. package/lib/Utils/signal.d.ts +0 -42
  183. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  184. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  185. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  186. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  187. package/lib/Utils/use-single-file-auth-state.js +0 -80
  188. package/lib/Utils/validate-connection.d.ts +0 -13
  189. package/lib/WABinary/constants.d.ts +0 -30
  190. package/lib/WABinary/decode.d.ts +0 -9
  191. package/lib/WABinary/encode.d.ts +0 -3
  192. package/lib/WABinary/generic-utils.d.ts +0 -28
  193. package/lib/WABinary/index.d.ts +0 -5
  194. package/lib/WABinary/jid-utils.d.ts +0 -58
  195. package/lib/WABinary/types.d.ts +0 -22
  196. package/lib/WAM/BinaryInfo.d.ts +0 -16
  197. package/lib/WAM/constants.d.ts +0 -47
  198. package/lib/WAM/encode.d.ts +0 -3
  199. package/lib/WAM/index.d.ts +0 -3
  200. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
  202. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -26
  203. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -14
  204. package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
  205. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -14
  206. package/lib/WAUSync/Protocols/index.d.ts +0 -6
  207. package/lib/WAUSync/USyncQuery.d.ts +0 -31
  208. package/lib/WAUSync/USyncUser.d.ts +0 -12
  209. package/lib/WAUSync/index.d.ts +0 -3
  210. package/lib/index.d.ts +0 -13
@@ -2,11 +2,20 @@
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 Defaults_1 = require("../Defaults")
8
- const WABinary_1 = require("../WABinary")
9
- const crypto_1 = require("./crypto")
5
+ const { Boom } = require("@hapi/boom")
6
+ const { proto } = require("../../WAProto")
7
+ const {
8
+ NOISE_MODE,
9
+ WA_CERT_DETAILS
10
+ } = require("../Defaults/constants")
11
+ const { decodeBinaryNode } = require("../WABinary")
12
+ const {
13
+ aesDecryptGCM,
14
+ aesEncryptGCM,
15
+ Curve,
16
+ hkdf,
17
+ sha256
18
+ } = require("./crypto")
10
19
 
11
20
  const generateIV = (counter) => {
12
21
  const iv = new ArrayBuffer(12)
@@ -16,35 +25,44 @@ const generateIV = (counter) => {
16
25
 
17
26
  const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, logger, routingInfo }) => {
18
27
  logger = logger.child({ class: 'ns' })
28
+
19
29
  const authenticate = (data) => {
20
30
  if (!isFinished) {
21
- hash = crypto_1.sha256(Buffer.concat([hash, data]))
31
+ hash = sha256(Buffer.concat([hash, data]))
22
32
  }
23
33
  }
34
+
24
35
  const encrypt = (plaintext) => {
25
- const result = crypto_1.aesEncryptGCM(plaintext, encKey, generateIV(writeCounter), hash)
36
+ const result = aesEncryptGCM(plaintext, encKey, generateIV(writeCounter), hash)
26
37
  writeCounter += 1
27
38
  authenticate(result)
28
39
  return result
29
40
  }
41
+
30
42
  const decrypt = (ciphertext) => {
31
43
  // before the handshake is finished, we use the same counter
32
44
  // after handshake, the counters are different
33
45
  const iv = generateIV(isFinished ? readCounter : writeCounter)
34
- const result = crypto_1.aesDecryptGCM(ciphertext, decKey, iv, hash)
46
+ const result = aesDecryptGCM(ciphertext, decKey, iv, hash)
47
+
35
48
  if (isFinished) {
36
49
  readCounter += 1
37
50
  }
51
+
38
52
  else {
39
53
  writeCounter += 1
40
54
  }
55
+
41
56
  authenticate(ciphertext)
57
+
42
58
  return result
43
59
  }
60
+
44
61
  const localHKDF = async (data) => {
45
- const key = await crypto_1.hkdf(Buffer.from(data), 64, { salt, info: '' })
62
+ const key = await hkdf(Buffer.from(data), 64, { salt, info: '' })
46
63
  return [key.slice(0, 32), key.slice(32)]
47
64
  }
65
+
48
66
  const mixIntoKey = async (data) => {
49
67
  const [write, read] = await localHKDF(data)
50
68
  salt = write
@@ -53,6 +71,7 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
53
71
  readCounter = 0
54
72
  writeCounter = 0
55
73
  }
74
+
56
75
  const finishInit = async () => {
57
76
  const [write, read] = await localHKDF(new Uint8Array(0))
58
77
  encKey = write
@@ -62,8 +81,10 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
62
81
  writeCounter = 0
63
82
  isFinished = true
64
83
  }
65
- const data = Buffer.from(Defaults_1.NOISE_MODE)
66
- let hash = data.byteLength === 32 ? data : crypto_1.sha256(data)
84
+
85
+ const data = Buffer.from(NOISE_MODE)
86
+
87
+ let hash = data.byteLength === 32 ? data : sha256(data)
67
88
  let salt = hash
68
89
  let encKey = hash
69
90
  let decKey = hash
@@ -72,8 +93,10 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
72
93
  let isFinished = false
73
94
  let sentIntro = false
74
95
  let inBytes = Buffer.alloc(0)
96
+
75
97
  authenticate(NOISE_HEADER)
76
98
  authenticate(publicKey)
99
+
77
100
  return {
78
101
  encrypt,
79
102
  decrypt,
@@ -82,24 +105,31 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
82
105
  finishInit,
83
106
  processHandshake: async ({ serverHello }, noiseKey) => {
84
107
  authenticate(serverHello.ephemeral)
85
- await mixIntoKey(crypto_1.Curve.sharedKey(privateKey, serverHello.ephemeral))
108
+ await mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral))
86
109
  const decStaticContent = decrypt(serverHello.static)
87
- await mixIntoKey(crypto_1.Curve.sharedKey(privateKey, decStaticContent))
110
+ await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent))
88
111
  const certDecoded = decrypt(serverHello.payload)
89
- const { intermediate: certIntermediate } = WAProto_1.proto.CertChain.decode(certDecoded)
90
- const { issuerSerial } = WAProto_1.proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details)
91
- if (issuerSerial !== Defaults_1.WA_CERT_DETAILS.SERIAL) {
92
- throw new boom_1.Boom('certification match failed', { statusCode: 400 })
112
+ const { intermediate: certIntermediate /*leaf*/ } = proto.CertChain.decode(certDecoded)
113
+ // TODO: handle this leaf stuff
114
+ const { issuerSerial } = proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details)
115
+
116
+ if (issuerSerial !== WA_CERT_DETAILS.SERIAL) {
117
+ throw new Boom('certification match failed', { statusCode: 400 })
93
118
  }
119
+
94
120
  const keyEnc = encrypt(noiseKey.public)
95
- await mixIntoKey(crypto_1.Curve.sharedKey(noiseKey.private, serverHello.ephemeral))
121
+
122
+ await mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral))
123
+
96
124
  return keyEnc
97
125
  },
98
126
  encodeFrame: (data) => {
99
127
  if (isFinished) {
100
128
  data = encrypt(data)
101
129
  }
130
+
102
131
  let header
132
+
103
133
  if (routingInfo) {
104
134
  header = Buffer.alloc(7)
105
135
  header.write('ED', 0, 'utf8')
@@ -112,12 +142,15 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
112
142
  else {
113
143
  header = Buffer.from(NOISE_HEADER)
114
144
  }
145
+
115
146
  const introSize = sentIntro ? 0 : header.length
116
147
  const frame = Buffer.alloc(introSize + 3 + data.byteLength)
148
+
117
149
  if (!sentIntro) {
118
150
  frame.set(header)
119
151
  sentIntro = true
120
152
  }
153
+
121
154
  frame.writeUInt8(data.byteLength >> 16, introSize)
122
155
  frame.writeUInt16BE(65535 & data.byteLength, introSize + 1)
123
156
  frame.set(data, introSize + 3)
@@ -132,18 +165,25 @@ const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey },
132
165
  return (inBytes.readUInt8() << 16) | inBytes.readUInt16BE(1)
133
166
  }
134
167
  }
168
+
135
169
  inBytes = Buffer.concat([inBytes, newData])
136
170
  logger.trace(`recv ${newData.length} bytes, total recv ${inBytes.length} bytes`)
171
+
137
172
  let size = getBytesSize()
138
173
  while (size && inBytes.length >= size + 3) {
139
174
  let frame = inBytes.slice(3, size + 3)
140
- inBytes = inBytes.slice(size + 3)
175
+
176
+ inBytes = inBytes.slice(size + 3)
177
+
141
178
  if (isFinished) {
142
179
  const result = decrypt(frame)
143
- frame = await WABinary_1.decodeBinaryNode(result)
180
+ frame = await decodeBinaryNode(result)
144
181
  }
182
+
145
183
  logger.trace({ msg: frame?.attrs?.id }, 'recv frame')
184
+
146
185
  onFrame(frame)
186
+
147
187
  size = getBytesSize()
148
188
  }
149
189
  }
@@ -0,0 +1,126 @@
1
+ "use strict"
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true })
4
+
5
+ const { default: PQueue } = require("p-queue")
6
+
7
+ /**
8
+ * Manages pre-key operations with proper concurrency control
9
+ */
10
+ class PreKeyManager {
11
+ constructor(store, logger) {
12
+ this.store = store
13
+ this.logger = logger
14
+ this.queues = new Map()
15
+ }
16
+
17
+ /**
18
+ * Get or create a queue for a specific key type
19
+ */
20
+ getQueue(keyType) {
21
+ if (!this.queues.has(keyType)) {
22
+ this.queues.set(keyType, new PQueue({ concurrency: 1 }))
23
+ }
24
+ return this.queues.get(keyType)
25
+ }
26
+
27
+ /**
28
+ * Process pre-key operations (updates and deletions)
29
+ */
30
+ async processOperations(data, keyType, transactionCache, mutations, isInTransaction) {
31
+ const keyData = data[keyType]
32
+
33
+ if (!keyData) return
34
+
35
+ return this.getQueue(keyType).add(async () => {
36
+ // Ensure structures exist
37
+ transactionCache[keyType] = transactionCache[keyType] || {}
38
+ mutations[keyType] = mutations[keyType] || {}
39
+
40
+ // Separate deletions from updates
41
+ const deletions = []
42
+ const updates = {}
43
+
44
+ for (const keyId in keyData) {
45
+ if (keyData[keyId] === null) {
46
+ deletions.push(keyId)
47
+ }
48
+ else {
49
+ updates[keyId] = keyData[keyId]
50
+ }
51
+ }
52
+
53
+ // Process updates (no validation needed)
54
+ if (Object.keys(updates).length > 0) {
55
+ Object.assign(transactionCache[keyType], updates)
56
+ Object.assign(mutations[keyType], updates)
57
+ }
58
+
59
+ // Process deletions with validation
60
+ if (deletions.length > 0) {
61
+ await this.processDeletions(keyType, deletions, transactionCache, mutations, isInTransaction)
62
+ }
63
+ })
64
+ }
65
+
66
+ /**
67
+ * Process deletions with validation
68
+ */
69
+ async processDeletions(keyType, ids, transactionCache, mutations, isInTransaction) {
70
+ if (isInTransaction) {
71
+ // In transaction, only allow deletion if key exists in cache
72
+ for (const keyId of ids) {
73
+ if (transactionCache[keyType]?.[keyId]) {
74
+ transactionCache[keyType][keyId] = null
75
+ mutations[keyType][keyId] = null
76
+ }
77
+ else {
78
+ this.logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`)
79
+ }
80
+ }
81
+ }
82
+ else {
83
+ // Outside transaction, validate against store
84
+ const existingKeys = await this.store.get(keyType, ids)
85
+ for (const keyId of ids) {
86
+ if (existingKeys[keyId]) {
87
+ transactionCache[keyType][keyId] = null
88
+ mutations[keyType][keyId] = null
89
+ }
90
+ else {
91
+ this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`)
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Validate and process pre-key deletions outside transactions
99
+ */
100
+ async validateDeletions(data, keyType) {
101
+ const keyData = data[keyType]
102
+
103
+ if (!keyData) return
104
+
105
+ return this.getQueue(keyType).add(async () => {
106
+ // Find all deletion requests
107
+ const deletionIds = Object.keys(keyData).filter(id => keyData[id] === null)
108
+
109
+ if (deletionIds.length === 0) return
110
+
111
+ // Validate deletions
112
+ const existingKeys = await this.store.get(keyType, deletionIds)
113
+
114
+ for (const keyId of deletionIds) {
115
+ if (!existingKeys[keyId]) {
116
+ this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`)
117
+ delete data[keyType][keyId]
118
+ }
119
+ }
120
+ })
121
+ }
122
+ }
123
+
124
+ module.exports = {
125
+ PreKeyManager
126
+ }