- Remove memo_from and memo_to fields from the transfers db table.

- Added method to obtain WIF key from the authorities db table.
- Make use of WIF in TransfersLoader to decrypt memo messages before saving them into the db.
This commit is contained in:
Severiano Jaramillo 2018-11-28 12:28:38 -06:00
parent 61aeca0575
commit d4e96f98e6
6 changed files with 39 additions and 16 deletions

View file

@ -293,7 +293,7 @@ class ImportBrainkeyActivity : ConnectedActivity() {
val authority = Authority(0, userId, authorityType, encryptedWIF, encryptedBrainKey, encryptedSequenceNumber) val authority = Authority(0, userId, authorityType, encryptedWIF, encryptedBrainKey, encryptedSequenceNumber)
val authorityRepository = AuthorityRepository(application) val authorityRepository = AuthorityRepository(this)
authorityRepository.insert(authority) authorityRepository.insert(authority)
} }
} }

View file

@ -5,6 +5,7 @@ import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import cy.agorise.bitsybitshareswallet.entities.Authority import cy.agorise.bitsybitshareswallet.entities.Authority
import io.reactivex.Single
@Dao @Dao
interface AuthorityDao { interface AuthorityDao {
@ -12,5 +13,8 @@ interface AuthorityDao {
fun insert(authority: Authority) fun insert(authority: Authority)
@Query("SELECT * FROM authorities") @Query("SELECT * FROM authorities")
fun getAllAuthorities(): LiveData<List<Authority>> fun getAll(): LiveData<List<Authority>>
@Query("SELECT encrypted_wif FROM authorities WHERE user_id=:userId AND authority_type=:authorityType")
fun getWIF(userId: String, authorityType: Int): Single<String>
} }

View file

@ -21,7 +21,7 @@ interface TransferDao {
fun getCount(): Single<Int> fun getCount(): Single<Int>
@Query("SELECT * FROM transfers") @Query("SELECT * FROM transfers")
fun getAllTransfers(): LiveData<List<Transfer>> fun getAll(): LiveData<List<Transfer>>
@Query("DELETE FROM transfers") @Query("DELETE FROM transfers")
fun deleteAll() fun deleteAll()

View file

@ -16,7 +16,5 @@ data class Transfer (
@ColumnInfo(name = "destination") val destination: String, // TODO should be foreign key to UserAccount @ColumnInfo(name = "destination") val destination: String, // TODO should be foreign key to UserAccount
@ColumnInfo(name = "transfer_amount") val transferAmount: Long, @ColumnInfo(name = "transfer_amount") val transferAmount: Long,
@ColumnInfo(name = "transfer_asset_id") val transferAssetId: String, // TODO should be foreign key to Asset @ColumnInfo(name = "transfer_asset_id") val transferAssetId: String, // TODO should be foreign key to Asset
@ColumnInfo(name = "memo") val memo: String, @ColumnInfo(name = "memo") val memo: String
@ColumnInfo(name = "memo_from_key") val memoFromKey: String,
@ColumnInfo(name = "memo_to_key") val memoToKey: String
) )

View file

@ -12,8 +12,10 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent import androidx.lifecycle.OnLifecycleEvent
import cy.agorise.bitsybitshareswallet.entities.Transfer import cy.agorise.bitsybitshareswallet.entities.Transfer
import cy.agorise.bitsybitshareswallet.models.HistoricalOperationEntry import cy.agorise.bitsybitshareswallet.models.HistoricalOperationEntry
import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository
import cy.agorise.bitsybitshareswallet.repositories.TransferRepository import cy.agorise.bitsybitshareswallet.repositories.TransferRepository
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.utils.CryptoUtils
import cy.agorise.graphenej.* import cy.agorise.graphenej.*
import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.api.ConnectionStatusUpdate
import cy.agorise.graphenej.api.android.NetworkService import cy.agorise.graphenej.api.android.NetworkService
@ -31,6 +33,7 @@ import io.reactivex.schedulers.Schedulers
import org.bitcoinj.core.DumpedPrivateKey import org.bitcoinj.core.DumpedPrivateKey
import org.bitcoinj.core.ECKey import org.bitcoinj.core.ECKey
import java.util.* import java.util.*
import javax.crypto.AEADBadTagException
/** /**
* This class is responsible for loading the local database with all past transfer operations of the * This class is responsible for loading the local database with all past transfer operations of the
@ -49,6 +52,9 @@ import java.util.*
class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Lifecycle) : LifecycleObserver { class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Lifecycle) : LifecycleObserver {
private val TAG = this.javaClass.name private val TAG = this.javaClass.name
/** Constant that specifies if we are on debug mode */
private val DEBUG = false
/* Constant used to fix the number of historical transfers to fetch from the network in one batch */ /* Constant used to fix the number of historical transfers to fetch from the network in one batch */
private val HISTORICAL_TRANSFER_BATCH_SIZE = 100 private val HISTORICAL_TRANSFER_BATCH_SIZE = 100
@ -83,6 +89,9 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
/** Repository to access and update Transfers */ /** Repository to access and update Transfers */
private var transferRepository: TransferRepository? = null private var transferRepository: TransferRepository? = null
/** Repository to access and update Transfers */
private var authorityRepository: AuthorityRepository? = null
/* Network service connection */ /* Network service connection */
private var mNetworkService: NetworkService? = null private var mNetworkService: NetworkService? = null
@ -123,8 +132,6 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
*/ */
private var mShouldUnbindNetwork: Boolean = false private var mShouldUnbindNetwork: Boolean = false
private val DEBUG = false
private var lastId: Long = 0 private var lastId: Long = 0
private var lastEquivalentValueBlockNum: Long = 0 private var lastEquivalentValueBlockNum: Long = 0
@ -161,12 +168,23 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
init { init {
this.mLifeCycle.addObserver(this) this.mLifeCycle.addObserver(this)
transferRepository = TransferRepository(mContext!!) transferRepository = TransferRepository(mContext!!)
authorityRepository = AuthorityRepository(mContext!!)
val pref = PreferenceManager.getDefaultSharedPreferences(mContext) val pref = PreferenceManager.getDefaultSharedPreferences(mContext)
val userId = pref.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") val userId = pref.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")
if (userId != "") { if (userId != "") {
mCurrentAccount = UserAccount(userId) mCurrentAccount = UserAccount(userId)
// wifkey = database.getWif(mContext, mCurrentAccount, AuthorityType.MEMO) TODO RESTORE val disposable = authorityRepository!!.getWIF(userId!!, AuthorityType.MEMO.ordinal)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { encryptedWIF ->
try {
wifKey = CryptoUtils.decrypt(mContext!!, encryptedWIF)
} catch (e: AEADBadTagException) {
Log.e(TAG, "AEADBadTagException. Class: " + e.javaClass + ", Msg: " + e.message)
}
}
mDisposable = RxBus.getBusInstance() mDisposable = RxBus.getBusInstance()
.asFlowable() .asFlowable()
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -364,11 +382,8 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
op.to.objectId, op.to.objectId,
op.assetAmount.amount.toLong(), op.assetAmount.amount.toLong(),
op.assetAmount.asset.objectId, op.assetAmount.asset.objectId,
memo.plaintextMessage, memo.plaintextMessage
memo.source.toString(),
memo.destination.toString()
) )
// TODO build transfer object, save Wif in ImportBrainkeyActivity
transfers.add(transfer) transfers.add(transfer)
} }
@ -624,6 +639,7 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
internal fun onDestroy() { internal fun onDestroy() {
Log.d(TAG, "Destroying TransfersLoader")
if (mDisposable != null && !mDisposable!!.isDisposed) mDisposable!!.dispose() if (mDisposable != null && !mDisposable!!.isDisposed) mDisposable!!.dispose()
if (mShouldUnbindNetwork) { if (mShouldUnbindNetwork) {
mContext!!.unbindService(mNetworkServiceConnection) mContext!!.unbindService(mNetworkServiceConnection)

View file

@ -1,17 +1,18 @@
package cy.agorise.bitsybitshareswallet.repositories package cy.agorise.bitsybitshareswallet.repositories
import android.app.Application import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import cy.agorise.bitsybitshareswallet.daos.AuthorityDao import cy.agorise.bitsybitshareswallet.daos.AuthorityDao
import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase
import cy.agorise.bitsybitshareswallet.entities.Authority import cy.agorise.bitsybitshareswallet.entities.Authority
import io.reactivex.Single
class AuthorityRepository internal constructor(application: Application) { class AuthorityRepository internal constructor(context: Context) {
private val mAuthorityDao: AuthorityDao private val mAuthorityDao: AuthorityDao
init { init {
val db = BitsyDatabase.getDatabase(application) val db = BitsyDatabase.getDatabase(context)
mAuthorityDao = db!!.authorityDao() mAuthorityDao = db!!.authorityDao()
} }
@ -19,6 +20,10 @@ class AuthorityRepository internal constructor(application: Application) {
insertAsyncTask(mAuthorityDao).execute(authority) insertAsyncTask(mAuthorityDao).execute(authority)
} }
fun getWIF(userId: String, authorityType: Int): Single<String> {
return mAuthorityDao.getWIF(userId, authorityType)
}
private class insertAsyncTask internal constructor(private val mAsyncTaskDao: AuthorityDao) : private class insertAsyncTask internal constructor(private val mAsyncTaskDao: AuthorityDao) :
AsyncTask<Authority, Void, Void>() { AsyncTask<Authority, Void, Void>() {