Updated on 2026-08-14

This commit is contained in:
Tangem 2018-11-01 17:42:20 +03:00
commit 1b480d32ca
12 changed files with 195 additions and 25 deletions

View file

@ -46,7 +46,7 @@ public class ServerApiHelper {
}
public void estimateFee(int blockCount) {
EstimatefeeApi estimatefeeApi = App.getComponent().getRetrofitEstimatefee().create(EstimatefeeApi.class);
EstimatefeeApi estimatefeeApi = App.getNetworkComponent().getRetrofitEstimatefee().create(EstimatefeeApi.class);
Call<String> call;
switch (blockCount) {
@ -114,7 +114,7 @@ public class ServerApiHelper {
}
public void infura(String method, int id, String wallet, String contract, String tx) {
InfuraApi infuraApi = App.getComponent().getRetrofitInfura().create(InfuraApi.class);
InfuraApi infuraApi = App.getNetworkComponent().getRetrofitInfura().create(InfuraApi.class);
InfuraBody infuraBody;
switch (method) {
@ -179,7 +179,7 @@ public class ServerApiHelper {
}
public void cardVerifyAndGetInfo(TangemCard card) {
TangemApi tangemApi = App.getComponent().getRetrofitTangem().create(TangemApi.class);
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())));
@ -224,7 +224,7 @@ public class ServerApiHelper {
}
public void requestArtwork(String artworkId, Date updateDate, TangemCard card) {
TangemApi tangemApi = App.getComponent().getRetrofitTangem().create(TangemApi.class);
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>() {
@ -267,7 +267,7 @@ public class ServerApiHelper {
@SuppressLint("CheckResult")
public void rateInfoData(String cryptoId) {
CoinmarketApi coinmarketApi = App.getComponent().getRetrofitCoinmarketcap().create(CoinmarketApi.class);
CoinmarketApi coinmarketApi = App.getNetworkComponent().getRetrofitCoinmarketcap().create(CoinmarketApi.class);
coinmarketApi.getRateInfoList()
.subscribeOn(Schedulers.io())
@ -302,7 +302,7 @@ public class ServerApiHelper {
}
public void requestLastVersion() {
UpdateVersionApi updateVersionApi = App.getComponent().getRetrofitGithubusercontent().create(UpdateVersionApi.class);
UpdateVersionApi updateVersionApi = App.getNetworkComponent().getRetrofitGithubusercontent().create(UpdateVersionApi.class);
Call<ResponseBody> call = updateVersionApi.getLastVersion();
call.enqueue(new Callback<ResponseBody>() {

View file

@ -9,6 +9,7 @@ import com.tangem.domain.wallet.Blockchain;
import com.tangem.domain.wallet.TangemCard;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
@ -22,6 +23,15 @@ import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
@ -41,6 +51,7 @@ public class ServerApiHelperElectrum {
public interface ElectrumRequestDataListener {
void onSuccess(ElectrumRequest electrumRequest);
void onFail(String method);
}
@ -52,6 +63,7 @@ public class ServerApiHelperElectrum {
Observable<ElectrumRequest> checkElectrumDataObserver = Observable.just(electrumRequest)
.doOnNext(electrumRequest1 -> doElectrumRequest(card, electrumRequest))
// .doOnNext(electrumRequest1 -> testSslSocket(card, electrumRequest))
.flatMap(electrumRequest1 -> {
if (electrumRequest1.answerData == null) {
@ -94,7 +106,61 @@ public class ServerApiHelperElectrum {
});
}
private List<ElectrumRequest> doElectrumRequest(TangemCard card, ElectrumRequest electrumRequest) throws ConnectException {
private List<ElectrumRequest> testSslSocket(TangemCard card, ElectrumRequest electrumRequest) {
SocketFactory sf = SSLSocketFactory.getDefault();
SSLSocket socket;
List<ElectrumRequest> result = new ArrayList<>();
Collections.addAll(result, electrumRequest);
try {
// socket = (SSLSocket) sf.createSocket("gmail.com", 443);
socket = (SSLSocket) sf.createSocket("electrum.hsmiths.com", 50002);
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession s = socket.getSession();
// if (!hv.verify("mail.google.com", s)) {
// throw new SSLHandshakeException("Expected mail.google.com, found " + s.getPeerPrincipal());
// }
try {
OutputStream os = socket.getOutputStream();
OutputStreamWriter out = new OutputStreamWriter(os, "UTF-8");
InputStream is = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
electrumRequest.setID(1);
out.write(electrumRequest.getAsString() + "\n");
out.flush();
electrumRequest.answerData = in.readLine();
// electrumRequest.host = host;
// electrumRequest.port = port;
if (electrumRequest.answerData != null) {
Log.i(TAG, ">> " + electrumRequest.answerData);
} else {
electrumRequest.error = "No answer from server";
Log.i(TAG, ">> <NULL>");
}
} catch (ConnectException e) {
e.printStackTrace();
Log.e(TAG, "electrumRequestData " + electrumRequest.getMethod() + " ConnectException " + e.getMessage());
} finally {
// Log.i(TAG, "electrumRequestData " + electrumRequest.getMethod() + " CLOSE");
socket.close();
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private List<ElectrumRequest> doElectrumRequest(TangemCard card, ElectrumRequest electrumRequest) {
BitcoinNode bitcoinNode = BitcoinNode.values()[new Random().nextInt(BitcoinNode.values().length)];
if (card.getBlockchain() == Blockchain.BitcoinTestNet || card.getBlockchain() == Blockchain.BitcoinCashTestNet) {
@ -111,8 +177,8 @@ public class ServerApiHelperElectrum {
Collections.addAll(result, electrumRequest);
try {
Socket socket = App.getComponent().getSocket();
socket.connect(new InetSocketAddress(InetAddress.getByName(host), port));
Socket socket = App.getNetworkComponent().getSocket();
socket.connect(new InetSocketAddress(InetAddress.getByName(host), port));
Log.i(TAG, host + " " + port);
try {
OutputStream os = socket.getOutputStream();

View file

@ -0,0 +1,31 @@
/**
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dmmatrix.epro.core.exception
/**
* Base Class for handling errors/failures/exceptions.
* Every feature specific failure should extend [FeatureFailure] class.
*/
sealed class Failure {
class ApplicationError(val message: String) : Failure()
class NotFound : FeatureFailure()
class NetworkConnection : Failure()
class ServerError : Failure()
class Unauthorized : Failure() // 401
/** * Extend this class for feature specific failures.*/
abstract class FeatureFailure : Failure()
}