@badzz88/baileys 8.5.4 → 8.5.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/README.md +2 -2
- package/WAProto/fix-imports.js +69 -79
- package/WAProto/index.d.ts +15305 -87935
- package/WAProto/index.js +41109 -194076
- package/engine-requirements.js +15 -8
- package/package.json +23 -19
- package/src/Defaults/index.js +186 -0
- package/src/Signal/Group/ciphertext-message.js +15 -0
- package/src/Signal/Group/group-session-builder.js +86 -0
- package/src/Signal/Group/group_cipher.js +82 -0
- package/src/Signal/Group/index.js +140 -0
- package/src/Signal/Group/keyhelper.js +77 -0
- package/src/Signal/Group/sender-chain-key.js +29 -0
- package/src/Signal/Group/sender-key-distribution-message.js +66 -0
- package/src/Signal/Group/sender-key-message.js +69 -0
- package/src/Signal/Group/sender-key-name.js +51 -0
- package/src/Signal/Group/sender-key-record.js +44 -0
- package/src/Signal/Group/sender-key-state.js +87 -0
- package/src/Signal/Group/sender-message-key.js +29 -0
- package/src/Signal/libsignal.js +455 -0
- package/src/Signal/lid-mapping.js +273 -0
- package/src/Socket/Client/index.js +31 -0
- package/src/Socket/Client/types.js +13 -0
- package/src/Socket/Client/websocket.js +61 -0
- package/src/Socket/aigroups.js +221 -0
- package/src/Socket/business.js +411 -0
- package/src/Socket/chats.js +1449 -0
- package/src/Socket/communities.js +463 -0
- package/src/Socket/graphql.js +523 -0
- package/src/Socket/groups.js +516 -0
- package/src/Socket/index.js +31 -0
- package/src/Socket/interactive-handler.js +527 -0
- package/src/Socket/interop.js +339 -0
- package/src/Socket/luxu.js +349 -0
- package/src/Socket/managed-account.js +98 -0
- package/src/Socket/messages-recv.js +2685 -0
- package/src/Socket/messages-send.js +2047 -0
- package/src/Socket/mex.js +57 -0
- package/src/Socket/newsletter.js +455 -0
- package/src/Socket/privacy.js +125 -0
- package/src/Socket/registration.js +236 -0
- package/src/Socket/socket.js +983 -0
- package/src/Socket/text-router.js +65 -0
- package/src/Socket/username.js +130 -0
- package/src/Store/index.js +33 -0
- package/src/Store/keyed-db.js +118 -0
- package/src/Store/make-cache-manager-store.js +84 -0
- package/src/Store/make-in-memory-store.js +551 -0
- package/src/Store/make-ordered-dictionary.js +81 -0
- package/src/Store/object-repository.js +26 -0
- package/src/Types/Auth.js +31 -0
- package/src/Types/Bussines.js +2 -0
- package/src/Types/Call.js +2 -0
- package/src/Types/Chat.js +4 -0
- package/src/Types/Contact.js +2 -0
- package/src/Types/Events.js +2 -0
- package/src/Types/GroupMetadata.js +2 -0
- package/src/Types/Label.js +26 -0
- package/src/Types/LabelAssociation.js +8 -0
- package/src/Types/Message.js +93 -0
- package/src/Types/Mex.js +112 -0
- package/src/Types/Newsletter.js +109 -0
- package/src/Types/Product.js +2 -0
- package/src/Types/Signal.js +2 -0
- package/src/Types/Socket.js +2 -0
- package/src/Types/State.js +62 -0
- package/src/Types/USync.js +2 -0
- package/src/Types/index.js +56 -0
- package/src/Utils/auth-utils.js +254 -0
- package/src/Utils/browser-utils.js +112 -0
- package/src/Utils/business.js +240 -0
- package/src/Utils/chat-utils.js +1230 -0
- package/src/Utils/command-loader.d.ts +27 -0
- package/src/Utils/command-loader.js +89 -0
- package/src/Utils/companion-reg-client-utils.js +40 -0
- package/src/Utils/consumer-application.js +105 -0
- package/src/Utils/crypto.js +118 -0
- package/src/Utils/curve25519-js.js +242 -0
- package/src/Utils/decode-wa-message.js +529 -0
- package/src/Utils/event-buffer.js +580 -0
- package/src/Utils/generics.js +483 -0
- package/src/Utils/group-history.js +44 -0
- package/src/Utils/history.js +232 -0
- package/src/Utils/identity-change-handler.js +52 -0
- package/src/Utils/index.js +58 -0
- package/src/Utils/jid-display-normalization.js +195 -0
- package/src/Utils/link-preview.js +135 -0
- package/src/Utils/logger.js +8 -0
- package/src/Utils/lt-hash.js +25 -0
- package/src/Utils/make-mutex.js +36 -0
- package/src/Utils/message-composer.js +471 -0
- package/src/Utils/message-retry-manager.js +217 -0
- package/src/Utils/messages-media.js +843 -0
- package/src/Utils/messages.js +2817 -0
- package/src/Utils/meta-ai-msmsg.js +222 -0
- package/src/Utils/native-bridge.js +68 -0
- package/src/Utils/noise-handler.js +169 -0
- package/src/Utils/offline-node-processor.js +34 -0
- package/src/Utils/pre-key-manager.js +90 -0
- package/src/Utils/process-message.js +950 -0
- package/src/Utils/reporting-utils.js +261 -0
- package/src/Utils/session-pool.d.ts +14 -0
- package/src/Utils/session-pool.js +70 -0
- package/src/Utils/signal.js +217 -0
- package/src/Utils/stanza-ack.js +30 -0
- package/src/Utils/sticker.d.ts +13 -0
- package/src/Utils/sticker.js +123 -0
- package/src/Utils/sync-action-utils.js +45 -0
- package/src/Utils/tc-token-utils.js +158 -0
- package/src/Utils/use-multi-file-auth-state.js +111 -0
- package/src/Utils/validate-connection.js +209 -0
- package/src/Utils/view-once-cache.d.ts +12 -0
- package/src/Utils/view-once-cache.js +63 -0
- package/src/Utils/voip-rekey.js +22 -0
- package/src/WABinary/constants.js +1304 -0
- package/src/WABinary/decode.js +342 -0
- package/src/WABinary/encode.js +225 -0
- package/src/WABinary/generic-utils.js +238 -0
- package/src/WABinary/index.js +34 -0
- package/src/WABinary/jid-utils.js +351 -0
- package/src/WABinary/types.js +2 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/constants.js +39484 -0
- package/src/WAM/encode.js +149 -0
- package/src/WAM/index.js +32 -0
- package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/src/WAUSync/Protocols/USyncBusinessProtocol.js +44 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/src/WAUSync/Protocols/USyncFeatureProtocol.js +54 -0
- package/src/WAUSync/Protocols/USyncLIDProtocol.js +32 -0
- package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +473 -0
- package/src/WAUSync/Protocols/USyncPictureProtocol.js +34 -0
- package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
- package/src/WAUSync/Protocols/index.js +40 -0
- package/src/WAUSync/USyncQuery.js +126 -0
- package/src/WAUSync/USyncUser.js +50 -0
- package/src/WAUSync/index.js +32 -0
- package/src/antiban.js +4152 -0
- package/{lib → src}/index.js +51 -49
- package/lib/Defaults/index.js +0 -195
- package/lib/Signal/Group/ciphertext-message.js +0 -15
- package/lib/Signal/Group/group-session-builder.js +0 -92
- package/lib/Signal/Group/group_cipher.js +0 -89
- package/lib/Signal/Group/index.js +0 -136
- package/lib/Signal/Group/keyhelper.js +0 -73
- package/lib/Signal/Group/sender-chain-key.js +0 -32
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
- package/lib/Signal/Group/sender-key-message.js +0 -69
- package/lib/Signal/Group/sender-key-name.js +0 -50
- package/lib/Signal/Group/sender-key-record.js +0 -44
- package/lib/Signal/Group/sender-key-state.js +0 -97
- package/lib/Signal/Group/sender-message-key.js +0 -30
- package/lib/Signal/libsignal.js +0 -470
- package/lib/Signal/lid-mapping.js +0 -280
- package/lib/Socket/Client/index.js +0 -30
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.js +0 -62
- package/lib/Socket/aigroups.js +0 -240
- package/lib/Socket/business.js +0 -414
- package/lib/Socket/chats.js +0 -2165
- package/lib/Socket/communities.js +0 -545
- package/lib/Socket/graphql.js +0 -863
- package/lib/Socket/groups.js +0 -685
- package/lib/Socket/index.js +0 -41
- package/lib/Socket/interactive-handler.js +0 -579
- package/lib/Socket/interop.js +0 -430
- package/lib/Socket/managed-account.js +0 -214
- package/lib/Socket/messages-recv.js +0 -3000
- package/lib/Socket/messages-send.js +0 -2155
- package/lib/Socket/mex.js +0 -47
- package/lib/Socket/newsletter.js +0 -814
- package/lib/Socket/privacy.js +0 -261
- package/lib/Socket/registration.js +0 -434
- package/lib/Socket/socket.js +0 -1074
- package/lib/Socket/username.js +0 -160
- package/lib/Store/index.js +0 -36
- package/lib/Store/make-cache-manager-store.js +0 -90
- package/lib/Store/make-in-memory-store.js +0 -488
- package/lib/Store/make-ordered-dictionary.js +0 -81
- package/lib/Store/object-repository.js +0 -29
- package/lib/Types/Auth.js +0 -38
- package/lib/Types/Bussines.js +0 -2
- package/lib/Types/Call.js +0 -2
- package/lib/Types/Chat.js +0 -4
- package/lib/Types/Contact.js +0 -2
- package/lib/Types/Events.js +0 -2
- package/lib/Types/GroupMetadata.js +0 -2
- package/lib/Types/Label.js +0 -27
- package/lib/Types/LabelAssociation.js +0 -9
- package/lib/Types/Message.js +0 -95
- package/lib/Types/Newsletter.js +0 -152
- package/lib/Types/Product.js +0 -2
- package/lib/Types/Signal.js +0 -2
- package/lib/Types/Socket.js +0 -2
- package/lib/Types/State.js +0 -70
- package/lib/Types/USync.js +0 -2
- package/lib/Types/index.js +0 -55
- package/lib/Utils/auth-utils.js +0 -301
- package/lib/Utils/browser-utils.js +0 -114
- package/lib/Utils/business.js +0 -243
- package/lib/Utils/chat-utils.js +0 -1272
- package/lib/Utils/consumer-application.js +0 -107
- package/lib/Utils/crypto.js +0 -125
- package/lib/Utils/decode-wa-message.js +0 -793
- package/lib/Utils/event-buffer.js +0 -583
- package/lib/Utils/generics.js +0 -617
- package/lib/Utils/group-history.js +0 -51
- package/lib/Utils/history.js +0 -244
- package/lib/Utils/identity-change-handler.js +0 -52
- package/lib/Utils/index.js +0 -53
- package/lib/Utils/jid-display-normalization.js +0 -218
- package/lib/Utils/link-preview.js +0 -138
- package/lib/Utils/logger.js +0 -9
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +0 -36
- package/lib/Utils/message-composer.js +0 -479
- package/lib/Utils/message-inspect.js +0 -393
- package/lib/Utils/message-retry-manager.js +0 -205
- package/lib/Utils/messages-media.js +0 -925
- package/lib/Utils/messages.js +0 -2482
- package/lib/Utils/meta-ai-msmsg.js +0 -122
- package/lib/Utils/noise-handler.js +0 -194
- package/lib/Utils/offline-node-processor.js +0 -37
- package/lib/Utils/pre-key-manager.js +0 -97
- package/lib/Utils/process-message.js +0 -1139
- package/lib/Utils/reporting-utils.js +0 -262
- package/lib/Utils/signal.js +0 -189
- package/lib/Utils/stanza-ack.js +0 -67
- package/lib/Utils/sync-action-utils.js +0 -51
- package/lib/Utils/tc-token-utils.js +0 -156
- package/lib/Utils/use-multi-file-auth-state.js +0 -137
- package/lib/Utils/validate-connection.js +0 -259
- package/lib/Utils/voip-rekey.js +0 -22
- package/lib/WABinary/constants.js +0 -1304
- package/lib/WABinary/decode.js +0 -377
- package/lib/WABinary/encode.js +0 -58
- package/lib/WABinary/generic-utils.js +0 -150
- package/lib/WABinary/index.js +0 -33
- package/lib/WABinary/jid-utils.js +0 -374
- package/lib/WABinary/types.js +0 -2
- package/lib/WAM/BinaryInfo.js +0 -13
- package/lib/WAM/constants.js +0 -39486
- package/lib/WAM/encode.js +0 -142
- package/lib/WAM/index.js +0 -31
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -55
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +0 -100
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -60
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -65
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -27
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +0 -72
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -31
- package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -865
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +0 -32
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -44
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
- package/lib/WAUSync/Protocols/index.js +0 -40
- package/lib/WAUSync/USyncBackoff.js +0 -26
- package/lib/WAUSync/USyncQuery.js +0 -204
- package/lib/WAUSync/USyncUser.js +0 -58
- package/lib/WAUSync/index.js +0 -32
- package/lib/antiban.js +0 -4391
- /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const rb = require('whatsapp-rust-bridge')
|
|
4
|
-
const { proto } = require('../../WAProto')
|
|
5
|
-
|
|
6
|
-
const BOT_MESSAGE_INFO = 'Bot Message'
|
|
7
|
-
const KEY_LENGTH = 32
|
|
8
|
-
|
|
9
|
-
const unpadRandomMax16 = value => {
|
|
10
|
-
const bytes = new Uint8Array(value)
|
|
11
|
-
if (!bytes.length) {
|
|
12
|
-
throw new Error('unpadPkcs7 given empty bytes')
|
|
13
|
-
}
|
|
14
|
-
const padLength = bytes[bytes.length - 1]
|
|
15
|
-
if (padLength > bytes.length) {
|
|
16
|
-
throw new Error(`unpad given ${bytes.length} bytes, but pad is ${padLength}`)
|
|
17
|
-
}
|
|
18
|
-
return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.length - padLength)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const toBuffer = value => {
|
|
22
|
-
if (Buffer.isBuffer(value)) return value
|
|
23
|
-
if (value instanceof Uint8Array) return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
|
|
24
|
-
return Buffer.from(value)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const normalizeLidJid = jid => {
|
|
30
|
-
if (!jid || !jid.endsWith('@lid') || !jid.includes(':')) return jid
|
|
31
|
-
return `${jid.split(':')[0]}@lid`
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const selectMsgIdCandidates = messageKey => {
|
|
37
|
-
const seen = new Set()
|
|
38
|
-
const result = []
|
|
39
|
-
for (const id of [messageKey?.botEditTargetId, messageKey?.stanzaId, messageKey?.metaTargetId]) {
|
|
40
|
-
const s = id ? String(id) : ''
|
|
41
|
-
if (s && !seen.has(s)) {
|
|
42
|
-
seen.add(s)
|
|
43
|
-
result.push(s)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return result
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const selectTargetJidCandidates = messageKey => {
|
|
52
|
-
const seen = new Set()
|
|
53
|
-
const result = []
|
|
54
|
-
for (const jid of [normalizeLidJid(messageKey?.meId), normalizeLidJid(messageKey?.meLid)]) {
|
|
55
|
-
const s = jid ? String(jid) : ''
|
|
56
|
-
if (s && !seen.has(s)) {
|
|
57
|
-
seen.add(s)
|
|
58
|
-
result.push(s)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return result
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const decodeDecryptedMsmsgMessage = decrypted => {
|
|
65
|
-
const buf = toBuffer(decrypted)
|
|
66
|
-
try {
|
|
67
|
-
const unpadded = Buffer.from(unpadRandomMax16(buf))
|
|
68
|
-
const decoded = proto.Message.decode(unpadded)
|
|
69
|
-
const hasContent = Object.keys(decoded).some(k => k !== 'messageContextInfo' && decoded[k] != null)
|
|
70
|
-
if (hasContent) return decoded
|
|
71
|
-
} catch {}
|
|
72
|
-
return proto.Message.decode(buf)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const decryptMsmsgBotMessage = async (messageSecret, messageKey, msMsg) => {
|
|
77
|
-
if (!messageSecret || (messageSecret instanceof Uint8Array && !messageSecret.byteLength)) {
|
|
78
|
-
throw new Error('Missing required messageSecret for msmsg decryption')
|
|
79
|
-
}
|
|
80
|
-
if (!messageKey?.participant) throw new Error('Missing required participant for msmsg decryption')
|
|
81
|
-
if (!messageKey?.meId) throw new Error('Missing required meId for msmsg decryption')
|
|
82
|
-
if (!msMsg?.encIv) throw new Error('Missing required encIv for msmsg decryption')
|
|
83
|
-
if (!msMsg?.encPayload) throw new Error('Missing required encPayload for msmsg decryption')
|
|
84
|
-
|
|
85
|
-
const msgIdCandidates = selectMsgIdCandidates(messageKey)
|
|
86
|
-
if (!msgIdCandidates.length) throw new Error('Missing required target message id for msmsg decryption')
|
|
87
|
-
|
|
88
|
-
const targetJidCandidates = selectTargetJidCandidates(messageKey)
|
|
89
|
-
if (!targetJidCandidates.length) throw new Error('Missing required target JID for msmsg decryption')
|
|
90
|
-
|
|
91
|
-
const botJidBuf = Buffer.from(String(messageKey.participant))
|
|
92
|
-
const payload = toBuffer(msMsg.encPayload)
|
|
93
|
-
const iv = toBuffer(msMsg.encIv)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const baseKey = Buffer.from(rb.hkdf(toBuffer(messageSecret), KEY_LENGTH, { info: BOT_MESSAGE_INFO }))
|
|
97
|
-
|
|
98
|
-
let lastError
|
|
99
|
-
for (const msgId of msgIdCandidates) {
|
|
100
|
-
|
|
101
|
-
const idBuf = Buffer.from(msgId)
|
|
102
|
-
for (const targetJid of targetJidCandidates) {
|
|
103
|
-
const info = Buffer.concat([idBuf, Buffer.from(targetJid), botJidBuf])
|
|
104
|
-
const key = Buffer.from(rb.hkdf(baseKey, KEY_LENGTH, { info }))
|
|
105
|
-
const aad = Buffer.concat([idBuf, Buffer.from([0x00]), botJidBuf])
|
|
106
|
-
try {
|
|
107
|
-
return Buffer.from(rb.aesDecryptGCM(payload, key, iv, aad))
|
|
108
|
-
} catch (e) {
|
|
109
|
-
lastError = e
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const err = new Error('msmsg decryption failed: all key derivation candidates exhausted')
|
|
115
|
-
err.cause = lastError
|
|
116
|
-
throw err
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
module.exports = {
|
|
120
|
-
decodeDecryptedMsmsgMessage,
|
|
121
|
-
decryptMsmsgBotMessage
|
|
122
|
-
}
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
-
exports.makeNoiseHandler = void 0
|
|
4
|
-
const boom_1 = require('@hapi/boom')
|
|
5
|
-
const index_js_1 = require('../../WAProto/index.js')
|
|
6
|
-
const Defaults_1 = require('../Defaults')
|
|
7
|
-
const rb = require('whatsapp-rust-bridge')
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const rustNodeToJs = node => {
|
|
11
|
-
if (!node || typeof node !== 'object') return node
|
|
12
|
-
const result = { tag: node.tag, attrs: node.attrs || {} }
|
|
13
|
-
const content = node.content
|
|
14
|
-
if (content === undefined || content === null) {
|
|
15
|
-
|
|
16
|
-
} else if (content instanceof Uint8Array) {
|
|
17
|
-
result.content = Buffer.from(content)
|
|
18
|
-
} else if (typeof content === 'string') {
|
|
19
|
-
result.content = content
|
|
20
|
-
} else if (Array.isArray(content)) {
|
|
21
|
-
result.content = content.map(rustNodeToJs)
|
|
22
|
-
} else {
|
|
23
|
-
result.content = content
|
|
24
|
-
}
|
|
25
|
-
return result
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const makeNoiseHandler = ({
|
|
29
|
-
keyPair: { private: privateKey, public: publicKey },
|
|
30
|
-
NOISE_HEADER,
|
|
31
|
-
logger,
|
|
32
|
-
routingInfo
|
|
33
|
-
}) => {
|
|
34
|
-
logger = logger.child({ class: 'ns' })
|
|
35
|
-
|
|
36
|
-
const session = new rb.NoiseSession(
|
|
37
|
-
publicKey instanceof Uint8Array ? publicKey : new Uint8Array(publicKey),
|
|
38
|
-
NOISE_HEADER instanceof Uint8Array ? NOISE_HEADER : new Uint8Array(NOISE_HEADER),
|
|
39
|
-
routingInfo ? (routingInfo instanceof Uint8Array ? routingInfo : new Uint8Array(routingInfo)) : undefined
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
let isWaitingForTransport = false
|
|
43
|
-
let pendingOnFrame = null
|
|
44
|
-
let pendingBytes = null
|
|
45
|
-
|
|
46
|
-
const processFrames = async onFrame => {
|
|
47
|
-
|
|
48
|
-
const frames = session.decodeFrame(new Uint8Array(0))
|
|
49
|
-
for (let i = 0; i < frames.length; i++) {
|
|
50
|
-
const item = frames[i]
|
|
51
|
-
let frame
|
|
52
|
-
if (item instanceof Uint8Array) {
|
|
53
|
-
frame = Buffer.from(item)
|
|
54
|
-
} else {
|
|
55
|
-
try {
|
|
56
|
-
frame = rustNodeToJs(item.toJSON())
|
|
57
|
-
} catch (decodeErr) {
|
|
58
|
-
logger.debug({ err: decodeErr?.message }, '[noise] decode error')
|
|
59
|
-
continue
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (logger.level === 'trace') {
|
|
63
|
-
logger.trace({ msg: frame?.attrs?.id }, 'recv frame')
|
|
64
|
-
}
|
|
65
|
-
onFrame(frame)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const finishInit = async () => {
|
|
70
|
-
isWaitingForTransport = true
|
|
71
|
-
session.finishInit()
|
|
72
|
-
isWaitingForTransport = false
|
|
73
|
-
logger.trace('Noise handler transitioned to Transport state (Rust)')
|
|
74
|
-
if (pendingOnFrame && pendingBytes) {
|
|
75
|
-
logger.trace({ length: pendingBytes.length }, 'Flushing buffered frames after transport ready')
|
|
76
|
-
const frames = session.decodeFrame(pendingBytes)
|
|
77
|
-
pendingBytes = null
|
|
78
|
-
const cb = pendingOnFrame
|
|
79
|
-
pendingOnFrame = null
|
|
80
|
-
for (let i = 0; i < frames.length; i++) {
|
|
81
|
-
const item = frames[i]
|
|
82
|
-
let frame
|
|
83
|
-
if (item instanceof Uint8Array) {
|
|
84
|
-
frame = Buffer.from(item)
|
|
85
|
-
} else {
|
|
86
|
-
try {
|
|
87
|
-
frame = rustNodeToJs(item.toJSON())
|
|
88
|
-
} catch {
|
|
89
|
-
continue
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
cb(frame)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
encrypt: plaintext => {
|
|
99
|
-
const u8 = plaintext instanceof Uint8Array ? plaintext : new Uint8Array(plaintext)
|
|
100
|
-
return Buffer.from(session.encrypt(u8))
|
|
101
|
-
},
|
|
102
|
-
decrypt: ciphertext => {
|
|
103
|
-
const u8 = ciphertext instanceof Uint8Array ? ciphertext : new Uint8Array(ciphertext)
|
|
104
|
-
return Buffer.from(session.decrypt(u8))
|
|
105
|
-
},
|
|
106
|
-
authenticate: data => {
|
|
107
|
-
session.authenticate(data instanceof Uint8Array ? data : new Uint8Array(data))
|
|
108
|
-
},
|
|
109
|
-
mixIntoKey: data => {
|
|
110
|
-
session.mixIntoKey(data instanceof Uint8Array ? data : new Uint8Array(data))
|
|
111
|
-
},
|
|
112
|
-
finishInit,
|
|
113
|
-
processHandshake: ({ serverHello }, noiseKey) => {
|
|
114
|
-
|
|
115
|
-
const certPayload = session.processHandshakeInit(
|
|
116
|
-
serverHello.ephemeral,
|
|
117
|
-
serverHello.static,
|
|
118
|
-
serverHello.payload,
|
|
119
|
-
privateKey instanceof Uint8Array ? privateKey : new Uint8Array(privateKey)
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const { intermediate: certIntermediate, leaf } = index_js_1.proto.CertChain.decode(certPayload)
|
|
124
|
-
if (!leaf?.details || !leaf?.signature) {
|
|
125
|
-
throw new boom_1.Boom('invalid noise leaf certificate', { statusCode: 400 })
|
|
126
|
-
}
|
|
127
|
-
if (!certIntermediate?.details || !certIntermediate?.signature) {
|
|
128
|
-
throw new boom_1.Boom('invalid noise intermediate certificate', { statusCode: 400 })
|
|
129
|
-
}
|
|
130
|
-
const details = index_js_1.proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details)
|
|
131
|
-
const { issuerSerial } = details
|
|
132
|
-
const { Curve } = require('./crypto')
|
|
133
|
-
if (!Curve.verify(details.key, leaf.details, leaf.signature)) {
|
|
134
|
-
throw new boom_1.Boom('noise certificate signature invalid', { statusCode: 400 })
|
|
135
|
-
}
|
|
136
|
-
if (!Curve.verify(Defaults_1.WA_CERT_DETAILS.PUBLIC_KEY, certIntermediate.details, certIntermediate.signature)) {
|
|
137
|
-
throw new boom_1.Boom('noise intermediate certificate signature invalid', { statusCode: 400 })
|
|
138
|
-
}
|
|
139
|
-
if (issuerSerial !== Defaults_1.WA_CERT_DETAILS.SERIAL) {
|
|
140
|
-
throw new boom_1.Boom('certification match failed', { statusCode: 400 })
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
let serverStaticPub
|
|
145
|
-
try {
|
|
146
|
-
const leafDetails = index_js_1.proto.CertChain.NoiseCertificate.Details.decode(leaf.details)
|
|
147
|
-
if (leafDetails.key?.length === 32) serverStaticPub = Buffer.from(leafDetails.key)
|
|
148
|
-
} catch {}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const keyEnc = session.processHandshakeFinish(
|
|
152
|
-
noiseKey.public instanceof Uint8Array ? noiseKey.public : new Uint8Array(noiseKey.public),
|
|
153
|
-
noiseKey.private instanceof Uint8Array ? noiseKey.private : new Uint8Array(noiseKey.private),
|
|
154
|
-
serverHello.ephemeral
|
|
155
|
-
)
|
|
156
|
-
return { keyEnc: Buffer.from(keyEnc), serverStaticPub }
|
|
157
|
-
},
|
|
158
|
-
encodeFrame: data => {
|
|
159
|
-
const u8 = data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
|
160
|
-
return Buffer.from(session.encodeFrameRaw(u8))
|
|
161
|
-
},
|
|
162
|
-
decodeFrame: async (newData, onFrame) => {
|
|
163
|
-
if (isWaitingForTransport) {
|
|
164
|
-
pendingBytes = pendingBytes ? Buffer.concat([pendingBytes, Buffer.from(newData)]) : Buffer.from(newData)
|
|
165
|
-
pendingOnFrame = onFrame
|
|
166
|
-
return
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const u8 =
|
|
170
|
-
newData instanceof Uint8Array ? newData : new Uint8Array(newData.buffer, newData.byteOffset, newData.byteLength)
|
|
171
|
-
const frames = session.decodeFrame(u8)
|
|
172
|
-
|
|
173
|
-
for (let i = 0; i < frames.length; i++) {
|
|
174
|
-
const item = frames[i]
|
|
175
|
-
let frame
|
|
176
|
-
if (item instanceof Uint8Array) {
|
|
177
|
-
frame = Buffer.from(item)
|
|
178
|
-
} else {
|
|
179
|
-
try {
|
|
180
|
-
frame = rustNodeToJs(item.toJSON())
|
|
181
|
-
} catch (decodeErr) {
|
|
182
|
-
logger.debug({ err: decodeErr?.message }, '[noise] decode error')
|
|
183
|
-
continue
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
if (logger.level === 'trace') {
|
|
187
|
-
logger.trace({ msg: frame?.attrs?.id }, 'recv frame')
|
|
188
|
-
}
|
|
189
|
-
onFrame(frame)
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
exports.makeNoiseHandler = makeNoiseHandler
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
-
exports.makeOfflineNodeProcessor = makeOfflineNodeProcessor
|
|
4
|
-
|
|
5
|
-
function makeOfflineNodeProcessor(nodeProcessorMap, deps, batchSize = 10) {
|
|
6
|
-
const nodes = []
|
|
7
|
-
let isProcessing = false
|
|
8
|
-
const enqueue = (type, node) => {
|
|
9
|
-
nodes.push({ type, node })
|
|
10
|
-
if (isProcessing) {
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
isProcessing = true
|
|
14
|
-
const promise = async () => {
|
|
15
|
-
let processedInBatch = 0
|
|
16
|
-
while (nodes.length && deps.isWsOpen()) {
|
|
17
|
-
const { type, node } = nodes.shift()
|
|
18
|
-
const nodeProcessor = nodeProcessorMap.get(type)
|
|
19
|
-
if (!nodeProcessor) {
|
|
20
|
-
deps.onUnexpectedError(new Error(`unknown offline node type: ${type}`), 'processing offline node')
|
|
21
|
-
continue
|
|
22
|
-
}
|
|
23
|
-
await nodeProcessor(node).catch(err => deps.onUnexpectedError(err, `processing offline ${type}`))
|
|
24
|
-
processedInBatch++
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (processedInBatch >= batchSize) {
|
|
28
|
-
processedInBatch = 0
|
|
29
|
-
await deps.yieldToEventLoop()
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
isProcessing = false
|
|
33
|
-
}
|
|
34
|
-
promise().catch(error => deps.onUnexpectedError(error, 'processing offline nodes'))
|
|
35
|
-
}
|
|
36
|
-
return { enqueue }
|
|
37
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
var __importDefault =
|
|
3
|
-
(this && this.__importDefault) ||
|
|
4
|
-
function (mod) {
|
|
5
|
-
return mod && mod.__esModule ? mod : { default: mod }
|
|
6
|
-
}
|
|
7
|
-
Object.defineProperty(exports, '__esModule', { value: true })
|
|
8
|
-
exports.PreKeyManager = void 0
|
|
9
|
-
const p_queue_1 = __importDefault(require('p-queue'))
|
|
10
|
-
|
|
11
|
-
class PreKeyManager {
|
|
12
|
-
constructor(store, logger) {
|
|
13
|
-
this.store = store
|
|
14
|
-
this.logger = logger
|
|
15
|
-
this.queues = new Map()
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
getQueue(keyType) {
|
|
19
|
-
if (!this.queues.has(keyType)) {
|
|
20
|
-
this.queues.set(keyType, new p_queue_1.default({ concurrency: 1 }))
|
|
21
|
-
}
|
|
22
|
-
return this.queues.get(keyType)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async processOperations(data, keyType, transactionCache, mutations, isInTransaction) {
|
|
26
|
-
const keyData = data[keyType]
|
|
27
|
-
if (!keyData) return
|
|
28
|
-
return this.getQueue(keyType).add(async () => {
|
|
29
|
-
|
|
30
|
-
transactionCache[keyType] = transactionCache[keyType] || {}
|
|
31
|
-
mutations[keyType] = mutations[keyType] || {}
|
|
32
|
-
|
|
33
|
-
const deletions = []
|
|
34
|
-
const updates = {}
|
|
35
|
-
for (const keyId in keyData) {
|
|
36
|
-
if (keyData[keyId] === null) {
|
|
37
|
-
deletions.push(keyId)
|
|
38
|
-
} else {
|
|
39
|
-
updates[keyId] = keyData[keyId]
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (Object.keys(updates).length > 0) {
|
|
44
|
-
Object.assign(transactionCache[keyType], updates)
|
|
45
|
-
Object.assign(mutations[keyType], updates)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (deletions.length > 0) {
|
|
49
|
-
await this.processDeletions(keyType, deletions, transactionCache, mutations, isInTransaction)
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async processDeletions(keyType, ids, transactionCache, mutations, isInTransaction) {
|
|
55
|
-
if (isInTransaction) {
|
|
56
|
-
|
|
57
|
-
for (const keyId of ids) {
|
|
58
|
-
if (transactionCache[keyType]?.[keyId]) {
|
|
59
|
-
transactionCache[keyType][keyId] = null
|
|
60
|
-
mutations[keyType][keyId] = null
|
|
61
|
-
} else {
|
|
62
|
-
this.logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
} else {
|
|
66
|
-
|
|
67
|
-
const existingKeys = await this.store.get(keyType, ids)
|
|
68
|
-
for (const keyId of ids) {
|
|
69
|
-
if (existingKeys[keyId]) {
|
|
70
|
-
transactionCache[keyType][keyId] = null
|
|
71
|
-
mutations[keyType][keyId] = null
|
|
72
|
-
} else {
|
|
73
|
-
this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async validateDeletions(data, keyType) {
|
|
80
|
-
const keyData = data[keyType]
|
|
81
|
-
if (!keyData) return
|
|
82
|
-
return this.getQueue(keyType).add(async () => {
|
|
83
|
-
|
|
84
|
-
const deletionIds = Object.keys(keyData).filter(id => keyData[id] === null)
|
|
85
|
-
if (deletionIds.length === 0) return
|
|
86
|
-
|
|
87
|
-
const existingKeys = await this.store.get(keyType, deletionIds)
|
|
88
|
-
for (const keyId of deletionIds) {
|
|
89
|
-
if (!existingKeys[keyId]) {
|
|
90
|
-
this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`)
|
|
91
|
-
delete data[keyType][keyId]
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
})
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
exports.PreKeyManager = PreKeyManager
|