@ -934,11 +934,17 @@ MessageReceiver.prototype.extend({
return this . innerHandleContentMessage ( envelope , plaintext ) ;
} ) ;
} ,
promptUserToAcceptFriendRequest ( pubK ey , message , preKeyBundl e) {
promptUserToAcceptFriendRequest ( envelo pe, message , preKeyBundl eMessag e) {
window . Whisper . events . trigger ( 'showFriendRequest' , {
pubKey ,
pubKey : envelope . source ,
message ,
preKeyBundle ,
preKeyBundle : this . decodePreKeyBundleMessage ( preKeyBundleMessage ) ,
options : {
source : envelope . source ,
sourceDevice : envelope . sourceDevice ,
timestamp : envelope . timestamp . toNumber ( ) ,
receivedAt : envelope . receivedAt ,
} ,
} ) ;
} ,
// A handler function for when a friend request is accepted or declined
@ -973,53 +979,50 @@ MessageReceiver.prototype.extend({
const content = textsecure . protobuf . Content . decode ( plaintext ) ;
if ( envelope . type === textsecure . protobuf . Envelope . Type . FRIEND _REQUEST ) {
// only prompt friend request if there is no conversation yet
let conversation ;
try {
conversation = ConversationController . get ( envelope . source ) ;
} catch ( e ) { }
// only prompt friend request if there is no conversation yet
if ( ! conversation ) {
this . promptUserToAcceptFriendRequest (
envelope .source ,
envelope ,
content . dataMessage . body ,
content . preKeyBundle ,
content . preKeyBundle Message ,
) ;
return ;
}
}
// Check if our friend request got accepted
if ( content . preKeyBundle ) {
// By default we don't want to save the preKey
let savePreKey = false ;
// The conversation
let conversation = null ;
try {
conversation = ConversationController . get ( envelope . source ) ;
// We only want to save the preKey if we have a outgoing friend request which is pending
} else {
const keyExchangeComplete = conversation . isKeyExchangeCompleted ( ) ;
// Check here if we received preKeys from the other user
// We are certain that other user accepted the friend request IF:
// - The message has a preKeyBundleMessage
// - We have an outgoing friend request that is pending
// The second check is crucial because it makes sure we don't save the preKeys of the incoming friend request (which is saved only when we press accept)
if ( ! keyExchangeComplete && content . preKeyBundleMessage ) {
// Check for any outgoing friend requests
const pending = await conversation . getPendingFriendRequests ( 'outgoing' ) ;
const successful = pending . filter ( p => ! p . hasErrors ( ) ) ;
// Save the key only if we have an outgoing friend request
savePreKey = ( successful . length > 0 ) ;
} catch ( e ) { }
const savePreKey = ( successful . length > 0 ) ;
// Save the pre key if we have a conversation
if ( savePreKey && conversation ) {
// Save the pre key
if ( savePreKey ) {
await this . handlePreKeyBundleMessage (
envelope . source ,
content . preKeyBundle
this . decodePreKeyBundleMessage ( content . preKeyBundle Message) ,
) ;
// Update the conversation
await conversation . onFriendRequestAccepted ( ) ;
}
}
}
// Exit early since the friend request reply will be a regular empty message
return ;
}
}
if ( content . syncMessage ) {
return this . handleSyncMessage ( envelope , content . syncMessage ) ;
@ -1235,8 +1238,7 @@ MessageReceiver.prototype.extend({
return this . removeFromCache ( envelope ) ;
} ,
async handlePreKeyBundleMessage ( pubKey , preKeyBundleMessage ) {
const { preKeyId , signedKeyId } = preKeyBundleMessage ;
decodePreKeyBundleMessage ( preKeyBundleMessage ) {
const [ identityKey , preKey , signedKey , signature ] = [
preKeyBundleMessage . identityKey ,
preKeyBundleMessage . preKey ,
@ -1244,6 +1246,24 @@ MessageReceiver.prototype.extend({
preKeyBundleMessage . signature ,
] . map ( k => dcodeIO . ByteBuffer . wrap ( k ) . toArrayBuffer ( ) ) ;
return {
... preKeyBundleMessage ,
identityKey ,
preKey ,
signedKey ,
signature ,
} ;
} ,
async handlePreKeyBundleMessage ( pubKey , preKeyBundleMessage ) {
const {
preKeyId ,
signedKeyId ,
identityKey ,
preKey ,
signedKey ,
signature ,
} = preKeyBundleMessage ;
if ( pubKey != StringView . arrayBufferToHex ( identityKey ) ) {
throw new Error (
'Error in handlePreKeyBundleMessage: envelope pubkey does not match pubkey in prekey bundle'