2019-06-04 16:37:45 +10:00
|
|
|
package org.thoughtcrime.securesms.loki
|
|
|
|
|
2019-06-05 16:05:08 +10:00
|
|
|
import android.content.ClipData
|
|
|
|
import android.content.ClipboardManager
|
|
|
|
import android.content.Context
|
2019-06-05 13:33:54 +10:00
|
|
|
import android.content.Intent
|
2019-06-04 16:37:45 +10:00
|
|
|
import android.os.Bundle
|
2019-06-05 16:05:08 +10:00
|
|
|
import android.widget.Toast
|
2019-06-04 16:37:45 +10:00
|
|
|
import kotlinx.android.synthetic.main.activity_key_pair.*
|
2019-06-18 12:49:21 +10:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext
|
2019-06-04 16:37:45 +10:00
|
|
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
2019-06-05 13:33:54 +10:00
|
|
|
import org.thoughtcrime.securesms.ConversationListActivity
|
2019-06-04 16:37:45 +10:00
|
|
|
import org.thoughtcrime.securesms.R
|
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
2019-06-05 13:33:54 +10:00
|
|
|
import org.thoughtcrime.securesms.database.Address
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
|
|
|
import org.thoughtcrime.securesms.database.IdentityDatabase
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
2019-06-04 16:37:45 +10:00
|
|
|
import org.whispersystems.libsignal.IdentityKeyPair
|
|
|
|
import org.whispersystems.signalservice.loki.crypto.MnemonicCodec
|
2019-06-05 15:33:31 +10:00
|
|
|
import org.whispersystems.signalservice.loki.utilities.hexEncodedPrivateKey
|
|
|
|
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
|
2019-06-04 16:37:45 +10:00
|
|
|
import java.io.File
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
|
|
|
class KeyPairActivity : BaseActionBarActivity() {
|
|
|
|
private lateinit var languageFileDirectory: File
|
|
|
|
private var keyPair: IdentityKeyPair? = null
|
|
|
|
set(newValue) { field = newValue; updateMnemonic() }
|
|
|
|
private var mnemonic: String? = null
|
|
|
|
set(newValue) { field = newValue; updateMnemonicTextView() }
|
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_key_pair)
|
|
|
|
setUpLanguageFileDirectory()
|
|
|
|
updateKeyPair()
|
2019-06-05 16:05:08 +10:00
|
|
|
copyButton.setOnClickListener { copy() }
|
|
|
|
registerButton.setOnClickListener { register() }
|
2019-06-04 16:37:45 +10:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region General
|
|
|
|
private fun setUpLanguageFileDirectory() {
|
|
|
|
val languages = listOf( "english", "japanese", "portuguese", "spanish" )
|
|
|
|
val directory = File(applicationInfo.dataDir)
|
|
|
|
for (language in languages) {
|
|
|
|
val fileName = "$language.txt"
|
|
|
|
if (directory.list().contains(fileName)) { continue }
|
|
|
|
val inputStream = assets.open("mnemonic/$fileName")
|
|
|
|
val file = File(directory, fileName)
|
|
|
|
val outputStream = FileOutputStream(file)
|
|
|
|
val buffer = ByteArray(1024)
|
|
|
|
while (true) {
|
|
|
|
val count = inputStream.read(buffer)
|
|
|
|
if (count < 0) { break }
|
|
|
|
outputStream.write(buffer, 0, count)
|
|
|
|
}
|
|
|
|
inputStream.close()
|
|
|
|
outputStream.close()
|
|
|
|
}
|
|
|
|
languageFileDirectory = directory
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
|
|
|
private fun updateKeyPair() {
|
|
|
|
IdentityKeyUtil.generateIdentityKeys(this)
|
|
|
|
keyPair = IdentityKeyUtil.getIdentityKeyPair(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateMnemonic() {
|
2019-06-05 10:02:06 +10:00
|
|
|
mnemonic = MnemonicCodec(languageFileDirectory).encode(keyPair!!.hexEncodedPrivateKey)
|
2019-06-04 16:37:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateMnemonicTextView() {
|
|
|
|
mnemonicTextView.text = mnemonic!!
|
|
|
|
}
|
|
|
|
// endregion
|
2019-06-05 13:33:54 +10:00
|
|
|
|
|
|
|
// region Interaction
|
2019-06-05 16:05:08 +10:00
|
|
|
private fun copy() {
|
|
|
|
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("mnemonic", mnemonic)
|
|
|
|
clipboard.primaryClip = clip
|
|
|
|
Toast.makeText(this, R.string.activity_key_pair_mnemonic_copied_message, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
|
2019-06-05 13:33:54 +10:00
|
|
|
private fun register() {
|
|
|
|
val publicKey = keyPair!!.publicKey
|
2019-06-05 15:33:31 +10:00
|
|
|
val hexEncodedPublicKey = keyPair!!.hexEncodedPublicKey
|
2019-06-05 13:33:54 +10:00
|
|
|
DatabaseFactory.getIdentityDatabase(this).saveIdentity(Address.fromSerialized(hexEncodedPublicKey), publicKey,
|
|
|
|
IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true)
|
|
|
|
TextSecurePreferences.setLocalNumber(this, hexEncodedPublicKey)
|
2019-06-05 14:56:10 +10:00
|
|
|
TextSecurePreferences.setPromptedPushRegistration(this, true)
|
2019-06-18 13:23:58 +10:00
|
|
|
val application = ApplicationContext.getInstance(this)
|
|
|
|
application.setUpP2PAPI()
|
|
|
|
application.startLongPolling()
|
2019-06-05 13:33:54 +10:00
|
|
|
startActivity(Intent(this, ConversationListActivity::class.java))
|
2019-06-05 14:56:10 +10:00
|
|
|
finish()
|
2019-06-05 13:33:54 +10:00
|
|
|
}
|
|
|
|
// endregion
|
2019-06-04 16:37:45 +10:00
|
|
|
}
|