private File dataPath;
private String accountPath;
+
private ServiceEnvironment serviceEnvironment;
private String number;
private String username;
private ACI aci;
private PNI pni;
- private String sessionId;
- private String sessionNumber;
private String encryptedDeviceName;
private int deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
- private boolean isMultiDevice = false;
private String password;
private String registrationLockPin;
private MasterKey pinMasterKey;
private StorageKey storageKey;
- private long storageManifestVersion = -1;
private ProfileKey profileKey;
+
private Settings settings;
- private long lastReceiveTimestamp = 0;
+ private String sessionId;
+ private String sessionNumber;
+ private long lastReceiveTimestamp = 0;
+ private long storageManifestVersion = -1;
+ private boolean isMultiDevice = false;
private boolean registered = false;
private final AccountData aciAccountData = new AccountData(ServiceIdType.ACI);
return signalAccount;
}
- public static SignalAccount createOrUpdateLinkedAccount(
+ private static SignalAccount createLinkedAccount(
File dataPath,
String accountPath,
String number,
ProfileKey profileKey,
final Settings settings
) throws IOException {
- IOUtils.createPrivateDirectories(dataPath);
var fileName = getFileName(dataPath, accountPath);
- if (!fileName.exists()) {
- return createLinkedAccount(dataPath,
- accountPath,
- number,
- serviceEnvironment,
- aci,
- pni,
- password,
- encryptedDeviceName,
- deviceId,
- aciIdentityKey,
- pniIdentityKey,
- registrationId,
- pniRegistrationId,
- profileKey,
- settings);
- }
+ IOUtils.createPrivateFile(fileName);
- final var signalAccount = load(dataPath, accountPath, true, settings);
+ final var pair = openFileChannel(fileName, true);
+ var signalAccount = new SignalAccount(pair.first(), pair.second());
+
+ signalAccount.dataPath = dataPath;
+ signalAccount.accountPath = accountPath;
+ signalAccount.serviceEnvironment = serviceEnvironment;
+ signalAccount.aciAccountData.setLocalRegistrationId(registrationId);
+ signalAccount.pniAccountData.setLocalRegistrationId(pniRegistrationId);
+ signalAccount.settings = settings;
signalAccount.setProvisioningData(number,
aci,
pni,
aciIdentityKey,
pniIdentityKey,
profileKey);
+
+ signalAccount.configurationStore = new ConfigurationStore(signalAccount::saveConfigurationStore);
+
signalAccount.getRecipientTrustedResolver()
.resolveSelfRecipientTrusted(signalAccount.getSelfRecipientAddress());
- signalAccount.aciAccountData.getSessionStore().archiveAllSessions();
- signalAccount.pniAccountData.getSessionStore().archiveAllSessions();
- signalAccount.getSenderKeyStore().deleteAll();
+ signalAccount.previousStorageVersion = CURRENT_STORAGE_VERSION;
+ signalAccount.migrateLegacyConfigs();
signalAccount.clearAllPreKeys();
- return signalAccount;
- }
-
- public void initDatabase() {
- getAccountDatabase();
- }
-
- private void clearAllPreKeys() {
- clearAllPreKeys(ServiceIdType.ACI);
- clearAllPreKeys(ServiceIdType.PNI);
- }
-
- private void clearAllPreKeys(ServiceIdType serviceIdType) {
- final var accountData = getAccountData(serviceIdType);
- resetPreKeyOffsets(serviceIdType);
- resetKyberPreKeyOffsets(serviceIdType);
- accountData.getPreKeyStore().removeAllPreKeys();
- accountData.getSignedPreKeyStore().removeAllSignedPreKeys();
- accountData.getKyberPreKeyStore().removeAllKyberPreKeys();
- save();
- }
-
- private void setPreKeys(ServiceIdType serviceIdType, PreKeyCollection preKeyCollection) {
- final var accountData = getAccountData(serviceIdType);
- final var preKeyMetadata = accountData.getPreKeyMetadata();
- preKeyMetadata.nextSignedPreKeyId = preKeyCollection.getSignedPreKey().getId();
- preKeyMetadata.kyberPreKeyIdOffset = preKeyCollection.getLastResortKyberPreKey().getId();
-
- accountData.getPreKeyStore().removeAllPreKeys();
- accountData.getSignedPreKeyStore().removeAllSignedPreKeys();
- accountData.getKyberPreKeyStore().removeAllKyberPreKeys();
-
- addSignedPreKey(serviceIdType, preKeyCollection.getSignedPreKey());
- addLastResortKyberPreKey(serviceIdType, preKeyCollection.getLastResortKyberPreKey());
+ signalAccount.save();
- save();
+ return signalAccount;
}
- private static SignalAccount createLinkedAccount(
+ public static SignalAccount createOrUpdateLinkedAccount(
File dataPath,
String accountPath,
String number,
ProfileKey profileKey,
final Settings settings
) throws IOException {
+ IOUtils.createPrivateDirectories(dataPath);
var fileName = getFileName(dataPath, accountPath);
- IOUtils.createPrivateFile(fileName);
-
- final var pair = openFileChannel(fileName, true);
- var signalAccount = new SignalAccount(pair.first(), pair.second());
+ if (!fileName.exists()) {
+ return createLinkedAccount(dataPath,
+ accountPath,
+ number,
+ serviceEnvironment,
+ aci,
+ pni,
+ password,
+ encryptedDeviceName,
+ deviceId,
+ aciIdentityKey,
+ pniIdentityKey,
+ registrationId,
+ pniRegistrationId,
+ profileKey,
+ settings);
+ }
- signalAccount.dataPath = dataPath;
- signalAccount.accountPath = accountPath;
- signalAccount.serviceEnvironment = serviceEnvironment;
- signalAccount.aciAccountData.setLocalRegistrationId(registrationId);
- signalAccount.pniAccountData.setLocalRegistrationId(pniRegistrationId);
- signalAccount.settings = settings;
+ final var signalAccount = load(dataPath, accountPath, true, settings);
signalAccount.setProvisioningData(number,
aci,
pni,
aciIdentityKey,
pniIdentityKey,
profileKey);
-
- signalAccount.configurationStore = new ConfigurationStore(signalAccount::saveConfigurationStore);
-
signalAccount.getRecipientTrustedResolver()
.resolveSelfRecipientTrusted(signalAccount.getSelfRecipientAddress());
- signalAccount.previousStorageVersion = CURRENT_STORAGE_VERSION;
- signalAccount.migrateLegacyConfigs();
+ signalAccount.aciAccountData.getSessionStore().archiveAllSessions();
+ signalAccount.pniAccountData.getSessionStore().archiveAllSessions();
+ signalAccount.getSenderKeyStore().deleteAll();
signalAccount.clearAllPreKeys();
- signalAccount.save();
-
return signalAccount;
}
trustSelfIdentity(ServiceIdType.PNI);
}
+ public void finishRegistration(
+ final ACI aci,
+ final PNI pni,
+ final MasterKey masterKey,
+ final String pin,
+ final PreKeyCollection aciPreKeys,
+ final PreKeyCollection pniPreKeys
+ ) {
+ this.pinMasterKey = masterKey;
+ this.storageManifestVersion = -1;
+ this.setStorageManifest(null);
+ this.storageKey = null;
+ this.encryptedDeviceName = null;
+ this.deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
+ this.isMultiDevice = false;
+ this.registered = true;
+ this.aci = aci;
+ this.pni = pni;
+ this.registrationLockPin = pin;
+ this.lastReceiveTimestamp = 0;
+ save();
+
+ setPreKeys(ServiceIdType.ACI, aciPreKeys);
+ setPreKeys(ServiceIdType.PNI, pniPreKeys);
+ aciAccountData.getSessionStore().archiveAllSessions();
+ pniAccountData.getSessionStore().archiveAllSessions();
+ getSenderKeyStore().deleteAll();
+ getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
+ trustSelfIdentity(ServiceIdType.ACI);
+ trustSelfIdentity(ServiceIdType.PNI);
+ }
+
+ public void initDatabase() {
+ getAccountDatabase();
+ }
+
private void migrateLegacyConfigs() {
if (getPassword() == null) {
setPassword(KeyUtils.createPassword());
}
}
+ private void clearAllPreKeys() {
+ clearAllPreKeys(ServiceIdType.ACI);
+ clearAllPreKeys(ServiceIdType.PNI);
+ }
+
+ private void clearAllPreKeys(ServiceIdType serviceIdType) {
+ final var accountData = getAccountData(serviceIdType);
+ resetPreKeyOffsets(serviceIdType);
+ resetKyberPreKeyOffsets(serviceIdType);
+ accountData.getPreKeyStore().removeAllPreKeys();
+ accountData.getSignedPreKeyStore().removeAllSignedPreKeys();
+ accountData.getKyberPreKeyStore().removeAllKyberPreKeys();
+ save();
+ }
+
+ private void setPreKeys(ServiceIdType serviceIdType, PreKeyCollection preKeyCollection) {
+ final var accountData = getAccountData(serviceIdType);
+ final var preKeyMetadata = accountData.getPreKeyMetadata();
+ preKeyMetadata.nextSignedPreKeyId = preKeyCollection.getSignedPreKey().getId();
+ preKeyMetadata.kyberPreKeyIdOffset = preKeyCollection.getLastResortKyberPreKey().getId();
+
+ accountData.getPreKeyStore().removeAllPreKeys();
+ accountData.getSignedPreKeyStore().removeAllSignedPreKeys();
+ accountData.getKyberPreKeyStore().removeAllKyberPreKeys();
+
+ addSignedPreKey(serviceIdType, preKeyCollection.getSignedPreKey());
+ addLastResortKyberPreKey(serviceIdType, preKeyCollection.getLastResortKyberPreKey());
+
+ save();
+ }
+
public void resetPreKeyOffsets(final ServiceIdType serviceIdType) {
final var preKeyMetadata = getAccountData(serviceIdType).getPreKeyMetadata();
preKeyMetadata.preKeyIdOffset = getRandomPreKeyIdOffset();
public AccountAttributes getAccountAttributes(String registrationLock) {
return new AccountAttributes(null,
- getLocalRegistrationId(),
+ aciAccountData.getLocalRegistrationId(),
false,
false,
true,
isDiscoverableByPhoneNumber(),
getAccountCapabilities(),
encryptedDeviceName,
- getLocalPniRegistrationId(),
+ pniAccountData.getLocalRegistrationId(),
null); // TODO recoveryPassword?
}
final int localPniRegistrationId
) {
setPniIdentityKeyPair(pniIdentityKeyPair);
- setLocalPniRegistrationId(localPniRegistrationId);
+ pniAccountData.setLocalRegistrationId(localPniRegistrationId);
final var preKeyMetadata = getAccountData(ServiceIdType.PNI).getPreKeyMetadata();
preKeyMetadata.nextSignedPreKeyId = pniSignedPreKey.getId();
preKeyMetadata.kyberPreKeyIdOffset = lastResortKyberPreKey.getId();
addLastResortKyberPreKey(ServiceIdType.PNI, lastResortKyberPreKey);
}
+ save();
}
public SignalServiceAddress getSelfAddress() {
save();
}
- public int getLocalRegistrationId() {
- return aciAccountData.getLocalRegistrationId();
- }
-
- public int getLocalPniRegistrationId() {
- return pniAccountData.getLocalRegistrationId();
- }
-
- public void setLocalPniRegistrationId(final int localPniRegistrationId) {
- pniAccountData.setLocalRegistrationId(localPniRegistrationId);
- save();
- }
-
public String getPassword() {
return password;
}
return UnidentifiedAccess.deriveAccessKeyFrom(getProfileKey());
}
- public int getPreKeyIdOffset(ServiceIdType serviceIdType) {
- return getAccountData(serviceIdType).getPreKeyMetadata().preKeyIdOffset;
- }
-
- public int getNextSignedPreKeyId(ServiceIdType serviceIdType) {
- return getAccountData(serviceIdType).getPreKeyMetadata().nextSignedPreKeyId;
- }
-
- public int getKyberPreKeyIdOffset(ServiceIdType serviceIdType) {
- return getAccountData(serviceIdType).getPreKeyMetadata().kyberPreKeyIdOffset;
- }
-
public boolean isRegistered() {
return registered;
}
return phoneNumberUnlisted == null || !phoneNumberUnlisted;
}
- public void finishRegistration(
- final ACI aci,
- final PNI pni,
- final MasterKey masterKey,
- final String pin,
- final PreKeyCollection aciPreKeys,
- final PreKeyCollection pniPreKeys
- ) {
- this.pinMasterKey = masterKey;
- this.storageManifestVersion = -1;
- this.setStorageManifest(null);
- this.storageKey = null;
- this.encryptedDeviceName = null;
- this.deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
- this.isMultiDevice = false;
- this.registered = true;
- this.aci = aci;
- this.pni = pni;
- this.registrationLockPin = pin;
- this.lastReceiveTimestamp = 0;
- save();
-
- setPreKeys(ServiceIdType.ACI, aciPreKeys);
- setPreKeys(ServiceIdType.PNI, pniPreKeys);
- aciAccountData.getSessionStore().archiveAllSessions();
- pniAccountData.getSessionStore().archiveAllSessions();
- getSenderKeyStore().deleteAll();
- getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
- trustSelfIdentity(ServiceIdType.ACI);
- trustSelfIdentity(ServiceIdType.PNI);
- }
-
private void trustSelfIdentity(ServiceIdType serviceIdType) {
final var accountData = getAccountData(serviceIdType);
final var serviceId = accountData.getServiceId();