]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/JsonIdentityKeyStore.java
Fix formatting
[signal-cli] / src / main / java / cli / JsonIdentityKeyStore.java
index 827bf055d0e8c52963d395e4a2d269995e395382..e4d18b8f83e73d3fc400e711707b52c8c68c47d3 100644 (file)
@@ -1,7 +1,9 @@
 package cli;
 
-import org.json.JSONArray;
-import org.json.JSONObject;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.*;
 import org.whispersystems.libaxolotl.IdentityKey;
 import org.whispersystems.libaxolotl.IdentityKeyPair;
 import org.whispersystems.libaxolotl.InvalidKeyException;
@@ -18,37 +20,14 @@ class JsonIdentityKeyStore implements IdentityKeyStore {
     private final IdentityKeyPair identityKeyPair;
     private final int localRegistrationId;
 
-    public JsonIdentityKeyStore(JSONObject jsonAxolotl) throws IOException, InvalidKeyException {
-        localRegistrationId = jsonAxolotl.getInt("registrationId");
-        identityKeyPair = new IdentityKeyPair(Base64.decode(jsonAxolotl.getString("identityKey")));
-
-        JSONArray list = jsonAxolotl.getJSONArray("trustedKeys");
-        for (int i = 0; i < list.length(); i++) {
-            JSONObject k = list.getJSONObject(i);
-            try {
-                trustedKeys.put(k.getString("name"), new IdentityKey(Base64.decode(k.getString("identityKey")), 0));
-            } catch (InvalidKeyException | IOException e) {
-                System.out.println("Error while decoding key for: " + k.getString("name"));
-            }
-        }
-    }
 
     public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair, int localRegistrationId) {
         this.identityKeyPair = identityKeyPair;
         this.localRegistrationId = localRegistrationId;
     }
 
-    public JSONObject getJson() {
-        JSONArray list = new JSONArray();
-        for (String name : trustedKeys.keySet()) {
-            list.put(new JSONObject().put("name", name).put("identityKey", Base64.encodeBytes(trustedKeys.get(name).serialize())));
-        }
-
-        JSONObject result = new JSONObject();
-        result.put("registrationId", localRegistrationId);
-        result.put("identityKey", Base64.encodeBytes(identityKeyPair.serialize()));
-        result.put("trustedKeys", list);
-        return result;
+    public void addTrustedKeys(Map<String, IdentityKey> keyMap) {
+        trustedKeys.putAll(keyMap);
     }
 
     @Override
@@ -71,4 +50,58 @@ class JsonIdentityKeyStore implements IdentityKeyStore {
         IdentityKey trusted = trustedKeys.get(name);
         return (trusted == null || trusted.equals(identityKey));
     }
+
+    public static class JsonIdentityKeyStoreDeserializer extends JsonDeserializer<JsonIdentityKeyStore> {
+
+        @Override
+        public JsonIdentityKeyStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
+            JsonNode node = jsonParser.getCodec().readTree(jsonParser);
+
+            try {
+                int localRegistrationId = node.get("registrationId").asInt();
+                IdentityKeyPair identityKeyPair = new IdentityKeyPair(Base64.decode(node.get("identityKey").asText()));
+
+
+                Map<String, IdentityKey> trustedKeyMap = new HashMap<>();
+                JsonNode trustedKeysNode = node.get("trustedKeys");
+                if (trustedKeysNode.isArray()) {
+                    for (JsonNode trustedKey : trustedKeysNode) {
+                        String trustedKeyName = trustedKey.get("name").asText();
+                        try {
+                            trustedKeyMap.put(trustedKeyName, new IdentityKey(Base64.decode(trustedKey.get("identityKey").asText()), 0));
+                        } catch (InvalidKeyException | IOException e) {
+                            System.out.println(String.format("Error while decoding key for: %s", trustedKeyName));
+                        }
+                    }
+                }
+
+                JsonIdentityKeyStore keyStore = new JsonIdentityKeyStore(identityKeyPair, localRegistrationId);
+                keyStore.addTrustedKeys(trustedKeyMap);
+
+                return keyStore;
+
+            } catch (InvalidKeyException e) {
+                throw new IOException(e);
+            }
+        }
+    }
+
+    public static class JsonIdentityKeyStoreSerializer extends JsonSerializer<JsonIdentityKeyStore> {
+
+        @Override
+        public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore, JsonGenerator json, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
+            json.writeStartObject();
+            json.writeNumberField("registrationId", jsonIdentityKeyStore.getLocalRegistrationId());
+            json.writeStringField("identityKey", Base64.encodeBytes(jsonIdentityKeyStore.getIdentityKeyPair().serialize()));
+            json.writeArrayFieldStart("trustedKeys");
+            for (Map.Entry<String, IdentityKey> trustedKey : jsonIdentityKeyStore.trustedKeys.entrySet()) {
+                json.writeStartObject();
+                json.writeStringField("name", trustedKey.getKey());
+                json.writeStringField("identityKey", Base64.encodeBytes(trustedKey.getValue().serialize()));
+                json.writeEndObject();
+            }
+            json.writeEndArray();
+            json.writeEndObject();
+        }
+    }
 }