]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/profiles/LegacyProfileStore.java
1c6369f077d91f22d24df91515193fc861568042
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / storage / profiles / LegacyProfileStore.java
1 package org.asamk.signal.manager.storage.profiles;
2
3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import com.fasterxml.jackson.core.JsonParser;
5 import com.fasterxml.jackson.databind.DeserializationContext;
6 import com.fasterxml.jackson.databind.JsonDeserializer;
7 import com.fasterxml.jackson.databind.JsonNode;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
10
11 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
12 import org.signal.zkgroup.InvalidInputException;
13 import org.signal.zkgroup.profiles.ProfileKey;
14 import org.signal.zkgroup.profiles.ProfileKeyCredential;
15 import org.whispersystems.signalservice.api.util.UuidUtil;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Base64;
20 import java.util.List;
21
22 public class LegacyProfileStore {
23
24 private static final ObjectMapper jsonProcessor = new ObjectMapper();
25
26 @JsonProperty("profiles")
27 @JsonDeserialize(using = ProfileStoreDeserializer.class)
28 private final List<LegacySignalProfileEntry> profiles = new ArrayList<>();
29
30 public List<LegacySignalProfileEntry> getProfileEntries() {
31 return profiles;
32 }
33
34 public static class ProfileStoreDeserializer extends JsonDeserializer<List<LegacySignalProfileEntry>> {
35
36 @Override
37 public List<LegacySignalProfileEntry> deserialize(
38 JsonParser jsonParser, DeserializationContext deserializationContext
39 ) throws IOException {
40 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
41
42 var profileEntries = new ArrayList<LegacySignalProfileEntry>();
43
44 if (node.isArray()) {
45 for (var entry : node) {
46 var name = entry.hasNonNull("name") ? entry.get("name").asText() : null;
47 var uuid = entry.hasNonNull("uuid") ? UuidUtil.parseOrNull(entry.get("uuid").asText()) : null;
48 final var address = new RecipientAddress(uuid, name);
49 ProfileKey profileKey = null;
50 try {
51 profileKey = new ProfileKey(Base64.getDecoder().decode(entry.get("profileKey").asText()));
52 } catch (InvalidInputException ignored) {
53 }
54 ProfileKeyCredential profileKeyCredential = null;
55 if (entry.hasNonNull("profileKeyCredential")) {
56 try {
57 profileKeyCredential = new ProfileKeyCredential(Base64.getDecoder()
58 .decode(entry.get("profileKeyCredential").asText()));
59 } catch (Throwable ignored) {
60 }
61 }
62 var lastUpdateTimestamp = entry.get("lastUpdateTimestamp").asLong();
63 var profile = jsonProcessor.treeToValue(entry.get("profile"), SignalProfile.class);
64 profileEntries.add(new LegacySignalProfileEntry(address,
65 profileKey,
66 lastUpdateTimestamp,
67 profile,
68 profileKeyCredential));
69 }
70 }
71
72 return profileEntries;
73 }
74 }
75 }