Updated on 2026-08-14

This commit is contained in:
Tangem 2018-05-30 14:05:11 +03:00
parent 8a0b654663
commit 7ab858de77
20 changed files with 129 additions and 133 deletions

View file

@ -6,6 +6,7 @@ package com.tangem.domain.wallet;
import android.util.Log;
import com.tangem.util.FormatUtil;
import com.tangem.util.Util;
import java.io.ByteArrayOutputStream;

View file

@ -4,6 +4,7 @@ import android.net.Uri;
import com.tangem.domain.cardReader.CardProtocol;
import com.tangem.domain.cardReader.TLV;
import com.tangem.util.FormatUtil;
import com.tangem.util.Util;
import java.io.ByteArrayOutputStream;
@ -18,7 +19,7 @@ import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static com.tangem.domain.wallet.FormatUtil.GetDecimalFormat;
import static com.tangem.util.FormatUtil.GetDecimalFormat;
/**
* Created by Ilia on 15.02.2018.

View file

@ -4,6 +4,7 @@ import android.net.Uri;
import com.tangem.domain.cardReader.CardProtocol;
import com.tangem.domain.cardReader.TLV;
import com.tangem.util.FormatUtil;
import com.tangem.util.Util;
import java.io.ByteArrayOutputStream;
@ -19,7 +20,7 @@ import java.util.Date;
import java.util.List;
import java.util.Random;
import static com.tangem.domain.wallet.FormatUtil.GetDecimalFormat;
import static com.tangem.util.FormatUtil.GetDecimalFormat;
/**
* Created by Ilia on 15.02.2018.

View file

@ -1,39 +0,0 @@
package com.tangem.domain.wallet;
public class ByteUtil {
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static byte[] and(byte[] b1, byte[] b2) {
if (b1.length != b2.length) throw new RuntimeException("Array sizes differ");
byte[] ret = new byte[b1.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = (byte) (b1[i] & b2[i]);
}
return ret;
}
public static byte[] or(byte[] b1, byte[] b2) {
if (b1.length != b2.length) throw new RuntimeException("Array sizes differ");
byte[] ret = new byte[b1.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = (byte) (b1[i] | b2[i]);
}
return ret;
}
public static boolean isNullOrZeroArray(byte[] array){
return (array == null) || (array.length == 0);
}
public static boolean isSingleZero(byte[] array){
return (array.length == 1 && array[0] == 0);
}
public static int length(byte[]... bytes) {
int result = 0;
for (byte[] array : bytes) {
result += (array == null) ? 0 : array.length;
}
return result;
}
}

View file

@ -81,7 +81,7 @@ public class CryptoUtil {
return CURVE.getCurve().decodePoint(compEnc);
}
public static byte[] recoverPubBytesFromSignature(int recId, ECDSASignature_ETH sig, byte[] messageHash) {
public static byte[] recoverPubBytesFromSignature(int recId, ECDSASignatureETH sig, byte[] messageHash) {
// 1.0 For j from 0 to h (h == recId here and the loop is outside this function)
// 1.1 Let x = r + jn

View file

@ -6,7 +6,7 @@ import java.math.BigInteger;
* Created by Ilia on 07.01.2018.
*/
public class ECDSASignature_ETH {
public class ECDSASignatureETH {
/**
* The two components of the signature.
*/
@ -19,7 +19,7 @@ public class ECDSASignature_ETH {
* @param r -
* @param s -
*/
public ECDSASignature_ETH(BigInteger r, BigInteger s) {
public ECDSASignatureETH(BigInteger r, BigInteger s) {
this.r = r;
this.s = s;
}
@ -30,8 +30,8 @@ public class ECDSASignature_ETH {
* @param s
* @return -
*/
private static ECDSASignature_ETH fromComponents(byte[] r, byte[] s) {
return new ECDSASignature_ETH(new BigInteger(1, r), new BigInteger(1, s));
private static ECDSASignatureETH fromComponents(byte[] r, byte[] s) {
return new ECDSASignatureETH(new BigInteger(1, r), new BigInteger(1, s));
}
/**
@ -41,8 +41,8 @@ public class ECDSASignature_ETH {
* @param v -
* @return -
*/
public static ECDSASignature_ETH fromComponents(byte[] r, byte[] s, byte v) {
ECDSASignature_ETH signature = fromComponents(r, s);
public static ECDSASignatureETH fromComponents(byte[] r, byte[] s, byte v) {
ECDSASignatureETH signature = fromComponents(r, s);
signature.v = v;
return signature;
}

View file

@ -17,7 +17,7 @@ import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Date;
import static com.tangem.domain.wallet.FormatUtil.GetDecimalFormat;
import static com.tangem.util.FormatUtil.GetDecimalFormat;
/**
* Created by Ilia on 15.02.2018.
@ -337,7 +337,7 @@ public class EthEngine extends CoinEngine {
BigInteger nonce = nonceValue;
BigInteger gasPrice = fee.divide(BigInteger.valueOf(21000));
BigInteger gasLimit = BigInteger.valueOf(21000);
Integer chainId = mCard.getBlockchain() == Blockchain.Ethereum ? ETH_Transaction.ChainEnum.Mainnet.getValue() : ETH_Transaction.ChainEnum.Rinkeby.getValue();
Integer chainId = mCard.getBlockchain() == Blockchain.Ethereum ? EthTransaction.ChainEnum.Mainnet.getValue() : EthTransaction.ChainEnum.Rinkeby.getValue();
Long multiplicator = 1000000000L;
amount = amount.multiply(BigInteger.valueOf(multiplicator));
@ -349,7 +349,7 @@ public class EthEngine extends CoinEngine {
to = to.substring(2);
}
ETH_Transaction tx = ETH_Transaction.create(to, amount, nonce, gasPrice, gasLimit, chainId);
EthTransaction tx = EthTransaction.create(to, amount, nonce, gasPrice, gasLimit, chainId);
byte[][] hashesForSign = new byte[1][];
byte[] for_hash = tx.getRawHash();
@ -377,7 +377,7 @@ public class EthEngine extends CoinEngine {
Log.e("ETH-CHECK", "Sign Failed.");
}
tx.signature = new ECDSASignature_ETH(r, s);
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");

View file

@ -3,6 +3,8 @@ package com.tangem.domain.wallet;
import android.util.Log;
import com.tangem.util.ByteUtil;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Sha256Hash;
import org.spongycastle.util.BigIntegers;
@ -11,13 +13,13 @@ import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
import java.util.Arrays;
import static com.tangem.domain.wallet.ByteUtil.EMPTY_BYTE_ARRAY;
import static com.tangem.util.ByteUtil.EMPTY_BYTE_ARRAY;
/**
* Created by Ilia on 07.01.2018.
*/
public class ETH_Transaction {
public class EthTransaction {
byte[] nonce;
byte[] gasPrice;
byte[] gasLimit;
@ -26,15 +28,15 @@ public class ETH_Transaction {
byte[] data;
Integer chainId;
byte[] rlpRaw;
public ECDSASignature_ETH signature;
public ECDSASignatureETH signature;
byte[] rlpEncoded;
private static final int CHAIN_ID_INC = 35;
private static final int LOWER_REAL_V = 27;
public static ETH_Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, Integer chainId){
return new ETH_Transaction(BigIntegers.asUnsignedByteArray(nonce),
public static EthTransaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, Integer chainId){
return new EthTransaction(BigIntegers.asUnsignedByteArray(nonce),
BigIntegers.asUnsignedByteArray(gasPrice),
BigIntegers.asUnsignedByteArray(gasLimit),
Hex.decode(to),
@ -44,9 +46,9 @@ public class ETH_Transaction {
}
public static ETH_Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, Integer chainId, byte[] data){
return new ETH_Transaction(BigIntegers.asUnsignedByteArray(nonce),
public static EthTransaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, Integer chainId, byte[] data){
return new EthTransaction(BigIntegers.asUnsignedByteArray(nonce),
BigIntegers.asUnsignedByteArray(gasPrice),
BigIntegers.asUnsignedByteArray(gasLimit),
Hex.decode(to),
@ -55,8 +57,8 @@ public class ETH_Transaction {
chainId);
}
public ETH_Transaction(byte[] nonce, byte[] gasPrice, byte[] gasLimit, byte[] receiveAddress, byte[] value, byte[] data,
Integer chainId) {
public EthTransaction(byte[] nonce, byte[] gasPrice, byte[] gasLimit, byte[] receiveAddress, byte[] value, byte[] data,
Integer chainId) {
this.nonce = nonce;
this.gasPrice = gasPrice;
this.gasLimit = gasLimit;
@ -112,7 +114,7 @@ public class ETH_Transaction {
return kec.digest(plainMsg);
}
public int BruteRecoveryID2(ECDSASignature_ETH sig, byte[] messageHash, byte[] thisKey)
public int BruteRecoveryID2(ECDSASignatureETH sig, byte[] messageHash, byte[] thisKey)
{
Log.e("ETH_KZ", BTCUtils.toHex(thisKey));
int recId = -1;

View file

@ -1,51 +0,0 @@
package com.tangem.domain.wallet;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.regex.Pattern;
/**
* Created by Ilia on 15.02.2018.
*/
public class FormatUtil {
public static long parseValue(String valueStr) throws NumberFormatException {
return new BigDecimal(valueStr).multiply(BigDecimal.valueOf(1_0000_0000)).setScale(0, BigDecimal.ROUND_HALF_DOWN).longValueExact();
}
public static String DoubleToString(double amount)
{
DecimalFormat myFormatter = GetDecimalFormat();
String output = myFormatter.format(amount);
return output;
}
public static DecimalFormat GetDecimalFormat()
{
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
String pattern = "#0.######";
DecimalFormat myFormatter = new DecimalFormat(pattern, symbols);
myFormatter.setParseBigDecimal(true);
return myFormatter;
}
public static long ConvertStringToLong(String caption) throws Exception {
BigDecimal d = new BigDecimal(caption);
d = d.multiply(new BigDecimal(100000));
d = d.setScale(5);
BigInteger b = d.toBigInteger();
long l = b.longValue();
return l;
}
}

View file

@ -1,197 +0,0 @@
package com.tangem.domain.wallet;
/**
* Created by Ilia on 19.12.2017.
*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Infura_Request {
public static final String METHOD_ETH_GetBalance = "eth_getBalance";
public static final String METHOD_ETH_GetOutTransactionCount = "eth_getTransactionCount";
public static final String METHOD_ETH_GetGasPrice = "eth_gasPrice";
public static final String METHOD_ETH_SendRawTransaction = "eth_sendRawTransaction";
public static final String METHOD_ETH_Call = "eth_call";
public JSONObject jsRequestData;
public String answerData;
public String error;
public String WalletAddress;
public int Dec;
public String amount;
public Blockchain blockchain;
public void setBlockchain(Blockchain value) {
blockchain = value;
}
public Blockchain getBlockchain()
{
return blockchain;
}
private Infura_Request() {
}
public Infura_Request(JSONObject jsRequest) {
try {
jsRequestData = new JSONObject(jsRequest.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
public JSONObject getAnswer() {
try {
return new JSONObject(answerData);
} catch (Exception e) {
try {
return new JSONObject(String.format("[\"Error\":\"%s\"]", e.getMessage()));
} catch (JSONException e1) {
e1.printStackTrace();
return null;
}
}
}
public String getAsString() {
return jsRequestData.toString();
}
public void setID(int value) {
try {
jsRequestData.put("id", value/*String.format("%d", value)*/);
} catch (JSONException e) {
e.printStackTrace();
}
}
public int getID() {
try {
return jsRequestData.getInt("id");
} catch (JSONException e) {
e.printStackTrace();
return 0;
}
}
public boolean isMethod(String methodName) throws JSONException {
return jsRequestData.getString("method").equals(methodName);
}
public static Infura_Request GetBalance(String wallet) {
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetBalance + "\", \"params\":[\"" + wallet + "\", \"latest\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Infura_Request GetTokenBalance(String wallet, String contract, int dec)
{
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.Dec = dec;
String address = wallet.substring(2);
String dataValue = String.format("{\"data\": \"0x70a08231000000000000000000000000%s\", \"to\": \"%s\"}", address, contract);
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_Call + "\", \"params\":[" +dataValue+", \"latest\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Infura_Request SendTransaction(String wallet, String tx) {
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_SendRawTransaction + "\", \"params\":[\"" + tx + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Infura_Request GetOutTransactionCount(String wallet) {
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetOutTransactionCount + "\", \"params\":[\"" + wallet + "\", \"latest\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Infura_Request GetPendingTransactionCount(String wallet) {
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetOutTransactionCount + "\", \"params\":[\"" + wallet + "\", \"pending\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Infura_Request GetGasPrise(String wallet) {
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_GetGasPrice + "\", \"params\":[] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Infura_Request SendTransactionCount(String wallet, String TX) {
Infura_Request request = new Infura_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ETH_SendRawTransaction + "\", \"params\":[\"" + TX + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
//METHOD_ETH_GetOutTransactionCount
public JSONArray getParams() throws JSONException {
return jsRequestData.getJSONArray("params");
}
public JSONObject getResult() throws JSONException {
return getAnswer().getJSONObject("result");
}
public String getResultString() throws JSONException {
return getAnswer().getString("result");
}
public JSONArray getResultArray() throws JSONException {
return getAnswer().getJSONArray("result");
}
}

View file

@ -6,8 +6,8 @@ package com.tangem.domain.wallet;
import java.util.Arrays;
import static com.tangem.domain.wallet.ByteUtil.isNullOrZeroArray;
import static com.tangem.domain.wallet.ByteUtil.isSingleZero;
import static com.tangem.util.ByteUtil.isNullOrZeroArray;
import static com.tangem.util.ByteUtil.isSingleZero;
public class RLP {
public static final byte[] EMPTY_ELEMENT_RLP = encodeElement(new byte[0]);

View file

@ -4,6 +4,7 @@ import android.os.Bundle;
import com.google.common.base.Strings;
import com.tangem.domain.cardReader.SettingsMask;
import com.tangem.util.FormatUtil;
import com.tangem.util.Util;
import java.math.BigInteger;

View file

@ -18,7 +18,7 @@ import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Date;
import static com.tangem.domain.wallet.FormatUtil.GetDecimalFormat;
import static com.tangem.util.FormatUtil.GetDecimalFormat;
/**
* Created by Ilia on 20.03.2018.
@ -331,7 +331,7 @@ public class TokenEngine extends CoinEngine {
BigInteger nonce = nonceValue;
BigInteger gasPrice = fee.divide(BigInteger.valueOf(21000));
BigInteger gasLimit = BigInteger.valueOf(60000);
Integer chainId = ETH_Transaction.ChainEnum.Mainnet.getValue();
Integer chainId = EthTransaction.ChainEnum.Mainnet.getValue();
BigInteger amountZero = BigInteger.ZERO;
Long multiplicator = 1000000000L;
@ -363,7 +363,7 @@ public class TokenEngine extends CoinEngine {
byte[] data = BTCUtils.fromHex(cmd);
ETH_Transaction tx = ETH_Transaction.create(contractAddress, amountZero, nonce, gasPrice, gasLimit, chainId, data);
EthTransaction tx = EthTransaction.create(contractAddress, amountZero, nonce, gasPrice, gasLimit, chainId, data);
byte[][] hashesForSign = new byte[1][];
byte[] for_hash = tx.getRawHash();
@ -390,7 +390,7 @@ public class TokenEngine extends CoinEngine {
Log.e("ETH-CHECK", "Sign Failed.");
}
tx.signature = new ECDSASignature_ETH(r, s);
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");