Added the BrainKey Bitshares dictionary as a text file in the app assets and make use of it to automatically generate BrainKeys in CreateAccountActivity when the user is trying to create a new account.

This commit is contained in:
Severiano Jaramillo 2019-01-05 16:30:57 -06:00
parent 991967cd7d
commit 14a4f79cb2
4 changed files with 65 additions and 3 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,21 +1,46 @@
package cy.agorise.bitsybitshareswallet.fragments package cy.agorise.bitsybitshareswallet.fragments
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.utils.toast
import cy.agorise.graphenej.Address
import cy.agorise.graphenej.BrainKey
import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.api.ConnectionStatusUpdate
import cy.agorise.graphenej.models.JsonRpcResponse import cy.agorise.graphenej.models.JsonRpcResponse
import kotlinx.android.synthetic.main.fragment_create_account.*
import org.bitcoinj.core.ECKey
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
class CreateAccountFragment : ConnectedFragment() { class CreateAccountFragment : ConnectedFragment() {
companion object {
private const val TAG = "CreateAccountFragment"
private const val BRAINKEY_FILE = "brainkeydict.txt"
}
private lateinit var mBrainKey: BrainKey
private lateinit var mAddress: String
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true) setHasOptionsMenu(true)
return inflater.inflate(R.layout.fragment_create_account, container, false) return inflater.inflate(R.layout.fragment_create_account, container, false)
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Generating BrainKey
generateKeys()
}
override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) { override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
@ -23,4 +48,37 @@ class CreateAccountFragment : ConnectedFragment() {
override fun handleConnectionStatusUpdate(connectionStatusUpdate: ConnectionStatusUpdate) { override fun handleConnectionStatusUpdate(connectionStatusUpdate: ConnectionStatusUpdate) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
/**
* Method that generates a fresh key that will be controlling the newly created account.
*/
private fun generateKeys() {
var reader: BufferedReader? = null
val dictionary: String
try {
reader = BufferedReader(InputStreamReader(context!!.assets.open(BRAINKEY_FILE), "UTF-8"))
dictionary = reader.readLine()
val brainKeySuggestion = BrainKey.suggest(dictionary)
mBrainKey = BrainKey(brainKeySuggestion, 0)
val address = Address(ECKey.fromPublicOnly(mBrainKey.privateKey.pubKey))
Log.d(TAG, "brain key: $brainKeySuggestion")
Log.d(TAG, "address would be: " + address.toString())
mAddress = address.toString()
tvBrainKey.text = mBrainKey.brainKey
} catch (e: IOException) {
Log.e(TAG, "IOException while trying to generate key. Msg: " + e.message)
context?.toast(getString(R.string.error__read_dict_file))
} finally {
if (reader != null) {
try {
reader.close()
} catch (e: IOException) {
Log.e(TAG, "IOException while trying to close BufferedReader. Msg: " + e.message)
}
}
}
}
} }

View file

@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" xmlns:android="http://schemas.android.com/apk/res/android"> android:paddingBottom="@dimen/activity_vertical_margin">
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -73,7 +75,7 @@
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<TextView <TextView
android:id="@+id/brain_key" android:id="@+id/tvBrainKey"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/outline_rounded_corners" android:background="@drawable/outline_rounded_corners"

View file

@ -27,6 +27,7 @@
<!-- Create Account --> <!-- Create Account -->
<string name="text__bitshares_account_name">BitShares account name</string> <string name="text__bitshares_account_name">BitShares account name</string>
<string name="error__read_dict_file">Error reading dictionary file</string>
<!-- Home --> <!-- Home -->
<string name="title_transactions">Transactions</string> <string name="title_transactions">Transactions</string>