@zervonexx/baileys 0.0.0-stage → 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 +23 -0
- package/README.md +4112 -2
- package/WAProto/WAProtoCompile.js +323 -0
- package/WAProto/fetcher.js +68 -0
- package/WAProto/index.js +96 -0
- package/WAProto/parser.js +334 -0
- package/lib/Builders/AIRich.js +674 -0
- package/lib/Builders/auto.js +50 -0
- package/lib/Builders/base.js +145 -0
- package/lib/Builders/builders.js +589 -0
- package/lib/Builders/index.js +4 -0
- package/lib/Defaults/index.js +248 -0
- package/lib/Signal/index.js +2 -0
- package/lib/Signal/libsignal.js +527 -0
- package/lib/Signal/lid-mapping.js +283 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +84 -0
- package/lib/Socket/business.js +152 -0
- package/lib/Socket/chats.js +855 -0
- package/lib/Socket/communities.js +147 -0
- package/lib/Socket/groups.js +294 -0
- package/lib/Socket/index.js +15 -0
- package/lib/Socket/messages-recv.js +994 -0
- package/lib/Socket/messages-send.js +723 -0
- package/lib/Socket/mex.js +34 -0
- package/lib/Socket/newsletter.js +784 -0
- package/lib/Socket/registration.js +155 -0
- package/lib/Socket/socket.js +798 -0
- package/lib/Socket/spam-report.js +144 -0
- package/lib/Socket/username.js +82 -0
- package/lib/Socket/zervonex-handler.js +58 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +80 -0
- package/lib/Store/make-in-memory-store.js +426 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Store/reporting-info-store.js +38 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +15 -0
- package/lib/Types/Newsletter.js +100 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +26 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +30 -0
- package/lib/Utils/auth-utils.js +159 -0
- package/lib/Utils/baileys-event-stream.js +33 -0
- package/lib/Utils/bot-capabilities.js +41 -0
- package/lib/Utils/bot-planning-replay.js +44 -0
- package/lib/Utils/browser-utils.js +30 -0
- package/lib/Utils/business.js +75 -0
- package/lib/Utils/chat-utils.js +317 -0
- package/lib/Utils/companion-reg-client-utils.js +20 -0
- package/lib/Utils/consumer-application.js +38 -0
- package/lib/Utils/crypto.js +53 -0
- package/lib/Utils/cstoken.js +15 -0
- package/lib/Utils/decode-wa-message.js +200 -0
- package/lib/Utils/event-buffer.js +273 -0
- package/lib/Utils/extras.js +186 -0
- package/lib/Utils/generics.js +195 -0
- package/lib/Utils/history.js +51 -0
- package/lib/Utils/identity-change-handler.js +19 -0
- package/lib/Utils/index.js +40 -0
- package/lib/Utils/link-preview.js +51 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.js +3 -0
- package/lib/Utils/make-mutex.js +16 -0
- package/lib/Utils/message-composer.js +62 -0
- package/lib/Utils/message-retry-manager.js +48 -0
- package/lib/Utils/messages-media.js +502 -0
- package/lib/Utils/messages.js +943 -0
- package/lib/Utils/meta-ai-msmsg.js +122 -0
- package/lib/Utils/meta-compositing.js +75 -0
- package/lib/Utils/modern-messages.js +235 -0
- package/lib/Utils/native-flow.js +81 -0
- package/lib/Utils/noise-handler.js +161 -0
- package/lib/Utils/pre-key-manager.js +76 -0
- package/lib/Utils/process-message.js +401 -0
- package/lib/Utils/reporting-utils.js +171 -0
- package/lib/Utils/signal.js +167 -0
- package/lib/Utils/spam-report-utils.js +166 -0
- package/lib/Utils/sync-action-utils.js +33 -0
- package/lib/Utils/tc-token-utils.js +154 -0
- package/lib/Utils/use-keyv-auth-state.js +200 -0
- package/lib/Utils/use-multi-file-auth-state.js +84 -0
- package/lib/Utils/validate-connection.js +172 -0
- package/lib/WABinary/constants.js +1321 -0
- package/lib/WABinary/decode.js +152 -0
- package/lib/WABinary/encode.js +140 -0
- package/lib/WABinary/generic-utils.js +203 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +235 -0
- package/lib/WABinary/types.js +4 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +88 -0
- package/lib/WAUSync/USyncUser.js +20 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +67 -0
- package/package.json +189 -4
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { createHmac } from 'crypto'
|
|
2
|
+
import { hkdf } from './crypto.js'
|
|
3
|
+
|
|
4
|
+
const reportingFields = [
|
|
5
|
+
{ f: 1 },
|
|
6
|
+
{ f: 3, s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }] },
|
|
7
|
+
{ f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
8
|
+
{ f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
9
|
+
{ f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
|
|
10
|
+
{ f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
|
|
11
|
+
{ f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
|
|
12
|
+
{ f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
|
|
13
|
+
{ f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
|
|
14
|
+
{ f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
15
|
+
{ f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
16
|
+
{ f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
|
|
17
|
+
{ f: 37, s: [{ f: 1, m: true }] },
|
|
18
|
+
{ f: 49, s: [{ f: 2 }, { f: 3, s: [{ f: 1 }, { f: 2 }] }, { f: 5, s: [{ f: 21 }, { f: 22 }] }, { f: 8, s: [{ f: 1 }, { f: 2 }] }] },
|
|
19
|
+
{ f: 53, s: [{ f: 1, m: true }] },
|
|
20
|
+
{ f: 55, s: [{ f: 1, m: true }] },
|
|
21
|
+
{ f: 58, s: [{ f: 1, m: true }] },
|
|
22
|
+
{ f: 59, s: [{ f: 1, m: true }] },
|
|
23
|
+
{ f: 60, s: [{ f: 2 }, { f: 3, s: [{ f: 1 }, { f: 2 }] }, { f: 5, s: [{ f: 21 }, { f: 22 }] }, { f: 8, s: [{ f: 1 }, { f: 2 }] }] },
|
|
24
|
+
{ f: 64, s: [{ f: 2 }, { f: 3, s: [{ f: 1 }, { f: 2 }] }, { f: 5, s: [{ f: 21 }, { f: 22 }] }, { f: 8, s: [{ f: 1 }, { f: 2 }] }] },
|
|
25
|
+
{ f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
|
|
26
|
+
{ f: 74, s: [{ f: 1, m: true }] },
|
|
27
|
+
{ f: 87, s: [{ f: 1, m: true }] },
|
|
28
|
+
{ f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
|
|
29
|
+
{ f: 92, s: [{ f: 1, m: true }] },
|
|
30
|
+
{ f: 93, s: [{ f: 1, m: true }] },
|
|
31
|
+
{ f: 94, s: [{ f: 1, m: true }] },
|
|
32
|
+
{ f: 97, s: [{ f: 2 }, { f: 3, s: [{ f: 1 }] }, { f: 4, s: [{ f: 21 }, { f: 22 }] }] }, // richResponseMessage
|
|
33
|
+
{ f: 98, s: [{ f: 1 }, { f: 2 }] }, // statusNotificationMessage
|
|
34
|
+
{ f: 100, s: [{ f: 1, m: true }] }, // botTaskMessage
|
|
35
|
+
{ f: 101, s: [{ f: 1, m: true }] }, // questionMessage
|
|
36
|
+
{ f: 103, s: [{ f: 1, m: true }] }, // groupStatusMessageV2
|
|
37
|
+
{ f: 104, s: [{ f: 1, m: true }] }, // botForwardedMessage
|
|
38
|
+
{ f: 105, s: [{ f: 1 }, { f: 2 }] }, // statusQuestionAnswerMessage
|
|
39
|
+
{ f: 107, s: [{ f: 1 }, { f: 2 }] }, // questionResponseMessage
|
|
40
|
+
{ f: 109, s: [{ f: 1 }, { f: 2 }, { f: 3 }, { f: 4 }] }, // statusQuotedMessage
|
|
41
|
+
{ f: 111, s: [{ f: 2 }, { f: 3, s: [{ f: 1 }, { f: 2 }] }, { f: 5, s: [{ f: 21 }, { f: 22 }] }, { f: 8, s: [{ f: 1 }, { f: 2 }] }] }, // pollCreationMessageV5
|
|
42
|
+
{ f: 115, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] }, // pollResultSnapshotMessageV3
|
|
43
|
+
{ f: 119, s: [{ f: 2 }, { f: 3, s: [{ f: 1 }, { f: 2 }] }, { f: 5, s: [{ f: 21 }, { f: 22 }] }, { f: 8, s: [{ f: 1 }, { f: 2 }] }] }, // pollCreationMessageV6
|
|
44
|
+
{ f: 125, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3 }, { f: 4 }] }, // splitPaymentMessage
|
|
45
|
+
{ f: 129, s: [{ f: 1, m: true }] }, // musicMessage
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
const EMPTY_MAP = new Map()
|
|
49
|
+
const ENC_SECRET_REPORT_TOKEN = 'Report Token'
|
|
50
|
+
const WIRE = { VARINT: 0, FIXED64: 1, BYTES: 2, FIXED32: 5 }
|
|
51
|
+
|
|
52
|
+
const compileReportingFields = fields => {
|
|
53
|
+
const map = new Map()
|
|
54
|
+
for (const f of fields) map.set(f.f, { m: f.m, children: f.s ? compileReportingFields(f.s) : undefined })
|
|
55
|
+
return map
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const compiledReportingFields = compileReportingFields(reportingFields)
|
|
59
|
+
|
|
60
|
+
const decodeVarint = (buffer, offset) => {
|
|
61
|
+
let value = 0, bytes = 0, shift = 0
|
|
62
|
+
while (offset + bytes < buffer.length) {
|
|
63
|
+
const current = buffer[offset + bytes]
|
|
64
|
+
value |= (current & 0x7f) << shift
|
|
65
|
+
bytes++
|
|
66
|
+
if ((current & 0x80) === 0) return { value, bytes, ok: true }
|
|
67
|
+
shift += 7
|
|
68
|
+
if (shift > 35) return { value: 0, bytes: 0, ok: false }
|
|
69
|
+
}
|
|
70
|
+
return { value: 0, bytes: 0, ok: false }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const encodeVarint = value => {
|
|
74
|
+
const parts = []
|
|
75
|
+
let remaining = value >>> 0
|
|
76
|
+
while (remaining > 0x7f) { parts.push((remaining & 0x7f) | 0x80); remaining >>>= 7 }
|
|
77
|
+
parts.push(remaining)
|
|
78
|
+
return Buffer.from(parts)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const extractReportingTokenContent = (data, cfg) => {
|
|
82
|
+
const out = []
|
|
83
|
+
let i = 0
|
|
84
|
+
while (i < data.length) {
|
|
85
|
+
const tag = decodeVarint(data, i)
|
|
86
|
+
if (!tag.ok) return null
|
|
87
|
+
const fieldNum = tag.value >> 3
|
|
88
|
+
const wireType = tag.value & 0x7
|
|
89
|
+
const fieldStart = i
|
|
90
|
+
i += tag.bytes
|
|
91
|
+
const fieldCfg = cfg.get(fieldNum)
|
|
92
|
+
const pushSlice = end => { if (end > data.length) return false; out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) }); i = end; return true }
|
|
93
|
+
const skip = end => { if (end > data.length) return false; i = end; return true }
|
|
94
|
+
|
|
95
|
+
if (wireType === WIRE.VARINT) {
|
|
96
|
+
const v = decodeVarint(data, i)
|
|
97
|
+
if (!v.ok) return null
|
|
98
|
+
const end = i + v.bytes
|
|
99
|
+
if (!fieldCfg) { if (!skip(end)) return null; continue }
|
|
100
|
+
if (!pushSlice(end)) return null
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
if (wireType === WIRE.FIXED64) {
|
|
104
|
+
const end = i + 8
|
|
105
|
+
if (!fieldCfg) { if (!skip(end)) return null; continue }
|
|
106
|
+
if (!pushSlice(end)) return null
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
if (wireType === WIRE.FIXED32) {
|
|
110
|
+
const end = i + 4
|
|
111
|
+
if (!fieldCfg) { if (!skip(end)) return null; continue }
|
|
112
|
+
if (!pushSlice(end)) return null
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
115
|
+
if (wireType === WIRE.BYTES) {
|
|
116
|
+
const len = decodeVarint(data, i)
|
|
117
|
+
if (!len.ok) return null
|
|
118
|
+
const valStart = i + len.bytes
|
|
119
|
+
const valEnd = valStart + len.value
|
|
120
|
+
if (valEnd > data.length) return null
|
|
121
|
+
if (!fieldCfg) { i = valEnd; continue }
|
|
122
|
+
if (fieldCfg.m || fieldCfg.children) {
|
|
123
|
+
const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP)
|
|
124
|
+
if (sub === null) return null
|
|
125
|
+
if (sub.length > 0) {
|
|
126
|
+
out.push({ num: fieldNum, bytes: Buffer.concat([encodeVarint(tag.value), encodeVarint(sub.length), sub]) })
|
|
127
|
+
}
|
|
128
|
+
i = valEnd
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) })
|
|
132
|
+
i = valEnd
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
if (out.length === 0) return Buffer.alloc(0)
|
|
138
|
+
out.sort((a, b) => a.num - b.num)
|
|
139
|
+
return Buffer.concat(out.map(f => f.bytes))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
|
|
143
|
+
const useCaseSecret = Buffer.concat([
|
|
144
|
+
Buffer.from(origMsgId, 'utf8'),
|
|
145
|
+
Buffer.from(origMsgSender, 'utf8'),
|
|
146
|
+
Buffer.from(modificationSender, 'utf8'),
|
|
147
|
+
Buffer.from(modificationType, 'utf8'),
|
|
148
|
+
])
|
|
149
|
+
return hkdf(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') })
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const shouldIncludeReportingToken = message =>
|
|
153
|
+
!message.reactionMessage && !message.encReactionMessage &&
|
|
154
|
+
!message.encEventResponseMessage && !message.pollUpdateMessage &&
|
|
155
|
+
!message.statusStickerInteractionMessage && !message.encCommentMessage
|
|
156
|
+
|
|
157
|
+
export const getMessageReportingToken = async (msgProtobuf, message, key) => {
|
|
158
|
+
const msgSecret = message.messageContextInfo?.messageSecret
|
|
159
|
+
if (!msgSecret || !key.id) return null
|
|
160
|
+
const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid
|
|
161
|
+
const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid
|
|
162
|
+
const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret)
|
|
163
|
+
const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields)
|
|
164
|
+
if (!content || content.length === 0) return null
|
|
165
|
+
const reportingToken = createHmac('sha256', reportingSecret).update(content).digest().subarray(0, 16)
|
|
166
|
+
return {
|
|
167
|
+
tag: 'reporting',
|
|
168
|
+
attrs: {},
|
|
169
|
+
content: [{ tag: 'reporting_token', attrs: { v: '2' }, content: reportingToken }],
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { getBinaryNodeChild, getBinaryNodeChildString, isLidUser, isHostedLidUser, isJidGroup, S_WHATSAPP_NET } from '../WABinary/index.js'
|
|
2
|
+
import { extractMessageContent } from './messages.js'
|
|
3
|
+
|
|
4
|
+
// ─── Constants ────────────────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
// Controls how WA routes the report internally — pass via options.spamFlow to override auto-selection.
|
|
7
|
+
// ACCOUNT_INFO_REPORT → tag-based 1:1 contact report (default)
|
|
8
|
+
// ACCOUNT_INFO_BLOCK → tag-based contact report signalling intent to block
|
|
9
|
+
// GROUP_INFO_REPORT → tag-based group report (auto-selected for group JIDs)
|
|
10
|
+
// CONTACT_INFO_REPORT → legacy lightweight report, no reporting_tag needed
|
|
11
|
+
// ONE_TO_ONE_SPAM_BANNER_REPORT → from the in-chat spam banner
|
|
12
|
+
// MESSAGE_MENU → from long-press message context menu
|
|
13
|
+
// OVERFLOW_MENU_REPORT → from the chat overflow (⋮) menu
|
|
14
|
+
export const SPAM_FLOWS = {
|
|
15
|
+
ACCOUNT_INFO_REPORT: 'account_info_report',
|
|
16
|
+
ACCOUNT_INFO_BLOCK: 'account_info_block',
|
|
17
|
+
GROUP_INFO_REPORT: 'group_info_report',
|
|
18
|
+
CONTACT_INFO_REPORT: 'contact_info_report',
|
|
19
|
+
ONE_TO_ONE_SPAM_BANNER_REPORT: '1_1_spam_banner_report',
|
|
20
|
+
MESSAGE_MENU: 'message_menu',
|
|
21
|
+
OVERFLOW_MENU_REPORT: 'overflow_menu_report',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Numeric reason codes for the w:report IQ endpoint — used by reportUser() and reportMessage().
|
|
25
|
+
export const REPORT_REASON_CODES = { spam: 1, harassment: 2, impersonation: 3, scam: 4, other: 5 }
|
|
26
|
+
|
|
27
|
+
// ─── Internal helpers ─────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
const toBuffer = data => {
|
|
30
|
+
if (!data) return null
|
|
31
|
+
if (Buffer.isBuffer(data)) return data
|
|
32
|
+
if (data instanceof Uint8Array) return Buffer.from(data)
|
|
33
|
+
if (typeof data === 'string') return Buffer.from(data, 'base64')
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ─── JID resolution ───────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
// Resolves a PN JID to its LID equivalent for tag-based spam IQs.
|
|
40
|
+
// Groups and already-LID/hosted-LID JIDs pass through unchanged.
|
|
41
|
+
export const resolveReportJid = async (jid, lidMapping) => {
|
|
42
|
+
if (!jid) throw new Error('resolveReportJid: jid required')
|
|
43
|
+
if (isJidGroup(jid) || isLidUser(jid) || isHostedLidUser(jid)) return jid
|
|
44
|
+
const lid = lidMapping?.getLIDForPN ? await lidMapping.getLIDForPN(jid) : null
|
|
45
|
+
return lid ?? jid
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Auto-detects JID type and returns the resolved JID + correct spam_flow for reportSpam().
|
|
49
|
+
// Returns { reportJid, spamFlow, isGroup }.
|
|
50
|
+
export const resolveReportContext = async (jid, lidMapping) => {
|
|
51
|
+
if (!jid) throw new Error('resolveReportContext: jid required')
|
|
52
|
+
if (isJidGroup(jid)) return { reportJid: jid, spamFlow: SPAM_FLOWS.GROUP_INFO_REPORT, isGroup: true }
|
|
53
|
+
return { reportJid: await resolveReportJid(jid, lidMapping), spamFlow: SPAM_FLOWS.ACCOUNT_INFO_REPORT, isGroup: false }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ─── Tag / text extraction ────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
// Normalizes a raw reporting_tag to a Buffer — returns null if invalid or empty.
|
|
59
|
+
export const normalizeReportingTag = tag => {
|
|
60
|
+
const buf = toBuffer(tag)
|
|
61
|
+
return buf?.length ? buf : null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Extracts plain text from a WAMessage across common message types.
|
|
65
|
+
export const extractMessageText = msg => {
|
|
66
|
+
if (!msg?.message) return ''
|
|
67
|
+
const c = extractMessageContent(msg.message)
|
|
68
|
+
if (!c) return ''
|
|
69
|
+
return c.conversation ?? c.extendedTextMessage?.text ?? c.imageMessage?.caption ?? c.videoMessage?.caption ?? ''
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Extracts { stanzaId, timestamp, reportingTag, reportingToken, tokenVersion, validationPolicy }
|
|
73
|
+
// from a raw incoming binary stanza. Call inside messages.upsert and persist via reportingInfoStore
|
|
74
|
+
// so reportSpam() can use the tags later.
|
|
75
|
+
export const extractReportingInfoFromStanza = stanza => {
|
|
76
|
+
const reporting = getBinaryNodeChild(stanza, 'reporting')
|
|
77
|
+
if (!reporting) return null
|
|
78
|
+
const tokenNode = getBinaryNodeChild(reporting, 'reporting_token')
|
|
79
|
+
const tagNode = getBinaryNodeChild(reporting, 'reporting_tag') ||
|
|
80
|
+
getBinaryNodeChild(getBinaryNodeChild(reporting, 'reporting_validation'), 'reporting_tag')
|
|
81
|
+
if (!tagNode?.content) return null
|
|
82
|
+
const raw = tagNode.content
|
|
83
|
+
const reportingTag = toBuffer(raw instanceof Uint8Array || Buffer.isBuffer(raw) ? raw : typeof raw === 'string' ? raw : null)
|
|
84
|
+
if (!reportingTag?.length) return null
|
|
85
|
+
const stanzaId = tagNode.attrs?.id || stanza.attrs?.id
|
|
86
|
+
if (!stanzaId) return null
|
|
87
|
+
const tsRaw = tagNode.attrs?.ts_s || stanza.attrs?.t
|
|
88
|
+
return {
|
|
89
|
+
stanzaId,
|
|
90
|
+
timestamp: tsRaw ? Number(tsRaw) : 0,
|
|
91
|
+
reportingTag,
|
|
92
|
+
reportingToken: tokenNode?.content ? toBuffer(tokenNode.content) : null,
|
|
93
|
+
tokenVersion: tokenNode?.attrs?.v ? parseInt(tokenNode.attrs.v) : null,
|
|
94
|
+
validationPolicy: reporting.attrs?.validation_policy || null,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ─── Node builders ────────────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
// Builds <message> child nodes for a spam_list IQ.
|
|
101
|
+
// Skips messages missing a reportingTag (warn) — throws only if all are filtered (error).
|
|
102
|
+
export const buildSpamReportMessageNodes = (messages, logger) => {
|
|
103
|
+
const ts = () => Math.floor(Date.now() / 1000)
|
|
104
|
+
const nodes = messages.slice(0, 5).reduce((acc, m) => {
|
|
105
|
+
const reportingTag = normalizeReportingTag(m.reportingTag)
|
|
106
|
+
if (!reportingTag) {
|
|
107
|
+
logger?.warn?.({ stanzaId: m.stanzaId }, 'buildSpamReportMessageNodes: skipping — missing reportingTag')
|
|
108
|
+
return acc
|
|
109
|
+
}
|
|
110
|
+
const t = String(m.timestamp || ts())
|
|
111
|
+
acc.push({
|
|
112
|
+
tag: 'message',
|
|
113
|
+
attrs: { t, id: m.stanzaId, reported_push_name: m.pushName || '', type: m.messageType || 'text', from: m.fromJid },
|
|
114
|
+
content: [
|
|
115
|
+
{ tag: 'raw', attrs: { local_message_type: '0', v: '2' }, content: m.text != null ? String(m.text) : '' },
|
|
116
|
+
{ tag: 'reporting', attrs: {}, content: [{ tag: 'reporting_validation', attrs: {}, content: [{ tag: 'reporting_tag', attrs: { id: m.stanzaId, ts_s: t }, content: reportingTag }] }] },
|
|
117
|
+
],
|
|
118
|
+
})
|
|
119
|
+
return acc
|
|
120
|
+
}, [])
|
|
121
|
+
if (!nodes.length) {
|
|
122
|
+
logger?.error?.({}, 'buildSpamReportMessageNodes: all messages filtered — none had a valid reportingTag')
|
|
123
|
+
throw new Error('reportSpam: no valid messages with reportingTag after filtering')
|
|
124
|
+
}
|
|
125
|
+
return nodes
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Builds the full xmlns="spam" IQ stanza for tag-based reporting — used by reportSpam().
|
|
129
|
+
export const buildSpamReportIq = (jid, messages, spamFlow = SPAM_FLOWS.ACCOUNT_INFO_REPORT, logger) => {
|
|
130
|
+
if (!messages?.length) {
|
|
131
|
+
logger?.error?.({ jid, spamFlow }, 'buildSpamReportIq: called with empty messages array')
|
|
132
|
+
throw new Error('reportSpam: at least one message with reporting_tag required')
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
tag: 'iq',
|
|
136
|
+
attrs: { type: 'set', xmlns: 'spam', to: S_WHATSAPP_NET },
|
|
137
|
+
content: [{ tag: 'spam_list', attrs: { jid, spam_flow: spamFlow }, content: buildSpamReportMessageNodes(messages, logger) }],
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Builds the simpler xmlns="spam" <report> node — used by reportContact() and reportGroup().
|
|
142
|
+
// Does NOT require reporting_tags, only message key ids or bare id strings.
|
|
143
|
+
export const buildSimpleReportNode = (jid, messageKeys) => ({
|
|
144
|
+
tag: 'report',
|
|
145
|
+
attrs: { jid, type: 'spam' },
|
|
146
|
+
content: (messageKeys || []).map(key => ({ tag: 'message', attrs: { id: key.id || key, ...(key.fromMe ? {} : { from: jid }) } })),
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
// ─── Response parser ──────────────────────────────────────────────────────────
|
|
150
|
+
|
|
151
|
+
// Parses the IQ result node from any spam report query.
|
|
152
|
+
// Returns { success: true, reportId? } on success or { success: false, error } on failure.
|
|
153
|
+
export const parseSpamReportResponse = result => {
|
|
154
|
+
if (!result) return { success: false, error: 'empty response' }
|
|
155
|
+
if (result.attrs?.type === 'error') {
|
|
156
|
+
const errNode = getBinaryNodeChild(result, 'error')
|
|
157
|
+
const code = errNode?.attrs?.code
|
|
158
|
+
const text = errNode?.content?.toString?.() || errNode?.attrs?.text
|
|
159
|
+
return { success: false, error: `spam IQ error${code ? ` ${code}` : ''}${text ? `: ${text}` : ''}` }
|
|
160
|
+
}
|
|
161
|
+
const reportNode = getBinaryNodeChild(result, 'report')
|
|
162
|
+
const reportId = reportNode?.attrs?.id || getBinaryNodeChildString(reportNode, 'id')
|
|
163
|
+
if (reportId) return { success: true, reportId: String(reportId) }
|
|
164
|
+
if (result.attrs?.type === 'result') return { success: true }
|
|
165
|
+
return { success: false, error: 'unexpected spam IQ response' }
|
|
166
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isLidUser, isPnUser } from '../WABinary/index.js'
|
|
2
|
+
|
|
3
|
+
export const processContactAction = (action, id, logger) => {
|
|
4
|
+
const results = []
|
|
5
|
+
if (!id) {
|
|
6
|
+
logger?.warn({ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid }, 'contactAction sync: missing id in index')
|
|
7
|
+
return results
|
|
8
|
+
}
|
|
9
|
+
const lidJid = action.lidJid
|
|
10
|
+
const idIsPn = isPnUser(id)
|
|
11
|
+
const phoneNumber = idIsPn ? id : action.pnJid || undefined
|
|
12
|
+
results.push({
|
|
13
|
+
event: 'contacts.upsert',
|
|
14
|
+
data: [{
|
|
15
|
+
id,
|
|
16
|
+
name: action.fullName || action.firstName || action.username || undefined,
|
|
17
|
+
username: action.username || undefined,
|
|
18
|
+
lid: lidJid || undefined,
|
|
19
|
+
phoneNumber
|
|
20
|
+
}]
|
|
21
|
+
})
|
|
22
|
+
if (lidJid && isLidUser(lidJid) && idIsPn) {
|
|
23
|
+
results.push({ event: 'lid-mapping.update', data: { lid: lidJid, pn: id } })
|
|
24
|
+
}
|
|
25
|
+
return results
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const emitSyncActionResults = (ev, results) => {
|
|
29
|
+
for (const result of results) {
|
|
30
|
+
if (result.event === 'contacts.upsert') ev.emit('contacts.upsert', result.data)
|
|
31
|
+
else ev.emit('lid-mapping.update', result.data)
|
|
32
|
+
}
|
|
33
|
+
}
|