Updated on 2026-08-14

This commit is contained in:
Tangem 2019-01-28 13:27:54 +03:00
parent 55ed35dab5
commit 8c00243de3
5 changed files with 95 additions and 488 deletions

View file

@ -11,11 +11,11 @@ public enum Blockchain {
BitcoinTestNet("BTC/test", "BTC", 100000000.0, R.drawable.ic_logo_bitcoin_testnet, "Bitcoin Testnet"),
Ethereum("ETH", "ETH", 1.0, R.drawable.ic_logo_ethereum, "Ethereum"),
EthereumTestNet("ETH/test", "ETH", 1.0, R.drawable.ic_logo_ethereum_testnet, "Ethereum Testnet"),
Token("Token", "ERC20", 1.0, R.drawable.ic_logo_bat_token, "Ethereum"),
Token("Token", "ETH", 1.0, R.drawable.ic_logo_bat_token, "Ethereum"),
BitcoinCash("BCH", "BCH", 100000000.0, R.drawable.ic_logo_bitcoin_cash, "Bitcoin Cash"),
Litecoin("LTC", "LTC", 100000000.0, R.drawable.ic_logo_bitcoin, "Litecoin"),
Rootstock("RSK", "RBTC", 1.0, R.drawable.ic_logo_bitcoin, "Rootstock"),
RootstockToken("Token", "ERC20", 1.0, R.drawable.ic_logo_bat_token, "Rootstock");
RootstockToken("Token", "RSK", 1.0, R.drawable.ic_logo_bat_token, "Rootstock");
Blockchain(String ID, String currency, double multiplier, int imageResource, String officialName) {
mID = ID;

View file

@ -45,7 +45,7 @@ public class EthEngine extends CoinEngine {
} else if (ctx.getCoinData() instanceof EthData) {
coinData = (EthData) ctx.getCoinData();
} else {
throw new Exception("Invalid type of Blockchain data for EthEngine");
throw new Exception("Invalid type of Blockchain data for " + this.getClass().getSimpleName());
}
}
@ -57,7 +57,7 @@ public class EthEngine extends CoinEngine {
return 18;
}
protected int getChainId() {
protected int getChainIdNum() {
return ctx.getBlockchain() == Blockchain.Ethereum ? EthTransaction.ChainEnum.Mainnet.getValue() : EthTransaction.ChainEnum.Rinkeby.getValue();
}
@ -395,6 +395,7 @@ public class EthEngine extends CoinEngine {
BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(21000));
BigInteger gasLimit = BigInteger.valueOf(21000);
Integer chainId = this.getChainIdNum();
String to = targetAddress;
@ -402,7 +403,7 @@ public class EthEngine extends CoinEngine {
to = to.substring(2);
}
final EthTransaction tx = EthTransaction.create(to, weiAmount, nonceValue, gasPrice, gasLimit, this.getChainId());
final EthTransaction tx = EthTransaction.create(to, weiAmount, nonceValue, gasPrice, gasLimit, chainId);
return new SignTask.PaymentToSign() {
@Override
@ -419,12 +420,12 @@ public class EthEngine extends CoinEngine {
@Override
public byte[] getRawDataToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for ETH");
throw new Exception("Signing of raw transaction not supported for " + this.getClass().getSimpleName());
}
@Override
public String getHashAlgToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for ETH");
throw new Exception("Signing of raw transaction not supported for " + this.getClass().getSimpleName());
}
@Override
@ -442,17 +443,17 @@ public class EthEngine extends CoinEngine {
boolean f = ECKey.verify(for_hash, new ECKey.ECDSASignature(r, s), pbKey);
if (!f) {
Log.e("ETH-CHECK", "sign Failed.");
Log.e(this.getClass().getSimpleName() + "-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(TAG, "invalid v");
throw new Exception("Error in EthEngine - invalid v");
Log.e(this.getClass().getSimpleName(), "invalid v");
throw new Exception("Error in " + this.getClass().getSimpleName() + " - invalid v");
}
tx.signature.v = (byte) v;
Log.e(TAG, "ETH_v: " +String.valueOf(v));
Log.e(this.getClass().getSimpleName(), " V: " +String.valueOf(v));
byte[] txForSend = tx.getEncoded();
notifyOnNeedSendPayment(txForSend);

View file

@ -28,15 +28,7 @@ public class RskEngine extends EthEngine {
private static final String TAG = RskEngine.class.getSimpleName();
public RskEngine(TangemContext ctx) throws Exception {
this.ctx = ctx;
if (ctx.getCoinData() == null) {
coinData = new EthData();
ctx.setCoinData(coinData);
} else if (ctx.getCoinData() instanceof EthData) {
coinData = (EthData) ctx.getCoinData();
} else {
throw new Exception("Invalid type of Blockchain data for RskEngine");
}
super(ctx);
}
public RskEngine() {
@ -48,7 +40,7 @@ public class RskEngine extends EthEngine {
}
@Override
protected int getChainId() {
protected int getChainIdNum() {
return EthTransaction.ChainEnum.Rootstock_mainnet.getValue();
}
@ -63,93 +55,13 @@ public class RskEngine extends EthEngine {
}
@Override
public Uri getShareWalletUri() { return Uri.parse(ctx.getCoinData().getWallet()); }
public Uri getShareWalletUri() {
return Uri.parse(ctx.getCoinData().getWallet());
}
@Override
public Uri getShareWalletUriExplorer() { return Uri.parse("https://explorer.rsk.co/address/" + ctx.getCoinData().getWallet()); }
@Override
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"));
BigInteger nonceValue = coinData.getConfirmedTXCount();
byte[] pbKey = ctx.getCard().getWalletPublicKey();
BigInteger weiFee = convertToInternalAmount(feeValue).toBigIntegerExact();
BigInteger weiAmount = convertToInternalAmount(amountValue).toBigIntegerExact();
if (IncFee) {
weiAmount = weiAmount.subtract(weiFee);
}
BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(21000));
BigInteger gasLimit = BigInteger.valueOf(21000);
Integer chainId = EthTransaction.ChainEnum.Rootstock_mainnet.getValue();
String to = targetAddress;
if (to.startsWith("0x") || to.startsWith("0X")) {
to = to.substring(2);
}
final EthTransaction tx = EthTransaction.create(to, weiAmount, nonceValue, gasPrice, gasLimit, chainId);
return new SignTask.PaymentToSign() {
@Override
public boolean isSigningMethodSupported(TangemCard.SigningMethod signingMethod) {
return signingMethod == TangemCard.SigningMethod.Sign_Hash;
}
@Override
public byte[][] getHashesToSign() {
byte[][] hashesForSign = new byte[1][];
hashesForSign[0] = tx.getRawHash();
return hashesForSign;
}
@Override
public byte[] getRawDataToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for RSK");
}
@Override
public String getHashAlgToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for RSK");
}
@Override
public byte[] getIssuerTransactionSignature(byte[] dataToSignByIssuer) throws Exception {
throw new Exception("Transaction validation by issuer not supported in this version");
}
@Override
public byte[] onSignCompleted(byte[] signFromCard) throws Exception {
byte[] for_hash = tx.getRawHash();
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("RSK-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(TAG, "invalid v");
throw new Exception("Error in RskEngine - invalid v");
}
tx.signature.v = (byte) v;
Log.e(TAG, "RSK_v: " +String.valueOf(v));
byte[] txForSend = tx.getEncoded();
notifyOnNeedSendPayment(txForSend);
return txForSend;
}
};
public Uri getShareWalletUriExplorer() {
return Uri.parse("https://explorer.rsk.co/address/" + ctx.getCoinData().getWallet());
}
@Override
@ -277,7 +189,7 @@ public class RskEngine extends EthEngine {
blockchainRequestsCallbacks.onComplete(false);
} else {
BigInteger nonce = coinData.getConfirmedTXCount();
nonce=nonce.add(BigInteger.valueOf(1));
nonce = nonce.add(BigInteger.valueOf(1));
coinData.setConfirmedTXCount(nonce);
ctx.setError(null);
blockchainRequestsCallbacks.onComplete(true);

View file

@ -1,7 +1,6 @@
package com.tangem.domain.wallet.rsk;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.google.common.base.Strings;
@ -13,8 +12,6 @@ import com.tangem.domain.wallet.CoinEngine;
import com.tangem.domain.wallet.ECDSASignatureETH;
import com.tangem.domain.wallet.EthTransaction;
import com.tangem.domain.wallet.TangemContext;
import com.tangem.domain.wallet.eth.EthData;
import com.tangem.domain.wallet.token.TokenData;
import com.tangem.domain.wallet.token.TokenEngine;
import com.tangem.tangemcard.data.TangemCard;
import com.tangem.tangemcard.tasks.SignTask;
@ -34,21 +31,6 @@ public class RskTokenEngine extends TokenEngine {
public RskTokenEngine(TangemContext ctx) throws Exception {
super(ctx);
if (ctx.getCoinData() == null) {
coinData = new TokenData();
ctx.setCoinData(coinData);
} else if (ctx.getCoinData() instanceof TokenData) {
coinData = (TokenData) ctx.getCoinData();
} else if (ctx.getCoinData() instanceof EthData) {
// special case with receive card data substitution from server at the moment
Bundle B=new Bundle();
ctx.getCoinData().saveToBundle(B);
coinData = new TokenData();
coinData.loadFromBundle(B);
ctx.setCoinData(coinData);
} else {
throw new Exception("Invalid type of Blockchain data for RskTokenEngine");
}
}
public RskTokenEngine() {
@ -56,60 +38,25 @@ public class RskTokenEngine extends TokenEngine {
}
@Override
public String getBalanceCurrency() {
String currency = ctx.getCard().getTokenSymbol();
if (Strings.isNullOrEmpty(currency))
return "NoN";
if (hasBalanceInfo()) {
if (coinData.getBalanceInInternalUnits().notZero()) {
return currency;
} else {
return Blockchain.Rootstock.getCurrency();
}
} else {
return currency;
}
public Blockchain getBlockchain() {
return Blockchain.RootstockToken;
}
@Override
public String getFeeCurrency() {
return Blockchain.Rootstock.getCurrency();
protected int getChainIdNum() {
return EthTransaction.ChainEnum.Rootstock_mainnet.getValue();
}
@Override
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, Blockchain.Rootstock.getCurrency());
} else if (internalAmount.getCurrency().equals(ctx.getCard().getTokenSymbol())) {
BigDecimal p = new BigDecimal(10);
p = p.pow(getTokenDecimals());
BigDecimal d = internalAmount.divide(p);
return new Amount(d, ctx.getCard().getTokenSymbol());
}
throw new Exception(String.format("Can't convert '%s' to '%s'", internalAmount.getCurrency(), ctx.getCard().getTokenSymbol()));
public Uri getShareWalletUriExplorer() {
return Uri.parse("https://explorer.rsk.co/address/" + ctx.getCoinData().getWallet());
} // Only RSK explorer for now
@Override
public Uri getShareWalletUri() {
return Uri.parse(ctx.getCoinData().getWallet());
}
@Override
public InternalAmount convertToInternalAmount(Amount amount) throws Exception {
if (amount.getCurrency().equals(Blockchain.Rootstock.getCurrency())) {
BigDecimal d = amount.multiply(new BigDecimal("1000000000000000000"));
return new InternalAmount(d, "wei");
} else if (amount.getCurrency().equals(ctx.getCard().getTokenSymbol())) {
BigDecimal p = new BigDecimal(10);
p = p.pow(getTokenDecimals());
BigDecimal d = amount.multiply(p);
return new InternalAmount(d, ctx.getCard().getTokenSymbol());
}
throw new Exception(String.format("Can't convert '%s' to '%s'", amount.getCurrency(), ctx.getCard().getTokenSymbol()));
}
@Override
public Uri getShareWalletUriExplorer() { return Uri.parse("https://explorer.rsk.co/address/" + ctx.getCoinData().getWallet()); } // Only RSK explorer for now
@Override
public Uri getShareWalletUri() { return Uri.parse(ctx.getCoinData().getWallet()); }
@Override
public boolean isExtractPossible() {
if (!hasBalanceInfo()) {
@ -126,266 +73,6 @@ public class RskTokenEngine extends TokenEngine {
return false;
}
@Override
public boolean checkNewTransactionAmount(Amount amount) {
if (!hasBalanceInfo()) return false;
Amount balance;
try {
if (amount.getCurrency().equals(ctx.getCard().tokenSymbol)) {
balance = convertToAmount(coinData.getBalanceInInternalUnits());
} else if (amount.getCurrency().equals(Blockchain.Rootstock.getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) {
balance = convertToAmount(coinData.getBalanceAlterInInternalUnits());
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return amount.compareTo(balance) <= 0;
}
@Override
public boolean checkNewTransactionAmountAndFee(Amount amount, Amount fee, Boolean isFeeIncluded) {
if (!hasBalanceInfo()) return false;
try {
Amount balanceRBTC = convertToAmount(coinData.getBalanceAlterInInternalUnits());
if (fee == null || amount == null || fee.isZero() || amount.isZero())
return false;
if (amount.getCurrency().equals(ctx.getCard().tokenSymbol)) {
// token transaction
if (fee.compareTo(balanceRBTC) > 0)
return false;
} else if (amount.getCurrency().equals(Blockchain.Rootstock.getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) {
// standard RBTC transaction
// try {
if (isFeeIncluded && (amount.compareTo(balanceRBTC) > 0 || fee.compareTo(balanceRBTC) > 0))
return false;
if (!isFeeIncluded && amount.add(fee).compareTo(balanceRBTC) > 0)
return false;
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
} else
{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public SignTask.PaymentToSign constructPayment(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception {
if (amountValue.getCurrency().equals(Blockchain.Rootstock.getCurrency())) {
return constructPaymentRBTC(feeValue, amountValue, IncFee, targetAddress);
} else {
return constructPaymentToken(feeValue, amountValue, IncFee, targetAddress);
}
}
private SignTask.PaymentToSign constructPaymentRBTC(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress) throws Exception {
Log.e(TAG, "Construct RBTC payment "+amountValue.toString()+" with fee "+feeValue.toString()+(IncFee?" including":" excluding"));
BigInteger nonceValue = coinData.getConfirmedTXCount();
byte[] pbKey = ctx.getCard().getWalletPublicKey();
BigInteger weiFee = convertToInternalAmount(feeValue).toBigIntegerExact();
BigInteger weiAmount = convertToInternalAmount(amountValue).toBigIntegerExact();
if (IncFee) {
weiAmount = weiAmount.subtract(weiFee);
}
BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(21000));
BigInteger gasLimit = BigInteger.valueOf(21000);
Integer chainId = EthTransaction.ChainEnum.Rootstock_mainnet.getValue();
String to = targetAddress;
if (to.startsWith("0x") || to.startsWith("0X")) {
to = to.substring(2);
}
final EthTransaction tx = EthTransaction.create(to, weiAmount, nonceValue, gasPrice, gasLimit, chainId);
return new SignTask.PaymentToSign() {
@Override
public boolean isSigningMethodSupported(TangemCard.SigningMethod signingMethod) {
return signingMethod == TangemCard.SigningMethod.Sign_Hash;
}
@Override
public byte[][] getHashesToSign() {
byte[][] hashesForSign = new byte[1][];
hashesForSign[0] = tx.getRawHash();
return hashesForSign;
}
@Override
public byte[] getRawDataToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for RSK");
}
@Override
public String getHashAlgToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for RSK");
}
@Override
public byte[] getIssuerTransactionSignature(byte[] dataToSignByIssuer) throws Exception {
throw new Exception("Transaction validation by issuer not supported in this version");
}
@Override
public byte[] onSignCompleted(byte[] signFromCard) throws Exception {
byte[] for_hash = tx.getRawHash();
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("RSK-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(TAG, "invalid v");
throw new Exception("Error in RskTokenEngine - invalid v");
}
tx.signature.v = (byte) v;
Log.e(TAG,"RSK_v "+ String.valueOf(v));
byte[] txForSend = tx.getEncoded();
notifyOnNeedSendPayment(txForSend);
return txForSend;
}
};
}
private SignTask.PaymentToSign constructPaymentToken(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress) throws Exception {
Log.e(TAG, "Construct TOKEN payment "+amountValue.toString()+" with fee "+feeValue.toString()+(IncFee?" including":" excluding"));
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 gasPrice = weiFee.divide(BigInteger.valueOf(60000));
BigInteger gasLimit = BigInteger.valueOf(60000);
Integer chainId = EthTransaction.ChainEnum.Rootstock_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, nonceValue, gasPrice, gasLimit, chainId, data);
return new SignTask.PaymentToSign() {
@Override
public boolean isSigningMethodSupported(TangemCard.SigningMethod signingMethod) {
return signingMethod == TangemCard.SigningMethod.Sign_Hash;
}
@Override
public byte[][] getHashesToSign() {
byte[][] hashesForSign = new byte[1][];
hashesForSign[0] = tx.getRawHash();
return hashesForSign;
}
@Override
public byte[] getRawDataToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for RSK");
}
@Override
public String getHashAlgToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for RSK");
}
@Override
public byte[] getIssuerTransactionSignature(byte[] dataToSignByIssuer) throws Exception {
throw new Exception("Transaction validation by issuer not supported in this version");
}
@Override
public byte[] onSignCompleted(byte[] signFromCard) throws Exception {
byte[] for_hash = tx.getRawHash();
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("RSK-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(TAG, "invalid v");
throw new Exception("Error in RskTokenEngine - invalid v");
}
tx.signature.v = (byte) v;
Log.e(TAG,"RSK_v: "+ String.valueOf(v));
byte[] txForSend = tx.getEncoded();
notifyOnNeedSendPayment(txForSend);
return txForSend;
}
};
}
@Override
public void requestBalanceAndUnspentTransactions(BlockchainRequestsCallbacks blockchainRequestsCallbacks) {
final ServerApiRootstock serverApiRootstock = new ServerApiRootstock();
@ -483,27 +170,28 @@ public class RskTokenEngine extends TokenEngine {
// rounding gas price to integer gwei
BigInteger l = new BigInteger(gasPrice, 16);
Log.i(TAG, "Rootstock gas price: "+gasPrice+" ("+l.toString()+")");
Log.i(TAG, "Rootstock gas price: " + gasPrice + " (" + l.toString() + ")");
BigInteger m;
if (!amount.getCurrency().equals(Blockchain.Rootstock.getCurrency())) m = BigInteger.valueOf(60000);
if (!amount.getCurrency().equals(Blockchain.Rootstock.getCurrency()))
m = BigInteger.valueOf(60000);
else m = BigInteger.valueOf(21000);
Log.i(TAG, "fee multiplier: "+m.toString());
Log.i(TAG, "fee multiplier: " + m.toString());
CoinEngine.InternalAmount weiMinFee = new CoinEngine.InternalAmount(l.multiply(m), "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");
Log.i(TAG, "min fee : " + weiMinFee.toValueString() + " wei");
Log.i(TAG, "normal fee: " + weiNormalFee.toValueString() + " wei");
Log.i(TAG, "max fee : " + weiMaxFee.toValueString() + " wei");
try {
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());
Log.i(TAG, "min fee : " + coinData.minFee.toString());
Log.i(TAG, "normal fee: " + coinData.normalFee.toString());
Log.i(TAG, "max fee : " + coinData.maxFee.toString());
} catch (Exception e) {
e.printStackTrace();
}
@ -537,7 +225,7 @@ public class RskTokenEngine extends TokenEngine {
blockchainRequestsCallbacks.onComplete(false);
} else {
BigInteger nonce = coinData.getConfirmedTXCount();
nonce=nonce.add(BigInteger.valueOf(1));
nonce = nonce.add(BigInteger.valueOf(1));
coinData.setConfirmedTXCount(nonce);
ctx.setError(null);
blockchainRequestsCallbacks.onComplete(true);

View file

@ -49,13 +49,13 @@ public class TokenEngine extends CoinEngine {
coinData = (TokenData) ctx.getCoinData();
} else if (ctx.getCoinData() instanceof EthData) {
// special case with receive card data substitution from server at the moment
Bundle B=new Bundle();
Bundle B = new Bundle();
ctx.getCoinData().saveToBundle(B);
coinData = new TokenData();
coinData.loadFromBundle(B);
ctx.setCoinData(coinData);
} else {
throw new Exception("Invalid type of Blockchain data for TokenEngine");
throw new Exception("Invalid type of Blockchain data for " + this.getClass().getSimpleName());
}
}
@ -63,6 +63,13 @@ public class TokenEngine extends CoinEngine {
super();
}
public Blockchain getBlockchain() {
return Blockchain.Token;
}
protected int getChainIdNum() {
return ctx.getBlockchain() == Blockchain.Ethereum ? EthTransaction.ChainEnum.Mainnet.getValue() : EthTransaction.ChainEnum.Rinkeby.getValue();
}
@Override
public boolean awaitingConfirmation() {
@ -90,7 +97,7 @@ public class TokenEngine extends CoinEngine {
public String getBalanceHTML() {
if (hasBalanceInfo()) {
try {
return " " + convertToAmount(coinData.getBalanceInInternalUnits()).toDescriptionString(getTokenDecimals()) + " <br><small><small> + " + convertToAmount(coinData.getBalanceAlterInInternalUnits()).toDescriptionString(getEthDecimals()) + " for fee</small></small>";
return " " + convertToAmount(coinData.getBalanceInInternalUnits()).toDescriptionString(getTokenDecimals()) + " <br><small><small> + " + convertToAmount(coinData.getBalanceAlterInInternalUnits()).toDescriptionString(getChainDecimals()) + " for fee</small></small>";
} catch (Exception e) {
e.printStackTrace();
return "";
@ -109,7 +116,7 @@ public class TokenEngine extends CoinEngine {
if (coinData.getBalanceInInternalUnits().notZero()) {
return currency;
} else {
return Blockchain.Ethereum.getCurrency();
return this.getBlockchain().getCurrency();
}
} else {
return currency;
@ -122,13 +129,13 @@ public class TokenEngine extends CoinEngine {
if (coinData.getBalanceInInternalUnits().notZero()) {
return new InputFilter[]{new DecimalDigitsInputFilter(getTokenDecimals())};
} else {
return new InputFilter[]{new DecimalDigitsInputFilter(getEthDecimals())};
return new InputFilter[]{new DecimalDigitsInputFilter(getChainDecimals())};
}
}
@Override
public String getFeeCurrency() {
return Blockchain.Ethereum.getCurrency();
return this.getBlockchain().getCurrency();
}
@Override
@ -136,7 +143,7 @@ public class TokenEngine extends CoinEngine {
return ctx.getString(R.string.not_implemented);
}
protected static int getEthDecimals() {
protected static int getChainDecimals() {
return 18;
}
@ -178,7 +185,8 @@ public class TokenEngine extends CoinEngine {
@Override
public boolean isBalanceNotZero() {
if (coinData == null) return false;
if (coinData.getBalanceInInternalUnits() == null && coinData.getBalanceAlterInInternalUnits() == null) return false;
if (coinData.getBalanceInInternalUnits() == null && coinData.getBalanceAlterInInternalUnits() == null)
return false;
return coinData.getBalanceInInternalUnits().notZero() || coinData.getBalanceAlterInInternalUnits().notZero();
}
@ -226,8 +234,8 @@ public class TokenEngine extends CoinEngine {
@Override
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, Blockchain.Ethereum.getCurrency());
BigDecimal d = internalAmount.divide(new BigDecimal("1000000000000000000"), getChainDecimals(), RoundingMode.DOWN);
return new Amount(d, this.getBlockchain().getCurrency());
} else if (internalAmount.getCurrency().equals(ctx.getCard().getTokenSymbol())) {
BigDecimal p = new BigDecimal(10);
p = p.pow(getTokenDecimals());
@ -244,7 +252,7 @@ public class TokenEngine extends CoinEngine {
@Override
public InternalAmount convertToInternalAmount(Amount amount) throws Exception {
if (amount.getCurrency().equals(Blockchain.Ethereum.getCurrency())) {
if (amount.getCurrency().equals(this.getBlockchain().getCurrency())) {
BigDecimal d = amount.multiply(new BigDecimal("1000000000000000000"));
return new InternalAmount(d, "wei");
} else if (amount.getCurrency().equals(ctx.getCard().getTokenSymbol())) {
@ -325,7 +333,7 @@ public class TokenEngine extends CoinEngine {
try {
if (amount.getCurrency().equals(ctx.getCard().tokenSymbol)) {
balance = convertToAmount(coinData.getBalanceInInternalUnits());
} else if (amount.getCurrency().equals(Blockchain.Ethereum.getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) {
} else if (amount.getCurrency().equals(this.getBlockchain().getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) {
balance = convertToAmount(coinData.getBalanceAlterInInternalUnits());
} else {
return false;
@ -352,21 +360,19 @@ public class TokenEngine extends CoinEngine {
// token transaction
if (fee.compareTo(balanceETH) > 0)
return false;
} else if (amount.getCurrency().equals(Blockchain.Ethereum.getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) {
} else if (amount.getCurrency().equals(this.getBlockchain().getCurrency()) && coinData.getBalanceInInternalUnits().isZero()) {
// standard ETH transaction
// try {
if (isFeeIncluded && (amount.compareTo(balanceETH) > 0 || fee.compareTo(balanceETH) > 0))
return false;
if (isFeeIncluded && (amount.compareTo(balanceETH) > 0 || fee.compareTo(balanceETH) > 0))
return false;
if (!isFeeIncluded && amount.add(fee).compareTo(balanceETH) > 0)
return false;
if (!isFeeIncluded && amount.add(fee).compareTo(balanceETH) > 0)
return false;
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
} else
{
} else {
return false;
}
} catch (Exception e) {
@ -432,15 +438,15 @@ public class TokenEngine extends CoinEngine {
@Override
public SignTask.PaymentToSign constructPayment(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception {
if (amountValue.getCurrency().equals(Blockchain.Ethereum.getCurrency())) {
return constructPaymentETH(feeValue, amountValue, IncFee, targetAddress);
if (amountValue.getCurrency().equals(this.getBlockchain().getCurrency())) {
return constructPaymentCoin(feeValue, amountValue, IncFee, targetAddress);
} else {
return constructPaymentToken(feeValue, amountValue, IncFee, targetAddress);
}
}
private SignTask.PaymentToSign constructPaymentETH(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress) throws Exception {
Log.e(TAG, "Construct ETH payment "+amountValue.toString()+" with fee "+feeValue.toString()+(IncFee?" including":" excluding"));
protected SignTask.PaymentToSign constructPaymentCoin(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress) throws Exception {
Log.e(TAG, "Construct " + this.getBlockchain().getCurrency() + " payment " + amountValue.toString() + " with fee " + feeValue.toString() + (IncFee ? " including" : " excluding"));
BigInteger nonceValue = coinData.getConfirmedTXCount();
byte[] pbKey = ctx.getCard().getWalletPublicKey();
@ -454,8 +460,7 @@ public class TokenEngine extends CoinEngine {
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!!!
Integer chainId = this.getChainIdNum(); // Token support on main net only!!!
String to = targetAddress;
@ -480,12 +485,12 @@ public class TokenEngine extends CoinEngine {
@Override
public byte[] getRawDataToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for ETH");
throw new Exception("Signing of raw transaction not supported for " + this.getClass().getSimpleName());
}
@Override
public String getHashAlgToSign() throws Exception {
throw new Exception("Signing of raw transaction not supported for ETH");
throw new Exception("Signing of raw transaction not supported for " + this.getClass().getSimpleName());
}
@Override
@ -503,17 +508,17 @@ public class TokenEngine extends CoinEngine {
boolean f = ECKey.verify(for_hash, new ECKey.ECDSASignature(r, s), pbKey);
if (!f) {
Log.e("ETH-CHECK", "sign Failed.");
Log.e(this.getClass().getSimpleName() + "-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(TAG, "invalid v");
throw new Exception("Error in EthEngine - invalid v");
throw new Exception("Error in " + this.getClass().getSimpleName() + " - invalid v");
}
tx.signature.v = (byte) v;
Log.e(TAG,"ETH_v "+ String.valueOf(v));
Log.e(TAG, this.getClass().getSimpleName() + " V: " + String.valueOf(v));
byte[] txForSend = tx.getEncoded();
notifyOnNeedSendPayment(txForSend);
@ -522,8 +527,8 @@ public class TokenEngine extends CoinEngine {
};
}
private SignTask.PaymentToSign constructPaymentToken(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress) throws Exception {
Log.e(TAG, "Construct TOKEN payment "+amountValue.toString()+" with fee "+feeValue.toString()+(IncFee?" including":" excluding"));
protected SignTask.PaymentToSign constructPaymentToken(Amount feeValue, Amount amountValue, boolean IncFee, String targetAddress) throws Exception {
Log.e(TAG, "Construct TOKEN payment " + amountValue.toString() + " with fee " + feeValue.toString() + (IncFee ? " including" : " excluding"));
BigInteger nonceValue = coinData.getConfirmedTXCount();
byte[] pbKey = ctx.getCard().getWalletPublicKey();
@ -543,7 +548,7 @@ public class TokenEngine extends CoinEngine {
BigInteger gasPrice = weiFee.divide(BigInteger.valueOf(60000));
BigInteger gasLimit = BigInteger.valueOf(60000);
Integer chainId = EthTransaction.ChainEnum.Mainnet.getValue();
Integer chainId = this.getChainIdNum();
BigInteger amountZero = BigInteger.ZERO;
String to = targetAddress;
@ -611,17 +616,17 @@ public class TokenEngine extends CoinEngine {
boolean f = ECKey.verify(for_hash, new ECKey.ECDSASignature(r, s), pbKey);
if (!f) {
Log.e("ETH-CHECK", "sign Failed.");
Log.e(this.getClass().getSimpleName() + "-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(TAG, "invalid v");
throw new Exception("Error in EthEngine - invalid v");
throw new Exception("Error in " + this.getClass().getSimpleName() + " - invalid v");
}
tx.signature.v = (byte) v;
Log.e(TAG,"ETH_v: "+ String.valueOf(v));
Log.e(TAG, this.getClass().getSimpleName() + " V: " + String.valueOf(v));
byte[] txForSend = tx.getEncoded();
notifyOnNeedSendPayment(txForSend);
@ -729,27 +734,28 @@ public class TokenEngine extends CoinEngine {
// rounding gas price to integer gwei
BigInteger l = new BigInteger(gasPrice, 16);
Log.i(TAG, "Infura gas price: "+gasPrice+" ("+l.toString()+")");
Log.i(TAG, "Infura gas price: " + gasPrice + " (" + l.toString() + ")");
BigInteger m;
if (!amount.getCurrency().equals(Blockchain.Ethereum.getCurrency())) m = BigInteger.valueOf(60000);
if (!amount.getCurrency().equals(Blockchain.Ethereum.getCurrency()))
m = BigInteger.valueOf(60000);
else m = BigInteger.valueOf(21000);
Log.i(TAG, "fee multiplier: "+m.toString());
Log.i(TAG, "fee multiplier: " + m.toString());
CoinEngine.InternalAmount weiMinFee = new CoinEngine.InternalAmount(l.multiply(m), "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");
Log.i(TAG, "min fee : " + weiMinFee.toValueString() + " wei");
Log.i(TAG, "normal fee: " + weiNormalFee.toValueString() + " wei");
Log.i(TAG, "max fee : " + weiMaxFee.toValueString() + " wei");
try {
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());
Log.i(TAG, "min fee : " + coinData.minFee.toString());
Log.i(TAG, "normal fee: " + coinData.normalFee.toString());
Log.i(TAG, "max fee : " + coinData.maxFee.toString());
} catch (Exception e) {
e.printStackTrace();
}
@ -783,7 +789,7 @@ public class TokenEngine extends CoinEngine {
blockchainRequestsCallbacks.onComplete(false);
} else {
BigInteger nonce = coinData.getConfirmedTXCount();
nonce=nonce.add(BigInteger.valueOf(1));
nonce = nonce.add(BigInteger.valueOf(1));
coinData.setConfirmedTXCount(nonce);
ctx.setError(null);
blockchainRequestsCallbacks.onComplete(true);