1 package org
.asamk
.signal
.storage
;
3 import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
4 import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
5 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
6 import com
.fasterxml
.jackson
.core
.JsonParser
;
7 import com
.fasterxml
.jackson
.databind
.DeserializationFeature
;
8 import com
.fasterxml
.jackson
.databind
.JsonNode
;
9 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
10 import com
.fasterxml
.jackson
.databind
.SerializationFeature
;
11 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
12 import org
.asamk
.signal
.storage
.contacts
.JsonContactsStore
;
13 import org
.asamk
.signal
.storage
.groups
.JsonGroupStore
;
14 import org
.asamk
.signal
.storage
.protocol
.JsonSignalProtocolStore
;
15 import org
.asamk
.signal
.storage
.threads
.JsonThreadStore
;
16 import org
.asamk
.signal
.util
.IOUtils
;
17 import org
.asamk
.signal
.util
.Util
;
18 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
19 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
20 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
21 import org
.whispersystems
.libsignal
.util
.Medium
;
22 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
23 import org
.whispersystems
.signalservice
.internal
.util
.Base64
;
26 import java
.io
.IOException
;
27 import java
.io
.RandomAccessFile
;
28 import java
.nio
.channels
.Channels
;
29 import java
.nio
.channels
.FileChannel
;
30 import java
.nio
.channels
.FileLock
;
31 import java
.util
.Collection
;
33 public class SignalAccount
{
35 private final ObjectMapper jsonProcessor
= new ObjectMapper();
36 private FileChannel fileChannel
;
37 private FileLock lock
;
38 private String username
;
39 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
40 private boolean isMultiDevice
= false;
41 private String password
;
42 private String registrationLockPin
;
43 private String signalingKey
;
44 private byte[] profileKey
;
45 private int preKeyIdOffset
;
46 private int nextSignedPreKeyId
;
48 private boolean registered
= false;
50 private JsonSignalProtocolStore signalProtocolStore
;
51 private JsonGroupStore groupStore
;
52 private JsonContactsStore contactStore
;
53 private JsonThreadStore threadStore
;
55 private SignalAccount() {
56 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
57 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
58 jsonProcessor
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
59 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
60 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
61 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
64 public static SignalAccount
load(String dataPath
, String username
) throws IOException
{
65 SignalAccount account
= new SignalAccount();
66 IOUtils
.createPrivateDirectories(dataPath
);
67 account
.openFileChannel(getFileName(dataPath
, username
));
72 public static SignalAccount
create(String dataPath
, String username
, IdentityKeyPair identityKey
, int registrationId
, byte[] profileKey
) throws IOException
{
73 IOUtils
.createPrivateDirectories(dataPath
);
75 SignalAccount account
= new SignalAccount();
76 account
.openFileChannel(getFileName(dataPath
, username
));
78 account
.username
= username
;
79 account
.profileKey
= profileKey
;
80 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
81 account
.groupStore
= new JsonGroupStore();
82 account
.threadStore
= new JsonThreadStore();
83 account
.contactStore
= new JsonContactsStore();
84 account
.registered
= false;
89 public static SignalAccount
createLinkedAccount(String dataPath
, String username
, String password
, int deviceId
, IdentityKeyPair identityKey
, int registrationId
, String signalingKey
, byte[] profileKey
) throws IOException
{
90 IOUtils
.createPrivateDirectories(dataPath
);
92 SignalAccount account
= new SignalAccount();
93 account
.openFileChannel(getFileName(dataPath
, username
));
95 account
.username
= username
;
96 account
.password
= password
;
97 account
.profileKey
= profileKey
;
98 account
.deviceId
= deviceId
;
99 account
.signalingKey
= signalingKey
;
100 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
101 account
.groupStore
= new JsonGroupStore();
102 account
.threadStore
= new JsonThreadStore();
103 account
.contactStore
= new JsonContactsStore();
104 account
.registered
= true;
105 account
.isMultiDevice
= true;
110 public static SignalAccount
createTemporaryAccount(IdentityKeyPair identityKey
, int registrationId
) {
111 SignalAccount account
= new SignalAccount();
113 account
.signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
114 account
.registered
= false;
119 public static String
getFileName(String dataPath
, String username
) {
120 return dataPath
+ "/" + username
;
123 public static boolean userExists(String dataPath
, String username
) {
124 if (username
== null) {
127 File f
= new File(getFileName(dataPath
, username
));
128 return !(!f
.exists() || f
.isDirectory());
131 private void load() throws IOException
{
132 JsonNode rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
134 JsonNode node
= rootNode
.get("deviceId");
136 deviceId
= node
.asInt();
138 username
= Util
.getNotNullNode(rootNode
, "username").asText();
139 password
= Util
.getNotNullNode(rootNode
, "password").asText();
140 JsonNode pinNode
= rootNode
.get("registrationLockPin");
141 registrationLockPin
= pinNode
== null ?
null : pinNode
.asText();
142 if (rootNode
.has("signalingKey")) {
143 signalingKey
= Util
.getNotNullNode(rootNode
, "signalingKey").asText();
145 if (rootNode
.has("preKeyIdOffset")) {
146 preKeyIdOffset
= Util
.getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
150 if (rootNode
.has("nextSignedPreKeyId")) {
151 nextSignedPreKeyId
= Util
.getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
153 nextSignedPreKeyId
= 0;
155 if (rootNode
.has("profileKey")) {
156 profileKey
= Base64
.decode(Util
.getNotNullNode(rootNode
, "profileKey").asText());
159 signalProtocolStore
= jsonProcessor
.convertValue(Util
.getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
160 registered
= Util
.getNotNullNode(rootNode
, "registered").asBoolean();
161 JsonNode groupStoreNode
= rootNode
.get("groupStore");
162 if (groupStoreNode
!= null) {
163 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
165 if (groupStore
== null) {
166 groupStore
= new JsonGroupStore();
169 JsonNode contactStoreNode
= rootNode
.get("contactStore");
170 if (contactStoreNode
!= null) {
171 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
173 if (contactStore
== null) {
174 contactStore
= new JsonContactsStore();
176 JsonNode threadStoreNode
= rootNode
.get("threadStore");
177 if (threadStoreNode
!= null) {
178 threadStore
= jsonProcessor
.convertValue(threadStoreNode
, JsonThreadStore
.class);
180 if (threadStore
== null) {
181 threadStore
= new JsonThreadStore();
186 if (fileChannel
== null) {
189 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
190 rootNode
.put("username", username
)
191 .put("deviceId", deviceId
)
192 .put("password", password
)
193 .put("registrationLockPin", registrationLockPin
)
194 .put("signalingKey", signalingKey
)
195 .put("preKeyIdOffset", preKeyIdOffset
)
196 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
197 .put("registered", registered
)
198 .putPOJO("axolotlStore", signalProtocolStore
)
199 .putPOJO("groupStore", groupStore
)
200 .putPOJO("contactStore", contactStore
)
201 .putPOJO("threadStore", threadStore
)
204 fileChannel
.position(0);
205 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
206 fileChannel
.truncate(fileChannel
.position());
207 fileChannel
.force(false);
208 } catch (Exception e
) {
209 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
213 private void openFileChannel(String fileName
) throws IOException
{
214 if (fileChannel
!= null) {
218 if (!new File(fileName
).exists()) {
219 IOUtils
.createPrivateFile(fileName
);
221 fileChannel
= new RandomAccessFile(new File(fileName
), "rw").getChannel();
222 lock
= fileChannel
.tryLock();
224 System
.err
.println("Config file is in use by another instance, waiting…");
225 lock
= fileChannel
.lock();
226 System
.err
.println("Config file lock acquired.");
230 public void addPreKeys(Collection
<PreKeyRecord
> records
) {
231 for (PreKeyRecord
record : records
) {
232 signalProtocolStore
.storePreKey(record.getId(), record);
234 preKeyIdOffset
= (preKeyIdOffset
+ records
.size()) % Medium
.MAX_VALUE
;
237 public void addSignedPreKey(SignedPreKeyRecord
record) {
238 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
239 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
242 public JsonSignalProtocolStore
getSignalProtocolStore() {
243 return signalProtocolStore
;
246 public JsonGroupStore
getGroupStore() {
250 public JsonContactsStore
getContactStore() {
254 public JsonThreadStore
getThreadStore() {
258 public String
getUsername() {
262 public int getDeviceId() {
266 public String
getPassword() {
270 public void setPassword(final String password
) {
271 this.password
= password
;
274 public String
getRegistrationLockPin() {
275 return registrationLockPin
;
278 public void setRegistrationLockPin(final String registrationLockPin
) {
279 this.registrationLockPin
= registrationLockPin
;
282 public String
getSignalingKey() {
286 public void setSignalingKey(final String signalingKey
) {
287 this.signalingKey
= signalingKey
;
290 public byte[] getProfileKey() {
294 public void setProfileKey(final byte[] profileKey
) {
295 this.profileKey
= profileKey
;
298 public int getPreKeyIdOffset() {
299 return preKeyIdOffset
;
302 public int getNextSignedPreKeyId() {
303 return nextSignedPreKeyId
;
306 public boolean isRegistered() {
310 public void setRegistered(final boolean registered
) {
311 this.registered
= registered
;
314 public boolean isMultiDevice() {
315 return isMultiDevice
;
318 public void setMultiDevice(final boolean multiDevice
) {
319 isMultiDevice
= multiDevice
;