From 2f562ce9e128473cbd78aefbf4b3f75b0ae797e4 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Thu, 23 Aug 2018 09:46:58 +1000 Subject: [PATCH] Change contact prekeys in db to allow multiple entries for each contact --- ...rations_0_database_with_attachment_data.js | 8 +++-- js/signal_protocol_store.js | 11 +++++-- libtextsecure/account_manager.js | 29 ++++++++++++++----- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/js/modules/migrations/migrations_0_database_with_attachment_data.js b/js/modules/migrations/migrations_0_database_with_attachment_data.js index 9114a5295..aec09f75c 100644 --- a/js/modules/migrations/migrations_0_database_with_attachment_data.js +++ b/js/modules/migrations/migrations_0_database_with_attachment_data.js @@ -47,8 +47,12 @@ const migrations = [ transaction.db.createObjectStore('preKeys'); transaction.db.createObjectStore('signedPreKeys'); transaction.db.createObjectStore('items'); - transaction.db.createObjectStore('contactPreKeys'); - transaction.db.createObjectStore('contactSignedPreKeys'); + + const contactPreKeys = transaction.db.createObjectStore('contactPreKeys', { autoIncrement : true }); + contactPreKeys.createIndex('publicKeyString', 'publicKeyString', { unique: false }); + + const contactSignedPreKeys = transaction.db.createObjectStore('contactSignedPreKeys', { autoIncrement : true }); + contactSignedPreKeys.createIndex('publicKeyString', 'publicKeyString', { unique: false }); window.log.info('creating debug log'); transaction.db.createObjectStore('debug'); diff --git a/js/signal_protocol_store.js b/js/signal_protocol_store.js index b0702b614..7da36892a 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -231,7 +231,8 @@ window.log.info('Successfully fetched contact prekey:', pubKey); resolve({ keyId: prekey.get('keyId'), - publicKey: prekey.get('publicKey') + publicKey: prekey.get('publicKey'), + identityKey: prekey.get('identityKey'), }); }, () => { @@ -243,7 +244,8 @@ }, storeContactPreKey(pubKey, preKey) { const prekey = new ContactPreKey({ - id: pubKey, + // id: (autoincrement) + publicKeyString: pubKey, publicKey: preKey.publicKey, keyId: preKey.keyId, }); @@ -328,6 +330,8 @@ prekey.get('id') ); resolve({ + id: preKey.get('id'), + publicKeyString: preKey.get('publicKeyString'), publicKey: prekey.get('publicKey'), signature: prekey.get('signature'), created_at: prekey.get('created_at'), @@ -378,7 +382,8 @@ }, storeContactSignedPreKey(pubKey, signedPreKey) { const prekey = new ContactSignedPreKey({ - id: pubKey, + // id: (autoincrement) + publicKeyString: pubKey, keyId: signedPreKey.keyId, publicKey: signedPreKey.publicKey, signature: signedPreKey.signature, diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index e37bd902a..b20ad28e1 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -65,17 +65,30 @@ 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), + const promises = [ 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 }); + .then((signedPreKey) => { + const contactSignedPreKey = { + publicKey: signedPreKey.keyPair.pubKey, + signature: signedPreKey.signature, + keyId: signedPreKey.keyId + }; + return textsecure.storage.protocol.storeContactSignedPreKey(pubKey, contactSignedPreKey); + }), + ]; + + for (let keyId = 0; keyId < 100; keyId += 1) { + promises.push( + libsignal.KeyHelper.generatePreKey(keyId) + .then((preKey) => { + return textsecure.storage.protocol.storeContactPreKey(pubKey, { publicKey: preKey.keyPair.pubKey, keyId: keyId }); + }), + ) + } + Promise.all(promises).then( log.info("Added mock contact with pubkey " + pubKey) - }); + ); }); }, registerSecondDevice(setProvisioningUrl, confirmNumber, progressCallback) {