mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add signal repository + tests
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { proto } from '../../WAProto'
|
||||
import { AuthenticationState, WAMessageKey } from '../Types'
|
||||
import { SignalRepository, WAMessageKey } from '../Types'
|
||||
import { areJidsSameUser, BinaryNode, isJidBroadcast, isJidGroup, isJidStatusBroadcast, isJidUser } from '../WABinary'
|
||||
import { unpadRandomMax16 } from './generics'
|
||||
import { decryptGroupSignalProto, decryptSignalProto, processSenderKeyMessage } from './signal'
|
||||
|
||||
const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node'
|
||||
|
||||
@@ -13,7 +12,10 @@ type MessageType = 'chat' | 'peer_broadcast' | 'other_broadcast' | 'group' | 'di
|
||||
* Decode the received node as a message.
|
||||
* @note this will only parse the message, not decrypt it
|
||||
*/
|
||||
export function decodeMessageNode(stanza: BinaryNode, meId: string) {
|
||||
export function decodeMessageNode(
|
||||
stanza: BinaryNode,
|
||||
meId: string
|
||||
) {
|
||||
let msgType: MessageType
|
||||
let chatId: string
|
||||
let author: string
|
||||
@@ -92,8 +94,12 @@ export function decodeMessageNode(stanza: BinaryNode, meId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export const decryptMessageNode = (stanza: BinaryNode, auth: AuthenticationState) => {
|
||||
const { fullMessage, author, sender } = decodeMessageNode(stanza, auth.creds.me!.id)
|
||||
export const decryptMessageNode = (
|
||||
stanza: BinaryNode,
|
||||
meId: string,
|
||||
repository: SignalRepository
|
||||
) => {
|
||||
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId)
|
||||
return {
|
||||
fullMessage,
|
||||
category: stanza.attrs.category,
|
||||
@@ -118,18 +124,26 @@ export const decryptMessageNode = (stanza: BinaryNode, auth: AuthenticationState
|
||||
|
||||
decryptables += 1
|
||||
|
||||
let msgBuffer: Buffer
|
||||
let msgBuffer: Uint8Array
|
||||
|
||||
try {
|
||||
const e2eType = attrs.type
|
||||
switch (e2eType) {
|
||||
case 'skmsg':
|
||||
msgBuffer = await decryptGroupSignalProto(sender, author, content, auth)
|
||||
msgBuffer = await repository.decryptGroupMessage({
|
||||
group: sender,
|
||||
authorJid: author,
|
||||
msg: content
|
||||
})
|
||||
break
|
||||
case 'pkmsg':
|
||||
case 'msg':
|
||||
const user = isJidUser(sender) ? sender : author
|
||||
msgBuffer = await decryptSignalProto(user, e2eType, content as Buffer, auth)
|
||||
msgBuffer = await repository.decryptMessage({
|
||||
jid: user,
|
||||
type: e2eType,
|
||||
ciphertext: content
|
||||
})
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown e2e type: ${e2eType}`)
|
||||
@@ -138,7 +152,10 @@ export const decryptMessageNode = (stanza: BinaryNode, auth: AuthenticationState
|
||||
let msg: proto.IMessage = proto.Message.decode(unpadRandomMax16(msgBuffer))
|
||||
msg = msg.deviceSentMessage?.message || msg
|
||||
if(msg.senderKeyDistributionMessage) {
|
||||
await processSenderKeyMessage(author, msg.senderKeyDistributionMessage, auth)
|
||||
await repository.processSenderKeyDistributionMessage({
|
||||
authorJid: author,
|
||||
item: msg.senderKeyDistributionMessage
|
||||
})
|
||||
}
|
||||
|
||||
if(fullMessage.message) {
|
||||
|
||||
Reference in New Issue
Block a user