refactor: Change Sync Functions to Promises to Prevent Blocking Event Loop (#896)

* initial commit

* lint

* lint 2
This commit is contained in:
vini
2024-07-03 04:29:16 -03:00
committed by GitHub
parent fdf9d48162
commit dbf25771a1
5 changed files with 21 additions and 15 deletions

View File

@@ -440,7 +440,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const ref = toRequiredBuffer(getBinaryNodeChildBuffer(linkCodeCompanionReg, 'link_code_pairing_ref'))
const primaryIdentityPublicKey = toRequiredBuffer(getBinaryNodeChildBuffer(linkCodeCompanionReg, 'primary_identity_pub'))
const primaryEphemeralPublicKeyWrapped = toRequiredBuffer(getBinaryNodeChildBuffer(linkCodeCompanionReg, 'link_code_pairing_wrapped_primary_ephemeral_pub'))
const codePairingPublicKey = decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped)
const codePairingPublicKey = await decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped)
const companionSharedKey = Curve.sharedKey(authState.creds.pairingEphemeralKeyPair.private, codePairingPublicKey)
const random = randomBytes(32)
const linkCodeSalt = randomBytes(32)
@@ -499,10 +499,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
}
function decipherLinkPublicKey(data: Uint8Array | Buffer) {
async function decipherLinkPublicKey(data: Uint8Array | Buffer) {
const buffer = toRequiredBuffer(data)
const salt = buffer.slice(0, 32)
const secretKey = derivePairingCodeKey(authState.creds.pairingCode!, salt)
const secretKey = await derivePairingCodeKey(authState.creds.pairingCode!, salt)
const iv = buffer.slice(32, 48)
const payload = buffer.slice(48, 80)
return aesDecryptCTR(payload, secretKey, iv)

View File

@@ -548,7 +548,7 @@ export const makeSocket = (config: SocketConfig) => {
async function generatePairingKey() {
const salt = randomBytes(32)
const randomIv = randomBytes(16)
const key = derivePairingCodeKey(authState.creds.pairingCode!, salt)
const key = await derivePairingCodeKey(authState.creds.pairingCode!, salt)
const ciphered = aesEncryptCTR(authState.creds.pairingEphemeralKeyPair.public, key, randomIv)
return Buffer.concat([salt, randomIv, ciphered])
}