Updated on 2026-08-14

This commit is contained in:
Tangem 2018-05-29 16:11:22 +03:00
parent 8c17a8d4f0
commit b19c306250
70 changed files with 6149 additions and 6206 deletions

View file

@ -1,4 +0,0 @@
package com.tangem.data.network;
public class Server {
}

View file

@ -0,0 +1,182 @@
package com.tangem.data.network.request;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by dvol on 16.07.2017.
*/
public class Electrum_Request {
public static final String METHOD_GetBalance = "blockchain.address.get_balance";
public static final String METHOD_ListUnspent = "blockchain.address.listunspent";
public static final String METHOD_GetHistory = "blockchain.address.get_history";
public static final String METHOD_GetTransaction = "blockchain.transaction.get";
public static final String METHOD_GetHeader = "blockchain.block.get_header";
public static final String METHOD_SendTransaction = "blockchain.transaction.broadcast";
public static final String METHOD_GetFee = "blockchain.estimatefee";
public JSONObject jsRequestData;
public String answerData;
public String error;
public String WalletAddress;
public String TxHash;
public String Host;
public int Port;
private Electrum_Request() {
}
public Electrum_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", 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 static Electrum_Request CheckBalance(String wallet) {
Electrum_Request request = new Electrum_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_GetBalance + "\", \"params\":[\"" + wallet + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Electrum_Request GetFee(String wallet) {
Electrum_Request request = new Electrum_Request();
try{
request.WalletAddress = wallet; //METHOD_GetFee
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_GetFee + "\", \"params\":[\"" + 6 + "\"] }");
}
catch(JSONException e)
{
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Electrum_Request GetHeader(String wallet, String height) {
Electrum_Request request = new Electrum_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_GetHeader + "\", \"params\":[\"" + height + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Electrum_Request ListUnspent(String wallet) {
Electrum_Request request = new Electrum_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_ListUnspent + "\", \"params\":[\"" + wallet + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Electrum_Request Broadcast(String wallet, String tx) {
Electrum_Request request = new Electrum_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_SendTransaction + "\", \"params\":[\"" + tx + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Electrum_Request ListHistory(String wallet) {
Electrum_Request request = new Electrum_Request();
try {
request.WalletAddress=wallet;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_GetHistory + "\", \"params\":[\"" + wallet + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public static Electrum_Request GetTransaction(String wallet, String tx_hash) {
Electrum_Request request = new Electrum_Request();
try {
request.WalletAddress=wallet;
request.TxHash = tx_hash;
request.jsRequestData = new JSONObject("{ \"method\":\"" + METHOD_GetTransaction + "\", \"params\":[\"" + tx_hash + "\"] }");
} catch (JSONException e) {
e.printStackTrace();
request.error = e.toString();
}
return request;
}
public boolean isMethod(String methodName) throws JSONException {
return jsRequestData.getString("method").equals(methodName);
}
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

@ -0,0 +1,92 @@
package com.tangem.data.network.request;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by dvol on 16.07.2017.
*/
public class ExchangeRequest {
public JSONObject jsRequestData;
public String answerData;
public String error;
public String WalletAddress;
public String currency;
public String currencyAlter;
private ExchangeRequest() {
}
public ExchangeRequest(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 JSONArray getAnswerList() throws JSONException {
return new JSONArray(answerData);
}
public String getAsString() {
return jsRequestData.toString();
}
public void setID(int value) {
try {
jsRequestData.put("id", 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 static ExchangeRequest GetRate(String wallet, String currency, String alterCurrency) {
ExchangeRequest request = new ExchangeRequest();
request.WalletAddress=wallet;
request.currency = currency;
request.currencyAlter = alterCurrency;
return request;
}
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

@ -0,0 +1,119 @@
package com.tangem.data.network.task;
import android.os.AsyncTask;
import android.util.Log;
import com.tangem.data.network.request.Electrum_Request;
import com.tangem.wallet.SharedData;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dvol on 16.07.2017.
*/
public class Electrum_Task extends AsyncTask<Electrum_Request, Integer, List<Electrum_Request>> {
public static final String logTag = "Electrum";
//public static final String Host = /*"hsmiths.changeip.net";*/ "testnetnode.arihanc.com";
//public static final int Port = /*8080*/51001;
private int reqID = 1;
private String Host = "";
private int Port = 0;
OutputStreamWriter out;
BufferedReader in;
public SharedData sharedCounter = null;
public Electrum_Task(String host, int port) {
super();
Host = host;
Port = port;
}
public Electrum_Task(String host, int port, SharedData sharedCounter) {
super();
Host = host;
Port = port;
this.sharedCounter = sharedCounter;
}
@Override
protected List<Electrum_Request> doInBackground(Electrum_Request... requests) {
List<Electrum_Request> result = new ArrayList<>();
for (int i = 0; i < requests.length; i++) {
result.add(requests[i]);
}
try {
InetAddress serverAddress = InetAddress.getByName(Host);
Log.v(logTag, "Connecting..."+Host);
Socket socket = new Socket(serverAddress, Port);
socket.setSoTimeout(5000);
try {
OutputStream os = socket.getOutputStream();
out = new OutputStreamWriter(os, "UTF-8");
Log.v(logTag, "Connected");
InputStream is = socket.getInputStream();
in = new BufferedReader(new InputStreamReader(is));
publishProgress(5);
for (int i = 0; i < requests.length; i++) {
requests[i].setID(reqID++);
doRequest(requests[i]);
publishProgress(5 + 90 * (i + 1) / requests.length);
}
publishProgress(100);
} catch (Exception e) {
Log.e(logTag, "Error: ", e);
} finally {
socket.close();
}
} catch (Exception e) {
Log.e(logTag, "Error: ", e);
for (int i = 0; i < requests.length; i++) {
result.get(i).error = e.toString();
}
}
return result;
}
private void doRequest(Electrum_Request request) {
try {
Log.v(logTag, "<< " + request.getAsString());
out.write(request.getAsString() + "\n");
out.flush();
request.answerData = in.readLine();
request.Host=Host;
request.Port=Port;
if (request.answerData != null) {
Log.v(logTag, ">> " + request.answerData);
} else {
request.error = "No answer from server";
Log.v(logTag, ">> <NULL>");
}
} catch (Exception e) {
request.error = e.toString();
}
}
public String getValidationNodeDescription() {
return "Electrum, "+Host+":"+String.valueOf(Port);
}
}

View file

@ -0,0 +1,66 @@
package com.tangem.data.network.task;
/**
* Created by Ilia on 16.01.2018.
*/
import android.os.AsyncTask;
import com.tangem.data.network.request.ExchangeRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ilia on 04.12.2017.
*/
public class ExchangeTask extends AsyncTask<ExchangeRequest, Void, List<ExchangeRequest>> {
public ExchangeTask()
{
}
protected List<ExchangeRequest> doInBackground(ExchangeRequest... requests) {
List<ExchangeRequest> result = new ArrayList<>();
for (int i = 0; i < requests.length; i++) {
result.add(requests[i]);
}
for (ExchangeRequest request: result)
{
HttpURLConnection httpcon = null;
try {
URL url = new URL("https://api.coinmarketcap.com/v1/ticker/?convert=USD&lmit=10");
httpcon = (HttpURLConnection) url.openConnection();
httpcon.setRequestMethod("GET");
httpcon.connect();
BufferedReader in = new BufferedReader(
new InputStreamReader(httpcon.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
request.answerData = response.toString();
} catch (Exception e) {
request.error = e.getMessage();
} finally {
httpcon.disconnect();
}
}
return result;
}
}

View file

@ -0,0 +1,66 @@
package com.tangem.data.network.task;
import android.os.AsyncTask;
import com.tangem.wallet.Fee_Request;
import com.tangem.wallet.SharedData;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ilia on 04.12.2017.
*/
public class Fee_Task extends AsyncTask<Fee_Request, Void, List<Fee_Request>> {
public SharedData sharedCounter = null;
public Fee_Task(SharedData sharedData)
{
sharedCounter = sharedData;
}
protected List<Fee_Request> doInBackground(Fee_Request... requests) {
List<Fee_Request> result = new ArrayList<>();
for (int i = 0; i < requests.length; i++) {
result.add(requests[i]);
}
for (Fee_Request request: result)
{
HttpURLConnection httpcon = null;
try {
URL url = new URL("https://estimatefee.com/n/"+String.valueOf(request.getBlockCount()));
httpcon = (HttpURLConnection) url.openConnection();
httpcon.setRequestMethod("GET");
httpcon.connect();
BufferedReader in = new BufferedReader(
new InputStreamReader(httpcon.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
request.answerData = response.toString();
} catch (Exception e) {
request.error = e.getMessage();
} finally {
httpcon.disconnect();
}
}
return result;
}
}

View file

@ -0,0 +1,137 @@
package com.tangem.data.network.task;
/**
* Created by Ilia on 19.12.2017.
*/
import android.os.AsyncTask;
import com.tangem.wallet.Blockchain;
import com.tangem.wallet.Infura_Request;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
/**
* Created by Ilia on 04.12.2017.
*/
public class Infura_Task extends AsyncTask<Infura_Request, Void, List<Infura_Request>> {
private Exception exception;
private Blockchain blockchain;
public Infura_Task(Blockchain blockchainNet)
{
blockchain = blockchainNet;
}
boolean useOurNode = false;
protected List<Infura_Request> doInBackground(Infura_Request... requests) {
List<Infura_Request> result = new ArrayList<>();
for (int i = 0; i < requests.length; i++) {
result.add(requests[i]);
}
for (Infura_Request request: result)
{
HttpURLConnection httpcon = null;
try {
URL url = new URL("https://rinkeby.infura.io/AfWg0tmYEX5Kukn2UkKV");
if(blockchain == Blockchain.Ethereum || blockchain == Blockchain.Token){
if(useOurNode) {
URL tmp = new URL("http://52.230.23.88");
url = new URL(tmp.getProtocol(), tmp.getHost(), 27172, tmp.getFile());
}else
url = new URL("https://mainnet.infura.io/AfWg0tmYEX5Kukn2UkKV");
}
if(useOurNode)
{
httpcon = (HttpURLConnection)url.openConnection();
}
else
{
httpcon = (HttpsURLConnection)url.openConnection();
}
if(httpcon == null)
{
request.error = String.format("Cann't connect to %s", url.getHost());
return result;
}
httpcon.setRequestMethod("POST");
httpcon.setRequestProperty("Content-Type", "application/json");
String params = request.getAsString();
OutputStream os = httpcon.getOutputStream();
if(os == null)
{
request.error = String.format("Cann't recieve data from %s", url.getHost());
return result;
}
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
if(writer == null)
{
request.error = String.format("Cann't send data to %s", url.getHost());
}
writer.write(params);
writer.flush();
writer.close();
os.close();
httpcon.connect();
request.getParams();
System.out.println("code:"+httpcon.getResponseCode());
int code = httpcon.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(httpcon.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
request.answerData = response.toString();
} catch (Exception e) {
this.exception = e;
request.error = e.getMessage();
} finally {
httpcon.disconnect();
}
}
return result;
}
public String getValidationNodeDescription() {
if(blockchain == Blockchain.Ethereum || blockchain == Blockchain.Token)
{
if(useOurNode)
return "52.230.23.88:27172";
else
return "Infura, infura.io";
}
return "Infura, rinkeby.infura.io";
}
}