1 package org
.asamk
.signal
.storage
.protocol
;
3 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
4 import com
.fasterxml
.jackson
.core
.JsonParser
;
5 import com
.fasterxml
.jackson
.databind
.*;
6 import org
.whispersystems
.libsignal
.InvalidKeyIdException
;
7 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
8 import org
.whispersystems
.libsignal
.state
.SignedPreKeyStore
;
9 import org
.whispersystems
.signalservice
.internal
.util
.Base64
;
11 import java
.io
.IOException
;
12 import java
.util
.HashMap
;
13 import java
.util
.LinkedList
;
14 import java
.util
.List
;
17 class JsonSignedPreKeyStore
implements SignedPreKeyStore
{
19 private final Map
<Integer
, byte[]> store
= new HashMap
<>();
21 public JsonSignedPreKeyStore() {
25 private void addSignedPreKeys(Map
<Integer
, byte[]> preKeys
) {
26 store
.putAll(preKeys
);
30 public SignedPreKeyRecord
loadSignedPreKey(int signedPreKeyId
) throws InvalidKeyIdException
{
32 if (!store
.containsKey(signedPreKeyId
)) {
33 throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId
);
36 return new SignedPreKeyRecord(store
.get(signedPreKeyId
));
37 } catch (IOException e
) {
38 throw new AssertionError(e
);
43 public List
<SignedPreKeyRecord
> loadSignedPreKeys() {
45 List
<SignedPreKeyRecord
> results
= new LinkedList
<>();
47 for (byte[] serialized
: store
.values()) {
48 results
.add(new SignedPreKeyRecord(serialized
));
52 } catch (IOException e
) {
53 throw new AssertionError(e
);
58 public void storeSignedPreKey(int signedPreKeyId
, SignedPreKeyRecord
record) {
59 store
.put(signedPreKeyId
, record.serialize());
63 public boolean containsSignedPreKey(int signedPreKeyId
) {
64 return store
.containsKey(signedPreKeyId
);
68 public void removeSignedPreKey(int signedPreKeyId
) {
69 store
.remove(signedPreKeyId
);
72 public static class JsonSignedPreKeyStoreDeserializer
extends JsonDeserializer
<JsonSignedPreKeyStore
> {
75 public JsonSignedPreKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
{
76 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
78 Map
<Integer
, byte[]> preKeyMap
= new HashMap
<>();
80 for (JsonNode preKey
: node
) {
81 Integer preKeyId
= preKey
.get("id").asInt();
83 preKeyMap
.put(preKeyId
, Base64
.decode(preKey
.get("record").asText()));
84 } catch (IOException e
) {
85 System
.out
.println(String
.format("Error while decoding prekey for: %s", preKeyId
));
90 JsonSignedPreKeyStore keyStore
= new JsonSignedPreKeyStore();
91 keyStore
.addSignedPreKeys(preKeyMap
);
98 public static class JsonSignedPreKeyStoreSerializer
extends JsonSerializer
<JsonSignedPreKeyStore
> {
101 public void serialize(JsonSignedPreKeyStore jsonPreKeyStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
{
102 json
.writeStartArray();
103 for (Map
.Entry
<Integer
, byte[]> signedPreKey
: jsonPreKeyStore
.store
.entrySet()) {
104 json
.writeStartObject();
105 json
.writeNumberField("id", signedPreKey
.getKey());
106 json
.writeStringField("record", Base64
.encodeBytes(signedPreKey
.getValue()));
107 json
.writeEndObject();
109 json
.writeEndArray();