diff --git a/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java b/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java index 910d847091..592df407dd 100644 --- a/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java +++ b/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java @@ -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()); diff --git a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt index 3930e69348..180ec53719 100644 --- a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt @@ -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,