]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/RegistrationManager.java
Update libsignal-service-java
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / RegistrationManager.java
index a16ead37c3a26b5680cb1add71d50d43c054fe09..88cba97bc475436306a2362b26ee5f0c829fbaed 100644 (file)
@@ -29,6 +29,7 @@ import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException;
 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
 import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations;
 import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
+import org.whispersystems.signalservice.api.kbs.MasterKey;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.api.util.SleepTimer;
 import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
@@ -102,21 +103,16 @@ public class RegistrationManager implements Closeable {
                     identityKey,
                     registrationId,
                     profileKey);
-            account.save();
 
             return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
         }
 
-        var account = SignalAccount.load(pathConfig.getDataPath(), username);
+        var account = SignalAccount.load(pathConfig.getDataPath(), username, true);
 
         return new RegistrationManager(account, pathConfig, serviceConfiguration, userAgent);
     }
 
     public void register(boolean voiceVerification, String captcha) throws IOException {
-        if (account.getPassword() == null) {
-            account.setPassword(KeyUtils.createPassword());
-        }
-
         if (voiceVerification) {
             accountManager.requestVoiceVerificationCode(Locale.getDefault(),
                     Optional.fromNullable(captcha),
@@ -124,18 +120,19 @@ public class RegistrationManager implements Closeable {
         } else {
             accountManager.requestSmsVerificationCode(false, Optional.fromNullable(captcha), Optional.absent());
         }
-
-        account.save();
     }
 
     public Manager verifyAccount(
             String verificationCode, String pin
-    ) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
+    ) throws IOException, LockedException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
         verificationCode = verificationCode.replace("-", "");
         VerifyAccountResponse response;
+        MasterKey masterKey;
         try {
-            response = verifyAccountWithCode(verificationCode, pin, null);
-            account.setPinMasterKey(null);
+            response = verifyAccountWithCode(verificationCode, null, null);
+
+            masterKey = null;
+            pin = null;
         } catch (LockedException e) {
             if (pin == null) {
                 throw e;
@@ -143,42 +140,33 @@ public class RegistrationManager implements Closeable {
 
             var registrationLockData = pinHelper.getRegistrationLockData(pin, e);
             if (registrationLockData == null) {
-                throw e;
+                response = verifyAccountWithCode(verificationCode, pin, null);
+                masterKey = null;
+            } else {
+                var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
+                try {
+                    response = verifyAccountWithCode(verificationCode, null, registrationLock);
+                } catch (LockedException _e) {
+                    throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
+                }
+                masterKey = registrationLockData.getMasterKey();
             }
-
-            var registrationLock = registrationLockData.getMasterKey().deriveRegistrationLock();
-            try {
-                response = verifyAccountWithCode(verificationCode, null, registrationLock);
-            } catch (LockedException _e) {
-                throw new AssertionError("KBS Pin appeared to matched but reg lock still failed!");
-            }
-            account.setPinMasterKey(registrationLockData.getMasterKey());
         }
 
         // TODO response.isStorageCapable()
         //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
-
-        account.setDeviceId(SignalServiceAddress.DEFAULT_DEVICE_ID);
-        account.setMultiDevice(false);
-        account.setRegistered(true);
-        account.setUuid(UuidUtil.parseOrNull(response.getUuid()));
-        account.setRegistrationLockPin(pin);
-        account.getSessionStore().archiveAllSessions();
-        account.getSignalProtocolStore()
-                .saveIdentity(account.getSelfAddress(),
-                        account.getIdentityKeyPair().getPublicKey(),
-                        TrustLevel.TRUSTED_VERIFIED);
+        account.finishRegistration(UuidUtil.parseOrNull(response.getUuid()), masterKey, pin);
 
         Manager m = null;
         try {
             m = new Manager(account, pathConfig, serviceEnvironmentConfig, userAgent);
+            account = null;
 
             m.refreshPreKeys();
-
-            account.save();
+            // Set an initial empty profile so user can be added to groups
+            m.setProfile(null, null, null, null, null);
 
             final var result = m;
-            account = null;
             m = null;
 
             return result;