Updated on 2026-08-14

This commit is contained in:
Tangem 2018-12-05 12:40:42 +03:00
parent 48db52b2a9
commit 71fe425c1b
61 changed files with 592 additions and 454 deletions

View file

@ -8,7 +8,7 @@ import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyPermanentlyInvalidatedException;
import android.security.keystore.KeyProperties;
import com.tangem.tangemcard.data.PINStorage;
import com.tangem.tangemcard.data.local.PINStorage;
import com.tangem.presentation.activity.PinRequestActivity;
import java.io.IOException;

View file

@ -2,20 +2,6 @@ package com.tangem.data.network;
public class Server {
/**
* https://tangem-webapp.appspot.com/
* https://tangem-services.appspot.com/
*/
public static class ApiTangem {
public static final String URL_TANGEM = ServerURL.API_TANGEM;
public static class Method {
static final String VERIFY = URL_TANGEM + "verify";
static final String VERIFY_AND_GET_INFO = URL_TANGEM + "card/verify-and-get-info";
static final String ARTWORK = URL_TANGEM + "card/artwork";
}
}
public static class ApiUpdateVersion {
public static final String URL_UPDATE_VERSION = ServerURL.API_UPDATE_VERSION;

View file

@ -5,7 +5,8 @@ import android.support.annotation.NonNull;
import android.util.Log;
import com.tangem.App;
import com.tangem.data.network.model.CardVerifyAndGetInfo;
import com.tangem.tangemcard.data.network.TangemApi;
import com.tangem.tangemcard.data.network.model.CardVerifyAndGetInfo;
import com.tangem.data.network.model.RateInfoResponse;
import com.tangem.tangemcard.data.TangemCard;
import com.tangem.tangemcard.util.Util;
@ -83,94 +84,6 @@ public class ServerApiCommon {
});
}
/**
* HTTP
* Card verify
*/
private CardVerifyAndGetInfoListener cardVerifyAndGetInfoListener;
public interface CardVerifyAndGetInfoListener {
void onSuccess(CardVerifyAndGetInfo.Response cardVerifyAndGetArtworkResponse);
void onFail(String message);
}
public void setCardVerifyAndGetInfoListener(CardVerifyAndGetInfoListener listener) {
cardVerifyAndGetInfoListener = listener;
}
public void cardVerifyAndGetInfo(TangemCard card) {
TangemApi tangemApi = App.getNetworkComponent().getRetrofitTangem().create(TangemApi.class);
List<CardVerifyAndGetInfo.Request.Item> requests = new ArrayList<>();
requests.add(new CardVerifyAndGetInfo.Request.Item(Util.bytesToHex(card.getCID()), Util.bytesToHex(card.getCardPublicKey())));
CardVerifyAndGetInfo.Request requestBody = new CardVerifyAndGetInfo.Request(requests);
Call<CardVerifyAndGetInfo.Response> call = tangemApi.getCardVerifyAndGetInfo(requestBody);
call.enqueue(new Callback<CardVerifyAndGetInfo.Response>() {
@Override
public void onResponse(@NonNull Call<CardVerifyAndGetInfo.Response> call, @NonNull Response<CardVerifyAndGetInfo.Response> response) {
if (response.code() == 200) {
CardVerifyAndGetInfo.Response cardVerifyAndGetArtworkResponse = response.body();
cardVerifyAndGetInfoListener.onSuccess(cardVerifyAndGetArtworkResponse);
Log.i(TAG, "cardVerifyAndGeInfo onResponse " + response.code());
} else {
cardVerifyAndGetInfoListener.onFail(String.valueOf(response.code()));
Log.e(TAG, "cardVerifyAndGetInfo onResponse " + response.code());
}
}
@Override
public void onFailure(@NonNull Call<CardVerifyAndGetInfo.Response> call, @NonNull Throwable t) {
cardVerifyAndGetInfoListener.onFail(t.getMessage());
Log.e(TAG, "cardVerifyAndGetInfo onFailure " + t.getMessage());
}
});
}
/**
* HTTP
* Last version request from GitHub
*/
private ArtworkListener artworkListener;
public interface ArtworkListener {
void onSuccess(String artworkId, InputStream inputStream, Date updateDate);
void onFail(String message);
}
public void setArtworkListener(ArtworkListener listener) {
artworkListener = listener;
}
public void requestArtwork(String artworkId, Date updateDate, TangemCard card) {
TangemApi tangemApi = App.getNetworkComponent().getRetrofitTangem().create(TangemApi.class);
Call<ResponseBody> call = tangemApi.getArtwork(artworkId, Util.bytesToHex(card.getCID()), Util.bytesToHex(card.getCardPublicKey()));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
Log.i(TAG, "getArtwork onResponse " + response.code());
if (response.code() == 200) {
try {
ResponseBody body = response.body();
if (body != null) {
artworkListener.onSuccess(artworkId, body.byteStream(), updateDate);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
artworkListener.onFail(t.getMessage());
Log.e(TAG, "getArtwork onFailure " + t.getMessage());
}
});
}
/**
* HTTP
* Used in Crypto-currency course

View file

@ -1,21 +0,0 @@
package com.tangem.data.network;
import com.tangem.data.network.model.CardVerifyAndGetInfo;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface TangemApi {
@Headers("Content-Type: application/json")
@POST(Server.ApiTangem.Method.VERIFY_AND_GET_INFO)
Call<CardVerifyAndGetInfo.Response> getCardVerifyAndGetInfo(@Body CardVerifyAndGetInfo.Request requestBody);
@GET(Server.ApiTangem.Method.ARTWORK)
Call<ResponseBody> getArtwork(@Query("artworkId") String artworkId, @Query("CID") String CID, @Query("publicKey") String publicKey);
}

View file

@ -1,11 +0,0 @@
package com.tangem.data.network.model;
public class CardVerify {
private String CID;
private String publicKey;
public CardVerify(String CID, String publicKey) {
this.CID = CID;
this.publicKey = publicKey;
}
}

View file

@ -1,55 +0,0 @@
package com.tangem.data.network.model
import java.text.SimpleDateFormat
import java.util.*
class CardVerifyAndGetInfo {
data class Request(
var requests: List<Item>? = null
) {
data class Item(
var CID: String = "",
var publicKey: String = ""
)
}
data class Response(
var results: List<Item>? = null
) {
data class Item(
var error: String? = null,
var CID: String = "",
var passed: Boolean = false,
var batch: String = "",
var artwork: ArtworkInfo? = null,
var substitution: SubstitutionInfo? = null
) {
data class ArtworkInfo(
var id: String = "",
var hash: String = "",
var date: String = ""
) {
fun getUpdateDate(): Date? {
return try {
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US).parse(date)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
data class SubstitutionInfo(
var data: String? = null,
var signature: String? = null
)
}
}
}

View file

@ -0,0 +1,159 @@
package com.tangem.data.nfc;
import android.app.Activity;
import android.content.Intent;
import android.nfc.tech.IsoDep;
import android.util.Log;
import com.tangem.domain.wallet.BTCUtils;
import com.tangem.domain.wallet.CoinEngine;
import com.tangem.domain.wallet.CoinEngineFactory;
import com.tangem.domain.wallet.TangemContext;
import com.tangem.presentation.activity.SendTransactionActivity;
import com.tangem.presentation.activity.SignPaymentActivity;
import com.tangem.tangemcard.reader.CardProtocol;
import com.tangem.tangemcard.reader.NfcManager;
import com.tangem.tangemcard.data.Blockchain;
import java.io.IOException;
public class SignPaymentTask extends Thread{
public static final String TAG = SignPaymentTask.class.getSimpleName();
private CoinEngine.Amount txAmount;
private CoinEngine.Amount txFee;
private Boolean txIncFee = true;
public void SetTransactionValue(CoinEngine.Amount amount, CoinEngine.Amount fee, Boolean incfee) {
txAmount = amount;
txFee = fee;
txIncFee = incfee;
}
private String txOutAddress;
private Activity mContext;
private TangemContext mCtx;
private NfcManager mNfcManager;
private IsoDep mIsoDep;
private CardProtocol.Notifications mNotifications;
private boolean isCancelled = false;
public SignPaymentTask(Activity context, TangemContext ctx, NfcManager nfcManager, IsoDep isoDep, CardProtocol.Notifications notifications, CoinEngine.Amount amount, CoinEngine.Amount fee, Boolean IncFee, String outAddress) {
mCtx=ctx;
mContext = context;
mNfcManager = nfcManager;
mIsoDep = isoDep;
mNotifications = notifications;
txOutAddress = outAddress;
SetTransactionValue(amount, fee, IncFee);
}
@Override
public void run() {
if (mIsoDep == null) {
return;
}
CardProtocol protocol = new CardProtocol(mContext, mIsoDep, mCtx.getCard(), mNotifications);
mNotifications.onReadStart(protocol);
try {
// for Samsung's bugs -
// Workaround for the Samsung Galaxy S5 (since the
// first connection always hangs on transceive).
int timeout = mIsoDep.getTimeout();
mIsoDep.connect();
mIsoDep.close();
mIsoDep.connect();
mIsoDep.setTimeout(timeout);
try {
mNotifications.onReadProgress(protocol, 5);
Log.i(TAG, "[-- Start sign payment --]");
if (isCancelled) return;
protocol.run_Read();
protocol.run_VerifyCard();
Log.i(TAG, "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName());
mNotifications.onReadProgress(protocol, 30);
if (isCancelled) return;
//
// if (mCard.getBlockchain() == Blockchain.Ethereum) {
// SignETH_TX(protocol);
// } else {
// SignBTC_TX(protocol);
// }
CoinEngine engine = CoinEngineFactory.INSTANCE.create(mCtx);
if (engine != null) {
if (mCtx.getCard().getPauseBeforePIN2() > 0) {
mNotifications.onReadWait(mCtx.getCard().getPauseBeforePIN2());
}
byte[] tx = null;
try {
tx = engine.sign(txFee, txAmount, txIncFee, txOutAddress, protocol);
}
catch (IOException e) {
e.printStackTrace();
protocol.setError(e);
} finally {
mNotifications.onReadWait(0);
}
if (tx != null) {
// TODO - move to engine!!!
String txStr = BTCUtils.toHex(tx);
if (mCtx.getBlockchain() == Blockchain.Ethereum || mCtx.getBlockchain() == Blockchain.EthereumTestNet || mCtx.getBlockchain() == Blockchain.Token) {
txStr = String.format("0x%s", txStr);
}
Intent intent = new Intent(mContext, SendTransactionActivity.class);
mCtx.saveToIntent(intent);
intent.putExtra(SendTransactionActivity.EXTRA_TX, txStr);
mContext.startActivityForResult(intent, SignPaymentActivity.REQUEST_CODE_SEND_PAYMENT);
}
}
mNotifications.onReadProgress(protocol, 100);
if (isCancelled) return;
} finally {
mNfcManager.ignoreTag(mIsoDep.getTag());
mNotifications.onReadWait(0);
}
} catch (CardProtocol.TangemException_InvalidPIN e) {
e.printStackTrace();
protocol.setError(e);
} catch (CardProtocol.TangemException_WrongAmount e) {
e.printStackTrace();
protocol.setError(e);
} catch (Exception e) {
e.printStackTrace();
protocol.setError(e);
} finally {
Log.i(TAG, "[-- Finish sign payment --]");
mNotifications.onReadFinish(protocol);
}
}
public void cancel(Boolean AllowInterrupt) {
try {
if (isAlive()) {
isCancelled = true;
join(500);
}
if (isAlive() && AllowInterrupt) {
interrupt();
mNotifications.onReadCancel();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}