1package im.conversations.android.xmpp.model.axolotl;
2
3import com.google.common.collect.Iterables;
4import im.conversations.android.annotation.XmlElement;
5import im.conversations.android.xmpp.model.Extension;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.List;
9import org.whispersystems.libsignal.ecc.ECPublicKey;
10import org.whispersystems.libsignal.state.PreKeyRecord;
11
12@XmlElement
13public class Bundle extends Extension {
14
15 public Bundle() {
16 super(Bundle.class);
17 }
18
19 public SignedPreKey getSignedPreKey() {
20 return this.getExtension(SignedPreKey.class);
21 }
22
23 public SignedPreKeySignature getSignedPreKeySignature() {
24 return this.getExtension(SignedPreKeySignature.class);
25 }
26
27 public IdentityKey getIdentityKey() {
28 return this.getExtension(IdentityKey.class);
29 }
30
31 public PreKey getRandomPreKey() {
32 final var preKeys = this.getExtension(PreKeys.class);
33 final Collection<PreKey> preKeyList =
34 preKeys == null ? Collections.emptyList() : preKeys.getExtensions(PreKey.class);
35 return Iterables.get(preKeyList, (int) (preKeyList.size() * Math.random()), null);
36 }
37
38 public void setIdentityKey(final ECPublicKey ecPublicKey) {
39 final var identityKey = this.addExtension(new IdentityKey());
40 identityKey.setContent(ecPublicKey);
41 }
42
43 public void setSignedPreKey(
44 final int id, final ECPublicKey ecPublicKey, final byte[] signature) {
45 final var signedPreKey = this.addExtension(new SignedPreKey());
46 signedPreKey.setId(id);
47 signedPreKey.setContent(ecPublicKey);
48 final var signedPreKeySignature = this.addExtension(new SignedPreKeySignature());
49 signedPreKeySignature.setContent(signature);
50 }
51
52 public void addPreKeys(final List<PreKeyRecord> preKeyRecords) {
53 final var preKeys = this.addExtension(new PreKeys());
54 for (final PreKeyRecord preKeyRecord : preKeyRecords) {
55 final var preKey = preKeys.addExtension(new PreKey());
56 preKey.setId(preKeyRecord.getId());
57 preKey.setContent(preKeyRecord.getKeyPair().getPublicKey());
58 }
59 }
60}