From 1ee423f0a538f360c02642c10a0473c1cb7b7f85 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 26 Dec 2018 11:28:13 +0300 Subject: [PATCH 1/3] Updated on 2026-08-14 --- .../tangem/domain/wallet/eth/EthEngine.java | 142 ++++---------- .../domain/wallet/token/TokenEngine.java | 175 ++---------------- .../activity/CreateNewWalletActivity.kt | 2 +- 3 files changed, 47 insertions(+), 272 deletions(-) diff --git a/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java b/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java index b3699c0502..3e21239a50 100644 --- a/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java @@ -4,18 +4,18 @@ import android.net.Uri; import android.text.InputFilter; import android.util.Log; +import com.tangem.data.Blockchain; import com.tangem.data.network.ServerApiInfura; import com.tangem.data.network.model.InfuraResponse; +import com.tangem.domain.wallet.BTCUtils; import com.tangem.domain.wallet.BalanceValidator; -import com.tangem.data.Blockchain; import com.tangem.domain.wallet.CoinData; import com.tangem.domain.wallet.CoinEngine; import com.tangem.domain.wallet.ECDSASignatureETH; import com.tangem.domain.wallet.EthTransaction; import com.tangem.domain.wallet.Keccak256; -import com.tangem.tangemcard.data.TangemCard; import com.tangem.domain.wallet.TangemContext; -import com.tangem.domain.wallet.BTCUtils; +import com.tangem.tangemcard.data.TangemCard; import com.tangem.tangemcard.tasks.SignTask; import com.tangem.util.CryptoUtil; import com.tangem.util.DecimalDigitsInputFilter; @@ -26,8 +26,6 @@ import org.bitcoinj.core.ECKey; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; import java.util.Arrays; /** @@ -356,11 +354,11 @@ public class EthEngine extends CoinEngine { } @Override - public String calculateAddress(byte[] pkUncompressed) throws NoSuchProviderException, NoSuchAlgorithmException { + public String calculateAddress(byte[] pkUncompressed) { Keccak256 kec = new Keccak256(); int lenPk = pkUncompressed.length; if (lenPk < 2) { - throw new IllegalArgumentException("Uncompress public key length is invald"); + throw new IllegalArgumentException("Uncompress public key length is invalid"); } byte[] cleanKey = new byte[lenPk - 1]; for (int i = 0; i < cleanKey.length; ++i) { @@ -377,9 +375,9 @@ public class EthEngine extends CoinEngine { } @Override - public SignTask.PaymentToSign constructPayment(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception { + public SignTask.PaymentToSign constructPayment(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) { - Log.e(TAG, "Construct payment "+amountValue.toString()+" with fee "+feeValue.toString()+(IncFee?" including":" excluding")); + Log.e(TAG, "Construct payment " + amountValue.toString() + " with fee " + feeValue.toString() + (IncFee ? " including" : " excluding")); BigInteger nonceValue = coinData.getConfirmedTXCount(); byte[] pbKey = ctx.getCard().getWalletPublicKey(); @@ -391,7 +389,6 @@ public class EthEngine extends CoinEngine { weiAmount = weiAmount.subtract(weiFee); } - BigInteger nonce = nonceValue; BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(21000)); BigInteger gasLimit = BigInteger.valueOf(21000); Integer chainId = ctx.getBlockchain() == Blockchain.Ethereum ? EthTransaction.ChainEnum.Mainnet.getValue() : EthTransaction.ChainEnum.Rinkeby.getValue(); @@ -402,7 +399,7 @@ public class EthEngine extends CoinEngine { to = to.substring(2); } - final EthTransaction tx = EthTransaction.create(to, weiAmount, nonce, gasPrice, gasLimit, chainId); + final EthTransaction tx = EthTransaction.create(to, weiAmount, nonceValue, gasPrice, gasLimit, chainId); return new SignTask.PaymentToSign() { @Override @@ -411,7 +408,7 @@ public class EthEngine extends CoinEngine { } @Override - public byte[][] getHashesToSign() throws Exception { + public byte[][] getHashesToSign() { byte[][] hashesForSign = new byte[1][]; hashesForSign[0] = tx.getRawHash(); return hashesForSign; @@ -504,7 +501,7 @@ public class EthEngine extends CoinEngine { if (serverApiInfura.isRequestsSequenceCompleted()) { blockchainRequestsCallbacks.onComplete(!ctx.hasError()); - }else{ + } else { blockchainRequestsCallbacks.onProgress(); } } @@ -525,50 +522,45 @@ public class EthEngine extends CoinEngine { } @Override - public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) throws Exception { + public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) { ServerApiInfura serverApiInfura = new ServerApiInfura(); // request infura eth gasPrice listener ServerApiInfura.InfuraBodyListener infuraBodyListener = new ServerApiInfura.InfuraBodyListener() { @Override public void onSuccess(String method, InfuraResponse infuraResponse) { - if (method.equals(ServerApiInfura.INFURA_ETH_GAS_PRICE)) { - String gasPrice = infuraResponse.getResult(); - gasPrice = gasPrice.substring(2); - // rounding gas price to integer gwei - BigInteger l = new BigInteger(gasPrice, 16);//.divide(BigInteger.valueOf(1000000000L)).multiply(BigInteger.valueOf(1000000000L)); + String gasPrice = infuraResponse.getResult(); + gasPrice = gasPrice.substring(2); + // rounding gas price to integer gwei + BigInteger l = new BigInteger(gasPrice, 16);//.divide(BigInteger.valueOf(1000000000L)).multiply(BigInteger.valueOf(1000000000L)); - Log.i(TAG, "Infura gas price: "+gasPrice+" ("+l.toString()+")"); - BigInteger m = BigInteger.valueOf(21000); + Log.i(TAG, "Infura gas price: " + gasPrice + " (" + l.toString() + ")"); + BigInteger m = BigInteger.valueOf(21000); - Log.e(TAG, "fee multiplier: "+m.toString()); + Log.e(TAG, "fee multiplier: " + m.toString()); - CoinEngine.InternalAmount weiMinFee = new CoinEngine.InternalAmount(l.multiply(m), "wei"); - CoinEngine.InternalAmount weiNormalFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(12)).divide(BigDecimal.valueOf(10)), "wei"); - CoinEngine.InternalAmount weiMaxFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(10)), "wei"); + CoinEngine.InternalAmount weiMinFee = new CoinEngine.InternalAmount(l.multiply(m), "wei"); + CoinEngine.InternalAmount weiNormalFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(12)).divide(BigDecimal.valueOf(10)), "wei"); + CoinEngine.InternalAmount weiMaxFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(10)), "wei"); - Log.i(TAG, "min fee : "+weiMinFee.toValueString()+" wei"); - Log.i(TAG, "normal fee: "+weiNormalFee.toValueString()+" wei"); - Log.i(TAG, "max fee : "+weiMaxFee.toValueString()+" wei"); + Log.i(TAG, "min fee : " + weiMinFee.toValueString() + " wei"); + Log.i(TAG, "normal fee: " + weiNormalFee.toValueString() + " wei"); + Log.i(TAG, "max fee : " + weiMaxFee.toValueString() + " wei"); - coinData.minFee = convertToAmount(weiMinFee); - coinData.normalFee = convertToAmount(weiNormalFee); - coinData.maxFee = convertToAmount(weiMaxFee); + coinData.minFee = convertToAmount(weiMinFee); + coinData.normalFee = convertToAmount(weiNormalFee); + coinData.maxFee = convertToAmount(weiMaxFee); - Log.i(TAG, "min fee : "+coinData.minFee.toString()); - Log.i(TAG, "normal fee: "+coinData.normalFee.toString()); - Log.i(TAG, "max fee : "+coinData.maxFee.toString()); - - blockchainRequestsCallbacks.onComplete(true); - } + Log.i(TAG, "min fee : " + coinData.minFee.toString()); + Log.i(TAG, "normal fee: " + coinData.normalFee.toString()); + Log.i(TAG, "max fee : " + coinData.maxFee.toString()); + blockchainRequestsCallbacks.onComplete(true); } @Override public void onFail(String method, String message) { - if (method == ServerApiInfura.INFURA_ETH_GAS_PRICE) { - ctx.setError(ctx.getContext().getString(R.string.cannot_calculate_fee_wrong_data_received_from_node)); - blockchainRequestsCallbacks.onComplete(false); - } + ctx.setError(ctx.getContext().getString(R.string.cannot_calculate_fee_wrong_data_received_from_node)); + blockchainRequestsCallbacks.onComplete(false); } }; serverApiInfura.setInfuraResponse(infuraBodyListener); @@ -577,7 +569,7 @@ public class EthEngine extends CoinEngine { } @Override - public void requestSendTransaction(BlockchainRequestsCallbacks blockchainRequestsCallbacks, byte[] txForSend) throws Exception { + public void requestSendTransaction(BlockchainRequestsCallbacks blockchainRequestsCallbacks, byte[] txForSend) { String txStr = String.format("0x%s", BTCUtils.toHex(txForSend)); @@ -592,7 +584,7 @@ public class EthEngine extends CoinEngine { blockchainRequestsCallbacks.onComplete(false); } else { BigInteger nonce = coinData.getConfirmedTXCount(); - nonce.add(BigInteger.valueOf(1)); + nonce=nonce.add(BigInteger.valueOf(1)); coinData.setConfirmedTXCount(nonce); ctx.setError(null); blockchainRequestsCallbacks.onComplete(true); @@ -614,68 +606,4 @@ public class EthEngine extends CoinEngine { serverApiInfura.infura(ServerApiInfura.INFURA_ETH_SEND_RAW_TRANSACTION, 67, coinData.getWallet(), "", txStr); } - - // @Override -// public byte[] sign(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress, CardProtocol protocol) throws Exception { -// -// BigInteger nonceValue = coinData.getConfirmedTXCount(); -// byte[] pbKey = ctx.getCard().getWalletPublicKey(); -//// boolean flag = (ctx.getCard().getSigningMethod() == TangemCard.SigningMethod.Sign_Hash_Validated_By_Issuer); -// Issuer issuer = ctx.getCard().getIssuer(); -// -// BigInteger weiFee=convertToInternalAmount(feeValue).toBigIntegerExact(); -// BigInteger weiAmount=convertToInternalAmount(amountValue).toBigIntegerExact(); -// -// if (IncFee) { -// weiAmount = weiAmount.subtract(weiFee); -// } -// -// BigInteger nonce = nonceValue; -// BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(21000)); -// BigInteger gasLimit = BigInteger.valueOf(21000); -// Integer chainId = ctx.getBlockchain() == Blockchain.Ethereum ? EthTransaction.ChainEnum.Mainnet.getValue() : EthTransaction.ChainEnum.Rinkeby.getValue(); -// -// String to = targetAddress; -// -// if (to.startsWith("0x") || to.startsWith("0X")) { -// to = to.substring(2); -// } -// -// EthTransaction tx = EthTransaction.create(to, weiAmount, nonce, gasPrice, gasLimit, chainId); -// -// byte[][] hashesForSign = new byte[1][]; -// byte[] for_hash = tx.getRawHash(); -// hashesForSign[0] = for_hash; -// -// byte[] signFromCard = null; -// try { -// signFromCard = protocol.run_SignHashes(PINStorage.getPIN2(), hashesForSign, null, null, null).getTLV(TLV.Tag.TAG_Signature).Value; -// // TODO slice signFromCard to hashes.length parts -// } catch (Exception ex) { -// Log.e("ETH", ex.getMessage()); -// return null; -// } -// -// BigInteger r = new BigInteger(1, Arrays.copyOfRange(signFromCard, 0, 32)); -// BigInteger s = new BigInteger(1, Arrays.copyOfRange(signFromCard, 32, 64)); -// s = CryptoUtil.toCanonicalised(s); -// -// boolean f = ECKey.verify(for_hash, new ECKey.ECDSASignature(r, s), pbKey); -// -// if (!f) { -// Log.e("ETH-CHECK", "sign Failed."); -// } -// -// tx.signature = new ECDSASignatureETH(r, s); -// int v = tx.BruteRecoveryID2(tx.signature, for_hash, pbKey); -// if (v != 27 && v != 28) { -// Log.e("ETH", "invalid v"); -// return null; -// } -// tx.signature.v = (byte) v; -// Log.e("ETH_v", String.valueOf(v)); -// -// byte[] realTX = tx.getEncoded(); -// return realTX; -// } } diff --git a/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java b/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java index 0e7c7a0c65..972ab292d4 100644 --- a/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java @@ -9,16 +9,16 @@ import com.google.common.base.Strings; import com.tangem.data.Blockchain; import com.tangem.data.network.ServerApiInfura; import com.tangem.data.network.model.InfuraResponse; +import com.tangem.domain.wallet.BTCUtils; import com.tangem.domain.wallet.BalanceValidator; import com.tangem.domain.wallet.CoinData; import com.tangem.domain.wallet.CoinEngine; import com.tangem.domain.wallet.ECDSASignatureETH; import com.tangem.domain.wallet.EthTransaction; import com.tangem.domain.wallet.Keccak256; +import com.tangem.domain.wallet.TangemContext; import com.tangem.domain.wallet.eth.EthData; import com.tangem.tangemcard.data.TangemCard; -import com.tangem.domain.wallet.TangemContext; -import com.tangem.domain.wallet.BTCUtils; import com.tangem.tangemcard.tasks.SignTask; import com.tangem.util.CryptoUtil; import com.tangem.util.DecimalDigitsInputFilter; @@ -29,8 +29,6 @@ import org.bitcoinj.core.ECKey; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; import java.util.Arrays; /** @@ -138,15 +136,15 @@ public class TokenEngine extends CoinEngine { return ctx.getString(R.string.not_implemented); } - public static int getEthDecimals() { + private static int getEthDecimals() { return 18; } - public int getTokenDecimals() { + private int getTokenDecimals() { return ctx.getCard().getTokensDecimal(); } - public String getContractAddress(TangemCard card) { + private String getContractAddress(TangemCard card) { return card.getContractAddress(); } @@ -171,7 +169,7 @@ public class TokenEngine extends CoinEngine { return true; } - public boolean isBalanceAlterNotZero() { + private boolean isBalanceAlterNotZero() { if (coinData == null) return false; if (coinData.getBalanceAlterInInternalUnits() == null) return false; return coinData.getBalanceAlterInInternalUnits().notZero(); @@ -205,11 +203,11 @@ public class TokenEngine extends CoinEngine { } @Override - public String calculateAddress(byte[] pkUncompressed) throws NoSuchProviderException, NoSuchAlgorithmException { + public String calculateAddress(byte[] pkUncompressed) { Keccak256 kec = new Keccak256(); int lenPk = pkUncompressed.length; if (lenPk < 2) { - throw new IllegalArgumentException("Uncompress public key length is invald"); + throw new IllegalArgumentException("Uncompress public key length is invalid"); } byte[] cleanKey = new byte[lenPk - 1]; for (int i = 0; i < cleanKey.length; ++i) { @@ -681,7 +679,6 @@ public class TokenEngine extends CoinEngine { String balanceCap = infuraResponse.getResult(); balanceCap = balanceCap.substring(2); BigInteger l = new BigInteger(balanceCap, 16); - Long balance = l.longValue(); coinData.setBalanceInInternalUnits(new CoinEngine.InternalAmount(l, ctx.getCard().tokenSymbol)); // Log.i("$TAG eth_call", balanceCap) @@ -722,7 +719,7 @@ public class TokenEngine extends CoinEngine { } @Override - public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) throws Exception { + public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) { ServerApiInfura serverApiInfura = new ServerApiInfura(); // request infura eth gasPrice listener ServerApiInfura.InfuraBodyListener infuraBodyListener = new ServerApiInfura.InfuraBodyListener() { @@ -774,7 +771,7 @@ public class TokenEngine extends CoinEngine { } @Override - public void requestSendTransaction(BlockchainRequestsCallbacks blockchainRequestsCallbacks, byte[] txForSend) throws Exception { + public void requestSendTransaction(BlockchainRequestsCallbacks blockchainRequestsCallbacks, byte[] txForSend) { String txStr = String.format("0x%s", BTCUtils.toHex(txForSend)); @@ -789,7 +786,7 @@ public class TokenEngine extends CoinEngine { blockchainRequestsCallbacks.onComplete(false); } else { BigInteger nonce = coinData.getConfirmedTXCount(); - nonce.add(BigInteger.valueOf(1)); + nonce=nonce.add(BigInteger.valueOf(1)); coinData.setConfirmedTXCount(nonce); ctx.setError(null); blockchainRequestsCallbacks.onComplete(true); @@ -812,154 +809,4 @@ public class TokenEngine extends CoinEngine { } - -// public byte[] signETH(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress, CardProtocol protocol) throws Exception { -// BigInteger nonceValue = coinData.getConfirmedTXCount(); -// byte[] pbKey = ctx.getCard().getWalletPublicKey(); -//// boolean flag = (ctx.getCard().getSigningMethod() == TangemCard.SigningMethod.Sign_Hash_Validated_By_Issuer); -// Issuer issuer = ctx.getCard().getIssuer(); -// -// BigInteger weiFee = convertToInternalAmount(feeValue).toBigIntegerExact(); -// BigInteger weiAmount = convertToInternalAmount(amountValue).toBigIntegerExact(); -// -// if (IncFee) { -// weiAmount = weiAmount.subtract(weiFee); -// } -// -// BigInteger nonce = nonceValue; -// BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(21000)); -// BigInteger gasLimit = BigInteger.valueOf(21000); - -// Integer chainId = ctx.getBlockchain() == Blockchain.Ethereum ? EthTransaction.ChainEnum.Mainnet.getValue() : EthTransaction.ChainEnum.Rinkeby.getValue(); -// Integer chainId = EthTransaction.ChainEnum.Mainnet.getValue(); // Token support on main net only!!! -// -// String to = targetAddress; -// -// if (to.startsWith("0x") || to.startsWith("0X")) { -// to = to.substring(2); -// } -// -// EthTransaction tx = EthTransaction.create(to, weiAmount, nonce, gasPrice, gasLimit, chainId); -// -// byte[][] hashesForSign = new byte[1][]; -// byte[] for_hash = tx.getRawHash(); -// hashesForSign[0] = for_hash; -// -// byte[] signFromCard = null; -// try { -// signFromCard = protocol.run_SignHashes(PINStorage.getPIN2(), hashesForSign, null, null, null).getTLV(TLV.Tag.TAG_Signature).Value; -// // TODO slice signFromCard to hashes.length parts -// } catch (Exception ex) { -// Log.e("ETH", ex.getMessage()); -// return null; -// } -// -// BigInteger r = new BigInteger(1, Arrays.copyOfRange(signFromCard, 0, 32)); -// BigInteger s = new BigInteger(1, Arrays.copyOfRange(signFromCard, 32, 64)); -// s = CryptoUtil.toCanonicalised(s); -// -// boolean f = ECKey.verify(for_hash, new ECKey.ECDSASignature(r, s), pbKey); -// -// if (!f) { -// Log.e("ETH-CHECK", "sign Failed."); -// } -// -// tx.signature = new ECDSASignatureETH(r, s); -// int v = tx.BruteRecoveryID2(tx.signature, for_hash, pbKey); -// if (v != 27 && v != 28) { -// Log.e("ETH", "invalid v"); -// return null; -// } -// tx.signature.v = (byte) v; -// Log.e("ETH_v", String.valueOf(v)); -// -// byte[] realTX = tx.getEncoded(); -// return realTX; -// } - -// public byte[] signToken(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress, CardProtocol protocol) throws Exception { -// BigInteger nonceValue = coinData.getConfirmedTXCount(); -// byte[] pbKey = ctx.getCard().getWalletPublicKey(); -//// boolean flag = (ctx.getCard().getSigningMethod() == TangemCard.SigningMethod.Sign_Hash_Validated_By_Issuer); -// Issuer issuer = ctx.getCard().getIssuer(); -// -// -// BigInteger gigaK = BigInteger.valueOf(1000000000L); -// -// BigInteger weiFee = convertToInternalAmount(feeValue).toBigIntegerExact(); -// -// InternalAmount amountDec=convertToInternalAmount(amountValue); -// BigInteger amount = amountDec.toBigInteger(); //new BigInteger(amountValue, 10); -// -// -// //amount = amount.subtract(fee); -// -// BigInteger nonce = nonceValue; -// BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(60000)); -// BigInteger gasLimit = BigInteger.valueOf(60000); -// Integer chainId = EthTransaction.ChainEnum.Mainnet.getValue(); -// BigInteger amountZero = BigInteger.ZERO; -// -// String to = targetAddress; -// -// if (to.startsWith("0x") || to.startsWith("0X")) { -// to = to.substring(2); -// } -// -// String contractAddress = getContractAddress(ctx.getCard()); -// -// if (contractAddress.startsWith("0x") || contractAddress.startsWith("0X")) { -// contractAddress = contractAddress.substring(2); -// } -// -// String amountLeadZero = amount.toString(16); -// if (amountLeadZero.startsWith("0x") || amountLeadZero.startsWith("0X")) { -// amountLeadZero = amountLeadZero.substring(2); -// } -// -// while (amountLeadZero.length() < 64) { -// amountLeadZero = "0" + amountLeadZero; -// } -// -// String cmd = "a9059cbb000000000000000000000000" + to + amountLeadZero; //TODO only for BAT -// -// -// byte[] data = BTCUtils.fromHex(cmd); -// EthTransaction tx = EthTransaction.create(contractAddress, amountZero, nonce, gasPrice, gasLimit, chainId, data); -// -// byte[][] hashesForSign = new byte[1][]; -// byte[] for_hash = tx.getRawHash(); -// hashesForSign[0] = for_hash; -// -// byte[] signFromCard = null; -// try { -// signFromCard = protocol.run_SignHashes(PINStorage.getPIN2(), hashesForSign, null, null, null).getTLV(TLV.Tag.TAG_Signature).Value; -// // TODO slice signFromCard to hashes.length parts -// } catch (Exception ex) { -// Log.e("ETH", ex.getMessage()); -// return null; -// } -// -// BigInteger r = new BigInteger(1, Arrays.copyOfRange(signFromCard, 0, 32)); -// BigInteger s = new BigInteger(1, Arrays.copyOfRange(signFromCard, 32, 64)); -// s = CryptoUtil.toCanonicalised(s); -// -// boolean f = ECKey.verify(for_hash, new ECKey.ECDSASignature(r, s), pbKey); -// -// if (!f) { -// Log.e("ETH-CHECK", "sign Failed."); -// } -// -// tx.signature = new ECDSASignatureETH(r, s); -// int v = tx.BruteRecoveryID2(tx.signature, for_hash, pbKey); -// if (v != 27 && v != 28) { -// Log.e("ETH", "invalid v"); -// return null; -// } -// tx.signature.v = (byte) v; -// Log.e("ETH_v", String.valueOf(v)); -// -// byte[] realTX = tx.getEncoded(); -// return realTX; -// } } diff --git a/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.kt b/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.kt index 48392031ed..f7f06b0b4a 100644 --- a/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.kt @@ -31,7 +31,7 @@ class CreateNewWalletActivity : AppCompatActivity(), NfcAdapter.ReaderCallback, companion object { fun callingIntent(context: Context, ctx: TangemContext): Intent { - val intent = Intent(context, EmptyWalletActivity::class.java) + val intent = Intent(context, CreateNewWalletActivity::class.java) intent.putExtra("UID", ctx.card!!.uid) intent.putExtra("Card", ctx.card!!.asBundle) return intent From de62d5cd864e8cc0e29b55ecbefa3d50d16893c4 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 26 Dec 2018 15:58:40 +0300 Subject: [PATCH 2/3] Updated on 2026-08-14 --- .../main/java/com/tangem/domain/wallet/eth/EthEngine.java | 4 ++-- .../java/com/tangem/domain/wallet/token/TokenEngine.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java b/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java index 3e21239a50..c3ba1ee6b4 100644 --- a/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java @@ -539,8 +539,8 @@ public class EthEngine extends CoinEngine { Log.e(TAG, "fee multiplier: " + m.toString()); CoinEngine.InternalAmount weiMinFee = new CoinEngine.InternalAmount(l.multiply(m), "wei"); - CoinEngine.InternalAmount weiNormalFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(12)).divide(BigDecimal.valueOf(10)), "wei"); - CoinEngine.InternalAmount weiMaxFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(10)), "wei"); + CoinEngine.InternalAmount weiNormalFee = new CoinEngine.InternalAmount(l.multiply(BigInteger.valueOf(12)).divide(BigInteger.valueOf(10)).multiply(m), "wei"); + CoinEngine.InternalAmount weiMaxFee = new CoinEngine.InternalAmount(l.multiply(BigInteger.valueOf(15)).divide(BigInteger.valueOf(10)).multiply(m), "wei"); Log.i(TAG, "min fee : " + weiMinFee.toValueString() + " wei"); Log.i(TAG, "normal fee: " + weiNormalFee.toValueString() + " wei"); diff --git a/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java b/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java index 972ab292d4..cadbac4caa 100644 --- a/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java @@ -738,10 +738,9 @@ public class TokenEngine extends CoinEngine { Log.i(TAG, "fee multiplier: "+m.toString()); - CoinEngine.InternalAmount weiMinFee = new CoinEngine.InternalAmount(l.multiply(m), "wei"); - CoinEngine.InternalAmount weiNormalFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(12)).divide(BigDecimal.valueOf(10)), "wei"); - CoinEngine.InternalAmount weiMaxFee = new CoinEngine.InternalAmount(weiMinFee.multiply(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(10)), "wei"); + CoinEngine.InternalAmount weiNormalFee = new CoinEngine.InternalAmount(l.multiply(BigInteger.valueOf(12)).divide(BigInteger.valueOf(10)).multiply(m), "wei"); + CoinEngine.InternalAmount weiMaxFee = new CoinEngine.InternalAmount(l.multiply(BigInteger.valueOf(15)).divide(BigInteger.valueOf(10)).multiply(m), "wei"); Log.i(TAG, "min fee : "+weiMinFee.toValueString()+" wei"); Log.i(TAG, "normal fee: "+weiNormalFee.toValueString()+" wei"); Log.i(TAG, "max fee : "+weiMaxFee.toValueString()+" wei"); From 1b5b0037d1c86e21f6217d12797565765c5bfe9f Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 27 Dec 2018 11:02:01 +0300 Subject: [PATCH 3/3] Updated on 2026-08-14 --- .../data/network/ServerApiElectrum.java | 3 +- .../com/tangem/domain/wallet/btc/BtcData.java | 16 +- .../tangem/domain/wallet/eth/EthEngine.java | 8 +- .../domain/wallet/token/TokenEngine.java | 40 ++-- .../activity/ConfirmPaymentActivity.kt | 2 +- .../activity/PreparePaymentActivity.kt | 2 +- .../presentation/fragment/LoadedWallet.kt | 171 +++++++++++------- 7 files changed, 142 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/com/tangem/data/network/ServerApiElectrum.java b/app/src/main/java/com/tangem/data/network/ServerApiElectrum.java index 38a86a3ebd..53b250d78b 100644 --- a/app/src/main/java/com/tangem/data/network/ServerApiElectrum.java +++ b/app/src/main/java/com/tangem/data/network/ServerApiElectrum.java @@ -157,7 +157,7 @@ public class ServerApiElectrum { Log.e(TAG, "electrumRequestData " + electrumRequest.getMethod() + " onComplete, answerData==null"); } Log.e(TAG, String.format("%d requests left in processing",requestsCount)); - if (electrumRequest.answerData != null) { + if (electrumRequest.answerData != null && electrumRequest.getError()==null) { electrumRequestDataListener.onSuccess(electrumRequest); } else { // if( error==null || error.isEmpty() ) setErrorOccurred(ctx.getString(R.string.cannot_obtain_data_from_blockchain)); @@ -172,6 +172,7 @@ public class ServerApiElectrum { String host; int port; String proto; + electrumRequest.setError(null); // todo - get available URL list from coinEngine, remove if( ctx.getBlockchain()==...) if (ctx.getBlockchain() == Blockchain.BitcoinTestNet) { BitcoinNodeTestNet bitcoinNodeTestNet = BitcoinNodeTestNet.values()[new Random().nextInt(BitcoinNodeTestNet.values().length)]; diff --git a/app/src/main/java/com/tangem/domain/wallet/btc/BtcData.java b/app/src/main/java/com/tangem/domain/wallet/btc/BtcData.java index 497ec30abe..f036a85aa0 100644 --- a/app/src/main/java/com/tangem/domain/wallet/btc/BtcData.java +++ b/app/src/main/java/com/tangem/domain/wallet/btc/BtcData.java @@ -18,11 +18,19 @@ public class BtcData extends CoinData { private Long balanceConfirmed, balanceUnconfirmed; public String getUnspentInputsDescription() { - int gatheredUnspents = 0; - for (int i=0; i 1) gatheredUnspents++; + try { + int gatheredUnspents = 0; + if( unspentTransactions==null ) return ""; + for (int i = 0; i < unspentTransactions.size(); i++) { + if (unspentTransactions.get(i).Raw != null && unspentTransactions.get(i).Raw.length() > 1) gatheredUnspents++; + } + return String.valueOf(unspentTransactions.size()) + " unspents (" + String.valueOf(gatheredUnspents) + " received)"; + } + catch (Exception e) + { + e.printStackTrace(); + return ""; } - return String.valueOf(unspentTransactions.size()) + " unspents (" + String.valueOf(gatheredUnspents) + " received)"; } public static class UnspentTransaction { diff --git a/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java b/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java index c3ba1ee6b4..876bc8bd17 100644 --- a/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/eth/EthEngine.java @@ -82,7 +82,7 @@ public class EthEngine extends CoinEngine { @Override public String getBalanceCurrency() { - return "ETH"; + return Blockchain.Ethereum.getCurrency(); } @Override @@ -101,7 +101,7 @@ public class EthEngine extends CoinEngine { @Override public String getFeeCurrency() { - return "ETH"; + return Blockchain.Ethereum.getCurrency(); } public boolean isNeedCheckNode() { @@ -445,11 +445,11 @@ public class EthEngine extends CoinEngine { tx.signature = new ECDSASignatureETH(r, s); int v = tx.BruteRecoveryID2(tx.signature, for_hash, pbKey); if (v != 27 && v != 28) { - Log.e("ETH", "invalid v"); + Log.e(TAG, "invalid v"); throw new Exception("Error in EthEngine - invalid v"); } tx.signature.v = (byte) v; - Log.e("ETH_v", String.valueOf(v)); + Log.e(TAG, "ETH_v: " +String.valueOf(v)); byte[] txForSend = tx.getEncoded(); notifyOnNeedSendPayment(txForSend); diff --git a/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java b/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java index cadbac4caa..23bdd7d2e5 100644 --- a/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java +++ b/app/src/main/java/com/tangem/domain/wallet/token/TokenEngine.java @@ -109,7 +109,7 @@ public class TokenEngine extends CoinEngine { if (coinData.getBalanceInInternalUnits().notZero()) { return currency; } else { - return "ETH"; + return Blockchain.Ethereum.getCurrency(); } } else { return currency; @@ -128,7 +128,7 @@ public class TokenEngine extends CoinEngine { @Override public String getFeeCurrency() { - return "ETH"; + return Blockchain.Ethereum.getCurrency(); } @Override @@ -227,7 +227,7 @@ public class TokenEngine extends CoinEngine { public Amount convertToAmount(InternalAmount internalAmount) throws Exception { if (internalAmount.getCurrency().equals("wei")) { BigDecimal d = internalAmount.divide(new BigDecimal("1000000000000000000"), getEthDecimals(), RoundingMode.DOWN); - return new Amount(d, "ETH"); + return new Amount(d, Blockchain.Ethereum.getCurrency()); } else if (internalAmount.getCurrency().equals(ctx.getCard().getTokenSymbol())) { BigDecimal p = new BigDecimal(10); p = p.pow(getTokenDecimals()); @@ -244,7 +244,7 @@ public class TokenEngine extends CoinEngine { @Override public InternalAmount convertToInternalAmount(Amount amount) throws Exception { - if (amount.getCurrency().equals("ETH")) { + if (amount.getCurrency().equals(Blockchain.Ethereum.getCurrency())) { BigDecimal d = amount.multiply(new BigDecimal("1000000000000000000")); return new InternalAmount(d, "wei"); } else if (amount.getCurrency().equals(ctx.getCard().getTokenSymbol())) { @@ -325,7 +325,7 @@ public class TokenEngine extends CoinEngine { try { if (amount.getCurrency().equals(ctx.getCard().tokenSymbol)) { balance = convertToAmount(coinData.getBalanceInInternalUnits()); - } else if (amount.getCurrency().equals("ETH") && coinData.getBalanceInInternalUnits().isZero()) { + } else if (amount.getCurrency().equals(Blockchain.Ethereum.getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) { balance = convertToAmount(coinData.getBalanceAlterInInternalUnits()); } else { return false; @@ -342,7 +342,7 @@ public class TokenEngine extends CoinEngine { if (!hasBalanceInfo()) return false; try { - Amount balance = convertToAmount(coinData.getBalanceAlterInInternalUnits()); + Amount balanceETH = convertToAmount(coinData.getBalanceAlterInInternalUnits()); if (fee == null || amount == null || fee.isZero() || amount.isZero()) return false; @@ -350,22 +350,20 @@ public class TokenEngine extends CoinEngine { if (amount.getCurrency().equals(ctx.getCard().tokenSymbol)) { // token transaction - if (fee.compareTo(balance) > 0) + if (fee.compareTo(balanceETH) > 0) return false; - } else if (amount.getCurrency().equals("ETH") && coinData.getBalanceInInternalUnits().isZero()) { + } else if (amount.getCurrency().equals(Blockchain.Ethereum.getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) { // standard ETH transaction - try { - BigDecimal cardBalance = getBalance(); - - if (isFeeIncluded && amount.compareTo(cardBalance) > 0) +// try { + if (isFeeIncluded && (amount.compareTo(balanceETH) > 0 || fee.compareTo(balanceETH) > 0)) return false; - if (!isFeeIncluded && amount.add(fee).compareTo(cardBalance) > 0) + if (!isFeeIncluded && amount.add(fee).compareTo(balanceETH) > 0) return false; - } catch (NumberFormatException e) { - e.printStackTrace(); - } +// } catch (NumberFormatException e) { +// e.printStackTrace(); +// } } else { @@ -434,7 +432,7 @@ public class TokenEngine extends CoinEngine { @Override public SignTask.PaymentToSign constructPayment(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception { - if (amountValue.getCurrency().equals("ETH")) { + if (amountValue.getCurrency().equals(Blockchain.Ethereum.getCurrency())) { return constructPaymentETH(feeValue, amountValue, IncFee, targetAddress); } else { return constructPaymentToken(feeValue, amountValue, IncFee, targetAddress); @@ -511,11 +509,11 @@ public class TokenEngine extends CoinEngine { tx.signature = new ECDSASignatureETH(r, s); int v = tx.BruteRecoveryID2(tx.signature, for_hash, pbKey); if (v != 27 && v != 28) { - Log.e("ETH", "invalid v"); + Log.e(TAG, "invalid v"); throw new Exception("Error in EthEngine - invalid v"); } tx.signature.v = (byte) v; - Log.e("ETH_v", String.valueOf(v)); + Log.e(TAG,"ETH_v "+ String.valueOf(v)); byte[] txForSend = tx.getEncoded(); notifyOnNeedSendPayment(txForSend); @@ -619,11 +617,11 @@ public class TokenEngine extends CoinEngine { tx.signature = new ECDSASignatureETH(r, s); int v = tx.BruteRecoveryID2(tx.signature, for_hash, pbKey); if (v != 27 && v != 28) { - Log.e("ETH", "invalid v"); + Log.e(TAG, "invalid v"); throw new Exception("Error in EthEngine - invalid v"); } tx.signature.v = (byte) v; - Log.e("ETH_v", String.valueOf(v)); + Log.e(TAG,"ETH_v: "+ String.valueOf(v)); byte[] txForSend = tx.getEncoded(); notifyOnNeedSendPayment(txForSend); diff --git a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt index a4a81817ad..5ab55e0c3c 100644 --- a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt @@ -62,7 +62,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { amount = CoinEngine.Amount(intent.getStringExtra(Constant.EXTRA_AMOUNT), intent.getStringExtra(Constant.EXTRA_AMOUNT_CURRENCY)) - if (ctx.blockchain == Blockchain.Token && amount.currency != "ETH") + if (ctx.blockchain == Blockchain.Token && amount.currency != Blockchain.Ethereum.currency) tvIncFee.visibility = View.INVISIBLE else tvIncFee.visibility = View.VISIBLE diff --git a/app/src/main/java/com/tangem/presentation/activity/PreparePaymentActivity.kt b/app/src/main/java/com/tangem/presentation/activity/PreparePaymentActivity.kt index 3089050014..faeffdc9a7 100644 --- a/app/src/main/java/com/tangem/presentation/activity/PreparePaymentActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/PreparePaymentActivity.kt @@ -60,7 +60,7 @@ class PreparePaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { tvBalance.text = html //TODO - to engine - if (ctx.blockchain == Blockchain.Token && engine.balance.currency!="ETH") { + if (ctx.blockchain == Blockchain.Token && engine.balance.currency!=Blockchain.Ethereum.currency) { rgIncFee!!.visibility = View.INVISIBLE } else { rgIncFee!!.visibility = View.VISIBLE diff --git a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt index 12e814738b..ca23b0aa1d 100644 --- a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt +++ b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.kt @@ -55,6 +55,7 @@ import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import java.io.InputStream import java.util.* +import kotlin.concurrent.timerTask class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notifications, SharedPreferences.OnSharedPreferenceChangeListener { companion object { @@ -83,14 +84,12 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific set(value) { field = value LOG.i(TAG, "requestCounter, set $field") - if (field <= 0 && srl != null && srl.isRefreshing) { + if (field <= 0) { LOG.e(TAG, "+++++++++++ FINISH REFRESH") - if (srl != null) srl!!.isRefreshing = false + if (srl != null && srl.isRefreshing) srl.isRefreshing = false //updateViews() - } + } else if (srl != null && !srl.isRefreshing) srl.isRefreshing = true } - private var timerRepeatRefresh: Timer? = null - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) nfcManager = NfcManager(activity, this) @@ -279,8 +278,10 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific override fun onPause() { super.onPause() nfcManager.onPause() - if (timerRepeatRefresh != null) - timerRepeatRefresh!!.cancel() + if (timerHideErrorAndMessage != null) { + timerHideErrorAndMessage!!.cancel() + timerHideErrorAndMessage = null + } } override fun onStart() { @@ -302,7 +303,10 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific @Subscribe fun onTransactionFinishWithSuccess(transactionFinishWithSuccess: TransactionFinishWithSuccess) { ctx.message = transactionFinishWithSuccess.message + ctx.coinData.clearInfo() updateViews() + srl?.isRefreshing=true + srl?.postDelayed({ refresh() }, 5000) } @Subscribe @@ -512,7 +516,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific } fun updateViews() { - if (activity == null || !UtilHelper.isOnline(activity!!)) return + if (activity == null) return if (timerHideErrorAndMessage != null) { timerHideErrorAndMessage!!.cancel() @@ -535,6 +539,21 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific tvMessage.visibility = View.VISIBLE } + if (tvError.visibility == View.VISIBLE || tvMessage.visibility == View.VISIBLE) { + timerHideErrorAndMessage = Timer() + timerHideErrorAndMessage!!.schedule( + timerTask { + activity?.runOnUiThread { + tvMessage?.visibility = View.GONE + tvError?.visibility = View.GONE + // clear only already viewed messages + if (tvMessage.text == ctx.message) ctx.message = null + if (tvError.text == ctx.error) ctx.error = null + } + }, + 5000) + } + if (srl!!.isRefreshing) { tvBalanceLine1.setTextColor(resources.getColor(R.color.primary)) tvBalanceLine1.text = getString(R.string.verifying_in_blockchain) @@ -599,63 +618,61 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific updateViews() + // Bitcoin + // Litecoin + // BitcoinCash + if (ctx.blockchain == Blockchain.Bitcoin || ctx.blockchain == Blockchain.BitcoinTestNet || + ctx.blockchain == Blockchain.Litecoin || ctx.blockchain == Blockchain.BitcoinCash) { + ctx.coinData.setIsBalanceEqual(true) + } + requestVerifyAndGetInfo() - val coinEngine = CoinEngineFactory.create(ctx) - requestCounter++ - coinEngine!!.requestBalanceAndUnspentTransactions( - object : CoinEngine.BlockchainRequestsCallbacks { - override fun onComplete(success: Boolean) { - LOG.i(TAG, "requestBalanceAndUnspentTransactions onComplete: " + success.toString() + ", request counter " + requestCounter.toString()) - if (activity == null || !UtilHelper.isOnline(activity!!)) return - requestCounter-- - if (!success) { - LOG.e(TAG, "ctx.error: " + ctx.error) + requestBalanceAndUnspentTransactions() + + requestRateInfo() + + if( requestCounter==0 ) + { + // if no connection and no requests posted + srl?.isRefreshing = false + updateViews() + } + } + + private fun requestBalanceAndUnspentTransactions() { + if (UtilHelper.isOnline(context as Activity)) { + val coinEngine = CoinEngineFactory.create(ctx) + requestCounter++ + coinEngine!!.requestBalanceAndUnspentTransactions( + object : CoinEngine.BlockchainRequestsCallbacks { + override fun onComplete(success: Boolean) { + LOG.i(TAG, "requestBalanceAndUnspentTransactions onComplete: " + success.toString() + ", request counter " + requestCounter.toString()) + if (activity == null) return + requestCounter-- + if (!success) { + LOG.e(TAG, "requestBalanceAndUnspentTransactions ctx.error: " + ctx.error) + } +// if (!UtilHelper.isOnline(activity!!)) { +// ctx.error = getString(R.string.no_connection) +// } + updateViews() + } + + override fun onProgress() { + if (activity == null) return + LOG.i(TAG, "requestBalanceAndUnspentTransactions onProgress") + updateViews() + } + + override fun allowAdvance(): Boolean { + return UtilHelper.isOnline(context as Activity) } - updateViews() } - - override fun onProgress() { - if (activity == null || !UtilHelper.isOnline(activity!!)) return - LOG.i(TAG, "requestBalanceAndUnspentTransactions onProgress") - updateViews() - } - - override fun allowAdvance(): Boolean { - return UtilHelper.isOnline(context as Activity) - } - } - ) - - // TODO - move requestRateInfo to CoinEngine - // Bitcoin - if (ctx.blockchain == Blockchain.Bitcoin || ctx.blockchain == Blockchain.BitcoinTestNet) { - ctx.coinData.setIsBalanceEqual(true) - - requestRateInfo("bitcoin") - } - - // Litecoin - if (ctx.blockchain == Blockchain.Litecoin) { - ctx.coinData.setIsBalanceEqual(true) - - requestRateInfo("litecoin") - } - - // BitcoinCash - else if (ctx.blockchain == Blockchain.BitcoinCash) { - ctx.coinData.setIsBalanceEqual(true) - requestRateInfo("bitcoin-cash") - } - - // Ethereum - else if (ctx.blockchain == Blockchain.Ethereum || ctx.blockchain == Blockchain.EthereumTestNet) { - requestRateInfo("ethereum") - } - - // Token - else if (ctx.blockchain == Blockchain.Token) { - requestRateInfo("ethereum") + ) + } else { + ctx.error = getString(R.string.no_connection) + updateViews() } } @@ -667,20 +684,38 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific serverApiTangem.cardVerifyAndGetInfo(ctx.card) } } else { - Toast.makeText(activity, getString(R.string.no_connection), Toast.LENGTH_SHORT).show() - LOG.e(TAG, "+++++++++++ Hide refresh 1") - srl?.isRefreshing = false + ctx.error = getString(R.string.no_connection) + updateViews() + //Toast.makeText(activity, getString(R.string.no_connection), Toast.LENGTH_SHORT).show() + //LOG.e(TAG, "+++++++++++ Hide refresh 1") + //srl?.isRefreshing = false } } - private fun requestRateInfo(cryptoId: String) { + private fun requestRateInfo() { if (UtilHelper.isOnline(context as Activity)) { LOG.i(TAG, "requestRateInfo") + + // TODO - move requestRateInfo to CoinEngine + val cryptoId: String = when (ctx.blockchain) { + Blockchain.Bitcoin -> "bitcoin" + Blockchain.BitcoinTestNet -> "bitcoin" + Blockchain.Ethereum -> "ethereum" + Blockchain.EthereumTestNet -> "ethereum" + Blockchain.Token -> "ethereum" + Blockchain.BitcoinCash -> "bitcoin-cash" + Blockchain.Litecoin -> "litecoin" + else -> { + throw Exception("Can''t get rate for blockchain " + ctx.blockchainName) + } + } serverApiCommon.rateInfoData(cryptoId) } else { - Toast.makeText(activity, getString(R.string.no_connection), Toast.LENGTH_SHORT).show() - LOG.e(TAG, "+++++++++++ Hide refresh 2") - srl?.isRefreshing = false + ctx.error = getString(R.string.no_connection) + updateViews() +// Toast.makeText(activity, getString(R.string.no_connection), Toast.LENGTH_SHORT).show() +// LOG.e(TAG, "+++++++++++ Hide refresh 2") +// srl?.isRefreshing = false } }