mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: Change Sync Functions to Promises to Prevent Blocking Event Loop (#896)
* initial commit * lint * lint 2
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import { createCipheriv, createDecipheriv, createHash, createHmac, pbkdf2Sync, randomBytes } from 'crypto'
|
||||
import { createCipheriv, createDecipheriv, createHash, createHmac, pbkdf2, randomBytes } from 'crypto'
|
||||
import HKDF from 'futoin-hkdf'
|
||||
import * as libsignal from 'libsignal'
|
||||
import { promisify } from 'util'
|
||||
import { KEY_BUNDLE_TYPE } from '../Defaults'
|
||||
import { KeyPair } from '../Types'
|
||||
|
||||
const pbkdf2Promise = promisify(pbkdf2)
|
||||
|
||||
/** prefix version byte to the pub keys, required for some curve crypto functions */
|
||||
export const generateSignalPubKey = (pubKey: Uint8Array | Buffer) => (
|
||||
pubKey.length === 33
|
||||
@@ -126,6 +129,6 @@ export function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info:
|
||||
return HKDF(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info)
|
||||
}
|
||||
|
||||
export function derivePairingCodeKey(pairingCode: string, salt: Buffer) {
|
||||
return pbkdf2Sync(pairingCode, salt, 2 << 16, 32, 'sha256')
|
||||
}
|
||||
export async function derivePairingCodeKey(pairingCode: string, salt: Buffer) {
|
||||
return await pbkdf2Promise(pairingCode, salt, 2 << 16, 32, 'sha256')
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ export const makeNoiseHandler = ({
|
||||
|
||||
return frame
|
||||
},
|
||||
decodeFrame: (newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
|
||||
decodeFrame: async(newData: Buffer | Uint8Array, onFrame: (buff: Uint8Array | BinaryNode) => void) => {
|
||||
// the binary protocol uses its own framing mechanism
|
||||
// on top of the WS frames
|
||||
// so we get this data and separate out the frames
|
||||
@@ -184,7 +184,7 @@ export const makeNoiseHandler = ({
|
||||
|
||||
if(isFinished) {
|
||||
const result = decrypt(frame as Uint8Array)
|
||||
frame = decodeBinaryNode(result)
|
||||
frame = await decodeBinaryNode(result)
|
||||
}
|
||||
|
||||
logger.trace({ msg: (frame as any)?.attrs?.id }, 'recv frame')
|
||||
|
||||
Reference in New Issue
Block a user