Implement restore from seed logic

This commit is contained in:
Niels Andriesse 2019-07-17 11:31:19 +10:00
parent 659d9e6a8f
commit 424998a8ee
2 changed files with 26 additions and 2 deletions

View File

@ -24,12 +24,14 @@ import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.backup.BackupProtos;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.Hex;
import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.ecc.Curve;
import org.whispersystems.libsignal.ecc.ECKeyPair;
import org.whispersystems.libsignal.ecc.ECPrivateKey;
import org.whispersystems.libsignal.logging.Log;
import java.io.IOException;
import java.util.LinkedList;
@ -84,6 +86,18 @@ public class IdentityKeyUtil {
}
}
public static void generateIdentityKeyPair(Context context, String hexEncodedPrivateKey) {
try {
byte[] privateKey = Hex.fromStringCondensed(hexEncodedPrivateKey);
ECKeyPair keyPair = Curve.generateKeyPair(privateKey);
IdentityKey publicKey = new IdentityKey(keyPair.getPublicKey());
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(publicKey.serialize()));
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(keyPair.getPrivateKey().serialize()));
} catch (Exception e) {
Log.d("Loki", "Couldn't restore key pair from seed due to error: " + e.getMessage());
}
}
public static void generateIdentityKeys(Context context) {
ECKeyPair djbKeyPair = Curve.generateKeyPair();
IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());

View File

@ -124,8 +124,18 @@ class KeyPairActivity : BaseActionBarActivity() {
}
private fun registerOrRestore() {
val publicKey = keyPair!!.publicKey
val hexEncodedPublicKey = keyPair!!.hexEncodedPublicKey
val keyPair: IdentityKeyPair
when (mode) {
Mode.Register -> keyPair = this.keyPair!!
Mode.Restore -> {
val mnemonic = mnemonicEditText.text.toString()
val hexEncodedPrivateKey = MnemonicCodec(languageFileDirectory).decode(mnemonic)
IdentityKeyUtil.generateIdentityKeyPair(this, hexEncodedPrivateKey)
keyPair = IdentityKeyUtil.getIdentityKeyPair(this)
}
}
val publicKey = keyPair.publicKey
val hexEncodedPublicKey = keyPair.hexEncodedPublicKey
val registrationID = KeyHelper.generateRegistrationId(false)
TextSecurePreferences.setLocalRegistrationId(this, registrationID)
DatabaseFactory.getIdentityDatabase(this).saveIdentity(Address.fromSerialized(hexEncodedPublicKey), publicKey,