Updated on 2026-08-14

This commit is contained in:
Tangem 2018-12-16 19:51:54 +03:00
parent e7c57f1e11
commit 97c60b5f9d
18 changed files with 1098 additions and 611 deletions

View file

@ -7,10 +7,6 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.widget.Toast
import com.tangem.data.network.ElectrumRequest
import com.tangem.data.network.ServerApiElectrum
import com.tangem.data.network.ServerApiInfura
import com.tangem.data.network.model.InfuraResponse
import com.tangem.tangemcard.android.reader.NfcManager
import com.tangem.domain.wallet.*
import com.tangem.domain.wallet.eth.EthData
@ -27,11 +23,11 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
const val EXTRA_TX: String = "TX"
}
private var serverApiInfura: ServerApiInfura = ServerApiInfura()
private var serverApiElectrum: ServerApiElectrum = ServerApiElectrum()
// private var serverApiInfura: ServerApiInfura = ServerApiInfura()
// private var serverApiElectrum: ServerApiElectrum = ServerApiElectrum()
private lateinit var ctx: TangemContext
private var tx: String? = null
private var tx: ByteArray? = null
private var nfcManager: NfcManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
@ -43,71 +39,89 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
nfcManager = NfcManager(this, this)
ctx = TangemContext.loadFromBundle(this, intent.extras)
tx = intent.getStringExtra(EXTRA_TX)
tx = intent.getByteArrayExtra(EXTRA_TX)
val engine = CoinEngineFactory.create(ctx)
if (ctx.blockchain == Blockchain.Ethereum || ctx.blockchain == Blockchain.EthereumTestNet || ctx.blockchain == Blockchain.Token)
requestInfura(ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION, "")
else if (ctx.blockchain == Blockchain.Bitcoin || ctx.blockchain == Blockchain.BitcoinTestNet)
requestElectrum(ctx, ElectrumRequest.broadcast(ctx.coinData!!.wallet, tx))
else if (ctx.blockchain == Blockchain.BitcoinCash)
requestElectrum(ctx, ElectrumRequest.broadcast(ctx.coinData!!.wallet, tx))
engine!!.requestSendTransaction(
object : CoinEngine.BlockchainRequestsCallbacks {
override fun onComplete(success: Boolean) {
if (success) {
finishWithSuccess()
} else {
finishWithError(this@SendTransactionActivity.getString(R.string.try_again_failed_to_send_transaction))
}
}
override fun onProgress() {
}
override fun allowAdvance(): Boolean {
return UtilHelper.isOnline(this@SendTransactionActivity)
}
},
tx
)
// if (ctx.blockchain == Blockchain.Ethereum || ctx.blockchain == Blockchain.EthereumTestNet || ctx.blockchain == Blockchain.Token)
// requestInfura(ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION, "")
// else if (ctx.blockchain == Blockchain.Bitcoin || ctx.blockchain == Blockchain.BitcoinTestNet)
// requestElectrum(ctx, ElectrumRequest.broadcast(ctx.coinData!!.wallet, tx))
// else if (ctx.blockchain == Blockchain.BitcoinCash)
// requestElectrum(ctx, ElectrumRequest.broadcast(ctx.coinData!!.wallet, tx))
// request electrum listener
val electrumBodyListener: ServerApiElectrum.ElectrumRequestDataListener = object : ServerApiElectrum.ElectrumRequestDataListener {
override fun onSuccess(electrumRequest: ElectrumRequest?) {
if (electrumRequest!!.isMethod(ElectrumRequest.METHOD_SendTransaction)) {
try {
if (electrumRequest.resultString.isNullOrEmpty())
finishWithError("Rejected by node: " + electrumRequest.getError())
else
finishWithSuccess()
}
catch (e: Exception)
{
if( e.message!=null )
{
finishWithError(e.message!!)
}else{
finishWithError(e.javaClass.name)
}
}
}
}
override fun onFail(message: String?) {
finishWithError(message!!)
}
}
serverApiElectrum.setElectrumRequestData(electrumBodyListener)
// val electrumBodyListener: ServerApiElectrum.ElectrumRequestDataListener = object : ServerApiElectrum.ElectrumRequestDataListener {
// override fun onSuccess(electrumRequest: ElectrumRequest?) {
// if (electrumRequest!!.isMethod(ElectrumRequest.METHOD_SendTransaction)) {
// try {
// if (electrumRequest.resultString.isNullOrEmpty())
// finishWithError("Rejected by node: " + electrumRequest.getError())
// else
// finishWithSuccess()
// } catch (e: Exception) {
// if (e.message != null) {
// finishWithError(e.message!!)
// } else {
// finishWithError(e.javaClass.name)
// }
// }
// }
// }
//
// override fun onFail(message: String?) {
// finishWithError(message!!)
// }
// }
// serverApiElectrum.setElectrumRequestData(electrumBodyListener)
// request infura listener
val infuraBodyListener: ServerApiInfura.InfuraBodyListener = object : ServerApiInfura.InfuraBodyListener {
override fun onSuccess(method: String, infuraResponse: InfuraResponse) {
when (method) {
ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION -> {
if (infuraResponse.result.isEmpty())
finishWithError("Rejected by node: " + infuraResponse.error)
else {
val nonce = (ctx.coinData!! as EthData).confirmedTXCount
nonce.add(BigInteger.valueOf(1))
(ctx.coinData!! as EthData).confirmedTXCount = nonce
finishWithSuccess()
}
}
}
}
override fun onFail(method: String, message: String) {
when (method) {
ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION -> {
finishWithError(message)
}
}
}
}
serverApiInfura.setInfuraResponse(infuraBodyListener)
// val infuraBodyListener: ServerApiInfura.InfuraBodyListener = object : ServerApiInfura.InfuraBodyListener {
// override fun onSuccess(method: String, infuraResponse: InfuraResponse) {
// when (method) {
// ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION -> {
// if (infuraResponse.result.isEmpty())
// finishWithError("Rejected by node: " + infuraResponse.error)
// else {
// val nonce = (ctx.coinData!! as EthData).confirmedTXCount
// nonce.add(BigInteger.valueOf(1))
// (ctx.coinData!! as EthData).confirmedTXCount = nonce
// finishWithSuccess()
// }
// }
// }
// }
//
// override fun onFail(method: String, message: String) {
// when (method) {
// ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION -> {
// finishWithError(message)
// }
// }
// }
// }
// serverApiInfura.setInfuraResponse(infuraBodyListener)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
@ -143,20 +157,20 @@ class SendTransactionActivity : AppCompatActivity(), NfcAdapter.ReaderCallback {
}
}
private fun requestInfura(method: String, contract: String) {
if (UtilHelper.isOnline(this)) {
serverApiInfura.infura(method, 67, ctx.coinData!!.wallet, contract, tx)
} else
finishWithError(getString(R.string.no_connection))
}
private fun requestElectrum(ctx: TangemContext, electrumRequest: ElectrumRequest) {
if (UtilHelper.isOnline(this)) {
serverApiElectrum.electrumRequestData(ctx, electrumRequest)
} else
finishWithError(getString(R.string.no_connection))
}
// private fun requestInfura(method: String, contract: String) {
// if (UtilHelper.isOnline(this)) {
// serverApiInfura.infura(method, 67, ctx.coinData!!.wallet, contract, tx)
// } else
// finishWithError(getString(R.string.no_connection))
// }
// private fun requestElectrum(ctx: TangemContext, electrumRequest: ElectrumRequest) {
// if (UtilHelper.isOnline(this)) {
// serverApiElectrum.electrumRequestData(ctx, 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))