@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
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
import { hkdf, aesDecryptGCM } from './crypto.js'
|
|
2
|
-
import { unpadRandomMax16 } from './generics.js'
|
|
3
|
-
import { jidNormalizedUser } from '../WABinary/index.js'
|
|
4
|
-
import { proto } from '../../WAProto/index.js'
|
|
5
|
-
|
|
6
|
-
const BOT_MESSAGE_INFO = 'Bot Message'
|
|
7
|
-
const KEY_LENGTH = 32
|
|
8
|
-
const AUTH_TAG_LENGTH = 16
|
|
9
|
-
const MSG_ID_HEX_RE = /^[0-9A-Fa-f]{32}$/
|
|
10
|
-
|
|
11
|
-
const toBuffer = value => {
|
|
12
|
-
if (Buffer.isBuffer(value)) return value
|
|
13
|
-
if (value instanceof Uint8Array) return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
|
|
14
|
-
return Buffer.from(value)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const buildMessageIdRepresentations = messageId => {
|
|
18
|
-
const ascii = Buffer.from(messageId)
|
|
19
|
-
const binary = MSG_ID_HEX_RE.test(messageId) ? Buffer.from(messageId, 'hex') : ascii
|
|
20
|
-
return [{ label: 'msgIdAscii', value: ascii }, ...(binary.equals(ascii) ? [] : [{ label: 'msgIdBinary', value: binary }])]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const pushUnique = (items, seen, item) => {
|
|
24
|
-
const key = JSON.stringify([item.messageId, item.idSource, item.idSources, item.infoSource, item.aadSource, item.info.toString('hex'), item.aad.toString('hex')])
|
|
25
|
-
if (!seen.has(key)) { seen.add(key); items.push(item) }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const getCandidateIds = messageKey => {
|
|
29
|
-
const ordered = [
|
|
30
|
-
messageKey?.botType === 'full' ? { source: 'stanzaId', messageId: messageKey?.stanzaId } : { source: 'botEditTargetId', messageId: messageKey?.botEditTargetId },
|
|
31
|
-
{ source: 'targetId', messageId: messageKey?.targetId },
|
|
32
|
-
{ source: 'metaTargetId', messageId: messageKey?.metaTargetId },
|
|
33
|
-
{ source: 'stanzaId', messageId: messageKey?.stanzaId },
|
|
34
|
-
]
|
|
35
|
-
const candidates = Array.isArray(messageKey?.targetIdCandidates) ? messageKey.targetIdCandidates : []
|
|
36
|
-
for (let i = 0; i < candidates.length; i++) ordered.push({ source: `targetIdCandidates[${i}]`, messageId: candidates[i] })
|
|
37
|
-
const grouped = new Map()
|
|
38
|
-
for (const c of ordered) {
|
|
39
|
-
if (!c.messageId) continue
|
|
40
|
-
const id = String(c.messageId)
|
|
41
|
-
const existing = grouped.get(id)
|
|
42
|
-
if (existing) { if (!existing.idSources.includes(c.source)) existing.idSources.push(c.source) }
|
|
43
|
-
else grouped.set(id, { messageId: id, idSource: c.source, idSources: [c.source] })
|
|
44
|
-
}
|
|
45
|
-
return Array.from(grouped.values())
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const getJidCandidates = messageKey => {
|
|
49
|
-
const ordered = [
|
|
50
|
-
{ source: 'meId', jid: messageKey?.meId },
|
|
51
|
-
{ source: 'conversationJid', jid: messageKey?.conversationJid },
|
|
52
|
-
{ source: 'senderJid', jid: messageKey?.senderJid },
|
|
53
|
-
{ source: 'meLidNormalized', jid: jidNormalizedUser(messageKey?.meLid) },
|
|
54
|
-
]
|
|
55
|
-
const seen = new Set()
|
|
56
|
-
const result = []
|
|
57
|
-
for (const c of ordered) {
|
|
58
|
-
if (!c.jid) continue
|
|
59
|
-
const jid = String(c.jid)
|
|
60
|
-
if (!seen.has(jid)) { seen.add(jid); result.push({ source: c.source, jid, value: Buffer.from(jid) }) }
|
|
61
|
-
}
|
|
62
|
-
return result
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const buildMsmsgDecryptionStrategies = messageKey => {
|
|
66
|
-
const botJid = String(messageKey?.participant || '')
|
|
67
|
-
const botJidBuffer = Buffer.from(botJid)
|
|
68
|
-
const targetIds = getCandidateIds(messageKey)
|
|
69
|
-
const jidCandidates = getJidCandidates(messageKey)
|
|
70
|
-
const primaryJid = jidCandidates[0]
|
|
71
|
-
const alternateJid = jidCandidates.find(c => c.source !== primaryJid?.source && c.jid !== botJid)
|
|
72
|
-
const strategies = [], seen = new Set()
|
|
73
|
-
for (const idCandidate of targetIds) {
|
|
74
|
-
for (const idForm of buildMessageIdRepresentations(idCandidate.messageId)) {
|
|
75
|
-
pushUnique(strategies, seen, { mode: '2step', idSource: idCandidate.idSource, idSources: idCandidate.idSources, infoSource: `${idForm.label}+meId+botJid`, aadSource: `${idForm.label}+0+botJid`, authTagLayout: 'trailing', messageId: idCandidate.messageId, info: Buffer.concat([idForm.value, primaryJid.value, botJidBuffer, Buffer.alloc(0)]), aad: Buffer.concat([idForm.value, Buffer.from([0]), botJidBuffer]), attemptLabel: `${idCandidate.idSource}:${idForm.label}:primary` })
|
|
76
|
-
if (alternateJid) pushUnique(strategies, seen, { mode: '2step', idSource: idCandidate.idSource, idSources: idCandidate.idSources, infoSource: `${idForm.label}+${alternateJid.source}+botJid`, aadSource: `${idForm.label}+0+${alternateJid.source}`, authTagLayout: 'trailing', messageId: idCandidate.messageId, info: Buffer.concat([idForm.value, alternateJid.value, botJidBuffer, Buffer.alloc(0)]), aad: Buffer.concat([idForm.value, Buffer.from([0]), alternateJid.value]), attemptLabel: `${idCandidate.idSource}:${idForm.label}:${alternateJid.source}` })
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return strategies.slice(0, 12)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const assertRequired = (value, label) => {
|
|
83
|
-
if (!value || (Buffer.isBuffer(value) && value.length === 0) || (value instanceof Uint8Array && value.byteLength === 0)) throw new Error(`Missing required ${label} for msmsg decryption`)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const decryptWithStrategy = (messageSecret, msMsg, strategy) => {
|
|
87
|
-
const baseSecret = Buffer.from(hkdf(toBuffer(messageSecret), KEY_LENGTH, { info: BOT_MESSAGE_INFO }))
|
|
88
|
-
const key = Buffer.from(hkdf(baseSecret, KEY_LENGTH, { info: strategy.info }))
|
|
89
|
-
const payload = toBuffer(msMsg.encPayload)
|
|
90
|
-
const ciphertextWithTag = Buffer.concat([payload.slice(0, -AUTH_TAG_LENGTH), payload.slice(-AUTH_TAG_LENGTH)])
|
|
91
|
-
return Buffer.from(aesDecryptGCM(ciphertextWithTag, key, toBuffer(msMsg.encIv), strategy.aad))
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export const decodeDecryptedMsmsgMessage = decrypted => {
|
|
95
|
-
const messageBuffer = toBuffer(decrypted)
|
|
96
|
-
try {
|
|
97
|
-
const unpadded = Buffer.from(unpadRandomMax16(messageBuffer))
|
|
98
|
-
const decoded = proto.Message.decode(unpadded)
|
|
99
|
-
if (Object.keys(decoded).some(key => key !== 'messageContextInfo' && decoded[key] != null)) return decoded
|
|
100
|
-
} catch { }
|
|
101
|
-
return proto.Message.decode(messageBuffer)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export const decryptMsmsgBotMessage = async (messageSecret, messageKey, msMsg) => {
|
|
105
|
-
assertRequired(messageSecret, 'messageSecret')
|
|
106
|
-
assertRequired(messageKey?.participant, 'participant')
|
|
107
|
-
assertRequired(messageKey?.meId, 'meId')
|
|
108
|
-
assertRequired(msMsg?.encIv, 'encIv')
|
|
109
|
-
assertRequired(msMsg?.encPayload, 'encPayload')
|
|
110
|
-
if (getCandidateIds(messageKey).length === 0) throw new Error('Missing required target message id for msmsg decryption')
|
|
111
|
-
const strategies = buildMsmsgDecryptionStrategies(messageKey)
|
|
112
|
-
const attemptedStrategies = []
|
|
113
|
-
let lastError
|
|
114
|
-
for (const strategy of strategies) {
|
|
115
|
-
try { return decryptWithStrategy(messageSecret, msMsg, strategy) }
|
|
116
|
-
catch (error) { attemptedStrategies.push({ idSource: strategy.idSource, idSources: strategy.idSources, infoSource: strategy.infoSource, aadSource: strategy.aadSource, messageId: strategy.messageId }); lastError = error }
|
|
117
|
-
}
|
|
118
|
-
const error = new Error('Failed to decrypt msmsg with bounded deterministic strategies')
|
|
119
|
-
error.attemptedStrategies = attemptedStrategies
|
|
120
|
-
error.cause = lastError
|
|
121
|
-
throw error
|
|
1
|
+
import { hkdf, aesDecryptGCM } from './crypto.js'
|
|
2
|
+
import { unpadRandomMax16 } from './generics.js'
|
|
3
|
+
import { jidNormalizedUser } from '../WABinary/index.js'
|
|
4
|
+
import { proto } from '../../WAProto/index.js'
|
|
5
|
+
|
|
6
|
+
const BOT_MESSAGE_INFO = 'Bot Message'
|
|
7
|
+
const KEY_LENGTH = 32
|
|
8
|
+
const AUTH_TAG_LENGTH = 16
|
|
9
|
+
const MSG_ID_HEX_RE = /^[0-9A-Fa-f]{32}$/
|
|
10
|
+
|
|
11
|
+
const toBuffer = value => {
|
|
12
|
+
if (Buffer.isBuffer(value)) return value
|
|
13
|
+
if (value instanceof Uint8Array) return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
|
|
14
|
+
return Buffer.from(value)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const buildMessageIdRepresentations = messageId => {
|
|
18
|
+
const ascii = Buffer.from(messageId)
|
|
19
|
+
const binary = MSG_ID_HEX_RE.test(messageId) ? Buffer.from(messageId, 'hex') : ascii
|
|
20
|
+
return [{ label: 'msgIdAscii', value: ascii }, ...(binary.equals(ascii) ? [] : [{ label: 'msgIdBinary', value: binary }])]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const pushUnique = (items, seen, item) => {
|
|
24
|
+
const key = JSON.stringify([item.messageId, item.idSource, item.idSources, item.infoSource, item.aadSource, item.info.toString('hex'), item.aad.toString('hex')])
|
|
25
|
+
if (!seen.has(key)) { seen.add(key); items.push(item) }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const getCandidateIds = messageKey => {
|
|
29
|
+
const ordered = [
|
|
30
|
+
messageKey?.botType === 'full' ? { source: 'stanzaId', messageId: messageKey?.stanzaId } : { source: 'botEditTargetId', messageId: messageKey?.botEditTargetId },
|
|
31
|
+
{ source: 'targetId', messageId: messageKey?.targetId },
|
|
32
|
+
{ source: 'metaTargetId', messageId: messageKey?.metaTargetId },
|
|
33
|
+
{ source: 'stanzaId', messageId: messageKey?.stanzaId },
|
|
34
|
+
]
|
|
35
|
+
const candidates = Array.isArray(messageKey?.targetIdCandidates) ? messageKey.targetIdCandidates : []
|
|
36
|
+
for (let i = 0; i < candidates.length; i++) ordered.push({ source: `targetIdCandidates[${i}]`, messageId: candidates[i] })
|
|
37
|
+
const grouped = new Map()
|
|
38
|
+
for (const c of ordered) {
|
|
39
|
+
if (!c.messageId) continue
|
|
40
|
+
const id = String(c.messageId)
|
|
41
|
+
const existing = grouped.get(id)
|
|
42
|
+
if (existing) { if (!existing.idSources.includes(c.source)) existing.idSources.push(c.source) }
|
|
43
|
+
else grouped.set(id, { messageId: id, idSource: c.source, idSources: [c.source] })
|
|
44
|
+
}
|
|
45
|
+
return Array.from(grouped.values())
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const getJidCandidates = messageKey => {
|
|
49
|
+
const ordered = [
|
|
50
|
+
{ source: 'meId', jid: messageKey?.meId },
|
|
51
|
+
{ source: 'conversationJid', jid: messageKey?.conversationJid },
|
|
52
|
+
{ source: 'senderJid', jid: messageKey?.senderJid },
|
|
53
|
+
{ source: 'meLidNormalized', jid: jidNormalizedUser(messageKey?.meLid) },
|
|
54
|
+
]
|
|
55
|
+
const seen = new Set()
|
|
56
|
+
const result = []
|
|
57
|
+
for (const c of ordered) {
|
|
58
|
+
if (!c.jid) continue
|
|
59
|
+
const jid = String(c.jid)
|
|
60
|
+
if (!seen.has(jid)) { seen.add(jid); result.push({ source: c.source, jid, value: Buffer.from(jid) }) }
|
|
61
|
+
}
|
|
62
|
+
return result
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const buildMsmsgDecryptionStrategies = messageKey => {
|
|
66
|
+
const botJid = String(messageKey?.participant || '')
|
|
67
|
+
const botJidBuffer = Buffer.from(botJid)
|
|
68
|
+
const targetIds = getCandidateIds(messageKey)
|
|
69
|
+
const jidCandidates = getJidCandidates(messageKey)
|
|
70
|
+
const primaryJid = jidCandidates[0]
|
|
71
|
+
const alternateJid = jidCandidates.find(c => c.source !== primaryJid?.source && c.jid !== botJid)
|
|
72
|
+
const strategies = [], seen = new Set()
|
|
73
|
+
for (const idCandidate of targetIds) {
|
|
74
|
+
for (const idForm of buildMessageIdRepresentations(idCandidate.messageId)) {
|
|
75
|
+
pushUnique(strategies, seen, { mode: '2step', idSource: idCandidate.idSource, idSources: idCandidate.idSources, infoSource: `${idForm.label}+meId+botJid`, aadSource: `${idForm.label}+0+botJid`, authTagLayout: 'trailing', messageId: idCandidate.messageId, info: Buffer.concat([idForm.value, primaryJid.value, botJidBuffer, Buffer.alloc(0)]), aad: Buffer.concat([idForm.value, Buffer.from([0]), botJidBuffer]), attemptLabel: `${idCandidate.idSource}:${idForm.label}:primary` })
|
|
76
|
+
if (alternateJid) pushUnique(strategies, seen, { mode: '2step', idSource: idCandidate.idSource, idSources: idCandidate.idSources, infoSource: `${idForm.label}+${alternateJid.source}+botJid`, aadSource: `${idForm.label}+0+${alternateJid.source}`, authTagLayout: 'trailing', messageId: idCandidate.messageId, info: Buffer.concat([idForm.value, alternateJid.value, botJidBuffer, Buffer.alloc(0)]), aad: Buffer.concat([idForm.value, Buffer.from([0]), alternateJid.value]), attemptLabel: `${idCandidate.idSource}:${idForm.label}:${alternateJid.source}` })
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return strategies.slice(0, 12)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const assertRequired = (value, label) => {
|
|
83
|
+
if (!value || (Buffer.isBuffer(value) && value.length === 0) || (value instanceof Uint8Array && value.byteLength === 0)) throw new Error(`Missing required ${label} for msmsg decryption`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const decryptWithStrategy = (messageSecret, msMsg, strategy) => {
|
|
87
|
+
const baseSecret = Buffer.from(hkdf(toBuffer(messageSecret), KEY_LENGTH, { info: BOT_MESSAGE_INFO }))
|
|
88
|
+
const key = Buffer.from(hkdf(baseSecret, KEY_LENGTH, { info: strategy.info }))
|
|
89
|
+
const payload = toBuffer(msMsg.encPayload)
|
|
90
|
+
const ciphertextWithTag = Buffer.concat([payload.slice(0, -AUTH_TAG_LENGTH), payload.slice(-AUTH_TAG_LENGTH)])
|
|
91
|
+
return Buffer.from(aesDecryptGCM(ciphertextWithTag, key, toBuffer(msMsg.encIv), strategy.aad))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const decodeDecryptedMsmsgMessage = decrypted => {
|
|
95
|
+
const messageBuffer = toBuffer(decrypted)
|
|
96
|
+
try {
|
|
97
|
+
const unpadded = Buffer.from(unpadRandomMax16(messageBuffer))
|
|
98
|
+
const decoded = proto.Message.decode(unpadded)
|
|
99
|
+
if (Object.keys(decoded).some(key => key !== 'messageContextInfo' && decoded[key] != null)) return decoded
|
|
100
|
+
} catch { }
|
|
101
|
+
return proto.Message.decode(messageBuffer)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const decryptMsmsgBotMessage = async (messageSecret, messageKey, msMsg) => {
|
|
105
|
+
assertRequired(messageSecret, 'messageSecret')
|
|
106
|
+
assertRequired(messageKey?.participant, 'participant')
|
|
107
|
+
assertRequired(messageKey?.meId, 'meId')
|
|
108
|
+
assertRequired(msMsg?.encIv, 'encIv')
|
|
109
|
+
assertRequired(msMsg?.encPayload, 'encPayload')
|
|
110
|
+
if (getCandidateIds(messageKey).length === 0) throw new Error('Missing required target message id for msmsg decryption')
|
|
111
|
+
const strategies = buildMsmsgDecryptionStrategies(messageKey)
|
|
112
|
+
const attemptedStrategies = []
|
|
113
|
+
let lastError
|
|
114
|
+
for (const strategy of strategies) {
|
|
115
|
+
try { return decryptWithStrategy(messageSecret, msMsg, strategy) }
|
|
116
|
+
catch (error) { attemptedStrategies.push({ idSource: strategy.idSource, idSources: strategy.idSources, infoSource: strategy.infoSource, aadSource: strategy.aadSource, messageId: strategy.messageId }); lastError = error }
|
|
117
|
+
}
|
|
118
|
+
const error = new Error('Failed to decrypt msmsg with bounded deterministic strategies')
|
|
119
|
+
error.attemptedStrategies = attemptedStrategies
|
|
120
|
+
error.cause = lastError
|
|
121
|
+
throw error
|
|
122
122
|
}
|
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { proto } from '../../WAProto/index.js'
|
|
2
|
-
import { BOT_RENDERING_CONFIG_METADATA } from '../Defaults/index.js'
|
|
3
|
-
import { BotCapabilityType, buildBotCapabilityMetadata } from './bot-capabilities.js'
|
|
4
|
-
import { delay } from './generics.js'
|
|
5
|
-
|
|
6
|
-
export const PlanningStepStatus = { IN_PROGRESS: 0, DONE: 1, FAILED: 2 }
|
|
7
|
-
|
|
8
|
-
export const buildProgressIndicator = (description, steps = [], estimatedMs) => {
|
|
9
|
-
const stepsMetadata = steps.map(s => {
|
|
10
|
-
const m = { statusTitle: s.title, status: s.status ?? PlanningStepStatus.IN_PROGRESS }
|
|
11
|
-
if (s.body) m.statusBody = s.body
|
|
12
|
-
if (s.isReasoning) m.isReasoning = true
|
|
13
|
-
if (s.isEnhancedSearch) m.isEnhancedSearch = true
|
|
14
|
-
return m
|
|
15
|
-
})
|
|
16
|
-
const indicator = { stepsMetadata }
|
|
17
|
-
if (description) indicator.progressDescription = description
|
|
18
|
-
if (estimatedMs != null) indicator.estimatedCompletionTime = estimatedMs
|
|
19
|
-
return indicator
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const buildCompositingPlaceholder = ({ description = 'Thinking…', steps = [], estimatedMs, placeholderText = '', verificationMetadata } = {}) => {
|
|
23
|
-
const progressIndicatorMetadata = buildProgressIndicator(description, steps, estimatedMs)
|
|
24
|
-
const unifiedData = new TextEncoder().encode(JSON.stringify({
|
|
25
|
-
response_id: crypto.randomUUID(),
|
|
26
|
-
sections: placeholderText ? [{ view_model: { primitive: { text: placeholderText, inline_entities: [], __typename: 'GenAIMarkdownTextUXPrimitive' }, __typename: 'GenAISingleLayoutViewModel' } }] : []
|
|
27
|
-
}))
|
|
28
|
-
const capabilities = [BotCapabilityType.PROGRESS_INDICATOR, ...(steps.length ? [BotCapabilityType.AGENTIC_PLANNING] : [])]
|
|
29
|
-
return {
|
|
30
|
-
messageContextInfo: {
|
|
31
|
-
botMetadata: {
|
|
32
|
-
capabilityMetadata: buildBotCapabilityMetadata(capabilities),
|
|
33
|
-
pluginMetadata: {},
|
|
34
|
-
progressIndicatorMetadata,
|
|
35
|
-
botRenderingConfigMetadata: BOT_RENDERING_CONFIG_METADATA,
|
|
36
|
-
...(verificationMetadata ? { verificationMetadata } : {}),
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
botForwardedMessage: {
|
|
40
|
-
message: {
|
|
41
|
-
richResponseMessage: {
|
|
42
|
-
messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
|
|
43
|
-
unifiedResponse: { data: unifiedData },
|
|
44
|
-
submessages: [],
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const buildPlainPlaceholder = (description = 'Thinking…', steps = [], placeholderText = '') => {
|
|
52
|
-
const lines = steps.map(s => {
|
|
53
|
-
const icon = s.status === PlanningStepStatus.DONE ? '✓' : s.status === PlanningStepStatus.FAILED ? '✗' : '○'
|
|
54
|
-
return `${icon} ${s.title}`
|
|
55
|
-
}).join('\n')
|
|
56
|
-
let text = `_${description}_`
|
|
57
|
-
if (lines) text += `\n\n${lines}`
|
|
58
|
-
if (placeholderText) text += `\n\n${placeholderText}`
|
|
59
|
-
return { text }
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export const metaTyping = async (sock, jid, { description = 'Thinking…', steps = [], estimatedMs, placeholderText = '' } = {}) => {
|
|
63
|
-
await sock.sendPresenceUpdate('composing', jid)
|
|
64
|
-
const pending = steps.map(s => ({ ...s, status: PlanningStepStatus.IN_PROGRESS }))
|
|
65
|
-
return sock.sendMessage(jid, buildPlainPlaceholder(description, pending, placeholderText))
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export const sendMetaComposited = async (sock, jid, content, { thinkingMs = 2000, description = 'Thinking…', steps = [], placeholderText = '', sendOptions = {} } = {}) => {
|
|
69
|
-
const placeholder = await metaTyping(sock, jid, { description, steps, estimatedMs: thinkingMs, placeholderText })
|
|
70
|
-
try { await delay(thinkingMs); if (placeholder?.key) await sock.sendMessage(jid, { delete: placeholder.key }) } catch (_) { }
|
|
71
|
-
await sock.sendPresenceUpdate('paused', jid)
|
|
72
|
-
return sock.sendMessage(jid, content, sendOptions)
|
|
73
|
-
}
|
|
74
|
-
|
|
1
|
+
import { proto } from '../../WAProto/index.js'
|
|
2
|
+
import { BOT_RENDERING_CONFIG_METADATA } from '../Defaults/index.js'
|
|
3
|
+
import { BotCapabilityType, buildBotCapabilityMetadata } from './bot-capabilities.js'
|
|
4
|
+
import { delay } from './generics.js'
|
|
5
|
+
|
|
6
|
+
export const PlanningStepStatus = { IN_PROGRESS: 0, DONE: 1, FAILED: 2 }
|
|
7
|
+
|
|
8
|
+
export const buildProgressIndicator = (description, steps = [], estimatedMs) => {
|
|
9
|
+
const stepsMetadata = steps.map(s => {
|
|
10
|
+
const m = { statusTitle: s.title, status: s.status ?? PlanningStepStatus.IN_PROGRESS }
|
|
11
|
+
if (s.body) m.statusBody = s.body
|
|
12
|
+
if (s.isReasoning) m.isReasoning = true
|
|
13
|
+
if (s.isEnhancedSearch) m.isEnhancedSearch = true
|
|
14
|
+
return m
|
|
15
|
+
})
|
|
16
|
+
const indicator = { stepsMetadata }
|
|
17
|
+
if (description) indicator.progressDescription = description
|
|
18
|
+
if (estimatedMs != null) indicator.estimatedCompletionTime = estimatedMs
|
|
19
|
+
return indicator
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const buildCompositingPlaceholder = ({ description = 'Thinking…', steps = [], estimatedMs, placeholderText = '', verificationMetadata } = {}) => {
|
|
23
|
+
const progressIndicatorMetadata = buildProgressIndicator(description, steps, estimatedMs)
|
|
24
|
+
const unifiedData = new TextEncoder().encode(JSON.stringify({
|
|
25
|
+
response_id: crypto.randomUUID(),
|
|
26
|
+
sections: placeholderText ? [{ view_model: { primitive: { text: placeholderText, inline_entities: [], __typename: 'GenAIMarkdownTextUXPrimitive' }, __typename: 'GenAISingleLayoutViewModel' } }] : []
|
|
27
|
+
}))
|
|
28
|
+
const capabilities = [BotCapabilityType.PROGRESS_INDICATOR, ...(steps.length ? [BotCapabilityType.AGENTIC_PLANNING] : [])]
|
|
29
|
+
return {
|
|
30
|
+
messageContextInfo: {
|
|
31
|
+
botMetadata: {
|
|
32
|
+
capabilityMetadata: buildBotCapabilityMetadata(capabilities),
|
|
33
|
+
pluginMetadata: {},
|
|
34
|
+
progressIndicatorMetadata,
|
|
35
|
+
botRenderingConfigMetadata: BOT_RENDERING_CONFIG_METADATA,
|
|
36
|
+
...(verificationMetadata ? { verificationMetadata } : {}),
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
botForwardedMessage: {
|
|
40
|
+
message: {
|
|
41
|
+
richResponseMessage: {
|
|
42
|
+
messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
|
|
43
|
+
unifiedResponse: { data: unifiedData },
|
|
44
|
+
submessages: [],
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const buildPlainPlaceholder = (description = 'Thinking…', steps = [], placeholderText = '') => {
|
|
52
|
+
const lines = steps.map(s => {
|
|
53
|
+
const icon = s.status === PlanningStepStatus.DONE ? '✓' : s.status === PlanningStepStatus.FAILED ? '✗' : '○'
|
|
54
|
+
return `${icon} ${s.title}`
|
|
55
|
+
}).join('\n')
|
|
56
|
+
let text = `_${description}_`
|
|
57
|
+
if (lines) text += `\n\n${lines}`
|
|
58
|
+
if (placeholderText) text += `\n\n${placeholderText}`
|
|
59
|
+
return { text }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const metaTyping = async (sock, jid, { description = 'Thinking…', steps = [], estimatedMs, placeholderText = '' } = {}) => {
|
|
63
|
+
await sock.sendPresenceUpdate('composing', jid)
|
|
64
|
+
const pending = steps.map(s => ({ ...s, status: PlanningStepStatus.IN_PROGRESS }))
|
|
65
|
+
return sock.sendMessage(jid, buildPlainPlaceholder(description, pending, placeholderText))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const sendMetaComposited = async (sock, jid, content, { thinkingMs = 2000, description = 'Thinking…', steps = [], placeholderText = '', sendOptions = {} } = {}) => {
|
|
69
|
+
const placeholder = await metaTyping(sock, jid, { description, steps, estimatedMs: thinkingMs, placeholderText })
|
|
70
|
+
try { await delay(thinkingMs); if (placeholder?.key) await sock.sendMessage(jid, { delete: placeholder.key }) } catch (_) { }
|
|
71
|
+
await sock.sendPresenceUpdate('paused', jid)
|
|
72
|
+
return sock.sendMessage(jid, content, sendOptions)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
75
|
export const buildSteps = (titles, status = PlanningStepStatus.IN_PROGRESS) => titles.map(title => ({ title, status }))
|