@@ -55,7 +55,7 @@ export const hkdfInfoKey = (type: MediaType) => {
5555}
5656
5757/** generates all the keys required to encrypt/decrypt & sign a media message */
58- export function getMediaKeys ( buffer : Uint8Array | string | null | undefined , mediaType : MediaType ) : MediaDecryptionKeyInfo {
58+ export async function getMediaKeys ( buffer : Uint8Array | string | null | undefined , mediaType : MediaType ) : Promise < MediaDecryptionKeyInfo > {
5959 if ( ! buffer ) {
6060 throw new Boom ( 'Cannot derive from empty media key' )
6161 }
@@ -65,7 +65,7 @@ export function getMediaKeys(buffer: Uint8Array | string | null | undefined, med
6565 }
6666
6767 // expand using HKDF to 112 bytes, also pass in the relevant app info
68- const expandedMediaKey = hkdf ( buffer , 112 , { info : hkdfInfoKey ( mediaType ) } )
68+ const expandedMediaKey = await hkdf ( buffer , 112 , { info : hkdfInfoKey ( mediaType ) } )
6969 return {
7070 iv : expandedMediaKey . slice ( 0 , 16 ) ,
7171 cipherKey : expandedMediaKey . slice ( 16 , 48 ) ,
@@ -344,7 +344,7 @@ export const encryptedStream = async(
344344 logger ?. debug ( 'fetched media stream' )
345345
346346 const mediaKey = Crypto . randomBytes ( 32 )
347- const { cipherKey, iv, macKey } = getMediaKeys ( mediaKey , mediaType )
347+ const { cipherKey, iv, macKey } = await getMediaKeys ( mediaKey , mediaType )
348348 const encWriteStream = new Readable ( { read : ( ) => { } } )
349349
350350 let bodyPath : string | undefined
@@ -458,13 +458,13 @@ export type MediaDownloadOptions = {
458458
459459export const getUrlFromDirectPath = ( directPath : string ) => `https://${ DEF_HOST } ${ directPath } `
460460
461- export const downloadContentFromMessage = (
461+ export const downloadContentFromMessage = async (
462462 { mediaKey, directPath, url } : DownloadableMessage ,
463463 type : MediaType ,
464464 opts : MediaDownloadOptions = { }
465465) => {
466466 const downloadUrl = url || getUrlFromDirectPath ( directPath ! )
467- const keys = getMediaKeys ( mediaKey , type )
467+ const keys = await getMediaKeys ( mediaKey , type )
468468
469469 return downloadEncryptedContent ( downloadUrl , keys , opts )
470470}
@@ -673,7 +673,7 @@ const getMediaRetryKey = (mediaKey: Buffer | Uint8Array) => {
673673/**
674674 * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
675675 */
676- export const encryptMediaRetryRequest = (
676+ export const encryptMediaRetryRequest = async (
677677 key : proto . IMessageKey ,
678678 mediaKey : Buffer | Uint8Array ,
679679 meId : string
@@ -682,7 +682,7 @@ export const encryptMediaRetryRequest = (
682682 const recpBuffer = proto . ServerErrorReceipt . encode ( recp ) . finish ( )
683683
684684 const iv = Crypto . randomBytes ( 12 )
685- const retryKey = getMediaRetryKey ( mediaKey )
685+ const retryKey = await getMediaRetryKey ( mediaKey )
686686 const ciphertext = aesEncryptGCM ( recpBuffer , retryKey , iv , Buffer . from ( key . id ! ) )
687687
688688 const req : BinaryNode = {
@@ -752,12 +752,12 @@ export const decodeMediaRetryNode = (node: BinaryNode) => {
752752 return event
753753}
754754
755- export const decryptMediaRetryData = (
755+ export const decryptMediaRetryData = async (
756756 { ciphertext, iv } : { ciphertext : Uint8Array , iv : Uint8Array } ,
757757 mediaKey : Uint8Array ,
758758 msgId : string
759759) => {
760- const retryKey = getMediaRetryKey ( mediaKey )
760+ const retryKey = await getMediaRetryKey ( mediaKey )
761761 const plaintext = aesDecryptGCM ( ciphertext , retryKey , iv , Buffer . from ( msgId ) )
762762 return proto . MediaRetryNotification . decode ( plaintext )
763763}
0 commit comments