Updated on 2026-08-14

This commit is contained in:
Tangem 2019-01-11 09:13:19 +03:00
parent 1b5b0037d1
commit 07bc724ecd
9 changed files with 839 additions and 17 deletions

View file

@ -6,25 +6,24 @@ import com.tangem.tangemcard.R;
* Created by dvol on 06.08.2017.
*/
public enum Blockchain {
Unknown("", "", 1.0, R.drawable.ic_logo_unknown, ""),
Bitcoin("BTC", "BTC", 100000000.0, R.drawable.ic_logo_bitcoin, "Bitcoin"),
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"),
BitcoinCash("BCH", "BCH", 100000000.0, R.drawable.ic_logo_bitcoin_cash, "Bitcoin Cash"),
Litecoin("LTC", "LTC", 100000000.0, R.drawable.ic_logo_bitcoin, "Litecoin");
Blockchain(String ID, String currency, double multiplier, int imageResource, String officialName) {
Unknown("", "", R.drawable.ic_logo_unknown, ""),
Bitcoin("BTC", "BTC", R.drawable.ic_logo_bitcoin, "Bitcoin"),
BitcoinTestNet("BTC/test", "BTC", R.drawable.ic_logo_bitcoin_testnet, "Bitcoin Testnet"),
Ethereum("ETH", "ETH", R.drawable.ic_logo_ethereum, "Ethereum"),
EthereumTestNet("ETH/test", "ETH", R.drawable.ic_logo_ethereum_testnet, "Ethereum Testnet"),
Token("Token", "ERC20", R.drawable.ic_logo_bat_token, "Ethereum"),
BitcoinCash("BCH", "BCH", R.drawable.ic_logo_bitcoin_cash, "Bitcoin Cash"),
Litecoin("LTC", "LTC", R.drawable.ic_logo_bitcoin, "Litecoin"),
Stellar("XLM", "XLM", R.drawable.ic_logo_stellar, "Stellar Lumens"),
StellarTestNet("XLM/test", "XLM", R.drawable.ic_logo_bitcoin, "Stellar Lumens");
Blockchain(String ID, String currency, int imageResource, String officialName) {
mID = ID;
mCurrency = currency;
// mMultiplier = multiplier;
mImageResource = imageResource;
mOfficialName = officialName;
}
private String mID, mOfficialName;
//private double mMultiplier;
private String mCurrency;
private int mImageResource;
@ -36,10 +35,6 @@ public enum Blockchain {
return mOfficialName;
}
// public double getMultiplier() {
// return mMultiplier;
// }
public String getCurrency() {
return mCurrency;
}
@ -83,6 +78,7 @@ public enum Blockchain {
return resourceId;
}
//TODO - ???
public static int getLogoImageResource(String blockchainID, String symbolName) {
switch (blockchainID) {
case "BTC":
@ -96,6 +92,9 @@ public enum Blockchain {
case "ETH":
return R.drawable.ic_logo_ethereum;
case "XLM":
return R.drawable.ic_logo_stellar;
}
return R.drawable.tangem2;
}

View file

@ -0,0 +1,151 @@
package com.tangem.data.network;
import android.util.Log;
import com.tangem.App;
import com.tangem.data.Blockchain;
import com.tangem.domain.wallet.TangemContext;
import com.tangem.wallet.R;
import org.stellar.sdk.Network;
import org.stellar.sdk.Server;
import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;
/**
* Request processor for Electrum Api
* Every request live cycle:
* 1. In application create request and call {@link ServerApiStellar}.requestData(..)
* 2. Try send every request for max 4 times,
* 3. If all 4 times fail call DefaultObserver<StellarRequest>.onError (defined in .requestData(..)) and than
* {@link Listener}.onFail(...) callback
* Error can be acquired with {@link StellarRequest}.getError() method
* 4. If request network communication finished successfully then call DefaultObserver<StellarRequest>.onComplete (defined in .requestData) and than
* {@link Listener}.onSuccess(...) callback
*/
public class ServerApiStellar {
private static String TAG = ServerApiStellar.class.getSimpleName();
/**
* TCP, SSL
* Used in BTC, BCH
*/
private Listener listener;
private int requestsCount=0;
public boolean isRequestsSequenceCompleted() {
Log.i(TAG, String.format("isRequestsSequenceCompleted: %s (%d requests left)", String.valueOf(requestsCount <= 0), requestsCount));
return requestsCount <= 0;
}
/**
* Interface for notification every request result
*/
public interface Listener {
/**
* Notify that request processing was successful
* @param stellarRequest - processed request containing received answer {@see stellarRequest.getAnswer() method}
*/
void onSuccess(StellarRequest.Base stellarRequest);
/**
* Notify that request processing was successful
* @param stellarRequest - processed request containing occurred error {@see stellarRequest.getError() method}
*/
void onFail(StellarRequest.Base stellarRequest);
}
/**
* Set notificaion listener
* @param listener
*/
public void setListener(Listener listener) {
this.listener = listener;
}
/**
* Start process request
* @param ctx
* @param stellarRequest
*/
public void requestData(TangemContext ctx, StellarRequest.Base stellarRequest) {
requestsCount++;
Log.i(TAG, String.format("New request[%d]: %s", requestsCount,stellarRequest.getClass().getSimpleName()));
Observable<StellarRequest.Base> stellarObserver = Observable.just(stellarRequest)
.doOnNext(stellarRequest1 -> doStellarRequest(ctx, stellarRequest))
// .flatMap(stellarRequest1 -> {
// if (stellarRequest1.answerData == null) {
// Log.e(TAG, "NullPointerException " + stellarRequest.getMethod());
// return Observable.error(new NullPointerException());
// } else
// return Observable.just(stellarRequest1);
// })
//
.retryWhen(errors -> errors
.filter(throwable -> throwable instanceof IOException)
.zipWith(Observable.range(1, 4), (n, i) -> i))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
stellarObserver.subscribe(new DefaultObserver<StellarRequest.Base>() {
@Override
public void onNext(StellarRequest.Base stellarRequest) {
}
@Override
public void onError(Throwable e) {
requestsCount--;
Log.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onError " + e.getMessage());
Log.e(TAG, String.format("%d requests left in processing",requestsCount));
stellarRequest.setError(ctx.getString(R.string.cannot_obtain_data_from_blockchain));
//setErrorOccurred(e.getMessage());//;
listener.onFail(stellarRequest);
}
/**
* Called after completion request processing
*/
@Override
public void onComplete() {
requestsCount--;
Log.e(TAG, String.format("%d requests left in processing",requestsCount));
if (stellarRequest.getError() != null) {
Log.i(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error!=null");
listener.onFail(stellarRequest);
} else {
Log.e(TAG, "requestData " + stellarRequest.getClass().getSimpleName() + " onComplete, error==null");
listener.onSuccess(stellarRequest);
}
}
});
}
private void doStellarRequest(TangemContext ctx, StellarRequest.Base stellarRequest) throws IOException {
stellarRequest.setError(null);
try {
if( ctx.getBlockchain()==Blockchain.StellarTestNet ) {
Network.useTestNetwork();
}
Server server = new Server(ServerURL.API_STELLAR);
stellarRequest.process(server);
}
catch (Exception e)
{
e.printStackTrace();
stellarRequest.setError(App.getInstance().getString(R.string.cannot_obtain_data_from_blockchain_communication_error));
throw e;
}
}
}

View file

@ -6,4 +6,5 @@ class ServerURL {
static final String API_INFURA = "https://mainnet.infura.io/v3/";
static final String API_ESTIMATEFEE = "https://estimatefee.com/";
static final String API_UPDATE_VERSION = "https://raw.githubusercontent.com/";
static final String API_STELLAR = "https://horizon-testnet.stellar.org";
}

View file

@ -0,0 +1,72 @@
package com.tangem.data.network;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Server;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.SubmitTransactionResponse;
import java.io.IOException;
/**
* Created by dvol on 16.07.2017.
*/
public class StellarRequest {
public static abstract class Base {
private String error = null;
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public abstract void process(Server server) throws IOException;
}
public static class Balance extends Base {
public KeyPair accountKeyPair;
public AccountResponse accountResponse;
public Balance(String walletAddress)
{
accountKeyPair=KeyPair.fromAccountId(walletAddress);
}
@Override
public void process(Server server) throws IOException {
accountResponse=server.accounts().account(accountKeyPair);
}
}
public static class SubmitTransaction extends Base {
// public KeyPair sourceAccount;
// public KeyPair targetAccount;
public Transaction transaction;
public SubmitTransactionResponse response;
public SubmitTransaction(Transaction transaction) {
this.transaction=transaction;
}
@Override
public void process(Server server) throws IOException {
// // First, check to make sure that the destination account exists.
// // You could skip this, but if the account does not exist, you will be charged
// // the transaction fee when the transaction fails.
// // It will throw HttpResponseException if account does not exist or there was another error.
// server.accounts().account(targetAccount);
//
// // If there was no error, load up-to-date information on your account.
// AccountResponse sourceAccount = server.accounts().account(sourceAccount);
// And finally, send it off to Stellar!
response = server.submitTransaction(transaction);
}
}
}