wip: added helper to create mock contacts, almost got a session working

pull/6/head
sachaaaaa 7 years ago
parent 258a89bc21
commit 0832806bd9

@ -90,8 +90,9 @@ function HTTPError(message, providedCode, response, stack) {
const e = new Error(`${message}; code: ${code}`);
e.name = 'HTTPError';
e.code = code;
if (stack)
if (stack) {
e.stack += `\nOriginal stack:\n${stack}`;
}
if (response) {
e.response = response;
}

@ -228,8 +228,8 @@
() => {
window.log.info('Successfully fetched contact prekey:', pubKey);
resolve({
pubKey: prekey.get('publicKey'),
privKey: prekey.get('privateKey'),
keyId: prekey.get('keyId'),
publicKey: prekey.get('publicKey')
});
},
() => {
@ -239,11 +239,11 @@
);
});
},
storeContactPreKey(pubKey, keyPair) {
storeContactPreKey(pubKey, preKey) {
const prekey = new ContactPreKey({
id: pubKey,
publicKey: keyPair.pubKey,
privateKey: keyPair.privKey,
publicKey: preKey.publicKey,
keyId: preKey.keyId,
});
return new Promise(resolve => {
prekey.save().always(() => {
@ -315,7 +315,7 @@
});
});
},
loadSignedPreKey(pubKey) {
loadContactSignedPreKey(pubKey) {
const prekey = new ContactSignedPreKey({ id: pubKey });
return new Promise(resolve => {
prekey
@ -326,10 +326,10 @@
prekey.get('id')
);
resolve({
pubKey: prekey.get('publicKey'),
privKey: prekey.get('privateKey'),
publicKey: prekey.get('publicKey'),
signature: prekey.get('signature'),
created_at: prekey.get('created_at'),
keyId: prekey.get('id'),
keyId: prekey.get('keyId'),
confirmed: prekey.get('confirmed'),
});
})
@ -374,13 +374,14 @@
});
});
},
storeContactSignedPreKey(pubKey, keyPair, confirmed) {
storeContactSignedPreKey(pubKey, signedPreKey) {
const prekey = new ContactSignedPreKey({
id: pubKey,
publicKey: keyPair.pubKey,
privateKey: keyPair.privKey,
keyId: signedPreKey.keyId,
publicKey: signedPreKey.publicKey,
signature: signedPreKey.signature,
created_at: Date.now(),
confirmed: Boolean(confirmed),
confirmed: false,
});
return new Promise(resolve => {
prekey.save().always(() => {

@ -62,6 +62,22 @@
})
);
},
addMockContact() {
libsignal.KeyHelper.generateIdentityKeyPair().then(keyPair => {
const pubKey = StringView.arrayBufferToHex(keyPair.pubKey);
const keyId = Math.floor((Math.random() * 1000) + 1);
const signedKeyId = Math.floor((Math.random() * 1000) + 1);
Promise.all([
libsignal.KeyHelper.generatePreKey(keyId),
libsignal.KeyHelper.generateSignedPreKey(keyPair, signedKeyId)
]).then((keys) => {
const [preKey, signedPreKey] = keys;
textsecure.storage.protocol.storeContactPreKey(pubKey, { publicKey: preKey.keyPair.pubKey, keyId: keyId });
textsecure.storage.protocol.storeContactSignedPreKey(pubKey, { publicKey: signedPreKey.keyPair.pubKey, signature: signedPreKey.signature, keyId: signedPreKey.keyId });
log.info("Added mock contact with pubkey " + pubKey)
});
});
},
registerSecondDevice(setProvisioningUrl, confirmNumber, progressCallback) {
const createAccount = this.createAccount.bind(this);
const clearSessionsAndPreKeys = this.clearSessionsAndPreKeys.bind(this);

@ -117,9 +117,19 @@ OutgoingMessage.prototype = {
let promise = Promise.resolve();
updateDevices.forEach(device => {
promise = promise.then(() =>
this.server
.getKeysForNumber(number, device)
.then(handleResult)
Promise.all([
textsecure.storage.protocol.loadContactPreKey(number),
textsecure.storage.protocol.loadContactSignedPreKey(number)
]).then((keys) => {
const [preKey, signedPreKey] = keys;
if (preKey == undefined || signedPreKey == undefined) {
log.error("Will need to request keys!")
}
else {
const identityKey = StringView.hexToArrayBuffer(number);
return handleResult({ identityKey, devices: [{ deviceId: device, preKey, signedPreKey, registrationId: 0}] })
}
})
.catch(e => {
if (e.name === 'HTTPError' && e.code === 404) {
if (device !== 1) {
@ -308,7 +318,7 @@ OutgoingMessage.prototype = {
},
sendToNumber(number) {
if (true /* skipEncryption */)
if (false /* skipEncryption */)
{
const pubKey = number;
const data = StringView.bytesToBase64(this.getPlaintext());

@ -102,5 +102,11 @@
arrayBufferToHex(aArrayBuffer) {
return Array.prototype.map.call(new Uint8Array(aArrayBuffer), x => ('00' + x.toString(16)).slice(-2)).join('');
},
hexToArrayBuffer(aString) {
return new Uint8Array(aString.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
})).buffer
},
};
})();

Loading…
Cancel
Save