Convert SignalService, Database, Group, Payment, and other remaining protos to wire.

This commit is contained in:
Cody Henthorne
2023-09-18 15:32:43 -04:00
committed by Alex Hart
parent a6b7d0bcc5
commit efbd5cab85
267 changed files with 7100 additions and 7214 deletions

View File

@@ -1,8 +1,8 @@
package org.thoughtcrime.securesms.keyvalue
import com.google.protobuf.InvalidProtocolBufferException
import org.thoughtcrime.securesms.conversation.colors.ChatColors
import org.thoughtcrime.securesms.database.model.databaseprotos.ChatColor
import java.io.IOException
internal class ChatColorsValues internal constructor(store: KeyValueStore) : SignalStoreValues(store) {
@@ -37,14 +37,14 @@ internal class ChatColorsValues internal constructor(store: KeyValueStore) : Sig
var chatColors: ChatColors?
get() = getBlob(KEY_CHAT_COLORS, null)?.let { bytes ->
try {
ChatColors.forChatColor(chatColorsId, ChatColor.parseFrom(bytes))
} catch (e: InvalidProtocolBufferException) {
ChatColors.forChatColor(chatColorsId, ChatColor.ADAPTER.decode(bytes))
} catch (e: IOException) {
null
}
}
set(value) {
if (value != null) {
putBlob(KEY_CHAT_COLORS, value.serialize().toByteArray())
putBlob(KEY_CHAT_COLORS, value.serialize().encode())
chatColorsId = value.id
} else {
remove(KEY_CHAT_COLORS)

View File

@@ -240,7 +240,7 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
fun setExpiredBadge(badge: Badge?) {
if (badge != null) {
putBlob(EXPIRED_BADGE, Badges.toDatabaseBadge(badge).toByteArray())
putBlob(EXPIRED_BADGE, Badges.toDatabaseBadge(badge).encode())
} else {
remove(EXPIRED_BADGE)
}
@@ -249,12 +249,12 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
fun getExpiredBadge(): Badge? {
val badgeBytes = getBlob(EXPIRED_BADGE, null) ?: return null
return Badges.fromDatabaseBadge(BadgeList.Badge.parseFrom(badgeBytes))
return Badges.fromDatabaseBadge(BadgeList.Badge.ADAPTER.decode(badgeBytes))
}
fun setExpiredGiftBadge(badge: Badge?) {
if (badge != null) {
putBlob(EXPIRED_GIFT_BADGE, Badges.toDatabaseBadge(badge).toByteArray())
putBlob(EXPIRED_GIFT_BADGE, Badges.toDatabaseBadge(badge).encode())
} else {
remove(EXPIRED_GIFT_BADGE)
}
@@ -263,7 +263,7 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
fun getExpiredGiftBadge(): Badge? {
val badgeBytes = getBlob(EXPIRED_GIFT_BADGE, null) ?: return null
return Badges.fromDatabaseBadge(BadgeList.Badge.parseFrom(badgeBytes))
return Badges.fromDatabaseBadge(BadgeList.Badge.ADAPTER.decode(badgeBytes))
}
fun getLastKeepAliveLaunchTime(): Long {

View File

@@ -2,9 +2,6 @@ package org.thoughtcrime.securesms.keyvalue;
import androidx.annotation.NonNull;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.core.util.logging.Log;
import org.signal.libsignal.zkgroup.InvalidInputException;
import org.signal.libsignal.zkgroup.auth.AuthCredentialWithPniResponse;
@@ -15,11 +12,15 @@ import org.thoughtcrime.securesms.database.model.databaseprotos.TemporalAuthCred
import org.thoughtcrime.securesms.groups.GroupsV2Authorization;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import okio.ByteString;
public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Authorization.ValueCache {
@@ -76,17 +77,17 @@ public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Auth
}
try {
TemporalAuthCredentialResponses temporalCredentials = TemporalAuthCredentialResponses.parseFrom(credentialBlob);
HashMap<Long, T> result = new HashMap<>(temporalCredentials.getCredentialResponseCount());
TemporalAuthCredentialResponses temporalCredentials = TemporalAuthCredentialResponses.ADAPTER.decode(credentialBlob);
HashMap<Long, T> result = new HashMap<>(temporalCredentials.credentialResponse.size());
for (TemporalAuthCredentialResponse credential : temporalCredentials.getCredentialResponseList()) {
result.put(credential.getDate(), factory.apply(credential.getAuthCredentialResponse().toByteArray()));
for (TemporalAuthCredentialResponse credential : temporalCredentials.credentialResponse) {
result.put(credential.date, factory.apply(credential.authCredentialResponse.toByteArray()));
}
Log.i(TAG, String.format(Locale.US, "Loaded %d credentials from local storage", result.size()));
return result;
} catch (InvalidProtocolBufferException | InvalidInputException e) {
} catch (IOException | InvalidInputException e) {
throw new AssertionError(e);
}
}
@@ -98,16 +99,18 @@ public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Auth
}
private <T extends ByteArray> void write(@NonNull String key, @NonNull Map<Long, T> values) {
TemporalAuthCredentialResponses.Builder builder = TemporalAuthCredentialResponses.newBuilder();
TemporalAuthCredentialResponses.Builder builder = new TemporalAuthCredentialResponses.Builder();
List<TemporalAuthCredentialResponse> respones = new ArrayList<>();
for (Map.Entry<Long, T> entry : values.entrySet()) {
builder.addCredentialResponse(TemporalAuthCredentialResponse.newBuilder()
.setDate(entry.getKey())
.setAuthCredentialResponse(ByteString.copyFrom(entry.getValue().serialize())));
respones.add(new TemporalAuthCredentialResponse.Builder()
.date(entry.getKey())
.authCredentialResponse(ByteString.of(entry.getValue().serialize()))
.build());
}
store.beginWrite()
.putBlob(key, builder.build().toByteArray())
.putBlob(key, builder.credentialResponse(respones).build().encode())
.commit();
Log.i(TAG, String.format(Locale.US, "Written %d credentials to local storage", values.size()));

View File

@@ -5,7 +5,6 @@ import androidx.annotation.WorkerThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.google.protobuf.InvalidProtocolBufferException
import com.mobilecoin.lib.Mnemonics
import com.mobilecoin.lib.exceptions.BadMnemonicException
import org.signal.core.util.logging.Log
@@ -23,6 +22,7 @@ import org.thoughtcrime.securesms.storage.StorageSyncHelper
import org.thoughtcrime.securesms.util.FeatureFlags
import org.thoughtcrime.securesms.util.Util
import org.whispersystems.signalservice.api.payments.Money
import java.io.IOException
import java.math.BigDecimal
import java.util.Arrays
import java.util.Currency
@@ -292,12 +292,12 @@ internal class PaymentsValues internal constructor(store: KeyValueStore) : Signa
}
fun mobileCoinLatestFullLedger(): MobileCoinLedgerWrapper {
val blob = store.getBlob(MOB_LEDGER, null) ?: return MobileCoinLedgerWrapper(MobileCoinLedger.getDefaultInstance())
val blob = store.getBlob(MOB_LEDGER, null) ?: return MobileCoinLedgerWrapper(MobileCoinLedger())
return try {
MobileCoinLedgerWrapper(MobileCoinLedger.parseFrom(blob))
} catch (e: InvalidProtocolBufferException) {
MobileCoinLedgerWrapper(MobileCoinLedger.ADAPTER.decode(blob))
} catch (e: IOException) {
Log.w(TAG, "Bad cached ledger, clearing", e)
setMobileCoinFullLedger(MobileCoinLedgerWrapper(MobileCoinLedger.getDefaultInstance()))
setMobileCoinFullLedger(MobileCoinLedgerWrapper(MobileCoinLedger()))
throw AssertionError(e)
}
}
@@ -351,7 +351,7 @@ internal class PaymentsValues internal constructor(store: KeyValueStore) : Signa
.putBoolean(USER_CONFIRMED_MNEMONIC, true)
.commit()
liveMobileCoinLedger.postValue(MobileCoinLedgerWrapper(MobileCoinLedger.getDefaultInstance()))
liveMobileCoinLedger.postValue(MobileCoinLedgerWrapper(MobileCoinLedger()))
StorageSyncHelper.scheduleSyncForDataChange()
return WalletRestoreResult.ENTROPY_CHANGED

View File

@@ -7,6 +7,6 @@ import org.thoughtcrime.securesms.database.model.databaseprotos.PendingChangeNum
* Serialize [PendingChangeNumberMetadata]
*/
object PendingChangeNumberMetadataSerializer : ByteSerializer<PendingChangeNumberMetadata> {
override fun serialize(data: PendingChangeNumberMetadata): ByteArray = data.toByteArray()
override fun deserialize(data: ByteArray): PendingChangeNumberMetadata = PendingChangeNumberMetadata.parseFrom(data)
override fun serialize(data: PendingChangeNumberMetadata): ByteArray = data.encode()
override fun deserialize(data: ByteArray): PendingChangeNumberMetadata = PendingChangeNumberMetadata.ADAPTER.decode(data)
}

View File

@@ -3,12 +3,11 @@ package org.thoughtcrime.securesms.keyvalue;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.core.util.ByteSerializer;
import org.signal.core.util.StringSerializer;
import org.thoughtcrime.securesms.database.model.databaseprotos.SignalStoreList;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -69,14 +68,14 @@ abstract class SignalStoreValues {
}
try {
SignalStoreList signalStoreList = SignalStoreList.parseFrom(blob);
SignalStoreList signalStoreList = SignalStoreList.ADAPTER.decode(blob);
return signalStoreList.getContentsList()
return signalStoreList.contents
.stream()
.map(serializer::deserialize)
.collect(Collectors.toList());
} catch (InvalidProtocolBufferException e) {
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
@@ -110,12 +109,12 @@ abstract class SignalStoreValues {
}
<T> void putList(@NonNull String key, @NonNull List<T> values, @NonNull StringSerializer<T> serializer) {
putBlob(key, SignalStoreList.newBuilder()
.addAllContents(values.stream()
.map(serializer::serialize)
.collect(Collectors.toList()))
.build()
.toByteArray());
putBlob(key, new SignalStoreList.Builder()
.contents(values.stream()
.map(serializer::serialize)
.collect(Collectors.toList()))
.build()
.encode());
}
void remove(@NonNull String key) {

View File

@@ -6,14 +6,13 @@ import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.database.model.databaseprotos.Wallpaper;
import org.thoughtcrime.securesms.wallpaper.ChatWallpaper;
import org.thoughtcrime.securesms.wallpaper.ChatWallpaperFactory;
import org.thoughtcrime.securesms.wallpaper.WallpaperStorage;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@@ -40,12 +39,12 @@ public final class WallpaperValues extends SignalStoreValues {
Wallpaper currentWallpaper = getCurrentWallpaper();
Uri currentUri = null;
if (currentWallpaper != null && currentWallpaper.hasFile()) {
currentUri = Uri.parse(currentWallpaper.getFile().getUri());
if (currentWallpaper != null && currentWallpaper.file_ != null) {
currentUri = Uri.parse(currentWallpaper.file_.uri);
}
if (wallpaper != null) {
putBlob(KEY_WALLPAPER, wallpaper.serialize().toByteArray());
putBlob(KEY_WALLPAPER, wallpaper.serialize().encode());
} else {
getStore().beginWrite().remove(KEY_WALLPAPER).apply();
}
@@ -74,10 +73,10 @@ public final class WallpaperValues extends SignalStoreValues {
if (currentWallpaper != null) {
putBlob(KEY_WALLPAPER,
currentWallpaper.toBuilder()
.setDimLevelInDarkTheme(enabled ? 0.2f : 0)
currentWallpaper.newBuilder()
.dimLevelInDarkTheme(enabled ? 0.2f : 0)
.build()
.toByteArray());
.encode());
} else {
throw new IllegalStateException("No wallpaper currently set!");
}
@@ -91,8 +90,8 @@ public final class WallpaperValues extends SignalStoreValues {
public @Nullable Uri getWallpaperUri() {
Wallpaper currentWallpaper = getCurrentWallpaper();
if (currentWallpaper != null && currentWallpaper.hasFile()) {
return Uri.parse(currentWallpaper.getFile().getUri());
if (currentWallpaper != null && currentWallpaper.file_ != null) {
return Uri.parse(currentWallpaper.file_.uri);
} else {
return null;
}
@@ -103,8 +102,8 @@ public final class WallpaperValues extends SignalStoreValues {
if (serialized != null) {
try {
return Wallpaper.parseFrom(serialized);
} catch (InvalidProtocolBufferException e) {
return Wallpaper.ADAPTER.decode(serialized);
} catch (IOException e) {
Log.w(TAG, "Invalid proto stored for wallpaper!");
return null;
}