Commit b43dc5f8 authored by pokkst's avatar pokkst

Improve Cash Account verification for restore. Now chain lock exception shouldn't happen

parent e017761b
package app.crescentcash.src.async
import android.os.AsyncTask
import android.view.View
import app.crescentcash.src.MainActivity
import org.bitcoinj.core.Address
import org.bitcoinj.core.AddressFactory
import org.bitcoinj.wallet.DeterministicSeed
import org.bitcoinj.wallet.Wallet
class AsyncTaskVerifyWallet(val tempWallet: Wallet, val cashAcctName: String, val seed: DeterministicSeed, val seedStr: String) : AsyncTask<Void, Void, Boolean>() {
lateinit var cashAcctEmoji: String
override fun doInBackground(vararg params: Void?): Boolean? {
val walletHelper = MainActivity.INSTANCE.walletHelper
val uiHelper = MainActivity.INSTANCE.uiHelper
val netHelper = MainActivity.INSTANCE.netHelper
try {
val cashAcctAddress = org.bitcoinj.net.NetHelper().getCashAccountAddress(walletHelper.parameters, cashAcctName)
cashAcctEmoji = netHelper.getCashAccountEmoji(cashAcctName)
println(cashAcctAddress)
var accountAddress: Address? = null
if (Address.isValidCashAddr(walletHelper.parameters, cashAcctAddress)) {
accountAddress = AddressFactory.create().getAddress(walletHelper.parameters, cashAcctAddress)
} else if (Address.isValidLegacyAddress(walletHelper.parameters, cashAcctAddress)) {
accountAddress = Address.fromBase58(walletHelper.parameters, cashAcctAddress)
} else {
uiHelper.showToastMessage("No address found!")
return false
}
if (accountAddress != null) {
return tempWallet.isPubKeyHashMine(accountAddress.hash160)
} else {
return false
}
} catch (e: NullPointerException) {
MainActivity.INSTANCE.runOnUiThread { uiHelper.showToastMessage("Cash Account not found.") }
return false
}
}
override fun onPreExecute() {
super.onPreExecute()
// ...
}
override fun onPostExecute(result: Boolean?) {
super.onPostExecute(result)
val walletHelper = MainActivity.INSTANCE.walletHelper
val uiHelper = MainActivity.INSTANCE.uiHelper
val isAddressMine = result!!
if (isAddressMine) {
walletHelper.setupSLPWallet(seedStr, true)
walletHelper.setupWalletKit(seed, cashAcctName, true)
val pref = MainActivity.INSTANCE.prefs.edit()
pref.putString("cashAccount", cashAcctName)
pref.putString("cashEmoji", cashAcctEmoji)
pref.putBoolean("isNewUser", false)
pref.apply()
MainActivity.INSTANCE.runOnUiThread {
uiHelper.restore_wallet.visibility = View.GONE
uiHelper.displayDownloadContent(true)
uiHelper.myEmoji.text = cashAcctEmoji
uiHelper.showToastMessage("Verified!")
}
} else {
MainActivity.INSTANCE.runOnUiThread { uiHelper.showToastMessage("Verification failed!") }
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import android.os.CountDownTimer
import android.text.TextUtils
import android.view.View
import app.crescentcash.src.MainActivity
import app.crescentcash.src.async.AsyncTaskVerifyWallet
import app.crescentcash.src.hash.HashHelper
import app.crescentcash.src.json.JSONHelper
import app.crescentcash.src.ui.UIHelper
......@@ -15,6 +16,7 @@ import org.bitcoinj.core.AddressFactory
import org.bitcoinj.core.ECKey
import org.bitcoinj.crypto.MnemonicCode
import org.bitcoinj.crypto.MnemonicException
import org.bitcoinj.utils.Threading
import org.bitcoinj.wallet.DeterministicSeed
import org.bitcoinj.wallet.Wallet
import org.json.JSONArray
......@@ -249,48 +251,10 @@ class NetHelper {
if (length == 12) {
val walletHelper = MainActivity.INSTANCE.walletHelper
val tempWallet = Wallet.fromSeed(walletHelper.parameters, seed)
try {
val cashAcctAddress = org.bitcoinj.net.NetHelper().getCashAccountAddress(walletHelper.parameters, cashAcctName)
val cashAcctEmoji = getCashAccountEmoji(cashAcctName)
println(cashAcctAddress)
var accountAddress: Address? = null
if (Address.isValidCashAddr(walletHelper.parameters, cashAcctAddress)) {
accountAddress = AddressFactory.create().getAddress(walletHelper.parameters, cashAcctAddress)
} else if (Address.isValidLegacyAddress(walletHelper.parameters, cashAcctAddress)) {
accountAddress = Address.fromBase58(walletHelper.parameters, cashAcctAddress)
} else {
uiHelper.showToastMessage("No address found!")
}
if (accountAddress != null) {
val isAddressMine = tempWallet.isPubKeyHashMine(accountAddress.hash160)
if (isAddressMine) {
walletHelper.setupSLPWallet(seedStr, true)
walletHelper.setupWalletKit(seed, cashAcctName, true)
val pref = MainActivity.INSTANCE.prefs.edit()
pref.putString("cashAccount", cashAcctName)
pref.putString("cashEmoji", cashAcctEmoji)
pref.putBoolean("isNewUser", false)
pref.apply()
MainActivity.INSTANCE.runOnUiThread {
uiHelper.restore_wallet.visibility = View.GONE
uiHelper.displayDownloadContent(true)
uiHelper.myEmoji.text = cashAcctEmoji
uiHelper.showToastMessage("Verified!")
uiHelper.refresh()
}
} else {
MainActivity.INSTANCE.runOnUiThread { uiHelper.showToastMessage("This is not your Cash Account!") }
}
}
} catch (e: NullPointerException) {
MainActivity.INSTANCE.runOnUiThread { uiHelper.showToastMessage("Cash Account not found.") }
}
uiHelper.showToastMessage("Verifying user...")
val task = AsyncTaskVerifyWallet(tempWallet, cashAcctName, seed, seedStr)
task.execute()
}
} else {
uiHelper.showToastMessage("Please include the identifier!")
......
......@@ -97,6 +97,7 @@ class WalletHelper {
init {
BriefLogFormatter.init()
setBitcoinSDKThread()
uiHelper = MainActivity.INSTANCE.uiHelper
netHelper = MainActivity.INSTANCE.netHelper
......@@ -115,7 +116,6 @@ class WalletHelper {
}
fun setupWalletKit(seed: DeterministicSeed?, cashAcctName: String, verifyingRestore: Boolean) {
setBitcoinSDKThread()
walletKit = object : WalletAppKit(parameters, walletDir, Constants.WALLET_NAME) {
override fun onSetupCompleted() {
......@@ -157,9 +157,7 @@ class WalletHelper {
}
}
if (verifyingRestore) {
} else {
if (!verifyingRestore) {
val hasSLPWallet = MainActivity.INSTANCE.prefs.getBoolean("hasSLPWallet", false)
if (!hasSLPWallet) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment