From 5238d4973c6a41dee862e6c89a9aa19a9efa626d Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Sun, 17 Mar 2019 09:24:50 -0600 Subject: [PATCH] Created SendTransactionViewModel and made use of it to safely obtain the user's private key from the database, to avoid a crash reported in Crashlytics. --- .../fragments/SendTransactionFragment.kt | 39 ++++++++----------- .../repositories/AuthorityRepository.kt | 1 + .../viewmodels/SendTransactionViewModel.kt | 14 +++++++ 3 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SendTransactionViewModel.kt diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt index b59d52d..51c323b 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt @@ -26,9 +26,9 @@ import com.jakewharton.rxbinding3.widget.textChanges import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.adapters.BalancesDetailsAdapter import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail -import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository import cy.agorise.bitsybitshareswallet.utils.* import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel +import cy.agorise.bitsybitshareswallet.viewmodels.SendTransactionViewModel import cy.agorise.graphenej.* import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.api.calls.BroadcastTransaction @@ -42,7 +42,6 @@ import cy.agorise.graphenej.models.JsonRpcResponse import cy.agorise.graphenej.operations.TransferOperation import cy.agorise.graphenej.operations.TransferOperationBuilder import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.schedulers.Schedulers import kotlinx.android.synthetic.main.fragment_send_transaction.* import me.dm7.barcodescanner.zxing.ZXingScannerView import org.bitcoinj.core.DumpedPrivateKey @@ -77,6 +76,8 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand // Navigation AAC Safe Args private val args: SendTransactionFragmentArgs by navArgs() + private lateinit var mViewModel: SendTransactionViewModel + /** Variables used in field's validation */ private var isCameraPreviewVisible = false private var isToAccountCorrect = false @@ -106,9 +107,6 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand /** Variable holding the current user's private key in the WIF format */ private var wifKey: String? = null - /** Repository to access and update Authorities */ - private var authorityRepository: AuthorityRepository? = null - /** This is one of the recipient account's public key, it will be used for memo encoding */ private var destinationPublicKey: PublicKey? = null @@ -145,6 +143,20 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand if (userId != "") mUserAccount = UserAccount(userId) + // Configure ViewModel + mViewModel= ViewModelProviders.of(this).get(SendTransactionViewModel::class.java) + + mViewModel.getWIF(userId, AuthorityType.ACTIVE.ordinal).observe(this, + androidx.lifecycle.Observer { encryptedWIF -> + context?.let { + try { + wifKey = CryptoUtils.decrypt(it, encryptedWIF) + } catch (e: AEADBadTagException) { + Log.e(TAG, "AEADBadTagException. Class: " + e.javaClass + ", Msg: " + e.message) + } + } + }) + // Use Navigation SafeArgs to decide if we should activate or not the camera feed if (args.openCamera) { // Delay the camera action to avoid flicker in the fragment transition @@ -181,23 +193,6 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand fabSendTransaction.setOnClickListener { verifySecurityLockSendTransfer() } fabSendTransaction.disable(R.color.lightGray) - authorityRepository = AuthorityRepository(context!!) - - // Obtain the WifKey from the db, which is used in the Send Transfer procedure - mDisposables.add( - authorityRepository!!.getWIFOld(userId, AuthorityType.ACTIVE.ordinal) - .subscribeOn(Schedulers.computation()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { encryptedWIF -> - try { - wifKey = CryptoUtils.decrypt(context!!, encryptedWIF) - } catch (e: AEADBadTagException) { - Log.e(TAG, "AEADBadTagException. Class: " + e.javaClass + ", Msg: " + e.message) - } - - } - ) - // Use RxJava Debounce to avoid making calls to the NetworkService on every text change event mDisposables.add( tietTo.textChanges() diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/AuthorityRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/AuthorityRepository.kt index 601c1f7..e1df27c 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/AuthorityRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/AuthorityRepository.kt @@ -25,6 +25,7 @@ class AuthorityRepository internal constructor(context: Context) { return mAuthorityDao.get(userId) } + // TODO remove when TransfersLoader is removed fun getWIFOld(userId: String, authorityType: Int): Single { return mAuthorityDao.getWIFOld(userId, authorityType) } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SendTransactionViewModel.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SendTransactionViewModel.kt new file mode 100644 index 0000000..922f9c9 --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/SendTransactionViewModel.kt @@ -0,0 +1,14 @@ +package cy.agorise.bitsybitshareswallet.viewmodels + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository + +class SendTransactionViewModel(application: Application) : AndroidViewModel(application) { + private var mAuthorityRepository = AuthorityRepository(application) + + internal fun getWIF(userId: String, authorityType: Int): LiveData { + return mAuthorityRepository.getWIF(userId, authorityType) + } +} \ No newline at end of file