+ if (previousStorageVersion < 8) {
+ final var userPath = getUserPath(dataPath, accountPath);
+ loadLegacyFile(userPath, rootNode);
+ migratedLegacyConfig = true;
+ } else {
+ final var storage = jsonProcessor.convertValue(rootNode, Storage.class);
+ serviceEnvironment = ServiceEnvironment.valueOf(storage.serviceEnvironment);
+ registered = storage.registered;
+ number = storage.number;
+ username = storage.username;
+ encryptedDeviceName = storage.encryptedDeviceName;
+ deviceId = storage.deviceId;
+ isMultiDevice = storage.isMultiDevice;
+ password = storage.password;
+ setAccountData(aciAccountData, storage.aciAccountData, ACI::parseOrThrow);
+ setAccountData(pniAccountData, storage.pniAccountData, PNI::parseOrThrow);
+ registrationLockPin = storage.registrationLockPin;
+ final var base64 = Base64.getDecoder();
+ if (storage.pinMasterKey != null) {
+ pinMasterKey = new MasterKey(base64.decode(storage.pinMasterKey));
+ }
+ if (storage.storageKey != null) {
+ storageKey = new StorageKey(base64.decode(storage.storageKey));
+ }
+ if (storage.profileKey != null) {
+ try {
+ profileKey = new ProfileKey(base64.decode(storage.profileKey));
+ } catch (InvalidInputException e) {
+ throw new IOException(
+ "Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
+ e);
+ }
+ }
+
+ }
+
+ if (migratedLegacyConfig) {
+ save();
+ }
+ }
+
+ private <SERVICE_ID extends ServiceId> void setAccountData(
+ AccountData<SERVICE_ID> accountData,
+ Storage.AccountData storage,
+ Function<String, SERVICE_ID> serviceIdParser
+ ) throws IOException {
+ if (storage.serviceId != null) {
+ try {
+ accountData.setServiceId(serviceIdParser.apply(storage.serviceId));
+ } catch (IllegalArgumentException e) {
+ throw new IOException("Config file contains an invalid serviceId, needs to be a valid UUID", e);
+ }
+ }
+ accountData.setLocalRegistrationId(storage.registrationId);
+ if (storage.identityPrivateKey != null && storage.identityPublicKey != null) {
+ final var base64 = Base64.getDecoder();
+ final var publicKeyBytes = base64.decode(storage.identityPublicKey);
+ final var privateKeyBytes = base64.decode(storage.identityPrivateKey);
+ final var keyPair = KeyUtils.getIdentityKeyPair(publicKeyBytes, privateKeyBytes);
+ accountData.setIdentityKeyPair(keyPair);
+ }
+ accountData.preKeyMetadata.preKeyIdOffset = storage.nextPreKeyId;
+ accountData.preKeyMetadata.nextSignedPreKeyId = storage.nextSignedPreKeyId;
+ accountData.preKeyMetadata.activeSignedPreKeyId = storage.activeSignedPreKeyId;
+ accountData.preKeyMetadata.kyberPreKeyIdOffset = storage.nextKyberPreKeyId;
+ accountData.preKeyMetadata.activeLastResortKyberPreKeyId = storage.activeLastResortKyberPreKeyId;
+ }
+
+ private void loadLegacyFile(final File userPath, final JsonNode rootNode) throws IOException {