]>
nmode's Git Repositories - signal-cli/blob - src/main/java/cli/JsonSignedPreKeyStore.java
3 import org
.json
.JSONArray
;
4 import org
.json
.JSONObject
;
5 import org
.whispersystems
.libaxolotl
.InvalidKeyIdException
;
6 import org
.whispersystems
.libaxolotl
.state
.SignedPreKeyRecord
;
7 import org
.whispersystems
.libaxolotl
.state
.SignedPreKeyStore
;
9 import java
.io
.IOException
;
10 import java
.util
.HashMap
;
11 import java
.util
.LinkedList
;
12 import java
.util
.List
;
15 public class JsonSignedPreKeyStore
implements SignedPreKeyStore
{
17 private final Map
<Integer
, byte[]> store
= new HashMap
<>();
19 public JsonSignedPreKeyStore() {
23 public JsonSignedPreKeyStore(JSONArray list
) throws IOException
{
24 for (int i
= 0; i
< list
.length(); i
++) {
25 JSONObject k
= list
.getJSONObject(i
);
27 store
.put(k
.getInt("id"), Base64
.decode(k
.getString("record")));
28 } catch (IOException e
) {
29 System
.out
.println("Error while decoding prekey for: " + k
.getString("name"));
34 public JSONArray
getJson() {
35 JSONArray result
= new JSONArray();
36 for (Integer id
: store
.keySet()) {
37 result
.put(new JSONObject().put("id", id
.toString()).put("record", store
.get(id
)));
43 public SignedPreKeyRecord
loadSignedPreKey(int signedPreKeyId
) throws InvalidKeyIdException
{
45 if (!store
.containsKey(signedPreKeyId
)) {
46 throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId
);
49 return new SignedPreKeyRecord(store
.get(signedPreKeyId
));
50 } catch (IOException e
) {
51 throw new AssertionError(e
);
56 public List
<SignedPreKeyRecord
> loadSignedPreKeys() {
58 List
<SignedPreKeyRecord
> results
= new LinkedList
<>();
60 for (byte[] serialized
: store
.values()) {
61 results
.add(new SignedPreKeyRecord(serialized
));
65 } catch (IOException e
) {
66 throw new AssertionError(e
);
71 public void storeSignedPreKey(int signedPreKeyId
, SignedPreKeyRecord
record) {
72 store
.put(signedPreKeyId
, record.serialize());
76 public boolean containsSignedPreKey(int signedPreKeyId
) {
77 return store
.containsKey(signedPreKeyId
);
81 public void removeSignedPreKey(int signedPreKeyId
) {
82 store
.remove(signedPreKeyId
);