Updated on 2026-08-14

This commit is contained in:
Tangem 2018-10-14 15:03:18 +03:00
parent 1b179aa1b8
commit 864059f381
4 changed files with 131 additions and 175 deletions

View file

@ -40,26 +40,22 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
private var serverApiHelper: ServerApiHelper = ServerApiHelper()
private var serverApiHelperElectrum: ServerApiHelperElectrum = ServerApiHelperElectrum()
var card: TangemCard? = null
var feeRequestSuccess = false
var balanceRequestSuccess = false
var etAmount: EditText? = null
var etFee: EditText? = null
var rgFee: RadioGroup? = null
var progressBar: ProgressBar? = null
var btnSend: Button? = null
var minFee: String? = null
var maxFee: String? = null
var normalFee: String? = null
private var card: TangemCard? = null
private var feeRequestSuccess = false
private var balanceRequestSuccess = false
private var etAmount: EditText? = null
private var etFee: EditText? = null
private var rgFee: RadioGroup? = null
private var progressBar: ProgressBar? = null
private var btnSend: Button? = null
private var minFee: String? = null
private var maxFee: String? = null
private var normalFee: String? = null
private var isIncludeFee: Boolean = true
var minFeeInInternalUnits: Long? = 0L
private var minFeeInInternalUnits: Long? = 0L
private var requestPIN2Count = 0
var nodeCheck = false
var dtVerified: Date? = null
private var nodeCheck = false
private var dtVerified: Date? = null
private var calcSize: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
@ -175,7 +171,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
calendar.add(Calendar.MINUTE, -1)
if (dtVerified == null || dtVerified!!.before(calendar.time)) {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.the_obtained_data_is_outdated_try_again))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.the_obtained_data_is_outdated_try_again))
return@setOnClickListener
}
@ -190,20 +186,20 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val txAmount = etAmount!!.text.toString()
if (!engineCoin.hasBalanceInfo(card)) {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes))
return@setOnClickListener
} else if (!engineCoin.isBalanceNotZero(card)) {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.the_wallet_is_empty))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.the_wallet_is_empty))
return@setOnClickListener
} else if (!engineCoin.checkUnspentTransaction(card)) {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.please_wait_for_confirmation_of_incoming_transaction))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.please_wait_for_confirmation_of_incoming_transaction))
return@setOnClickListener
}
if (!engineCoin.checkAmountValue(card, txAmount, txFee, minFeeInInternalUnits, isIncludeFee)) {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.not_enough_funds_on_your_card))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.not_enough_funds_on_your_card))
return@setOnClickListener
}
@ -235,7 +231,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
}
} catch (e: JSONException) {
e.printStackTrace()
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes))
}
}
}
@ -255,13 +251,13 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
if (fee == BigDecimal.ZERO) {
progressBar!!.visibility = View.INVISIBLE
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_wrong_data_received_from_node))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_wrong_data_received_from_node))
}
if (calcSize.toLong() != 0L) {
fee = fee.multiply(BigDecimal(calcSize.toLong())).divide(BigDecimal(1024)) // per Kb -> per byte
} else {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_tx_length_unknown))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_tx_length_unknown))
}
progressBar!!.visibility = View.INVISIBLE
@ -303,7 +299,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
ServerApiHelper.INFURA_ETH_GAS_PRICE -> {
var gasPrice = infuraResponse.result
gasPrice = gasPrice.substring(2)
// Rounding gas price to integer gwei
// rounding gas price to integer gwei
val l = BigInteger(gasPrice, 16).divide(BigInteger.valueOf(1000000000L)).multiply(BigInteger.valueOf(1000000000L))
val m = if (card!!.blockchain == Blockchain.Token) BigInteger.valueOf(60000) else BigInteger.valueOf(21000)
@ -322,11 +318,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
dtVerified = Date()
minFeeInInternalUnits = card!!.internalUnitsFromString(normalFeeInGwei)
Log.i("eth_gas_price", gasPrice)
}
else -> {
// Log.i("eth_gas_price", gasPrice)
}
}
}
@ -334,11 +326,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
override fun onInfuraFail(method: String, message: String) {
when (method) {
ServerApiHelper.INFURA_ETH_GAS_PRICE -> {
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
}
else -> {
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
}
}
}
@ -347,22 +335,6 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
serverApiHelper.setInfuraResponse(infuraBodyListener)
}
private fun requestElectrum(card: TangemCard, electrumRequest: ElectrumRequest) {
if (UtilHelper.isOnline(this)) {
serverApiHelperElectrum.electrumRequestData(card, electrumRequest)
} else {
Toast.makeText(this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
}
}
private fun requestInfura(method: String) {
if (UtilHelper.isOnline(this)) {
serverApiHelper.infura(method, 67, card!!.wallet, "", "")
} else {
Toast.makeText(this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
}
}
public override fun onResume() {
super.onResume()
nfcManager!!.onResume()
@ -427,16 +399,8 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
return super.onKeyDown(keyCode, event)
}
fun finishActivityWithError(errorCode: Int, message: String) {
val intent = Intent()
intent.putExtra("message", message)
setResult(errorCode, intent)
finish()
}
override fun onTagDiscovered(tag: Tag) {
try {
// Log.w(getClass().getName(), "Ignore discovered tag!");
nfcManager!!.ignoreTag(tag)
} catch (e: IOException) {
e.printStackTrace()
@ -461,7 +425,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
fullAmount += unspentOutputs[i].value
}
// Get first unspent
// get first unspent
val outPut = unspentOutputs[0]
val outPutIndex = outPut.outputIndex
@ -470,7 +434,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val fees = FormatUtil.ConvertStringToLong(outFee)
var amount = FormatUtil.ConvertStringToLong(outAmount)
amount = amount - fees
amount -= fees
val change = fullAmount - fees - amount
@ -482,17 +446,14 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
for (i in unspentOutputs.indices) {
val newTX = BTCUtils.buildTXForSign(myAddress, outputAddress, myAddress, unspentOutputs, i, amount, change)
val hashData = Util.calculateSHA256(newTX)
val doubleHashData = Util.calculateSHA256(hashData)
// Log.e("TX_BODY_1", BTCUtils.toHex(newTX));
// Log.e("TX_HASH_1", BTCUtils.toHex(hashData));
// Log.e("TX_HASH_2", BTCUtils.toHex(doubleHashData));
// Log.e("TX_BODY_1", BTCUtils.toHex(newTX))
// Log.e("TX_HASH_1", BTCUtils.toHex(hashData))
// Log.e("TX_HASH_2", BTCUtils.toHex(doubleHashData))
unspentOutputs[i].bodyDoubleHash = doubleHashData
unspentOutputs[i].bodyHash = hashData
hashesForSign[i] = doubleHashData
}
@ -510,26 +471,47 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
return realTX.size
}
fun doSetFee(checkedRadioButtonId: Int) {
private fun requestElectrum(card: TangemCard, electrumRequest: ElectrumRequest) {
if (UtilHelper.isOnline(this)) {
serverApiHelperElectrum.electrumRequestData(card, electrumRequest)
} else
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
}
private fun requestInfura(method: String) {
if (UtilHelper.isOnline(this)) {
serverApiHelper.infura(method, 67, card!!.wallet, "", "")
} else
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
}
private fun doSetFee(checkedRadioButtonId: Int) {
var txtFee = ""
when (checkedRadioButtonId) {
R.id.rbMinimalFee ->
if (minFee != null)
txtFee = minFee.toString()
else
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
R.id.rbNormalFee ->
if (normalFee != null)
txtFee = normalFee.toString()
else
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
R.id.rbMaximumFee ->
if (maxFee != null)
txtFee = maxFee.toString()
else
finishActivityWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain))
}
etFee!!.setText(txtFee.replace(',', '.'))
}
private fun finishWithError(errorCode: Int, message: String) {
val intent = Intent()
intent.putExtra("message", message)
setResult(errorCode, intent)
finish()
}
}

View file

@ -5,13 +5,12 @@ import android.nfc.NfcAdapter
import android.nfc.Tag
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.KeyEvent
import android.widget.Toast
import com.tangem.data.network.ElectrumRequest
import com.tangem.data.network.ServerApiHelper
import com.tangem.data.network.ServerApiHelperElectrum
import com.tangem.data.network.model.InfuraResponse
import com.tangem.data.network.ElectrumRequest
import com.tangem.domain.cardReader.NfcManager
import com.tangem.domain.wallet.Blockchain
import com.tangem.domain.wallet.CoinEngineFactory
@ -33,7 +32,7 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
private var serverApiHelper: ServerApiHelper = ServerApiHelper()
private var serverApiHelperElectrum: ServerApiHelperElectrum = ServerApiHelperElectrum()
var card: TangemCard? = null
private var card: TangemCard? = null
private var tx: String? = null
private var nfcManager: NfcManager? = null
@ -70,29 +69,26 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
if (electrumRequest!!.isMethod(ElectrumRequest.METHOD_SendTransaction)) {
try {
var hashTX = electrumRequest.resultString
try {
if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) {
hashTX = hashTX.substring(2)
}
Log.e("TX_RESULT", hashTX)
finishWithSuccess()
} catch (e: Exception) {
e.printStackTrace()
// finishWithError(hashTX)
finishWithError(hashTX)
requestElectrum(card!!, ElectrumRequest.broadcast(card!!.wallet, tx))
}
} catch (e: JSONException) {
e.printStackTrace()
// finishWithError(e.toString())
finishWithError(e.toString())
requestElectrum(card!!, ElectrumRequest.broadcast(card!!.wallet, tx))
}
}
}
override fun onElectrumFail(message: String?) {
finishWithError(message!!)
}
}
@ -109,6 +105,7 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val tmp = infuraResponse.result
hashTX = tmp
} catch (e: JSONException) {
finishWithError(e.message!!)
return
}
@ -119,18 +116,13 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
val nonce = card!!.confirmedTXCount
nonce.add(BigInteger.valueOf(1))
card!!.confirmedTXCount = nonce
// Log.e("TX_RESULT", hashTX)
finishWithSuccess()
} catch (e: Exception) {
e.printStackTrace()
finishWithError(e.message!!)
}
}
else -> {
}
}
}
@ -139,10 +131,6 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
ServerApiHelper.INFURA_ETH_SEND_RAW_TRANSACTION -> {
finishWithError(message)
}
else -> {
}
}
}
}
@ -160,36 +148,6 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
return super.onKeyDown(keyCode, event)
}
private fun requestInfura(method: String, contract: String) {
if (UtilHelper.isOnline(this)) {
serverApiHelper.infura(method, 67, card!!.wallet, contract, tx)
} else {
Toast.makeText(this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
}
}
private fun requestElectrum(card: TangemCard, electrumRequest: ElectrumRequest) {
if (UtilHelper.isOnline(this)) {
serverApiHelperElectrum.electrumRequestData(card, electrumRequest)
} else {
Toast.makeText(this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
}
}
fun finishWithError(message: String) {
val intent = Intent()
intent.putExtra("message", String.format(getString(R.string.try_again_failed_to_send_transaction), message))
setResult(RESULT_CANCELED, intent)
finish()
}
fun finishWithSuccess() {
val intent = Intent()
intent.putExtra("message", getString(R.string.transaction_has_been_successfully_signed))
setResult(RESULT_OK, intent)
finish()
}
public override fun onResume() {
super.onResume()
nfcManager!!.onResume()
@ -207,12 +165,38 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
override fun onTagDiscovered(tag: Tag) {
try {
// Log.w(javaClass.name, "Ignore discovered tag!")
nfcManager!!.ignoreTag(tag)
} catch (e: IOException) {
e.printStackTrace()
}
}
private fun requestInfura(method: String, contract: String) {
if (UtilHelper.isOnline(this)) {
serverApiHelper.infura(method, 67, card!!.wallet, contract, tx)
} else
finishWithError(getString(R.string.no_connection))
}
private fun requestElectrum(card: TangemCard, electrumRequest: ElectrumRequest) {
if (UtilHelper.isOnline(this)) {
serverApiHelperElectrum.electrumRequestData(card, electrumRequest)
} else
finishWithError(getString(R.string.no_connection))
}
private fun finishWithSuccess() {
val intent = Intent()
intent.putExtra("message", getString(R.string.transaction_has_been_successfully_signed))
setResult(RESULT_OK, intent)
finish()
}
private fun finishWithError(message: String) {
val intent = Intent()
intent.putExtra("message", String.format(getString(R.string.try_again_failed_to_send_transaction), message))
setResult(RESULT_CANCELED, intent)
finish()
}
}

View file

@ -55,13 +55,13 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
private const val REQUEST_CODE_RECEIVE_PAYMENT = 9
}
private var singleToast: Toast? = null
private var nfcManager: NfcManager? = null
private var serverApiHelper: ServerApiHelper = ServerApiHelper()
private var serverApiHelperElectrum: ServerApiHelperElectrum = ServerApiHelperElectrum()
var card: TangemCard? = null
private var singleToast: Toast? = null
private var card: TangemCard? = null
private var lastTag: Tag? = null
private var lastReadSuccess = true
private var verifyCardTask: VerifyCardTask? = null
@ -74,9 +74,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
private val inactiveColor: ColorStateList by lazy { resources.getColorStateList(R.color.btn_dark) }
private val activeColor: ColorStateList by lazy { resources.getColorStateList(R.color.colorAccent) }
private var requestCounter = 0
private var timerRepeatRefresh: Timer? = null
private lateinit var localStorage: LocalStorage
override fun onCreate(savedInstanceState: Bundle?) {
@ -104,7 +102,6 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
if (card!!.blockchain == Blockchain.Token)
tvBalance.setSingleLine(false)
//ivTangemCard.setImageResource(card!!.cardImageResource)
ivTangemCard.setImageBitmap(localStorage.getCardArtworkBitmap(card!!))
val engine = CoinEngineFactory.create(card!!.blockchain)
@ -171,7 +168,6 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
else -> {
}
}
}
dialog.show()
} else {
@ -294,8 +290,8 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
}
override fun onElectrumFail(method: String?) {
// srl.isRefreshing = false
Log.e(TAG, "FAIL $method")
srl!!.isRefreshing = false
// Log.e(TAG, "FAIL $method")
}
}
@ -456,10 +452,6 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
e.printStackTrace()
}
}
else -> {
}
}
requestCounter--
@ -492,38 +484,6 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
}
private fun requestInfura(method: String, contract: String) {
if (UtilHelper.isOnline(activity!!)) {
requestCounter++
serverApiHelper.infura(method, 67, card!!.wallet, contract, "")
} else {
Toast.makeText(activity!!, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
srl!!.isRefreshing = false
}
}
private fun requestCardVerify() {
if (UtilHelper.isOnline(activity!!)) {
if ((card!!.isOnlineVerified == null || !card!!.isOnlineVerified)) {
requestCounter++
serverApiHelper.cardVerifyAndGetInfo(card)
}
} else {
Toast.makeText(activity!!, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
srl!!.isRefreshing = false
}
}
private fun requestRateInfo(cryptoId: String) {
if (UtilHelper.isOnline(activity!!)) {
requestCounter++
serverApiHelper.rateInfoData(cryptoId)
} else {
Toast.makeText(activity!!, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
srl!!.isRefreshing = false
}
}
override fun onResume() {
super.onResume()
nfcManager!!.onResume()
@ -883,7 +843,6 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
// Ethereum
else if (card!!.blockchain == Blockchain.Ethereum || card!!.blockchain == Blockchain.EthereumTestNet) {
requestInfura(ServerApiHelper.INFURA_ETH_GET_BALANCE, "")
requestInfura(ServerApiHelper.INFURA_ETH_GET_TRANSACTION_COUNT, "")
requestInfura(ServerApiHelper.INFURA_ETH_GET_PENDING_COUNT, "")
@ -892,19 +851,50 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
// Token
else if (card!!.blockchain == Blockchain.Token) {
requestInfura(ServerApiHelper.INFURA_ETH_CALL, engine.getContractAddress(card))
requestRateInfo("ethereum")
}
}
fun prepareResultIntent(): Intent {
val data = Intent()
data.putExtra("UID", card!!.uid)
data.putExtra("Card", card!!.asBundle)
return data
private fun requestInfura(method: String, contract: String) {
if (UtilHelper.isOnline(activity!!)) {
requestCounter++
serverApiHelper.infura(method, 67, card!!.wallet, contract, "")
} else {
Toast.makeText(activity!!, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
srl!!.isRefreshing = false
}
}
private fun requestCardVerify() {
if (UtilHelper.isOnline(activity!!)) {
if ((card!!.isOnlineVerified == null || !card!!.isOnlineVerified)) {
requestCounter++
serverApiHelper.cardVerifyAndGetInfo(card)
}
} else {
Toast.makeText(activity!!, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
srl!!.isRefreshing = false
}
}
private fun requestRateInfo(cryptoId: String) {
if (UtilHelper.isOnline(activity!!)) {
requestCounter++
serverApiHelper.rateInfoData(cryptoId)
} else {
Toast.makeText(activity!!, getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
srl!!.isRefreshing = false
}
}
// private fun prepareResultIntent(): Intent {
// val data = Intent()
// data.putExtra("UID", card!!.uid)
// data.putExtra("Card", card!!.asBundle)
// return data
// }
private fun openVerifyCard(cardProtocol: CardProtocol) {
val intent = Intent(context, VerifyCardActivity::class.java)
intent.putExtra("UID", cardProtocol.card.uid)

View file

@ -2,7 +2,7 @@
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="no_connection">Отсутствует подключение к сети</string>
<string name="no_connection">No network connection</string>
<string name="contin">Continue</string>
<string name="not_found">Not found</string>
<string name="wrong_tag_err">Not ISO tag!</string>