@zervonexx/baileys 5.0.0 → 5.0.1
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/LICENSE +22 -22
- package/README.md +4112 -4112
- package/WAProto/WAProtoCompile.js +322 -322
- package/WAProto/fetcher.js +67 -67
- package/WAProto/index.js +95 -95
- package/WAProto/parser.js +333 -333
- package/lib/Builders/AIRich.js +673 -673
- package/lib/Builders/auto.js +49 -49
- package/lib/Builders/base.js +144 -144
- package/lib/Builders/builders.js +588 -588
- package/lib/Builders/index.js +3 -3
- package/lib/Defaults/index.js +248 -248
- package/lib/Signal/index.js +2 -2
- package/lib/Signal/libsignal.js +526 -526
- package/lib/Signal/lid-mapping.js +282 -282
- package/lib/Socket/Client/index.js +1 -1
- package/lib/Socket/Client/types.js +9 -9
- package/lib/Socket/Client/websocket.js +83 -83
- package/lib/Socket/business.js +151 -151
- package/lib/Socket/chats.js +854 -854
- package/lib/Socket/communities.js +146 -146
- package/lib/Socket/groups.js +293 -293
- package/lib/Socket/index.js +14 -14
- package/lib/Socket/messages-recv.js +993 -993
- package/lib/Socket/messages-send.js +722 -722
- package/lib/Socket/mex.js +33 -33
- package/lib/Socket/newsletter.js +783 -783
- package/lib/Socket/registration.js +154 -154
- package/lib/Socket/socket.js +797 -797
- package/lib/Socket/spam-report.js +143 -143
- package/lib/Socket/username.js +81 -81
- package/lib/Socket/zervonex-handler.js +57 -57
- package/lib/Store/index.js +6 -6
- package/lib/Store/make-cache-manager-store.js +80 -80
- package/lib/Store/make-in-memory-store.js +426 -426
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Store/reporting-info-store.js +37 -37
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +14 -14
- package/lib/Types/Newsletter.js +99 -99
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +14 -14
- package/lib/Types/State.js +26 -26
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +30 -30
- package/lib/Utils/auth-utils.js +159 -159
- package/lib/Utils/baileys-event-stream.js +32 -32
- package/lib/Utils/bot-capabilities.js +40 -40
- package/lib/Utils/bot-planning-replay.js +43 -43
- package/lib/Utils/browser-utils.js +29 -29
- package/lib/Utils/business.js +74 -74
- package/lib/Utils/chat-utils.js +316 -316
- package/lib/Utils/companion-reg-client-utils.js +19 -19
- package/lib/Utils/consumer-application.js +37 -37
- package/lib/Utils/crypto.js +52 -52
- package/lib/Utils/cstoken.js +14 -14
- package/lib/Utils/decode-wa-message.js +199 -199
- package/lib/Utils/event-buffer.js +272 -272
- package/lib/Utils/extras.js +186 -186
- package/lib/Utils/generics.js +194 -194
- package/lib/Utils/history.js +50 -50
- package/lib/Utils/identity-change-handler.js +18 -18
- package/lib/Utils/index.js +40 -40
- package/lib/Utils/link-preview.js +50 -50
- package/lib/Utils/logger.js +2 -2
- package/lib/Utils/lt-hash.js +3 -3
- package/lib/Utils/make-mutex.js +15 -15
- package/lib/Utils/message-composer.js +61 -61
- package/lib/Utils/message-retry-manager.js +47 -47
- package/lib/Utils/messages-media.js +501 -501
- package/lib/Utils/messages.js +942 -942
- package/lib/Utils/meta-ai-msmsg.js +121 -121
- package/lib/Utils/meta-compositing.js +74 -74
- package/lib/Utils/modern-messages.js +234 -234
- package/lib/Utils/native-flow.js +80 -80
- package/lib/Utils/noise-handler.js +160 -160
- package/lib/Utils/pre-key-manager.js +75 -75
- package/lib/Utils/process-message.js +400 -400
- package/lib/Utils/reporting-utils.js +170 -170
- package/lib/Utils/signal.js +166 -166
- package/lib/Utils/spam-report-utils.js +165 -165
- package/lib/Utils/sync-action-utils.js +33 -33
- package/lib/Utils/tc-token-utils.js +153 -153
- package/lib/Utils/use-keyv-auth-state.js +199 -199
- package/lib/Utils/use-multi-file-auth-state.js +83 -83
- package/lib/Utils/validate-connection.js +171 -171
- package/lib/WABinary/constants.js +1320 -1320
- package/lib/WABinary/decode.js +151 -151
- package/lib/WABinary/encode.js +139 -139
- package/lib/WABinary/generic-utils.js +202 -202
- package/lib/WABinary/index.js +6 -6
- package/lib/WABinary/jid-utils.js +235 -235
- package/lib/WABinary/types.js +4 -4
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
- package/lib/WAUSync/Protocols/index.js +12 -12
- package/lib/WAUSync/USyncQuery.js +88 -88
- package/lib/WAUSync/USyncUser.js +20 -20
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +66 -66
- package/package.json +191 -192
package/lib/Utils/signal.js
CHANGED
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
import { KEY_BUNDLE_TYPE } from '../Defaults/index.js'
|
|
2
|
-
import { assertNodeErrorFree, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildUInt, getServerFromDomainType, jidDecode, S_WHATSAPP_NET, WAJIDDomains } from '../WABinary/index.js'
|
|
3
|
-
import { Curve, generateSignalPubKey } from './crypto.js'
|
|
4
|
-
import { encodeBigEndian } from './generics.js'
|
|
5
|
-
|
|
6
|
-
const chunk = (array, size) => {
|
|
7
|
-
const chunks = []
|
|
8
|
-
for (let i = 0; i < array.length; i += size) chunks.push(array.slice(i, i + size))
|
|
9
|
-
return chunks
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const createSignalIdentity = (wid, accountSignatureKey) => ({
|
|
13
|
-
identifier: { name: wid, deviceId: 0 },
|
|
14
|
-
identifierKey: generateSignalPubKey(accountSignatureKey),
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
export const getPreKeys = async ({ get }, min, limit) => {
|
|
18
|
-
const idList = []
|
|
19
|
-
for (let id = min; id < limit; id++) idList.push(id.toString())
|
|
20
|
-
return get('pre-key', idList)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const generateOrGetPreKeys = (creds, range) => {
|
|
24
|
-
const available = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId
|
|
25
|
-
const remaining = range - available
|
|
26
|
-
const lastPreKeyId = creds.nextPreKeyId + remaining - 1
|
|
27
|
-
const newPreKeys = {}
|
|
28
|
-
if (remaining > 0) {
|
|
29
|
-
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) newPreKeys[i] = Curve.generateKeyPair()
|
|
30
|
-
}
|
|
31
|
-
return { newPreKeys, lastPreKeyId, preKeysRange: [creds.firstUnuploadedPreKeyId, range] }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const xmppSignedPreKey = key => ({
|
|
35
|
-
tag: 'skey',
|
|
36
|
-
attrs: {},
|
|
37
|
-
content: [
|
|
38
|
-
{ tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
|
|
39
|
-
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
40
|
-
{ tag: 'signature', attrs: {}, content: key.signature },
|
|
41
|
-
],
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
export const xmppPreKey = (pair, id) => ({
|
|
45
|
-
tag: 'key',
|
|
46
|
-
attrs: {},
|
|
47
|
-
content: [
|
|
48
|
-
{ tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
|
|
49
|
-
{ tag: 'value', attrs: {}, content: pair.public },
|
|
50
|
-
],
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
const isValidUInt = n => typeof n === 'number' && Number.isInteger(n) && n >= 0
|
|
54
|
-
|
|
55
|
-
export const extractE2ESessionFromRetryReceipt = receipt => {
|
|
56
|
-
const keysNode = getBinaryNodeChild(receipt, 'keys')
|
|
57
|
-
if (!keysNode) return null
|
|
58
|
-
|
|
59
|
-
const typeBuf = getBinaryNodeChildBuffer(keysNode, 'type')
|
|
60
|
-
if (!typeBuf || typeBuf.length !== 1 || typeBuf[0] !== KEY_BUNDLE_TYPE[0]) return null
|
|
61
|
-
|
|
62
|
-
const identity = getBinaryNodeChildBuffer(keysNode, 'identity')
|
|
63
|
-
const skey = getBinaryNodeChild(keysNode, 'skey')
|
|
64
|
-
if (!identity || identity.length !== 32 || !skey) return null
|
|
65
|
-
|
|
66
|
-
const registrationId = getBinaryNodeChildUInt(receipt, 'registration', 4)
|
|
67
|
-
if (!isValidUInt(registrationId)) return null
|
|
68
|
-
|
|
69
|
-
const signedPubKey = getBinaryNodeChildBuffer(skey, 'value')
|
|
70
|
-
const signedSig = getBinaryNodeChildBuffer(skey, 'signature')
|
|
71
|
-
const signedKeyId = getBinaryNodeChildUInt(skey, 'id', 3)
|
|
72
|
-
if (!signedPubKey || signedPubKey.length !== 32 || !signedSig || !isValidUInt(signedKeyId)) return null
|
|
73
|
-
|
|
74
|
-
const preKeyNode = getBinaryNodeChild(keysNode, 'key')
|
|
75
|
-
let preKey
|
|
76
|
-
if (preKeyNode) {
|
|
77
|
-
const preKeyPub = getBinaryNodeChildBuffer(preKeyNode, 'value')
|
|
78
|
-
const preKeyId = getBinaryNodeChildUInt(preKeyNode, 'id', 3)
|
|
79
|
-
if (!preKeyPub || preKeyPub.length !== 32 || !isValidUInt(preKeyId)) return null
|
|
80
|
-
preKey = { keyId: preKeyId, publicKey: generateSignalPubKey(preKeyPub) }
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
registrationId,
|
|
85
|
-
identityKey: generateSignalPubKey(identity),
|
|
86
|
-
signedPreKey: { keyId: signedKeyId, publicKey: generateSignalPubKey(signedPubKey), signature: signedSig },
|
|
87
|
-
preKey,
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export const parseAndInjectE2ESessions = async (node, repository) => {
|
|
92
|
-
const extractKey = key => key ? {
|
|
93
|
-
keyId: getBinaryNodeChildUInt(key, 'id', 3),
|
|
94
|
-
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')),
|
|
95
|
-
signature: getBinaryNodeChildBuffer(key, 'signature'),
|
|
96
|
-
} : undefined
|
|
97
|
-
|
|
98
|
-
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user').filter(node => {
|
|
99
|
-
try { assertNodeErrorFree(node); return true } catch { return false }
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
for (const nodesChunk of chunk(nodes, 100)) {
|
|
103
|
-
await Promise.all(nodesChunk.map(node => repository.injectE2ESession({
|
|
104
|
-
jid: node.attrs.jid,
|
|
105
|
-
session: {
|
|
106
|
-
registrationId: getBinaryNodeChildUInt(node, 'registration', 4),
|
|
107
|
-
identityKey: generateSignalPubKey(getBinaryNodeChildBuffer(node, 'identity')),
|
|
108
|
-
signedPreKey: extractKey(getBinaryNodeChild(node, 'skey')),
|
|
109
|
-
preKey: extractKey(getBinaryNodeChild(node, 'key')),
|
|
110
|
-
},
|
|
111
|
-
})))
|
|
112
|
-
await new Promise(resolve => setImmediate(resolve)) // yield between chunks to avoid blocking on large batches
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
|
|
117
|
-
const { user: myUser, device: myDevice } = jidDecode(myJid)
|
|
118
|
-
const extracted = []
|
|
119
|
-
for (const { devices, id } of result) {
|
|
120
|
-
const decoded = jidDecode(id)
|
|
121
|
-
const { user, server } = decoded
|
|
122
|
-
let { domainType } = decoded
|
|
123
|
-
const deviceList = devices?.deviceList
|
|
124
|
-
if (!Array.isArray(deviceList)) continue
|
|
125
|
-
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
|
126
|
-
if ((!excludeZeroDevices || device !== 0) &&
|
|
127
|
-
((myUser !== user && myLid !== user) || myDevice !== device) &&
|
|
128
|
-
(device === 0 || !!keyIndex)) {
|
|
129
|
-
if (isHosted) domainType = domainType === WAJIDDomains.LID ? WAJIDDomains.HOSTED_LID : WAJIDDomains.HOSTED
|
|
130
|
-
extracted.push({ user, device, domainType, server: getServerFromDomainType(server, domainType) })
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return extracted
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
138
|
-
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count)
|
|
139
|
-
await keys.set({ 'pre-key': newPreKeys })
|
|
140
|
-
const preKeys = await getPreKeys(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1])
|
|
141
|
-
return {
|
|
142
|
-
update: {
|
|
143
|
-
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
144
|
-
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1),
|
|
145
|
-
},
|
|
146
|
-
preKeys,
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export const getNextPreKeysNode = async (state, count) => {
|
|
151
|
-
const { creds } = state
|
|
152
|
-
const { update, preKeys } = await getNextPreKeys(state, count)
|
|
153
|
-
return {
|
|
154
|
-
update,
|
|
155
|
-
node: {
|
|
156
|
-
tag: 'iq',
|
|
157
|
-
attrs: { xmlns: 'encrypt', type: 'set', to: S_WHATSAPP_NET },
|
|
158
|
-
content: [
|
|
159
|
-
{ tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
|
|
160
|
-
{ tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
|
|
161
|
-
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
162
|
-
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
|
|
163
|
-
xmppSignedPreKey(creds.signedPreKey),
|
|
164
|
-
],
|
|
165
|
-
},
|
|
166
|
-
}
|
|
1
|
+
import { KEY_BUNDLE_TYPE } from '../Defaults/index.js'
|
|
2
|
+
import { assertNodeErrorFree, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildUInt, getServerFromDomainType, jidDecode, S_WHATSAPP_NET, WAJIDDomains } from '../WABinary/index.js'
|
|
3
|
+
import { Curve, generateSignalPubKey } from './crypto.js'
|
|
4
|
+
import { encodeBigEndian } from './generics.js'
|
|
5
|
+
|
|
6
|
+
const chunk = (array, size) => {
|
|
7
|
+
const chunks = []
|
|
8
|
+
for (let i = 0; i < array.length; i += size) chunks.push(array.slice(i, i + size))
|
|
9
|
+
return chunks
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const createSignalIdentity = (wid, accountSignatureKey) => ({
|
|
13
|
+
identifier: { name: wid, deviceId: 0 },
|
|
14
|
+
identifierKey: generateSignalPubKey(accountSignatureKey),
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
export const getPreKeys = async ({ get }, min, limit) => {
|
|
18
|
+
const idList = []
|
|
19
|
+
for (let id = min; id < limit; id++) idList.push(id.toString())
|
|
20
|
+
return get('pre-key', idList)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const generateOrGetPreKeys = (creds, range) => {
|
|
24
|
+
const available = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId
|
|
25
|
+
const remaining = range - available
|
|
26
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1
|
|
27
|
+
const newPreKeys = {}
|
|
28
|
+
if (remaining > 0) {
|
|
29
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) newPreKeys[i] = Curve.generateKeyPair()
|
|
30
|
+
}
|
|
31
|
+
return { newPreKeys, lastPreKeyId, preKeysRange: [creds.firstUnuploadedPreKeyId, range] }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const xmppSignedPreKey = key => ({
|
|
35
|
+
tag: 'skey',
|
|
36
|
+
attrs: {},
|
|
37
|
+
content: [
|
|
38
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
|
|
39
|
+
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
40
|
+
{ tag: 'signature', attrs: {}, content: key.signature },
|
|
41
|
+
],
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
export const xmppPreKey = (pair, id) => ({
|
|
45
|
+
tag: 'key',
|
|
46
|
+
attrs: {},
|
|
47
|
+
content: [
|
|
48
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
|
|
49
|
+
{ tag: 'value', attrs: {}, content: pair.public },
|
|
50
|
+
],
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const isValidUInt = n => typeof n === 'number' && Number.isInteger(n) && n >= 0
|
|
54
|
+
|
|
55
|
+
export const extractE2ESessionFromRetryReceipt = receipt => {
|
|
56
|
+
const keysNode = getBinaryNodeChild(receipt, 'keys')
|
|
57
|
+
if (!keysNode) return null
|
|
58
|
+
|
|
59
|
+
const typeBuf = getBinaryNodeChildBuffer(keysNode, 'type')
|
|
60
|
+
if (!typeBuf || typeBuf.length !== 1 || typeBuf[0] !== KEY_BUNDLE_TYPE[0]) return null
|
|
61
|
+
|
|
62
|
+
const identity = getBinaryNodeChildBuffer(keysNode, 'identity')
|
|
63
|
+
const skey = getBinaryNodeChild(keysNode, 'skey')
|
|
64
|
+
if (!identity || identity.length !== 32 || !skey) return null
|
|
65
|
+
|
|
66
|
+
const registrationId = getBinaryNodeChildUInt(receipt, 'registration', 4)
|
|
67
|
+
if (!isValidUInt(registrationId)) return null
|
|
68
|
+
|
|
69
|
+
const signedPubKey = getBinaryNodeChildBuffer(skey, 'value')
|
|
70
|
+
const signedSig = getBinaryNodeChildBuffer(skey, 'signature')
|
|
71
|
+
const signedKeyId = getBinaryNodeChildUInt(skey, 'id', 3)
|
|
72
|
+
if (!signedPubKey || signedPubKey.length !== 32 || !signedSig || !isValidUInt(signedKeyId)) return null
|
|
73
|
+
|
|
74
|
+
const preKeyNode = getBinaryNodeChild(keysNode, 'key')
|
|
75
|
+
let preKey
|
|
76
|
+
if (preKeyNode) {
|
|
77
|
+
const preKeyPub = getBinaryNodeChildBuffer(preKeyNode, 'value')
|
|
78
|
+
const preKeyId = getBinaryNodeChildUInt(preKeyNode, 'id', 3)
|
|
79
|
+
if (!preKeyPub || preKeyPub.length !== 32 || !isValidUInt(preKeyId)) return null
|
|
80
|
+
preKey = { keyId: preKeyId, publicKey: generateSignalPubKey(preKeyPub) }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
registrationId,
|
|
85
|
+
identityKey: generateSignalPubKey(identity),
|
|
86
|
+
signedPreKey: { keyId: signedKeyId, publicKey: generateSignalPubKey(signedPubKey), signature: signedSig },
|
|
87
|
+
preKey,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const parseAndInjectE2ESessions = async (node, repository) => {
|
|
92
|
+
const extractKey = key => key ? {
|
|
93
|
+
keyId: getBinaryNodeChildUInt(key, 'id', 3),
|
|
94
|
+
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')),
|
|
95
|
+
signature: getBinaryNodeChildBuffer(key, 'signature'),
|
|
96
|
+
} : undefined
|
|
97
|
+
|
|
98
|
+
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user').filter(node => {
|
|
99
|
+
try { assertNodeErrorFree(node); return true } catch { return false }
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
for (const nodesChunk of chunk(nodes, 100)) {
|
|
103
|
+
await Promise.all(nodesChunk.map(node => repository.injectE2ESession({
|
|
104
|
+
jid: node.attrs.jid,
|
|
105
|
+
session: {
|
|
106
|
+
registrationId: getBinaryNodeChildUInt(node, 'registration', 4),
|
|
107
|
+
identityKey: generateSignalPubKey(getBinaryNodeChildBuffer(node, 'identity')),
|
|
108
|
+
signedPreKey: extractKey(getBinaryNodeChild(node, 'skey')),
|
|
109
|
+
preKey: extractKey(getBinaryNodeChild(node, 'key')),
|
|
110
|
+
},
|
|
111
|
+
})))
|
|
112
|
+
await new Promise(resolve => setImmediate(resolve)) // yield between chunks to avoid blocking on large batches
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
|
|
117
|
+
const { user: myUser, device: myDevice } = jidDecode(myJid)
|
|
118
|
+
const extracted = []
|
|
119
|
+
for (const { devices, id } of result) {
|
|
120
|
+
const decoded = jidDecode(id)
|
|
121
|
+
const { user, server } = decoded
|
|
122
|
+
let { domainType } = decoded
|
|
123
|
+
const deviceList = devices?.deviceList
|
|
124
|
+
if (!Array.isArray(deviceList)) continue
|
|
125
|
+
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
|
126
|
+
if ((!excludeZeroDevices || device !== 0) &&
|
|
127
|
+
((myUser !== user && myLid !== user) || myDevice !== device) &&
|
|
128
|
+
(device === 0 || !!keyIndex)) {
|
|
129
|
+
if (isHosted) domainType = domainType === WAJIDDomains.LID ? WAJIDDomains.HOSTED_LID : WAJIDDomains.HOSTED
|
|
130
|
+
extracted.push({ user, device, domainType, server: getServerFromDomainType(server, domainType) })
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return extracted
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
138
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count)
|
|
139
|
+
await keys.set({ 'pre-key': newPreKeys })
|
|
140
|
+
const preKeys = await getPreKeys(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1])
|
|
141
|
+
return {
|
|
142
|
+
update: {
|
|
143
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
144
|
+
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1),
|
|
145
|
+
},
|
|
146
|
+
preKeys,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export const getNextPreKeysNode = async (state, count) => {
|
|
151
|
+
const { creds } = state
|
|
152
|
+
const { update, preKeys } = await getNextPreKeys(state, count)
|
|
153
|
+
return {
|
|
154
|
+
update,
|
|
155
|
+
node: {
|
|
156
|
+
tag: 'iq',
|
|
157
|
+
attrs: { xmlns: 'encrypt', type: 'set', to: S_WHATSAPP_NET },
|
|
158
|
+
content: [
|
|
159
|
+
{ tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
|
|
160
|
+
{ tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
|
|
161
|
+
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
162
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
|
|
163
|
+
xmppSignedPreKey(creds.signedPreKey),
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
167
|
}
|