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.

master
Severiano Jaramillo 2019-03-17 09:24:50 -06:00
parent 1e2695db51
commit 5238d4973c
3 changed files with 32 additions and 22 deletions

View File

@ -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<String> { 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()

View File

@ -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<String> {
return mAuthorityDao.getWIFOld(userId, authorityType)
}

View File

@ -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<String> {
return mAuthorityRepository.getWIF(userId, authorityType)
}
}