@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.
- package/lib/Defaults/baileys-version.json +2 -2
- package/lib/Defaults/connection.js +51 -0
- package/lib/Defaults/constants.js +74 -0
- package/lib/Defaults/history.js +19 -0
- package/lib/Defaults/index.js +36 -142
- package/lib/Defaults/media.js +48 -0
- package/lib/Defaults/prefix.js +18 -0
- package/lib/Signal/Group/group-session-builder.js +10 -42
- package/lib/Signal/Group/group_cipher.js +9 -6
- package/lib/Signal/Group/index.js +39 -53
- package/lib/Signal/Group/keyhelper.js +8 -41
- package/lib/Signal/Group/sender-chain-key.js +5 -18
- package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
- package/lib/Signal/Group/sender-key-message.js +12 -8
- package/lib/Signal/Group/sender-key-record.js +7 -16
- package/lib/Signal/Group/sender-key-state.js +15 -61
- package/lib/Signal/Group/sender-message-key.js +2 -2
- package/lib/Signal/libsignal.js +237 -177
- package/lib/Signal/lid-mapping.js +128 -71
- package/lib/Socket/Client/types.js +2 -2
- package/lib/Socket/Client/websocket.js +25 -16
- package/lib/Socket/business.js +46 -33
- package/lib/Socket/chats.js +286 -170
- package/lib/Socket/community.js +215 -77
- package/lib/Socket/groups.js +77 -61
- package/lib/Socket/index.js +4 -4
- package/lib/Socket/messages-recv.js +629 -457
- package/lib/Socket/messages-send.js +645 -656
- package/lib/Socket/mex.js +61 -0
- package/lib/Socket/newsletter.js +166 -245
- package/lib/Socket/socket.js +396 -170
- package/lib/Store/index.js +27 -11
- package/lib/Store/make-cache-manager-store.js +14 -15
- package/lib/Store/make-in-memory-store.js +28 -24
- package/lib/Types/LabelAssociation.js +2 -2
- package/lib/Types/Message.js +6 -6
- package/lib/Types/MexUpdates.js +5 -5
- package/lib/Types/Newsletter.js +32 -25
- package/lib/Types/State.js +4 -4
- package/lib/Types/index.js +28 -12
- package/lib/Utils/auth-utils.js +212 -375
- package/lib/Utils/baileys-event-stream.js +68 -69
- package/lib/Utils/browser-utils.js +43 -0
- package/lib/Utils/business.js +63 -53
- package/lib/Utils/chat-utils.js +241 -106
- package/lib/Utils/crypto.js +25 -45
- package/lib/Utils/decode-wa-message.js +361 -311
- package/lib/Utils/event-buffer.js +97 -42
- package/lib/Utils/generics.js +90 -207
- package/lib/Utils/history.js +29 -27
- package/lib/Utils/index.js +28 -14
- package/lib/Utils/link-preview.js +24 -62
- package/lib/Utils/logger.js +5 -5
- package/lib/Utils/lt-hash.js +29 -23
- package/lib/Utils/make-mutex.js +26 -28
- package/lib/Utils/message-retry-manager.js +55 -7
- package/lib/Utils/messages-media.js +434 -247
- package/lib/Utils/messages.js +963 -917
- package/lib/Utils/noise-handler.js +60 -20
- package/lib/Utils/pre-key-manager.js +126 -0
- package/lib/Utils/process-message.js +216 -141
- package/lib/Utils/signal.js +75 -37
- package/lib/Utils/use-multi-file-auth-state.js +18 -22
- package/lib/Utils/validate-connection.js +96 -66
- package/lib/WABinary/constants.js +1268 -1268
- package/lib/WABinary/decode.js +62 -34
- package/lib/WABinary/encode.js +57 -36
- package/lib/WABinary/generic-utils.js +4 -4
- package/lib/WABinary/index.js +27 -11
- package/lib/WABinary/jid-utils.js +58 -11
- package/lib/WAM/constants.js +19064 -11563
- package/lib/WAM/encode.js +71 -14
- package/lib/WAM/index.js +27 -11
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
- package/lib/WAUSync/Protocols/index.js +27 -11
- package/lib/WAUSync/USyncQuery.js +51 -28
- package/lib/WAUSync/index.js +27 -11
- package/lib/index.js +60 -31
- package/package.json +12 -17
- package/WAProto/AICommon/AICommon.d.ts +0 -11702
- package/WAProto/Adv/Adv.d.ts +0 -643
- package/WAProto/BotMetadata/BotMetadata.d.ts +0 -5654
- package/WAProto/Cert/Cert.d.ts +0 -613
- package/WAProto/ChatLockSettings/ChatLockSettings.d.ts +0 -476
- package/WAProto/CompanionReg/CompanionReg.d.ts +0 -1361
- package/WAProto/DeviceCapabilities/DeviceCapabilities.d.ts +0 -577
- package/WAProto/E2E/E2E.d.ts +0 -41724
- package/WAProto/Ephemeral/Ephemeral.d.ts +0 -114
- package/WAProto/HistorySync/HistorySync.d.ts +0 -51700
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.d.ts +0 -229
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.d.ts +0 -583
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.d.ts +0 -42897
- package/WAProto/MmsRetry/MmsRetry.d.ts +0 -243
- package/WAProto/Protocol/Protocol.d.ts +0 -270
- package/WAProto/Reporting/Reporting.d.ts +0 -371
- package/WAProto/ServerSync/ServerSync.d.ts +0 -1285
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.d.ts +0 -1868
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.d.ts +0 -767
- package/WAProto/StatusAttributions/StatusAttributions.d.ts +0 -1027
- package/WAProto/SyncAction/SyncAction.d.ts +0 -11193
- package/WAProto/UserPassword/UserPassword.d.ts +0 -363
- package/WAProto/VnameCert/VnameCert.d.ts +0 -821
- package/WAProto/Wa6/Wa6.d.ts +0 -2128
- package/WAProto/Web/Web.d.ts +0 -46383
- package/WAProto/index.d.ts +0 -55
- package/lib/Defaults/index.d.ts +0 -77
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
- package/lib/Signal/Group/group-session-builder.d.ts +0 -17
- package/lib/Signal/Group/group_cipher.d.ts +0 -19
- package/lib/Signal/Group/index.d.ts +0 -11
- package/lib/Signal/Group/keyhelper.d.ts +0 -16
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -19
- package/lib/Signal/Group/sender-key-record.d.ts +0 -32
- package/lib/Signal/Group/sender-key-state.d.ts +0 -44
- package/lib/Signal/Group/sender-message-key.d.ts +0 -11
- package/lib/Signal/libsignal.d.ts +0 -8
- package/lib/Signal/lid-mapping.d.ts +0 -28
- package/lib/Socket/Client/index.d.ts +0 -2
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -187
- package/lib/Socket/chats.d.ts +0 -97
- package/lib/Socket/community.d.ts +0 -129
- package/lib/Socket/groups.d.ts +0 -129
- package/lib/Socket/index.d.ts +0 -191
- package/lib/Socket/messages-recv.d.ts +0 -174
- package/lib/Socket/messages-send.d.ts +0 -165
- package/lib/Socket/newsletter.d.ts +0 -145
- package/lib/Socket/socket.d.ts +0 -45
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Socket/usync.js +0 -83
- package/lib/Store/index.d.ts +0 -4
- package/lib/Store/make-cache-manager-store.d.ts +0 -14
- package/lib/Store/make-in-memory-store.d.ts +0 -123
- package/lib/Store/make-ordered-dictionary.d.ts +0 -12
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -121
- package/lib/Types/Bussiness.d.ts +0 -28
- package/lib/Types/Call.d.ts +0 -14
- package/lib/Types/Chat.d.ts +0 -143
- package/lib/Types/Contact.d.ts +0 -23
- package/lib/Types/Events.d.ts +0 -226
- package/lib/Types/GroupMetadata.d.ts +0 -66
- package/lib/Types/Label.d.ts +0 -48
- package/lib/Types/LabelAssociation.d.ts +0 -35
- package/lib/Types/Message.d.ts +0 -484
- package/lib/Types/MexUpdates.d.ts +0 -9
- package/lib/Types/Newsletter.d.ts +0 -109
- package/lib/Types/Product.d.ts +0 -92
- package/lib/Types/Signal.d.ts +0 -98
- package/lib/Types/Socket.d.ts +0 -141
- package/lib/Types/State.d.ts +0 -41
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -80
- package/lib/Utils/auth-utils.d.ts +0 -21
- package/lib/Utils/baileys-event-stream.d.ts +0 -18
- package/lib/Utils/business.d.ts +0 -29
- package/lib/Utils/chat-utils.d.ts +0 -82
- package/lib/Utils/crypto.d.ts +0 -56
- package/lib/Utils/decode-wa-message.d.ts +0 -53
- package/lib/Utils/event-buffer.d.ts +0 -39
- package/lib/Utils/generics.d.ts +0 -117
- package/lib/Utils/history.d.ts +0 -23
- package/lib/Utils/index.d.ts +0 -20
- package/lib/Utils/link-preview.d.ts +0 -23
- package/lib/Utils/logger.d.ts +0 -13
- package/lib/Utils/lt-hash.d.ts +0 -14
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -88
- package/lib/Utils/messages-media.d.ts +0 -135
- package/lib/Utils/messages.d.ts +0 -105
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -49
- package/lib/Utils/signal.d.ts +0 -42
- package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
- package/lib/Utils/use-mongo-file-auth-state.js +0 -84
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.js +0 -80
- package/lib/Utils/validate-connection.d.ts +0 -13
- package/lib/WABinary/constants.d.ts +0 -30
- package/lib/WABinary/decode.d.ts +0 -9
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -28
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -58
- package/lib/WABinary/types.d.ts +0 -22
- package/lib/WAM/BinaryInfo.d.ts +0 -16
- package/lib/WAM/constants.d.ts +0 -47
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -14
- package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -14
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -31
- package/lib/WAUSync/USyncUser.d.ts +0 -12
- package/lib/WAUSync/index.d.ts +0 -3
- 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
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
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 =
|
|
31
|
+
hash = sha256(Buffer.concat([hash, data]))
|
|
22
32
|
}
|
|
23
33
|
}
|
|
34
|
+
|
|
24
35
|
const encrypt = (plaintext) => {
|
|
25
|
-
const result =
|
|
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 =
|
|
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
|
|
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
|
-
|
|
66
|
-
|
|
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(
|
|
108
|
+
await mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral))
|
|
86
109
|
const decStaticContent = decrypt(serverHello.static)
|
|
87
|
-
await mixIntoKey(
|
|
110
|
+
await mixIntoKey(Curve.sharedKey(privateKey, decStaticContent))
|
|
88
111
|
const certDecoded = decrypt(serverHello.payload)
|
|
89
|
-
const { intermediate: certIntermediate } =
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
175
|
+
|
|
176
|
+
inBytes = inBytes.slice(size + 3)
|
|
177
|
+
|
|
141
178
|
if (isFinished) {
|
|
142
179
|
const result = decrypt(frame)
|
|
143
|
-
frame = await
|
|
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
|
+
}
|