]> nmode's Git Repositories - signal-cli/commitdiff
Don't store self profile key in recipient store
authorAsamK <asamk@gmx.de>
Sun, 12 Nov 2023 12:13:58 +0000 (13:13 +0100)
committerAsamK <asamk@gmx.de>
Tue, 21 Nov 2023 19:40:10 +0000 (20:40 +0100)
lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java
lib/src/main/java/org/asamk/signal/manager/storage/profiles/ProfileStore.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/SelfProfileKeyProvider.java [new file with mode: 0644]

index 077ff732f20757fc71aab746f6a39f415b146424..60b53b24e8735d71afd3160b9164a96bc3df223d 100644 (file)
@@ -282,7 +282,6 @@ public class SignalAccount implements Closeable {
         getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
         this.password = password;
         this.profileKey = profileKey;
-        getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
         this.encryptedDeviceName = encryptedDeviceName;
         this.aciAccountData.setIdentityKeyPair(aciIdentity);
         this.pniAccountData.setIdentityKeyPair(pniIdentity);
@@ -654,14 +653,11 @@ public class SignalAccount implements Closeable {
             // Old config file, creating new profile key
             setProfileKey(KeyUtils.createProfileKey());
         }
-        getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
 
         if (previousStorageVersion < 5) {
             final var legacyRecipientsStoreFile = new File(userPath, "recipients-store");
             if (legacyRecipientsStoreFile.exists()) {
                 LegacyRecipientStore2.migrate(legacyRecipientsStoreFile, getRecipientStore());
-                // Ensure our profile key is stored in profile store
-                getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
             }
         }
         if (previousStorageVersion < 6) {
@@ -1185,12 +1181,11 @@ public class SignalAccount implements Closeable {
     }
 
     public RecipientStore getRecipientStore() {
-        return getOrCreate(() -> recipientStore, () -> {
-            recipientStore = new RecipientStore(this::mergeRecipients,
-                    this::getSelfRecipientAddress,
-                    getAccountDatabase());
-            getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
-        });
+        return getOrCreate(() -> recipientStore,
+                () -> recipientStore = new RecipientStore(this::mergeRecipients,
+                        this::getSelfRecipientAddress,
+                        this::getProfileKey,
+                        getAccountDatabase()));
     }
 
     public ProfileStore getProfileStore() {
@@ -1547,7 +1542,6 @@ public class SignalAccount implements Closeable {
         }
         this.profileKey = profileKey;
         save();
-        getProfileStore().storeSelfProfileKey(getSelfRecipientId(), getProfileKey());
     }
 
     public byte[] getSelfUnidentifiedAccessKey() {
index 8128435dce42143f5e96db252bfd9912f5c9b7ba..5d3a95a8ff0f0583dc6d7e2314421db47b9554d3 100644 (file)
@@ -15,8 +15,6 @@ public interface ProfileStore {
 
     void storeProfile(RecipientId recipientId, Profile profile);
 
-    void storeSelfProfileKey(RecipientId recipientId, ProfileKey profileKey);
-
     void storeProfileKey(RecipientId recipientId, ProfileKey profileKey);
 
     void storeExpiringProfileKeyCredential(
index e87dd6bb0cf6eeab77c089f7c87dce9aa497a6b8..9a016136638082a00c2a05097a3833d198cf3fa5 100644 (file)
@@ -42,6 +42,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
 
     private final RecipientMergeHandler recipientMergeHandler;
     private final SelfAddressProvider selfAddressProvider;
+    private final SelfProfileKeyProvider selfProfileKeyProvider;
     private final Database database;
 
     private final Object recipientsLock = new Object();
@@ -88,10 +89,12 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
     public RecipientStore(
             final RecipientMergeHandler recipientMergeHandler,
             final SelfAddressProvider selfAddressProvider,
+            final SelfProfileKeyProvider selfProfileKeyProvider,
             final Database database
     ) {
         this.recipientMergeHandler = recipientMergeHandler;
         this.selfAddressProvider = selfAddressProvider;
+        this.selfProfileKeyProvider = selfProfileKeyProvider;
         this.database = database;
     }
 
@@ -363,6 +366,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                 WHERE (r.number IS NOT NULL OR r.uuid IS NOT NULL) AND %s
                 """
         ).formatted(TABLE_RECIPIENT, sqlWhere.isEmpty() ? "TRUE" : String.join(" AND ", sqlWhere));
+        final var selfServiceId = selfAddressProvider.getSelfAddress().serviceId();
         try (final var connection = database.getConnection()) {
             try (final var statement = connection.prepareStatement(sql)) {
                 if (blocked.isPresent()) {
@@ -371,7 +375,14 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                 try (var result = Utils.executeQueryForStream(statement, this::getRecipientFromResultSet)) {
                     return result.filter(r -> name.isEmpty() || (
                             r.getContact() != null && name.get().equals(r.getContact().getName())
-                    ) || (r.getProfile() != null && name.get().equals(r.getProfile().getDisplayName()))).toList();
+                    ) || (r.getProfile() != null && name.get().equals(r.getProfile().getDisplayName()))).map(r -> {
+                        if (r.getAddress().serviceId().equals(selfServiceId)) {
+                            return Recipient.newBuilder(r)
+                                    .withProfileKey(selfProfileKeyProvider.getSelfProfileKey())
+                                    .build();
+                        }
+                        return r;
+                    }).toList();
                 }
             }
         } catch (SQLException e) {
@@ -416,10 +427,14 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                 WHERE r.uuid IS NOT NULL AND r.profile_key IS NOT NULL
                 """
         ).formatted(TABLE_RECIPIENT);
+        final var selfServiceId = selfAddressProvider.getSelfAddress().serviceId().orElse(null);
         try (final var connection = database.getConnection()) {
             try (final var statement = connection.prepareStatement(sql)) {
                 return Utils.executeQueryForStream(statement, resultSet -> {
                     final var serviceId = ServiceId.parseOrThrow(resultSet.getBytes("uuid"));
+                    if (serviceId.equals(selfServiceId)) {
+                        return new Pair<>(serviceId, selfProfileKeyProvider.getSelfProfileKey());
+                    }
                     final var profileKey = getProfileKeyFromResultSet(resultSet);
                     return new Pair<>(serviceId, profileKey);
                 }).filter(Objects::nonNull).collect(Collectors.toMap(Pair::first, Pair::second));
@@ -463,6 +478,10 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
 
     @Override
     public ProfileKey getProfileKey(final RecipientId recipientId) {
+        final var selfRecipientId = resolveRecipient(selfAddressProvider.getSelfAddress());
+        if (recipientId.equals(selfRecipientId)) {
+            return selfProfileKeyProvider.getSelfProfileKey();
+        }
         try (final var connection = database.getConnection()) {
             return getProfileKey(connection, recipientId);
         } catch (SQLException e) {
@@ -488,15 +507,6 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
         }
     }
 
-    @Override
-    public void storeSelfProfileKey(final RecipientId recipientId, final ProfileKey profileKey) {
-        try (final var connection = database.getConnection()) {
-            storeProfileKey(connection, recipientId, profileKey, false);
-        } catch (SQLException e) {
-            throw new RuntimeException("Failed update recipient store", e);
-        }
-    }
-
     @Override
     public void storeProfileKey(RecipientId recipientId, final ProfileKey profileKey) {
         try (final var connection = database.getConnection()) {
diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/SelfProfileKeyProvider.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/SelfProfileKeyProvider.java
new file mode 100644 (file)
index 0000000..c3f7f00
--- /dev/null
@@ -0,0 +1,8 @@
+package org.asamk.signal.manager.storage.recipients;
+
+import org.signal.libsignal.zkgroup.profiles.ProfileKey;
+
+public interface SelfProfileKeyProvider {
+
+    ProfileKey getSelfProfileKey();
+}