diff --git a/src/Signal/Group/sender-chain-key.ts b/src/Signal/Group/sender-chain-key.ts index 74e390d1..768a131c 100644 --- a/src/Signal/Group/sender-chain-key.ts +++ b/src/Signal/Group/sender-chain-key.ts @@ -10,12 +10,14 @@ export class SenderChainKey { constructor(iteration: number, chainKey: any) { this.iteration = iteration - if (chainKey instanceof Buffer) { + if (Buffer.isBuffer(chainKey)) { this.chainKey = chainKey } else if (chainKey instanceof Uint8Array) { this.chainKey = Buffer.from(chainKey) + } else if (chainKey && typeof chainKey === 'object') { + this.chainKey = Buffer.from(Object.values(chainKey)) } else { - this.chainKey = Buffer.from(chainKey || []) + this.chainKey = Buffer.alloc(0) } } diff --git a/src/Signal/Group/sender-key-state.ts b/src/Signal/Group/sender-key-state.ts index 47e46e3b..c176a698 100644 --- a/src/Signal/Group/sender-key-state.ts +++ b/src/Signal/Group/sender-key-state.ts @@ -30,17 +30,25 @@ export class SenderKeyState { constructor( id?: number | null, iteration?: number | null, - chainKey?: Uint8Array | null, - signatureKeyPair?: { public: Uint8Array; private: Uint8Array } | null, - signatureKeyPublic?: Uint8Array | null, - signatureKeyPrivate?: Uint8Array | null, + chainKey?: Uint8Array | null | string, + signatureKeyPair?: { public: Uint8Array | string; private: Uint8Array | string } | null, + signatureKeyPublic?: Uint8Array | string | null, + signatureKeyPrivate?: Uint8Array | string | null, senderKeyStateStructure?: SenderKeyStateStructure | null ) { + const toBuffer = (val: any) => { + if (!val) return Buffer.alloc(0) + if (typeof val === 'string') return Buffer.from(val, 'base64') + if (val instanceof Uint8Array || Array.isArray(val)) return Buffer.from(val) + return Buffer.alloc(0) + } + if (senderKeyStateStructure) { - if (!Array.isArray(senderKeyStateStructure.senderMessageKeys)) { - this.senderKeyStateStructure = { ...senderKeyStateStructure, senderMessageKeys: [] } - } else { - this.senderKeyStateStructure = senderKeyStateStructure + this.senderKeyStateStructure = { + ...senderKeyStateStructure, + senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys) + ? senderKeyStateStructure.senderMessageKeys + : [] } } else { if (signatureKeyPair) { @@ -52,19 +60,12 @@ export class SenderKeyState { const senderChainKeyStructure: SenderChainKeyStructure = { iteration: iteration || 0, - seed: chainKey || Buffer.alloc(0) + seed: chainKey ? toBuffer(chainKey) : Buffer.alloc(0) } const signingKeyStructure: SenderSigningKeyStructure = { - public: - typeof signatureKeyPublic === 'string' - ? Buffer.from(signatureKeyPublic, 'base64') - : signatureKeyPublic || Buffer.alloc(0) - } - - if (signatureKeyPrivate) { - signingKeyStructure.private = - typeof signatureKeyPrivate === 'string' ? Buffer.from(signatureKeyPrivate, 'base64') : signatureKeyPrivate + public: toBuffer(signatureKeyPublic), + private: signatureKeyPrivate ? toBuffer(signatureKeyPrivate) : undefined } this.senderKeyStateStructure = { @@ -95,16 +96,29 @@ export class SenderKeyState { } public getSigningKeyPublic(): Buffer { - const publicKey = this.senderKeyStateStructure.senderSigningKey.public - if (publicKey instanceof Buffer) { - return publicKey - } else if (publicKey instanceof Uint8Array) { - return Buffer.from(publicKey) - } else if (typeof publicKey === 'string') { - return Buffer.from(publicKey, 'base64') + let key = this.senderKeyStateStructure.senderSigningKey.public + + // normalize into Buffer + if (!(key instanceof Buffer)) { + if (key instanceof Uint8Array) { + key = Buffer.from(key) + } else if (typeof key === 'string') { + key = Buffer.from(key, 'base64') + } else { + key = Buffer.from(key || []) + } } - return Buffer.from(publicKey || []) + const publicKey = key as Buffer + + if (publicKey.length === 32) { + const fixed = Buffer.alloc(33) + fixed[0] = 0x05 + publicKey.copy(fixed, 1) + return fixed + } + + return publicKey } public getSigningKeyPrivate(): Buffer | undefined { diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 3307d1f7..a7cd37c5 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -1159,11 +1159,11 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const altServer = jidDecode(alt)?.server const lidMapping = signalRepository.getLIDMappingStore() if (altServer === 'lid') { - if (typeof await lidMapping.getPNForLID(alt) == "string") { + if (typeof (await lidMapping.getPNForLID(alt)) === 'string') { await lidMapping.storeLIDPNMapping(alt, msg.key.participant || msg.key.remoteJid!) } } else { - if (typeof await lidMapping.getLIDForPN(alt) == "string") { + if (typeof (await lidMapping.getLIDForPN(alt)) === 'string') { await lidMapping.storeLIDPNMapping(msg.key.participant || msg.key.remoteJid!, alt) } }