Merge pull request #75 from BeaudanBrown/loki-store-clean

Clean up function names and loki store
pull/79/head
sachaaaaa 7 years ago committed by GitHub
commit d0133f3c0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -649,6 +649,7 @@
<script type='text/javascript' src='js/storage.js'></script> <script type='text/javascript' src='js/storage.js'></script>
<script type='text/javascript' src='js/legacy_storage.js'></script> <script type='text/javascript' src='js/legacy_storage.js'></script>
<script type='text/javascript' src='js/signal_protocol_store.js'></script> <script type='text/javascript' src='js/signal_protocol_store.js'></script>
<script type='text/javascript' src='js/loki_protocol_store.js'></script>
<script type='text/javascript' src='js/libtextsecure.js'></script> <script type='text/javascript' src='js/libtextsecure.js'></script>
<script type='text/javascript' src='js/libloki.js'></script> <script type='text/javascript' src='js/libloki.js'></script>

@ -0,0 +1,120 @@
// eslint-disable-next-line func-names
(function() {
'use strict';
const store = window.SignalProtocolStore.prototype;
store.storeContactPreKey = async (pubKey, preKey) => {
const key = {
// id: (autoincrement)
identityKeyString: pubKey,
publicKey: preKey.publicKey,
keyId: preKey.keyId,
};
await window.Signal.Data.createOrUpdateContactPreKey(key);
};
store.loadContactPreKey = async pubKey => {
const preKey = await window.Signal.Data.getContactPreKeyByIdentityKey(pubKey);
if (preKey) {
return {
id: preKey.id,
keyId: preKey.keyId,
publicKey: preKey.publicKey,
identityKeyString: preKey.identityKeyString,
}
}
window.log.warn('Failed to fetch contact prekey:', pubKey);
return undefined;
};
store.loadContactPreKeys = async filters => {
const { keyId, identityKeyString } = filters;
const keys = await window.Signal.Data.getContactPreKeys(keyId, identityKeyString);
if (keys) {
return keys.map(preKey => ({
id: preKey.id,
keyId: preKey.keyId,
publicKey: preKey.publicKey,
identityKeyString: preKey.identityKeyString,
}));
}
window.log.warn(
'Failed to fetch signed prekey with filters',
filters
);
return undefined;
};
store.removeContactPreKey = async pubKey => {
await window.Signal.Data.removeContactPreKeyByIdentityKey(pubKey);
};
store.clearContactPreKeysStore = async () => {
await window.Signal.Data.removeAllContactPreKeys();
};
store.storeContactSignedPreKey = async (pubKey, signedPreKey) => {
const key = {
// id: (autoincrement)
identityKeyString: pubKey,
keyId: signedPreKey.keyId,
publicKey: signedPreKey.publicKey,
signature: signedPreKey.signature,
created_at: Date.now(),
confirmed: false,
};
await window.Signal.Data.createOrUpdateContactSignedPreKey(key);
};
store.loadContactSignedPreKey = async pubKey => {
const preKey = await window.Signal.Data.getContactSignedPreKeyByIdentityKey(pubKey);
if (preKey) {
return {
id: preKey.id,
identityKeyString: preKey.identityKeyString,
publicKey: preKey.publicKey,
signature: preKey.signature,
created_at: preKey.created_at,
keyId: preKey.keyId,
confirmed: preKey.confirmed,
};
}
window.log.warn('Failed to fetch contact signed prekey:', pubKey);
return undefined;
};
store.loadContactSignedPreKeys = async filters => {
const { keyId, identityKeyString } = filters;
const keys = await window.Signal.Data.getContactSignedPreKeys(keyId, identityKeyString);
if (keys) {
return keys.map(preKey => ({
id: preKey.id,
identityKeyString: preKey.identityKeyString,
publicKey: preKey.publicKey,
signature: preKey.signature,
created_at: preKey.created_at,
keyId: preKey.keyId,
confirmed: preKey.confirmed,
}));
}
window.log.warn(
'Failed to fetch contact signed prekey with filters',
filters
);
return undefined;
};
store.removeContactSignedPreKey = async pubKey => {
await window.Signal.Data.removeContactSignedPreKeyByIdentityKey(pubKey);
};
store.clearContactSignedPreKeysStore = async () => {
await window.Signal.Data.removeAllContactSignedPreKeys();
};
})();

@ -513,7 +513,7 @@
response: 'declined', response: 'declined',
direction: 'incoming', direction: 'incoming',
}); });
await window.libloki.removePreKeyBundleForNumber(this.id); await window.libloki.removeContactPreKeyBundle(this.id);
}, },
// We have accepted an incoming friend request // We have accepted an incoming friend request
async onAcceptFriendRequest() { async onAcceptFriendRequest() {

@ -183,11 +183,11 @@
window.log.error('Failed to fetch prekey:', keyId); window.log.error('Failed to fetch prekey:', keyId);
return undefined; return undefined;
}, },
async loadPreKeyForContactIdentityKeyString(contactIdentityKeyString) { async loadPreKeyForContact(contactPubKey) {
const key = await window.Signal.Data.getPreKeyByRecipient(contactIdentityKeyString); const key = await window.Signal.Data.getPreKeyByRecipient(contactPubKey);
if (key) { if (key) {
window.log.info('Successfully fetched prekey for recipient:', contactIdentityKeyString); window.log.info('Successfully fetched prekey for recipient:', contactPubKey);
return { return {
pubKey: key.publicKey, pubKey: key.publicKey,
privKey: key.privateKey, privKey: key.privateKey,
@ -198,54 +198,12 @@
return undefined; return undefined;
}, },
async loadContactPreKey(pubKey) { async storePreKey(keyId, keyPair, contactPubKey) {
const preKey = await window.Signal.Data.getContactPreKeyByIdentityKey(pubKey);
if (preKey) {
return {
id: preKey.id,
keyId: preKey.keyId,
publicKey: preKey.publicKey,
identityKeyString: preKey.identityKeyString,
}
}
window.log.warn('Failed to fetch contact prekey:', pubKey);
return undefined;
},
async loadContactPreKeys(filters) {
const { keyId, identityKeyString } = filters;
const keys = await window.Signal.Data.getContactPreKeys(keyId, identityKeyString);
if (keys) {
return keys.map(preKey => ({
id: preKey.id,
keyId: preKey.keyId,
publicKey: preKey.publicKey,
identityKeyString: preKey.identityKeyString,
}));
}
window.log.warn(
'Failed to fetch signed prekey with filters',
filters
);
return undefined;
},
async storeContactPreKey(pubKey, preKey) {
const key = {
// id: (autoincrement)
identityKeyString: pubKey,
publicKey: preKey.publicKey,
keyId: preKey.keyId,
};
await window.Signal.Data.createOrUpdateContactPreKey(key);
},
async storePreKey(keyId, keyPair, contactIdentityKeyString) {
const data = { const data = {
id: keyId, id: keyId,
publicKey: keyPair.pubKey, publicKey: keyPair.pubKey,
privateKey: keyPair.privKey, privateKey: keyPair.privKey,
recipient: contactIdentityKeyString, recipient: contactPubKey,
}; };
await window.Signal.Data.createOrUpdatePreKey(data); await window.Signal.Data.createOrUpdatePreKey(data);
@ -265,14 +223,6 @@
async clearPreKeyStore() { async clearPreKeyStore() {
await window.Signal.Data.removeAllPreKeys(); await window.Signal.Data.removeAllPreKeys();
}, },
async removeContactPreKey(pubKey) {
await window.Signal.Data.removeContactPreKeyByIdentityKey(pubKey);
},
async clearContactPreKeysStore() {
await window.Signal.Data.removeAllContactPreKeys();
},
/* Returns a signed keypair object or undefined */ /* Returns a signed keypair object or undefined */
async loadSignedPreKey(keyId) { async loadSignedPreKey(keyId) {
const key = await window.Signal.Data.getSignedPreKeyById(keyId); const key = await window.Signal.Data.getSignedPreKeyById(keyId);
@ -291,43 +241,6 @@
window.log.error('Failed to fetch signed prekey:', keyId); window.log.error('Failed to fetch signed prekey:', keyId);
return undefined; return undefined;
}, },
async loadContactSignedPreKeys(filters) {
const { keyId, identityKeyString } = filters;
const keys = await window.Signal.Data.getContactSignedPreKeys(keyId, identityKeyString);
if (keys) {
return keys.map(preKey => ({
id: preKey.id,
identityKeyString: preKey.identityKeyString,
publicKey: preKey.publicKey,
signature: preKey.signature,
created_at: preKey.created_at,
keyId: preKey.keyId,
confirmed: preKey.confirmed,
}));
}
window.log.warn(
'Failed to fetch contact signed prekey with filters',
filters
);
return undefined;
},
async loadContactSignedPreKey(pubKey) {
const preKey = await window.Signal.Data.getContactSignedPreKeyByIdentityKey(pubKey);
if (preKey) {
return {
id: preKey.id,
identityKeyString: preKey.identityKeyString,
publicKey: preKey.publicKey,
signature: preKey.signature,
created_at: preKey.created_at,
keyId: preKey.keyId,
confirmed: preKey.confirmed,
};
}
window.log.warn('Failed to fetch contact signed prekey:', pubKey);
return undefined;
},
async loadSignedPreKeys() { async loadSignedPreKeys() {
if (arguments.length > 0) { if (arguments.length > 0) {
throw new Error('loadSignedPreKeys takes no arguments'); throw new Error('loadSignedPreKeys takes no arguments');
@ -354,32 +267,12 @@
}; };
await window.Signal.Data.createOrUpdateSignedPreKey(key); await window.Signal.Data.createOrUpdateSignedPreKey(key);
}, },
async storeContactSignedPreKey(pubKey, signedPreKey) {
const key = {
// id: (autoincrement)
identityKeyString: pubKey,
keyId: signedPreKey.keyId,
publicKey: signedPreKey.publicKey,
signature: signedPreKey.signature,
created_at: Date.now(),
confirmed: false,
};
await window.Signal.Data.createOrUpdateContactSignedPreKey(key);
},
async removeSignedPreKey(keyId) { async removeSignedPreKey(keyId) {
await window.Signal.Data.removeSignedPreKeyById(keyId); await window.Signal.Data.removeSignedPreKeyById(keyId);
}, },
async clearSignedPreKeysStore() { async clearSignedPreKeysStore() {
await window.Signal.Data.removeAllSignedPreKeys(); await window.Signal.Data.removeAllSignedPreKeys();
}, },
async removeContactSignedPreKey(pubKey) {
await window.Signal.Data.removeContactSignedPreKeyByIdentityKey(pubKey);
},
async clearContactSignedPreKeysStore() {
await window.Signal.Data.removeAllContactSignedPreKeys();
},
async loadSession(encodedNumber) { async loadSession(encodedNumber) {
if (encodedNumber === null || encodedNumber === undefined) { if (encodedNumber === null || encodedNumber === undefined) {
throw new Error('Tried to get session for undefined/null number'); throw new Error('Tried to get session for undefined/null number');

@ -46,7 +46,7 @@
} }
} }
async function getPreKeyBundleForNumber(pubKey) { async function getPreKeyBundleForContact(pubKey) {
const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair(); const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair();
const identityKey = myKeyPair.pubKey; const identityKey = myKeyPair.pubKey;
@ -57,7 +57,7 @@
textsecure.storage.protocol.loadSignedPreKey(signedKeyId), textsecure.storage.protocol.loadSignedPreKey(signedKeyId),
new Promise(async resolve => { new Promise(async resolve => {
// retrieve existing prekey if we already generated one for that recipient // retrieve existing prekey if we already generated one for that recipient
const storedPreKey = await textsecure.storage.protocol.loadPreKeyForContactIdentityKeyString( const storedPreKey = await textsecure.storage.protocol.loadPreKeyForContact(
pubKey pubKey
); );
if (storedPreKey) { if (storedPreKey) {
@ -77,7 +77,7 @@
}), }),
]); ]);
const preKeyMessage = new textsecure.protobuf.PreKeyBundleMessage({ return {
identityKey: new Uint8Array(identityKey), identityKey: new Uint8Array(identityKey),
deviceId: 1, // TODO: fetch from somewhere deviceId: 1, // TODO: fetch from somewhere
preKeyId: preKey.keyId, preKeyId: preKey.keyId,
@ -85,12 +85,10 @@
preKey: new Uint8Array(preKey.pubKey), preKey: new Uint8Array(preKey.pubKey),
signedKey: new Uint8Array(signedKey.pubKey), signedKey: new Uint8Array(signedKey.pubKey),
signature: new Uint8Array(signedKey.signature), signature: new Uint8Array(signedKey.signature),
}); };
return preKeyMessage;
} }
async function savePreKeyBundleForNumber({ async function saveContactPreKeyBundle({
pubKey, pubKey,
preKeyId, preKeyId,
preKey, preKey,
@ -122,7 +120,7 @@
await Promise.all([signedKeyPromise, preKeyPromise]); await Promise.all([signedKeyPromise, preKeyPromise]);
} }
async function removePreKeyBundleForNumber(pubKey) { async function removeContactPreKeyBundle(pubKey) {
await Promise.all([ await Promise.all([
textsecure.storage.protocol.removeContactPreKey(pubKey), textsecure.storage.protocol.removeContactPreKey(pubKey),
textsecure.storage.protocol.removeContactSignedPreKey(pubKey), textsecure.storage.protocol.removeContactSignedPreKey(pubKey),
@ -156,9 +154,9 @@
} }
window.libloki.FallBackSessionCipher = FallBackSessionCipher; window.libloki.FallBackSessionCipher = FallBackSessionCipher;
window.libloki.getPreKeyBundleForNumber = getPreKeyBundleForNumber; window.libloki.getPreKeyBundleForContact = getPreKeyBundleForContact;
window.libloki.FallBackDecryptionError = FallBackDecryptionError; window.libloki.FallBackDecryptionError = FallBackDecryptionError;
window.libloki.savePreKeyBundleForNumber = savePreKeyBundleForNumber; window.libloki.saveContactPreKeyBundle = saveContactPreKeyBundle;
window.libloki.removePreKeyBundleForNumber = removePreKeyBundleForNumber; window.libloki.removeContactPreKeyBundle = removeContactPreKeyBundle;
window.libloki.sendFriendRequestAccepted = sendFriendRequestAccepted; window.libloki.sendFriendRequestAccepted = sendFriendRequestAccepted;
})(); })();

@ -1235,7 +1235,7 @@ MessageReceiver.prototype.extend({
); );
} }
await libloki.savePreKeyBundleForNumber({ await libloki.saveContactPreKeyBundle({
pubKey, pubKey,
preKeyId, preKeyId,
signedKeyId, signedKeyId,

@ -287,7 +287,9 @@ OutgoingMessage.prototype = {
let sessionCipher; let sessionCipher;
if (this.messageType === 'friend-request') { if (this.messageType === 'friend-request') {
// Encrypt them with the fallback // Encrypt them with the fallback
this.message.preKeyBundleMessage = await libloki.getPreKeyBundleForNumber(number); const pkb = await libloki.getPreKeyBundleForContact(number);
const preKeyBundleMessage = new textsecure.protobuf.PreKeyBundleMessage(pkb);
this.message.preKeyBundleMessage = preKeyBundleMessage;
window.log.info('attaching prekeys to outgoing message'); window.log.info('attaching prekeys to outgoing message');
sessionCipher = fallBackCipher; sessionCipher = fallBackCipher;
} else { } else {

Loading…
Cancel
Save