@@ -55,7 +55,7 @@ export const hkdfInfoKey = (type: MediaType) => {
55
55
}
56
56
57
57
/** 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 > {
59
59
if ( ! buffer ) {
60
60
throw new Boom ( 'Cannot derive from empty media key' )
61
61
}
@@ -65,7 +65,7 @@ export function getMediaKeys(buffer: Uint8Array | string | null | undefined, med
65
65
}
66
66
67
67
// 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 ) } )
69
69
return {
70
70
iv : expandedMediaKey . slice ( 0 , 16 ) ,
71
71
cipherKey : expandedMediaKey . slice ( 16 , 48 ) ,
@@ -344,7 +344,7 @@ export const encryptedStream = async(
344
344
logger ?. debug ( 'fetched media stream' )
345
345
346
346
const mediaKey = Crypto . randomBytes ( 32 )
347
- const { cipherKey, iv, macKey } = getMediaKeys ( mediaKey , mediaType )
347
+ const { cipherKey, iv, macKey } = await getMediaKeys ( mediaKey , mediaType )
348
348
const encWriteStream = new Readable ( { read : ( ) => { } } )
349
349
350
350
let bodyPath : string | undefined
@@ -458,13 +458,13 @@ export type MediaDownloadOptions = {
458
458
459
459
export const getUrlFromDirectPath = ( directPath : string ) => `https://${ DEF_HOST } ${ directPath } `
460
460
461
- export const downloadContentFromMessage = (
461
+ export const downloadContentFromMessage = async (
462
462
{ mediaKey, directPath, url } : DownloadableMessage ,
463
463
type : MediaType ,
464
464
opts : MediaDownloadOptions = { }
465
465
) => {
466
466
const downloadUrl = url || getUrlFromDirectPath ( directPath ! )
467
- const keys = getMediaKeys ( mediaKey , type )
467
+ const keys = await getMediaKeys ( mediaKey , type )
468
468
469
469
return downloadEncryptedContent ( downloadUrl , keys , opts )
470
470
}
@@ -673,7 +673,7 @@ const getMediaRetryKey = (mediaKey: Buffer | Uint8Array) => {
673
673
/**
674
674
* Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
675
675
*/
676
- export const encryptMediaRetryRequest = (
676
+ export const encryptMediaRetryRequest = async (
677
677
key : proto . IMessageKey ,
678
678
mediaKey : Buffer | Uint8Array ,
679
679
meId : string
@@ -682,7 +682,7 @@ export const encryptMediaRetryRequest = (
682
682
const recpBuffer = proto . ServerErrorReceipt . encode ( recp ) . finish ( )
683
683
684
684
const iv = Crypto . randomBytes ( 12 )
685
- const retryKey = getMediaRetryKey ( mediaKey )
685
+ const retryKey = await getMediaRetryKey ( mediaKey )
686
686
const ciphertext = aesEncryptGCM ( recpBuffer , retryKey , iv , Buffer . from ( key . id ! ) )
687
687
688
688
const req : BinaryNode = {
@@ -752,12 +752,12 @@ export const decodeMediaRetryNode = (node: BinaryNode) => {
752
752
return event
753
753
}
754
754
755
- export const decryptMediaRetryData = (
755
+ export const decryptMediaRetryData = async (
756
756
{ ciphertext, iv } : { ciphertext : Uint8Array , iv : Uint8Array } ,
757
757
mediaKey : Uint8Array ,
758
758
msgId : string
759
759
) => {
760
- const retryKey = getMediaRetryKey ( mediaKey )
760
+ const retryKey = await getMediaRetryKey ( mediaKey )
761
761
const plaintext = aesDecryptGCM ( ciphertext , retryKey , iv , Buffer . from ( msgId ) )
762
762
return proto . MediaRetryNotification . decode ( plaintext )
763
763
}
0 commit comments