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 if (rootNode
.has("isMultiDevice")) isMultiDevice
= Util
.getNotNullNode(rootNode
, "isMultiDevice").asBoolean();
139 username
= Util
.getNotNullNode(rootNode
, "username").asText();
140 password
= Util
.getNotNullNode(rootNode
, "password").asText();
141 JsonNode pinNode
= rootNode
.get("registrationLockPin");
142 registrationLockPin
= pinNode
== null || pinNode
.isNull() ?
null : pinNode
.asText();
143 if (rootNode
.has("signalingKey")) {
144 signalingKey
= Util
.getNotNullNode(rootNode
, "signalingKey").asText();
146 if (rootNode
.has("preKeyIdOffset")) {
147 preKeyIdOffset
= Util
.getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
151 if (rootNode
.has("nextSignedPreKeyId")) {
152 nextSignedPreKeyId
= Util
.getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
154 nextSignedPreKeyId
= 0;
156 if (rootNode
.has("profileKey")) {
157 profileKey
= Base64
.decode(Util
.getNotNullNode(rootNode
, "profileKey").asText());
160 signalProtocolStore
= jsonProcessor
.convertValue(Util
.getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
161 registered
= Util
.getNotNullNode(rootNode
, "registered").asBoolean();
162 JsonNode groupStoreNode
= rootNode
.get("groupStore");
163 if (groupStoreNode
!= null) {
164 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
166 if (groupStore
== null) {
167 groupStore
= new JsonGroupStore();
170 JsonNode contactStoreNode
= rootNode
.get("contactStore");
171 if (contactStoreNode
!= null) {
172 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
174 if (contactStore
== null) {
175 contactStore
= new JsonContactsStore();
177 JsonNode threadStoreNode
= rootNode
.get("threadStore");
178 if (threadStoreNode
!= null) {
179 threadStore
= jsonProcessor
.convertValue(threadStoreNode
, JsonThreadStore
.class);
181 if (threadStore
== null) {
182 threadStore
= new JsonThreadStore();
187 if (fileChannel
== null) {
190 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
191 rootNode
.put("username", username
)
192 .put("deviceId", deviceId
)
193 .put("isMultiDevice", isMultiDevice
)
194 .put("password", password
)
195 .put("registrationLockPin", registrationLockPin
)
196 .put("signalingKey", signalingKey
)
197 .put("preKeyIdOffset", preKeyIdOffset
)
198 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
199 .put("profileKey", Base64
.encodeBytes(profileKey
))
200 .put("registered", registered
)
201 .putPOJO("axolotlStore", signalProtocolStore
)
202 .putPOJO("groupStore", groupStore
)
203 .putPOJO("contactStore", contactStore
)
204 .putPOJO("threadStore", threadStore
)
207 fileChannel
.position(0);
208 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
209 fileChannel
.truncate(fileChannel
.position());
210 fileChannel
.force(false);
211 } catch (Exception e
) {
212 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
216 private void openFileChannel(String fileName
) throws IOException
{
217 if (fileChannel
!= null) {
221 if (!new File(fileName
).exists()) {
222 IOUtils
.createPrivateFile(fileName
);
224 fileChannel
= new RandomAccessFile(new File(fileName
), "rw").getChannel();
225 lock
= fileChannel
.tryLock();
227 System
.err
.println("Config file is in use by another instance, waiting…");
228 lock
= fileChannel
.lock();
229 System
.err
.println("Config file lock acquired.");
233 public void addPreKeys(Collection
<PreKeyRecord
> records
) {
234 for (PreKeyRecord
record : records
) {
235 signalProtocolStore
.storePreKey(record.getId(), record);
237 preKeyIdOffset
= (preKeyIdOffset
+ records
.size()) % Medium
.MAX_VALUE
;
240 public void addSignedPreKey(SignedPreKeyRecord
record) {
241 signalProtocolStore
.storeSignedPreKey(record.getId(), record);
242 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
245 public JsonSignalProtocolStore
getSignalProtocolStore() {
246 return signalProtocolStore
;
249 public JsonGroupStore
getGroupStore() {
253 public JsonContactsStore
getContactStore() {
257 public JsonThreadStore
getThreadStore() {
261 public String
getUsername() {
265 public int getDeviceId() {
269 public String
getPassword() {
273 public void setPassword(final String password
) {
274 this.password
= password
;
277 public String
getRegistrationLockPin() {
278 return registrationLockPin
;
281 public void setRegistrationLockPin(final String registrationLockPin
) {
282 this.registrationLockPin
= registrationLockPin
;
285 public String
getSignalingKey() {
289 public void setSignalingKey(final String signalingKey
) {
290 this.signalingKey
= signalingKey
;
293 public byte[] getProfileKey() {
297 public void setProfileKey(final byte[] profileKey
) {
298 this.profileKey
= profileKey
;
301 public int getPreKeyIdOffset() {
302 return preKeyIdOffset
;
305 public int getNextSignedPreKeyId() {
306 return nextSignedPreKeyId
;
309 public boolean isRegistered() {
313 public void setRegistered(final boolean registered
) {
314 this.registered
= registered
;
317 public boolean isMultiDevice() {
318 return isMultiDevice
;
321 public void setMultiDevice(final boolean multiDevice
) {
322 isMultiDevice
= multiDevice
;