From 025d13a72b9cdd9e1b8a5a80ffc44424748ac69a Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Thu, 18 Oct 2018 16:46:33 +1100 Subject: [PATCH] Add keyId index for contact prekeys and allow retrieving prekeys for a specific pubkey and keyid --- ...rations_0_database_with_attachment_data.js | 2 + js/signal_protocol_store.js | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) 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 e7ca8847d..2cfac0de4 100644 --- a/js/modules/migrations/migrations_0_database_with_attachment_data.js +++ b/js/modules/migrations/migrations_0_database_with_attachment_data.js @@ -52,9 +52,11 @@ const migrations = [ const contactPreKeys = transaction.db.createObjectStore('contactPreKeys', { keyPath: 'id', autoIncrement : true }); contactPreKeys.createIndex('identityKeyString', 'identityKeyString', { unique: false }); + contactPreKeys.createIndex('keyId', 'keyId', { unique: false }); const contactSignedPreKeys = transaction.db.createObjectStore('contactSignedPreKeys', { keyPath: 'id', autoIncrement : true }); contactSignedPreKeys.createIndex('identityKeyString', 'identityKeyString', { unique: false }); + contactSignedPreKeys.createIndex('keyId', 'keyId', { 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 467ab29b9..9bb562ff9 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -179,7 +179,23 @@ const Group = Model.extend({ storeName: 'groups' }); const Item = Model.extend({ storeName: 'items' }); const ContactPreKey = Model.extend({ storeName: 'contactPreKeys' }); + const ContactPreKeyCollection = Backbone.Collection.extend({ + storeName: 'contactPreKeys', + database: Whisper.Database, + model: ContactPreKey, + fetchBy(filter) { + return this.fetch({ conditions: filter, }); + }, + }); const ContactSignedPreKey = Model.extend({ storeName: 'contactSignedPreKeys' }); + const ContactSignedPreKeyCollection = Backbone.Collection.extend({ + storeName: 'contactSignedPreKeys', + database: Whisper.Database, + model: ContactSignedPreKey, + fetchBy(filter) { + return this.fetch({ conditions: filter, }); + }, + }); function SignalProtocolStore() {} @@ -261,6 +277,24 @@ ); }); }, + loadContactPreKeys(filters) { + const contactPreKeys = new ContactPreKeyCollection(); + return new Promise((resolve, reject) => { + contactPreKeys.fetchBy(filters).then(() => { + resolve( + contactPreKeys.map(prekey => ({ + id: prekey.get('id'), + keyId: prekey.get('keyId'), + publicKey: prekey.get('publicKey'), + identityKeyString: prekey.get('identityKeyString'), + })) + ); + }).fail(e => { + window.log.error('Failed to fetch signed prekey with filters', filters); + reject(e); + }); + }); + }, storeContactPreKey(pubKey, preKey) { const prekey = new ContactPreKey({ // id: (autoincrement) @@ -340,6 +374,27 @@ }); }); }, + loadContactSignedPreKeys(filters) { + const contactSignedPreKeys = new ContactSignedPreKeyCollection(); + return new Promise((resolve, reject) => { + contactSignedPreKeys.fetchBy(filters).then(() => { + resolve( + contactSignedPreKeys.map(prekey => ({ + id: prekey.get('id'), + identityKeyString: prekey.get('identityKeyString'), + publicKey: prekey.get('publicKey'), + signature: prekey.get('signature'), + created_at: prekey.get('created_at'), + keyId: prekey.get('keyId'), + confirmed: prekey.get('confirmed'), + })) + ); + }).fail(e => { + window.log.error('Failed to fetch signed prekey with filters', filters); + reject(e); + }); + }); + }, loadContactSignedPreKey(pubKey) { const prekey = new ContactSignedPreKey({ identityKeyString: pubKey }); return new Promise(resolve => {