This commit is contained in:
Niels Andriesse 2019-06-21 10:39:05 +10:00
parent bd00013f02
commit a1b6eaf3f5
9 changed files with 99 additions and 140 deletions

View File

@ -97,27 +97,22 @@ public class PreKeyUtil {
try { try {
return signedPreKeyStore.loadSignedPreKey(getActiveSignedPreKeyId(context)); return signedPreKeyStore.loadSignedPreKey(getActiveSignedPreKeyId(context));
} catch (InvalidKeyIdException e) { } catch (InvalidKeyIdException e) {
// Fall through
}
return null; return null;
} }
}
public synchronized static List<PreKeyRecord> generatePreKeys(Context context, int amount) { public synchronized static List<PreKeyRecord> generatePreKeys(Context context, int amount) {
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context); PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
List<PreKeyRecord> records = new LinkedList<>(); List<PreKeyRecord> records = new LinkedList<>();
int preKeyIdOffset = TextSecurePreferences.getNextPreKeyId(context); int preKeyIDOffset = TextSecurePreferences.getNextPreKeyId(context);
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE; int preKeyID = (preKeyIDOffset + i) % Medium.MAX_VALUE;
ECKeyPair keyPair = Curve.generateKeyPair(); ECKeyPair keyPair = Curve.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair); PreKeyRecord record = new PreKeyRecord(preKeyID, keyPair);
preKeyStore.storePreKey(preKeyID, record);
preKeyStore.storePreKey(preKeyId, record);
records.add(record); records.add(record);
} }
TextSecurePreferences.setNextPreKeyId(context, (preKeyIDOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
TextSecurePreferences.setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
return records; return records;
} }
@ -128,9 +123,9 @@ public class PreKeyUtil {
} }
} }
public synchronized static PreKeyRecord loadPreKey(Context context, int preKeyId) throws InvalidKeyIdException { public synchronized static PreKeyRecord loadPreKey(Context context, int preKeyID) throws InvalidKeyIdException {
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context); PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
return preKeyStore.loadPreKey(preKeyId); return preKeyStore.loadPreKey(preKeyID);
} }
// endregion // endregion
} }

View File

@ -253,15 +253,14 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
if (content.lokiMessage.isPresent()) { if (content.lokiMessage.isPresent()) {
LokiServiceMessage lokiMessage = content.lokiMessage.get(); LokiServiceMessage lokiMessage = content.lokiMessage.get();
if (lokiMessage.getPreKeyBundleMessage() != null) { if (lokiMessage.getPreKeyBundleMessage() != null) {
Log.i(TAG, "[Loki] Received a prekey bundle from: " + envelope.getSource()); Log.d("Loki", "Received a pre key bundle from: " + envelope.getSource() + ".");
int registrationId = TextSecurePreferences.getLocalRegistrationId(context); int registrationID = TextSecurePreferences.getLocalRegistrationId(context);
if (registrationId > 0) { if (registrationID > 0) {
LokiPreKeyBundleDatabase preKeyBundleDatabase = DatabaseFactory.getLokiPreKeyBundleDatabase(context); LokiPreKeyBundleDatabase preKeyBundleDatabase = DatabaseFactory.getLokiPreKeyBundleDatabase(context);
PreKeyBundle preKeyBundle = lokiMessage.getPreKeyBundleMessage().getPreKeyBundle(registrationId); PreKeyBundle preKeyBundle = lokiMessage.getPreKeyBundleMessage().getPreKeyBundle(registrationID);
preKeyBundleDatabase.setPreKeyBundle(envelope.getSource(), preKeyBundle); preKeyBundleDatabase.setPreKeyBundle(envelope.getSource(), preKeyBundle);
} }
} }
if (lokiMessage.getAddressMessage() != null) { if (lokiMessage.getAddressMessage() != null) {
// TODO: Loki - Handle address message // TODO: Loki - Handle address message
} }
@ -825,12 +824,10 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
} }
private void handleFriendRequestIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message) { private void handleFriendRequestIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message) {
Recipient recipient = getMessageDestination(content, message); Recipient recipient = getMessageDestination(content, message);
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient); long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient);
LokiThreadFriendRequestDatabase database = DatabaseFactory.getLokiThreadFriendRequestDatabase(context); LokiThreadFriendRequestDatabase database = DatabaseFactory.getLokiThreadFriendRequestDatabase(context);
LokiFriendRequestStatus friendRequestStatus = database.getFriendRequestStatus(threadId); LokiFriendRequestStatus friendRequestStatus = database.getFriendRequestStatus(threadID);
if (envelope.isFriendRequest()) { if (envelope.isFriendRequest()) {
if (friendRequestStatus == LokiFriendRequestStatus.REQUEST_SENT) { if (friendRequestStatus == LokiFriendRequestStatus.REQUEST_SENT) {
// This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his // This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his
@ -843,34 +840,33 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
// before updating Alice's thread's friend request status to `FRIENDS`, // before updating Alice's thread's friend request status to `FRIENDS`,
// we can end up in a deadlock where both users' threads' friend request statuses are // we can end up in a deadlock where both users' threads' friend request statuses are
// `REQUEST_SENT`. // `REQUEST_SENT`.
database.setFriendRequestStatus(threadId, LokiFriendRequestStatus.FRIENDS); database.setFriendRequestStatus(threadID, LokiFriendRequestStatus.FRIENDS);
// Accept the friend request // Accept the friend request
sendEmptyMessageTo(envelope.getSource()); sendEmptyMessage(envelope.getSource());
} else if (friendRequestStatus != LokiFriendRequestStatus.FRIENDS) { } else if (friendRequestStatus != LokiFriendRequestStatus.FRIENDS) {
// Checking that the sender of the message isn't already a friend is necessary because otherwise // Checking that the sender of the message isn't already a friend is necessary because otherwise
// the following situation can occur: Alice and Bob are friends. Bob loses his database and his // the following situation can occur: Alice and Bob are friends. Bob loses his database and his
// friend request status is reset to `NONE`. Bob now sends Alice a friend // friend request status is reset to `NONE`. Bob now sends Alice a friend
// request. Alice's thread's friend request status is reset to // request. Alice's thread's friend request status is reset to
// `REQUEST_RECEIVED`. // `REQUEST_RECEIVED`.
database.setFriendRequestStatus(threadId, LokiFriendRequestStatus.REQUEST_RECEIVED); database.setFriendRequestStatus(threadID, LokiFriendRequestStatus.REQUEST_RECEIVED);
} }
} else if (friendRequestStatus != LokiFriendRequestStatus.FRIENDS) { } else if (friendRequestStatus != LokiFriendRequestStatus.FRIENDS) {
// If the thread's friend request status is not `FRIENDS`, but we're receiving a message, // If the thread's friend request status is not `FRIENDS`, but we're receiving a message,
// it must be a friend request accepted message. Declining a friend request doesn't send a message. // it must be a friend request accepted message. Declining a friend request doesn't send a message.
database.setFriendRequestStatus(threadId, LokiFriendRequestStatus.FRIENDS); database.setFriendRequestStatus(threadID, LokiFriendRequestStatus.FRIENDS);
// TODO: Send p2p details here // TODO: Send p2p details here
} }
} }
private void sendEmptyMessageTo(String pubKey) { private void sendEmptyMessage(String contactHexEncodedPublicKey) {
try { try {
SignalServiceAddress address = new SignalServiceAddress(pubKey); SignalServiceAddress address = new SignalServiceAddress(contactHexEncodedPublicKey);
SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), ""); SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), "");
Optional<UnidentifiedAccessPair> access = Optional.absent(); Optional<UnidentifiedAccessPair> access = Optional.absent();
messageSender.sendMessage(address, access, message); messageSender.sendMessage(address, access, message);
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Failed to send empty message to " + pubKey); Log.d("Loki", "Failed to send empty message to: " + contactHexEncodedPublicKey + ".");
} }
} }

View File

@ -156,7 +156,7 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException
{ {
try { try {
// rotateSenderCertificateIfNecessary(); // rotateSenderCertificateIfNecessary(); // LOKITODO: Uncomment this?
SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getAddress()); SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getAddress());
Optional<byte[]> profileKey = getProfileKey(message.getIndividualRecipient()); Optional<byte[]> profileKey = getProfileKey(message.getIndividualRecipient());

View File

@ -56,16 +56,14 @@ public class RefreshPreKeysJob extends BaseJob implements InjectableType {
public void onRun() throws IOException { public void onRun() throws IOException {
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) { if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
Log.i(TAG, "Already have a signed pre key registered."); Log.i(TAG, "Already have a signed pre key registered.");
return; } else {
}
Log.i(TAG, "Registering new signed pre key..."); Log.i(TAG, "Registering new signed pre key...");
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context); IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
PreKeyUtil.generateSignedPreKey(context, identityKey, true); PreKeyUtil.generateSignedPreKey(context, identityKey, true);
TextSecurePreferences.setSignedPreKeyRegistered(context, true); TextSecurePreferences.setSignedPreKeyRegistered(context, true);
ApplicationContext.getInstance(context).getJobManager().add(new CleanPreKeysJob()); ApplicationContext.getInstance(context).getJobManager().add(new CleanPreKeysJob());
} }
}
/* Loki - Original code /* Loki - Original code
@Override @Override

View File

@ -8,50 +8,42 @@ import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.whispersystems.libsignal.state.PreKeyRecord import org.whispersystems.libsignal.state.PreKeyRecord
/**
* A database for associating pre key records to contact public keys.
*/
class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
companion object { companion object {
private val tableName = "loki_contact_pre_key_database" private val tableName = "loki_contact_pre_key_database"
private val preKeyId = "pre_key_id" private val preKeyID = "pre_key_id"
private val pubKey = "pub_key" private val hexEncodedPublicKey = "public_key"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($preKeyID INTEGER PRIMARY KEY, $hexEncodedPublicKey TEXT);"
@JvmStatic
val createTableCommand = "CREATE TABLE $tableName ($preKeyId INTEGER PRIMARY KEY, $pubKey TEXT);"
} }
fun hasPreKey(pubKey: String): Boolean { fun hasPreKey(hexEncodedPublicKey: String): Boolean {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor -> return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf(hexEncodedPublicKey)) { cursor ->
cursor.count > 0 cursor.count > 0
} ?: false } ?: false
} }
fun getPreKey(pubKey: String): PreKeyRecord? { fun getPreKey(hexEncodedPublicKey: String): PreKeyRecord? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor -> return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf(hexEncodedPublicKey)) { cursor ->
val preKeyId = cursor.getInt(preKeyId) val preKeyID = cursor.getInt(preKeyID)
PreKeyUtil.loadPreKey(context, preKeyId) PreKeyUtil.loadPreKey(context, preKeyID)
} }
} }
fun getOrCreatePreKey(pubKey: String): PreKeyRecord { fun getOrCreatePreKey(hexEncodedPublicKey: String): PreKeyRecord {
return getPreKey(pubKey) ?: generateAndStorePreKey(pubKey) return getPreKey(hexEncodedPublicKey) ?: generateAndStorePreKey(hexEncodedPublicKey)
} }
private fun generateAndStorePreKey(pubKey: String): PreKeyRecord { private fun generateAndStorePreKey(hexEncodedPublicKey: String): PreKeyRecord {
val records = PreKeyUtil.generatePreKeys(context, 1) val preKeyRecords = PreKeyUtil.generatePreKeys(context, 1)
PreKeyUtil.storePreKeyRecords(context, records) PreKeyUtil.storePreKeyRecords(context, preKeyRecords)
val record = preKeyRecords.first()
val record = records.first()
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val values = ContentValues() val values = ContentValues()
values.put(Companion.pubKey, pubKey) values.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
values.put(preKeyId, record.id) values.put(preKeyID, record.id)
database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE) database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE)
return record return record
} }

View File

@ -9,30 +9,25 @@ class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHe
companion object { companion object {
private val tableName = "loki_sms_friend_request_database" private val tableName = "loki_sms_friend_request_database"
private val smsId = "_id" private val messageID = "message_id"
private val isFriendRequest = "is_friend_request" private val isFriendRequest = "is_friend_request"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $isFriendRequest INTEGER DEFAULT 0);"
@JvmStatic
val createTableCommand = "CREATE TABLE $tableName ($smsId INTEGER PRIMARY KEY, $isFriendRequest INTEGER DEFAULT 0);"
} }
fun getIsFriendRequest(messageId: Long): Boolean { fun getIsFriendRequest(messageID: Long): Boolean {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(tableName, ID_WHERE, arrayOf( messageId.toString() )) { cursor -> return database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor ->
val rawIsFriendRequest = cursor.getInt(isFriendRequest) val rawIsFriendRequest = cursor.getInt(isFriendRequest)
rawIsFriendRequest == 1 rawIsFriendRequest == 1
} ?: false } ?: false
} }
fun setIsFriendRequest(messageId: Long, isFriendRequest: Boolean) { fun setIsFriendRequest(messageID: Long, isFriendRequest: Boolean) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val rawIsFriendRequest = if (isFriendRequest) 1 else 0 val rawIsFriendRequest = if (isFriendRequest) 1 else 0
val contentValues = ContentValues() val contentValues = ContentValues()
contentValues.put(smsId, messageId) contentValues.put(Companion.messageID, messageID)
contentValues.put(Companion.isFriendRequest, rawIsFriendRequest) contentValues.put(Companion.isFriendRequest, rawIsFriendRequest)
database.insertOrUpdate(tableName, contentValues, "${Companion.messageID} = ?", arrayOf( messageID.toString() ))
database.insertOrUpdate(tableName, contentValues, ID_WHERE, arrayOf( messageId.toString() ))
} }
} }

View File

@ -19,101 +19,86 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) :
companion object { companion object {
private val tableName = "loki_pre_key_bundle_database" private val tableName = "loki_pre_key_bundle_database"
private val pubKey = "pub_key" private val hexEncodedPublicKey = "public_key"
private val preKeyId = "pre_key_id" private val preKeyID = "pre_key_id"
private val preKeyPublic = "pre_key_public" private val preKeyPublic = "pre_key_public"
private val signedPreKeyId = "signed_pre_key_id" private val signedPreKeyID = "signed_pre_key_id"
private val signedPreKeyPublic = "signed_pre_key_public" private val signedPreKeyPublic = "signed_pre_key_public"
private val signedPreKeySignature = "signed_pre_key_signature" private val signedPreKeySignature = "signed_pre_key_signature"
private val identityKey = "identity_key" private val identityKey = "identity_key"
private val deviceId = "device_id" private val deviceID = "device_id"
private val registrationId = "registration_id" private val registrationID = "registration_id"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName (" + "$hexEncodedPublicKey TEXT PRIMARY KEY," + "$preKeyID INTEGER," +
@JvmStatic "$preKeyPublic TEXT NOT NULL," + "$signedPreKeyID INTEGER," + "$signedPreKeyPublic TEXT NOT NULL," +
val createTableCommand = "CREATE TABLE $tableName (" + "$signedPreKeySignature TEXT," + "$identityKey TEXT NOT NULL," + "$deviceID INTEGER," + "$registrationID INTEGER" + ");"
"$pubKey TEXT PRIMARY KEY," +
"$preKeyId INTEGER," +
"$preKeyPublic TEXT NOT NULL," +
"$signedPreKeyId INTEGER," +
"$signedPreKeyPublic TEXT NOT NULL," +
"$signedPreKeySignature TEXT," +
"$identityKey TEXT NOT NULL," +
"$deviceId INTEGER," +
"$registrationId INTEGER" +
");"
} }
/** /**
* Generate a `PreKeyBundle` for the given contact. * Generate a `PreKeyBundle` for the given contact.
* This generated bundle shouldn't be stored locally since this is used to generate bundles to send to other users. * This generated bundle shouldn't be stored locally since this is used to generate bundles to send to other users.
* *
* @param pubKey String The hex encoded public key of the contact. * @param hexEncodedPublicKey String The hex encoded public key of the contact.
* @return PreKeyBundle? A pre key bundle or `null` if something went wrong. * @return PreKeyBundle? A pre key bundle or `null` if something went wrong.
*/ */
fun generatePreKeyBundle(pubKey: String): PreKeyBundle? { fun generatePreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context) val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context)
val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null
val preKeyRecord = DatabaseFactory.getLokiContactPreKeyDatabase(context).getOrCreatePreKey(hexEncodedPublicKey)
val preKeyRecord = DatabaseFactory.getLokiContactPreKeyDatabase(context).getOrCreatePreKey(pubKey) val registrationID = TextSecurePreferences.getLocalRegistrationId(context)
val registrationId = TextSecurePreferences.getLocalRegistrationId(context) if (registrationID == 0) return null
if (registrationId == 0) return null val deviceID = SignalServiceAddress.DEFAULT_DEVICE_ID
return PreKeyBundle(registrationID, deviceID,preKeyRecord.id, preKeyRecord.keyPair.publicKey, signedPreKey.id, signedPreKey.keyPair.publicKey, signedPreKey.signature, identityKeyPair.publicKey)
val deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID
return PreKeyBundle(registrationId, deviceId,preKeyRecord.id, preKeyRecord.keyPair.publicKey, signedPreKey.id, signedPreKey.keyPair.publicKey, signedPreKey.signature, identityKeyPair.publicKey)
} }
/** /**
* Get the `PreKeyBundle` associated with the given contact. * Get the `PreKeyBundle` associated with the given contact.
* *
* @param pubKey String The hex encoded public key of the contact. * @param hexEncodedPublicKey String The hex encoded public key of the contact.
* @return PreKeyBundle? The pre key bundle or `null` if it doesn't exist. * @return PreKeyBundle? The pre key bundle or `null` if it doesn't exist.
*/ */
fun getPreKeyBundle(pubKey: String): PreKeyBundle? { fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
val database = databaseHelper.readableDatabase val database = databaseHelper.readableDatabase
return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor -> return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor ->
val registrationId = cursor.getInt(registrationId) val registrationID = cursor.getInt(registrationID)
val deviceId = cursor.getInt(deviceId) val deviceID = cursor.getInt(deviceID)
val preKeyId = cursor.getInt(preKeyId) val preKeyID = cursor.getInt(preKeyID)
val preKey = Curve.decodePoint(cursor.getBase64EncodedData(preKeyPublic), 0) val preKey = Curve.decodePoint(cursor.getBase64EncodedData(preKeyPublic), 0)
val signedPreKeyId = cursor.getInt(signedPreKeyId) val signedPreKeyID = cursor.getInt(signedPreKeyID)
val signedPreKey = Curve.decodePoint(cursor.getBase64EncodedData(signedPreKeyPublic), 0) val signedPreKey = Curve.decodePoint(cursor.getBase64EncodedData(signedPreKeyPublic), 0)
val signedPreKeySignature = cursor.getBase64EncodedData(signedPreKeySignature) val signedPreKeySignature = cursor.getBase64EncodedData(signedPreKeySignature)
val identityKey = IdentityKey(cursor.getBase64EncodedData(identityKey), 0) val identityKey = IdentityKey(cursor.getBase64EncodedData(identityKey), 0)
PreKeyBundle(registrationID, deviceID, preKeyID, preKey, signedPreKeyID, signedPreKey, signedPreKeySignature, identityKey)
PreKeyBundle(registrationId, deviceId, preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, identityKey)
} }
} }
/** /**
* Set the `PreKeyBundle` for the given contact. * Set the `PreKeyBundle` for the given contact.
* *
* @param pubKey String The hex encoded public key of the contact. * @param hexEncodedPublicKey String The hex encoded public key of the contact.
* @param preKeyBundle PreKeyBundle The pre key bundle. * @param preKeyBundle PreKeyBundle The pre key bundle.
*/ */
fun setPreKeyBundle(pubKey: String, preKeyBundle: PreKeyBundle) { fun setPreKeyBundle(hexEncodedPublicKey: String, preKeyBundle: PreKeyBundle) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val contentValues = ContentValues() val contentValues = ContentValues()
contentValues.put(registrationId, preKeyBundle.registrationId) contentValues.put(registrationID, preKeyBundle.registrationId)
contentValues.put(deviceId, preKeyBundle.deviceId) contentValues.put(deviceID, preKeyBundle.deviceId)
contentValues.put(preKeyId, preKeyBundle.preKeyId) contentValues.put(preKeyID, preKeyBundle.preKeyId)
contentValues.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize())) contentValues.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize()))
contentValues.put(signedPreKeyId, preKeyBundle.signedPreKeyId) contentValues.put(signedPreKeyID, preKeyBundle.signedPreKeyId)
contentValues.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize())) contentValues.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize()))
contentValues.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature)) contentValues.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature))
contentValues.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize())) contentValues.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize()))
contentValues.put(Companion.pubKey, pubKey) contentValues.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE) database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
} }
/** /**
* Remove the `PreKeyBundle` for the given contact. * Remove the `PreKeyBundle` for the given contact.
* *
* @param pubKey String The hex encoded public key of the contact. * @param hexEncodedPublicKey String The hex encoded public key of the contact.
*/ */
fun removePreKeyBundle(pubKey: String) { fun removePreKeyBundle(hexEncodedPublicKey: String) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
database.delete(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) database.delete(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
} }
} }

View File

@ -11,15 +11,15 @@ class LokiPreKeyBundleStore(val context: Context) : LokiPreKeyBundleStoreProtoco
private val lock = Object() private val lock = Object()
} }
override fun getPreKeyBundle(pubKey: String): PreKeyBundle? { override fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
synchronized(lock) { synchronized(lock) {
return DatabaseFactory.getLokiPreKeyBundleDatabase(context).getPreKeyBundle(pubKey) return DatabaseFactory.getLokiPreKeyBundleDatabase(context).getPreKeyBundle(hexEncodedPublicKey)
} }
} }
override fun removePreKeyBundle(pubKey: String) { override fun removePreKeyBundle(hexEncodedPublicKey: String) {
synchronized(lock) { synchronized(lock) {
DatabaseFactory.getLokiPreKeyBundleDatabase(context).removePreKeyBundle(pubKey) DatabaseFactory.getLokiPreKeyBundleDatabase(context).removePreKeyBundle(hexEncodedPublicKey)
} }
} }
} }

View File

@ -9,16 +9,14 @@ class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHel
companion object { companion object {
private val tableName = "loki_thread_friend_request_database" private val tableName = "loki_thread_friend_request_database"
private val threadId = "_id" private val threadID = "thread_id"
private val friendRequestStatus = "friend_request_status" private val friendRequestStatus = "friend_request_status"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($threadID INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);"
@JvmStatic
val createTableCommand = "CREATE TABLE $tableName ($threadId INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);"
} }
fun getFriendRequestStatus(threadId: Long): LokiFriendRequestStatus { fun getFriendRequestStatus(threadID: Long): LokiFriendRequestStatus {
val db = databaseHelper.readableDatabase val db = databaseHelper.readableDatabase
val result = db.get(tableName, ID_WHERE, arrayOf( threadId.toString() )) { cursor -> val result = db.get(tableName, "${Companion.threadID} = ?", arrayOf( threadID.toString() )) { cursor ->
cursor.getInt(friendRequestStatus) cursor.getInt(friendRequestStatus)
} }
return if (result != null) { return if (result != null) {
@ -28,12 +26,12 @@ class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHel
} }
} }
fun setFriendRequestStatus(threadId: Long, status: LokiFriendRequestStatus) { fun setFriendRequestStatus(threadID: Long, status: LokiFriendRequestStatus) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val contentValues = ContentValues(1) val contentValues = ContentValues(1)
contentValues.put(Companion.threadId, threadId) contentValues.put(Companion.threadID, threadID)
contentValues.put(friendRequestStatus, status.rawValue) contentValues.put(friendRequestStatus, status.rawValue)
database.insertOrUpdate(tableName, contentValues, ID_WHERE, arrayOf( threadId.toString() )) database.insertOrUpdate(tableName, contentValues, "${Companion.threadID} = ?", arrayOf( threadID.toString() ))
notifyConversationListListeners() notifyConversationListListeners()
} }
} }