X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/9944b666b0859a78a08725f9b8e4b93eab00c2d8..d248f249e37f7b35a3b7dd69f2a06af8eddd3996:/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java diff --git a/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java b/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java index 1f9d10ff..ca6e305c 100644 --- a/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java +++ b/lib/src/main/java/org/asamk/signal/manager/DeviceLinkInfo.java @@ -1,5 +1,6 @@ package org.asamk.signal.manager; +import org.asamk.signal.manager.api.InvalidDeviceLinkException; import org.whispersystems.libsignal.InvalidKeyException; import org.whispersystems.libsignal.ecc.Curve; import org.whispersystems.libsignal.ecc.ECPublicKey; @@ -15,12 +16,9 @@ import java.util.Map; import static org.whispersystems.signalservice.internal.util.Util.isEmpty; -public class DeviceLinkInfo { +public record DeviceLinkInfo(String deviceIdentifier, ECPublicKey deviceKey) { - final String deviceIdentifier; - final ECPublicKey deviceKey; - - public static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws InvalidKeyException { + public static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws InvalidDeviceLinkException { final var rawQuery = linkUri.getRawQuery(); if (isEmpty(rawQuery)) { throw new RuntimeException("Invalid device link uri"); @@ -31,16 +29,21 @@ public class DeviceLinkInfo { var publicKeyEncoded = query.get("pub_key"); if (isEmpty(deviceIdentifier) || isEmpty(publicKeyEncoded)) { - throw new RuntimeException("Invalid device link uri"); + throw new InvalidDeviceLinkException("Invalid device link uri"); } final byte[] publicKeyBytes; try { publicKeyBytes = Base64.getDecoder().decode(publicKeyEncoded); } catch (IllegalArgumentException e) { - throw new RuntimeException("Invalid device link uri", e); + throw new InvalidDeviceLinkException("Invalid device link uri", e); + } + ECPublicKey deviceKey; + try { + deviceKey = Curve.decodePoint(publicKeyBytes, 0); + } catch (InvalidKeyException e) { + throw new InvalidDeviceLinkException("Invalid device link", e); } - var deviceKey = Curve.decodePoint(publicKeyBytes, 0); return new DeviceLinkInfo(deviceIdentifier, deviceKey); } @@ -57,15 +60,10 @@ public class DeviceLinkInfo { return map; } - public DeviceLinkInfo(final String deviceIdentifier, final ECPublicKey deviceKey) { - this.deviceIdentifier = deviceIdentifier; - this.deviceKey = deviceKey; - } - public URI createDeviceLinkUri() { final var deviceKeyString = Base64.getEncoder().encodeToString(deviceKey.serialize()).replace("=", ""); try { - return new URI("tsdevice:/?uuid=" + return new URI("sgnl://linkdevice?uuid=" + URLEncoder.encode(deviceIdentifier, StandardCharsets.UTF_8) + "&pub_key=" + URLEncoder.encode(deviceKeyString, StandardCharsets.UTF_8));