| 
									
										
										
										
											2015-01-14 13:42:01 -10:00
										 |  |  | ;(function() { | 
					
						
							| 
									
										
										
										
											2015-04-01 13:08:09 -07:00
										 |  |  |     'use strict'; | 
					
						
							|  |  |  |     window.textsecure = window.textsecure || {}; | 
					
						
							|  |  |  |     window.textsecure.storage = window.textsecure.storage || {}; | 
					
						
							|  |  |  |     textsecure.storage.axolotl = new AxolotlStore(); | 
					
						
							|  |  |  |     var axolotlInstance = axolotl.protocol(textsecure.storage.axolotl); | 
					
						
							| 
									
										
										
										
											2015-01-15 10:42:32 -10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-15 14:30:55 -10:00
										 |  |  |     var decodeMessageContents = function(res) { | 
					
						
							|  |  |  |         var finalMessage = textsecure.protobuf.PushMessageContent.decode(res[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-18 11:10:39 -10:00
										 |  |  |         if ((finalMessage.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION) | 
					
						
							| 
									
										
										
										
											2015-02-25 17:33:45 -08:00
										 |  |  |                 == textsecure.protobuf.PushMessageContent.Flags.END_SESSION && | 
					
						
							| 
									
										
										
										
											2015-02-19 14:53:08 -08:00
										 |  |  |                 finalMessage.sync !== null) | 
					
						
							| 
									
										
										
										
											2015-01-18 11:10:39 -10:00
										 |  |  |             res[1](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return finalMessage; | 
					
						
							| 
									
										
										
										
											2015-02-19 14:53:08 -08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var handlePreKeyWhisperMessage = function(from, message) { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2015-04-01 13:08:09 -07:00
										 |  |  |             return axolotlInstance.handlePreKeyWhisperMessage(from, message); | 
					
						
							| 
									
										
										
										
											2015-02-19 14:53:08 -08:00
										 |  |  |         } catch(e) { | 
					
						
							|  |  |  |             if (e.message === 'Unknown identity key') { | 
					
						
							|  |  |  |                 // create an error that the UI will pick up and ask the
 | 
					
						
							|  |  |  |                 // user if they want to re-negotiate
 | 
					
						
							|  |  |  |                 throw new textsecure.IncomingIdentityKeyError(from, message); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             throw e; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-01-18 11:10:39 -10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-15 10:42:32 -10:00
										 |  |  |     window.textsecure = window.textsecure || {}; | 
					
						
							|  |  |  |     window.textsecure.protocol_wrapper = { | 
					
						
							|  |  |  |         handleIncomingPushMessageProto: function(proto) { | 
					
						
							|  |  |  |             switch(proto.type) { | 
					
						
							|  |  |  |             case textsecure.protobuf.IncomingPushMessageSignal.Type.PLAINTEXT: | 
					
						
							|  |  |  |                 return Promise.resolve(textsecure.protobuf.PushMessageContent.decode(proto.message)); | 
					
						
							|  |  |  |             case textsecure.protobuf.IncomingPushMessageSignal.Type.CIPHERTEXT: | 
					
						
							|  |  |  |                 var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); | 
					
						
							| 
									
										
										
										
											2015-04-01 13:08:09 -07:00
										 |  |  |                 return axolotlInstance.decryptWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); | 
					
						
							| 
									
										
										
										
											2015-01-15 10:42:32 -10:00
										 |  |  |             case textsecure.protobuf.IncomingPushMessageSignal.Type.PREKEY_BUNDLE: | 
					
						
							|  |  |  |                 if (proto.message.readUint8() != ((3 << 4) | 3)) | 
					
						
							|  |  |  |                     throw new Error("Bad version byte"); | 
					
						
							|  |  |  |                 var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); | 
					
						
							| 
									
										
										
										
											2015-02-19 14:53:08 -08:00
										 |  |  |                 return handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); | 
					
						
							| 
									
										
										
										
											2015-01-15 10:42:32 -10:00
										 |  |  |             case textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT: | 
					
						
							|  |  |  |                 return Promise.resolve(null); | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return new Promise(function(resolve, reject) { reject(new Error("Unknown message type")); }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-03-17 14:31:33 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         closeOpenSessionForDevice: function(encodedNumber) { | 
					
						
							|  |  |  |             return axolotlInstance.closeOpenSessionForDevice(encodedNumber) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         encryptMessageFor: function(deviceObject, pushMessageContent) { | 
					
						
							|  |  |  |             return axolotlInstance.encryptMessageFor(deviceObject, pushMessageContent); | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2015-05-04 11:55:47 -07:00
										 |  |  |         startWorker: function() { | 
					
						
							|  |  |  |             axolotlInstance.startWorker('/js/libaxolotl-worker.js'); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         stopWorker: function() { | 
					
						
							|  |  |  |             axolotlInstance.stopWorker(); | 
					
						
							| 
									
										
										
										
											2015-03-17 14:31:33 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         createIdentityKeyRecvSocket: function() { | 
					
						
							|  |  |  |             return axolotlInstance.createIdentityKeyRecvSocket(); | 
					
						
							| 
									
										
										
										
											2015-04-01 13:08:09 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         hasOpenSession: function(encodedNumber) { | 
					
						
							|  |  |  |             return axolotlInstance.hasOpenSession(encodedNumber); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         getRegistrationId: function(encodedNumber) { | 
					
						
							|  |  |  |             return axolotlInstance.getRegistrationId(encodedNumber); | 
					
						
							| 
									
										
										
										
											2015-01-15 10:42:32 -10:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-01-15 14:30:55 -10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-23 16:23:22 -08:00
										 |  |  |     var tryMessageAgain = function(from, encodedMessage) { | 
					
						
							| 
									
										
										
										
											2015-04-01 13:08:09 -07:00
										 |  |  |         return axolotlInstance.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:30:55 -10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-23 16:23:22 -08:00
										 |  |  |     textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.INIT_SESSION); | 
					
						
							| 
									
										
										
										
											2015-04-01 13:08:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 13:42:01 -10:00
										 |  |  | })(); |