diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/KeysDynamoDb.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/KeysDynamoDb.java index a486d430f..c95cbb71f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/KeysDynamoDb.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/KeysDynamoDb.java @@ -20,6 +20,7 @@ import io.micrometer.core.instrument.DistributionSummary; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Timer; import org.whispersystems.textsecuregcm.entities.PreKey; +import org.whispersystems.textsecuregcm.util.UUIDUtil; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -174,10 +175,7 @@ public class KeysDynamoDb extends AbstractDynamoDbStore implements PreKeyStore { } private static byte[] getPartitionKey(final UUID accountUuid) { - final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); - byteBuffer.putLong(accountUuid.getMostSignificantBits()); - byteBuffer.putLong(accountUuid.getLeastSignificantBits()); - return byteBuffer.array(); + return UUIDUtil.toBytes(accountUuid); } private static byte[] getSortKey(final long deviceId, final long keyId) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java index 149fa3e6e..204b38ae6 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesDynamoDb.java @@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Timer; import org.apache.commons.lang3.StringUtils; import org.whispersystems.textsecuregcm.entities.MessageProtos; import org.whispersystems.textsecuregcm.entities.OutgoingMessageEntity; +import org.whispersystems.textsecuregcm.util.UUIDUtil; import javax.annotation.Nonnull; import java.nio.ByteBuffer; @@ -93,7 +94,7 @@ public class MessagesDynamoDb extends AbstractDynamoDbStore { item.withString(KEY_SOURCE, message.getSource()); } if (message.hasSourceUuid()) { - item.withBinary(KEY_SOURCE_UUID, convertUuidToBytes(UUID.fromString(message.getSourceUuid()))); + item.withBinary(KEY_SOURCE_UUID, UUIDUtil.toBytes(UUID.fromString(message.getSourceUuid()))); } if (message.hasSourceDevice()) { item.withInt(KEY_SOURCE_DEVICE, message.getSourceDevice()); @@ -242,7 +243,7 @@ public class MessagesDynamoDb extends AbstractDynamoDbStore { } private static byte[] convertPartitionKey(final UUID destinationAccountUuid) { - return convertUuidToBytes(destinationAccountUuid); + return UUIDUtil.toBytes(destinationAccountUuid); } private static byte[] convertSortKey(final long destinationDeviceId, final long serverTimestamp, final UUID messageUuid) { @@ -274,29 +275,19 @@ public class MessagesDynamoDb extends AbstractDynamoDbStore { } private static byte[] convertLocalIndexMessageUuidSortKey(final UUID messageUuid) { - return convertUuidToBytes(messageUuid); + return UUIDUtil.toBytes(messageUuid); } private static UUID convertLocalIndexMessageUuidSortKey(final byte[] bytes) { return convertUuidFromBytes(bytes, "local index message uuid sort key"); } - private static byte[] convertUuidToBytes(final UUID uuid) { - ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); - byteBuffer.putLong(uuid.getMostSignificantBits()); - byteBuffer.putLong(uuid.getLeastSignificantBits()); - return byteBuffer.array(); - } - private static UUID convertUuidFromBytes(final byte[] bytes, final String name) { - if (bytes.length != 16) { + try { + return UUIDUtil.fromBytes(bytes); + } catch (final IllegalArgumentException e) { throw new IllegalArgumentException("unexpected " + name + " byte length; was " + bytes.length + " but expected 16"); } - - ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - final long mostSigBits = byteBuffer.getLong(); - final long leastSigBits = byteBuffer.getLong(); - return new UUID(mostSigBits, leastSigBits); } private static final class SortKey { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/util/UUIDUtil.java b/service/src/main/java/org/whispersystems/textsecuregcm/util/UUIDUtil.java new file mode 100644 index 000000000..40bc5501f --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/util/UUIDUtil.java @@ -0,0 +1,30 @@ +/* + * Copyright 2021 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.util; + +import java.nio.ByteBuffer; +import java.util.UUID; + +public class UUIDUtil { + + public static byte[] toBytes(final UUID uuid) { + final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); + byteBuffer.putLong(uuid.getMostSignificantBits()); + byteBuffer.putLong(uuid.getLeastSignificantBits()); + return byteBuffer.array(); + } + + public static UUID fromBytes(final byte[] bytes) { + if (bytes.length != 16) { + throw new IllegalArgumentException("unexpected byte array length; was " + bytes.length + " but expected 16"); + } + + final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + final long mostSigBits = byteBuffer.getLong(); + final long leastSigBits = byteBuffer.getLong(); + return new UUID(mostSigBits, leastSigBits); + } +}