mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 01:07:47 +00:00
Implement restore from seed logic
This commit is contained in:
parent
659d9e6a8f
commit
424998a8ee
@ -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());
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user