Run key generation in a worker
Ground work for a smoother registration flow. Overall UX still needs some polish but at least now we can have a progress gif or animation or whatever. Also adds the phonenumber-confirmation step as a simple alert box, which will be replaced with a nice dialogue in a later commit.
This commit is contained in:
76
js/generate_keys.js
Normal file
76
js/generate_keys.js
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
/* vim: ts=4:sw=4
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Load this script in a Web Worker to generate new prekeys without
|
||||||
|
* tying up the main thread.
|
||||||
|
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
|
||||||
|
*
|
||||||
|
* Because workers don't have access to the window or localStorage, we
|
||||||
|
* create our own version that proxies back to the caller for actual
|
||||||
|
* storage.
|
||||||
|
*
|
||||||
|
* Example usage:
|
||||||
|
*
|
||||||
|
var myWorker = new Worker('/js/generate_keys.js');
|
||||||
|
myWorker.onmessage = function(e) {
|
||||||
|
switch(e.data.method) {
|
||||||
|
case 'set':
|
||||||
|
localStorage.setItem(e.data.key, e.data.value);
|
||||||
|
break;
|
||||||
|
case 'remove':
|
||||||
|
localStorage.removeItem(e.data.key);
|
||||||
|
break;
|
||||||
|
case 'done':
|
||||||
|
console.log(e.data.keys);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
var store = {};
|
||||||
|
var window = this;
|
||||||
|
importScripts('libtextsecure.js');
|
||||||
|
window.textsecure.storage.impl = {
|
||||||
|
/*****************************
|
||||||
|
*** Override Storage Routines ***
|
||||||
|
*****************************/
|
||||||
|
put: function(key, value) {
|
||||||
|
if (value === undefined)
|
||||||
|
throw new Error("Tried to store undefined");
|
||||||
|
store[key] = value;
|
||||||
|
postMessage({method: 'set', key: key, value: value});
|
||||||
|
},
|
||||||
|
|
||||||
|
get: function(key, defaultValue) {
|
||||||
|
if (key in store) {
|
||||||
|
return store[key];
|
||||||
|
} else {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
remove: function(key) {
|
||||||
|
delete store[key];
|
||||||
|
postMessage({method: 'remove', key: key});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
onmessage = function(e) {
|
||||||
|
store = e.data;
|
||||||
|
textsecure.protocol_wrapper.generateKeys().then(function(keys) {
|
||||||
|
postMessage({method: 'done', keys: keys});
|
||||||
|
});
|
||||||
|
}
|
@@ -38388,7 +38388,7 @@ window.axolotl.sessions = {
|
|||||||
;(function() {
|
;(function() {
|
||||||
|
|
||||||
function loadProtoBufs(filename) {
|
function loadProtoBufs(filename) {
|
||||||
return dcodeIO.ProtoBuf.loadProtoFile({root: 'protos', file: filename}).build('textsecure');
|
return dcodeIO.ProtoBuf.loadProtoFile({root: '/protos', file: filename}).build('textsecure');
|
||||||
};
|
};
|
||||||
|
|
||||||
var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto');
|
var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto');
|
||||||
@@ -38926,11 +38926,7 @@ window.textsecure.registerSingleDevice = function(number, verificationCode, step
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryptoInfo, stepDone) {
|
window.textsecure.registerSecondDevice = function(provisionMessage) {
|
||||||
var envelope = textsecure.protobuf.ProvisionEnvelope.decode(encodedProvisionEnvelope, 'binary');
|
|
||||||
return cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(identityKey) {
|
|
||||||
stepDone(1);
|
|
||||||
|
|
||||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
||||||
textsecure.storage.put('signaling_key', signalingKey);
|
textsecure.storage.put('signaling_key', signalingKey);
|
||||||
|
|
||||||
@@ -38942,18 +38938,9 @@ window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryp
|
|||||||
registrationId = registrationId & 0x3fff;
|
registrationId = registrationId & 0x3fff;
|
||||||
textsecure.storage.put("registrationId", registrationId);
|
textsecure.storage.put("registrationId", registrationId);
|
||||||
|
|
||||||
return textsecure.api.confirmCode(identityKey.number, identityKey.provisioningCode, password, signalingKey, registrationId, false).then(function(result) {
|
return textsecure.api.confirmCode(provisionMessage.number, provisionMessage.provisioningCode, password, signalingKey, registrationId, false).then(function(result) {
|
||||||
textsecure.storage.user.setNumberAndDeviceId(identityKey.number, result.deviceId);
|
textsecure.storage.user.setNumberAndDeviceId(provisionMessage.number, result.deviceId);
|
||||||
textsecure.storage.put("regionCode", libphonenumber.util.getRegionCodeForNumber(identityKey.number));
|
textsecure.storage.put("regionCode", libphonenumber.util.getRegionCodeForNumber(provisionMessage.number));
|
||||||
stepDone(2);
|
|
||||||
|
|
||||||
return textsecure.protocol_wrapper.generateKeys().then(function(keys) {
|
|
||||||
stepDone(3);
|
|
||||||
return textsecure.api.registerKeys(keys).then(function() {
|
|
||||||
stepDone(4);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -56,23 +56,42 @@
|
|||||||
request.respond(200, 'OK');
|
request.respond(200, 'OK');
|
||||||
} else if (request.path == "/v1/message" && request.verb == "PUT") {
|
} else if (request.path == "/v1/message" && request.verb == "PUT") {
|
||||||
$('#qr').hide();
|
$('#qr').hide();
|
||||||
textsecure.registerSecondDevice(request.body, cryptoInfo, function(step) {
|
|
||||||
switch(step) {
|
var envelope = textsecure.protobuf.ProvisionEnvelope.decode(request.body, 'binary');
|
||||||
case 1:
|
cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(provisionMessage) {
|
||||||
|
if (confirm(provisionMessage.number)) {
|
||||||
$('#status').text('Registering new device...');
|
$('#status').text('Registering new device...');
|
||||||
break;
|
window.textsecure.registerSecondDevice(provisionMessage).then(function() {
|
||||||
case 2:
|
|
||||||
$('#status').text('Generating keys...');
|
$('#status').text('Generating keys...');
|
||||||
|
var counter = 0;
|
||||||
|
var myWorker = new Worker('/js/generate_keys.js');
|
||||||
|
myWorker.postMessage({
|
||||||
|
maxPreKeyId: textsecure.storage.get("maxPreKeyId", 0),
|
||||||
|
signedKeyId: textsecure.storage.get("signedKeyId", 0),
|
||||||
|
libaxolotl25519KeyidentityKey: textsecure.storage.get("libaxolotl25519KeyidentityKey"),
|
||||||
|
});
|
||||||
|
myWorker.onmessage = function(e) {
|
||||||
|
switch(e.data.method) {
|
||||||
|
case 'set':
|
||||||
|
textsecure.storage.put(e.data.key, e.data.value);
|
||||||
|
counter = counter + 1;
|
||||||
|
$('#status').text('Generating keys...' + counter);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 'remove':
|
||||||
|
textsecure.storage.remove(e.data.key);
|
||||||
|
break;
|
||||||
|
case 'done':
|
||||||
$('#status').text('Uploading keys...');
|
$('#status').text('Uploading keys...');
|
||||||
break;
|
textsecure.api.registerKeys(e.data.keys).then(function() {
|
||||||
case 4:
|
|
||||||
$('#status').text('All done!');
|
$('#status').text('All done!');
|
||||||
textsecure.registration.done();
|
textsecure.registration.done();
|
||||||
$('#init-setup').hide();
|
$('#init-setup').hide();
|
||||||
$('#setup-complete').show().addClass('in');
|
$('#setup-complete').show().addClass('in');
|
||||||
initOptions();
|
initOptions();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else
|
} else
|
||||||
|
@@ -279,11 +279,7 @@ window.textsecure.registerSingleDevice = function(number, verificationCode, step
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryptoInfo, stepDone) {
|
window.textsecure.registerSecondDevice = function(provisionMessage) {
|
||||||
var envelope = textsecure.protobuf.ProvisionEnvelope.decode(encodedProvisionEnvelope, 'binary');
|
|
||||||
return cryptoInfo.decryptAndHandleDeviceInit(envelope).then(function(identityKey) {
|
|
||||||
stepDone(1);
|
|
||||||
|
|
||||||
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
var signalingKey = textsecure.crypto.getRandomBytes(32 + 20);
|
||||||
textsecure.storage.put('signaling_key', signalingKey);
|
textsecure.storage.put('signaling_key', signalingKey);
|
||||||
|
|
||||||
@@ -295,17 +291,8 @@ window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryp
|
|||||||
registrationId = registrationId & 0x3fff;
|
registrationId = registrationId & 0x3fff;
|
||||||
textsecure.storage.put("registrationId", registrationId);
|
textsecure.storage.put("registrationId", registrationId);
|
||||||
|
|
||||||
return textsecure.api.confirmCode(identityKey.number, identityKey.provisioningCode, password, signalingKey, registrationId, false).then(function(result) {
|
return textsecure.api.confirmCode(provisionMessage.number, provisionMessage.provisioningCode, password, signalingKey, registrationId, false).then(function(result) {
|
||||||
textsecure.storage.user.setNumberAndDeviceId(identityKey.number, result.deviceId);
|
textsecure.storage.user.setNumberAndDeviceId(provisionMessage.number, result.deviceId);
|
||||||
textsecure.storage.put("regionCode", libphonenumber.util.getRegionCodeForNumber(identityKey.number));
|
textsecure.storage.put("regionCode", libphonenumber.util.getRegionCodeForNumber(provisionMessage.number));
|
||||||
stepDone(2);
|
|
||||||
|
|
||||||
return textsecure.protocol_wrapper.generateKeys().then(function(keys) {
|
|
||||||
stepDone(3);
|
|
||||||
return textsecure.api.registerKeys(keys).then(function() {
|
|
||||||
stepDone(4);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
;(function() {
|
;(function() {
|
||||||
|
|
||||||
function loadProtoBufs(filename) {
|
function loadProtoBufs(filename) {
|
||||||
return dcodeIO.ProtoBuf.loadProtoFile({root: 'protos', file: filename}).build('textsecure');
|
return dcodeIO.ProtoBuf.loadProtoFile({root: '/protos', file: filename}).build('textsecure');
|
||||||
};
|
};
|
||||||
|
|
||||||
var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto');
|
var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto');
|
||||||
|
Reference in New Issue
Block a user