Implementation of model for alt-coin (not graphene cryptos)

Insight api base implementation
This commit is contained in:
hvarona 2018-08-02 23:02:23 -04:00
parent 44f43cd126
commit f40d996282
19 changed files with 2397 additions and 0 deletions

View file

@ -83,4 +83,9 @@ dependencies {
implementation 'com.andrognito.patternlockview:patternlockview:1.0.0'
implementation 'commons-codec:commons-codec:1.11'
implementation ('io.socket:socket.io-client:0.8.3') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
}

View file

@ -0,0 +1,160 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import cy.agorise.crystalwallet.models.GeneralCoinAccount;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
/**
* Handles all the calls for the Socket.IO of the insight api
*
* Only gets new transaction in real time for each address of an Account
*
*/
public class AccountActivityWatcher {
/**
* The mAccount to be monitor
*/
private final GeneralCoinAccount mAccount;
/**
* The list of address to monitor
*/
private List<String> mWatchAddress = new ArrayList<>();
/**
* the Socket.IO
*/
private Socket mSocket;
/**
* This app mContext, used to save on the DB
*/
private final Context mContext;
/**
* Handles the address/transaction notification.
* Then calls the GetTransactionData to get the info of the new transaction
*/
private final Emitter.Listener onAddressTransaction = new Emitter.Listener() {
@Override
public void call(Object... os) {
try {
System.out.println("Receive accountActivtyWatcher " + os[0].toString() );
String txid = ((JSONObject) os[0]).getString(InsightApiConstants.sTxTag);
new GetTransactionData(txid, mAccount, mContext).start();
} catch (JSONException ex) {
Logger.getLogger(AccountActivityWatcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
/**
* Handles the connect of the Socket.IO
*/
private final Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... os) {
System.out.println("Connected to accountActivityWatcher");
JSONArray array = new JSONArray();
for(String addr : mWatchAddress) {
array.put(addr);
}
mSocket.emit(InsightApiConstants.sSubscribeEmmit, InsightApiConstants.sChangeAddressRoom, array);
}
};
/**
* Handles the disconnect of the Socket.Io
* Reconcects the mSocket
*/
private final Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... os) {
System.out.println("Disconnected to accountActivityWatcher");
mSocket.connect();
}
};
/**
* Error handler, doesn't need reconnect, the mSocket.io do that by default
*/
private final Emitter.Listener onError = new Emitter.Listener() {
@Override
public void call(Object... os) {
System.out.println("Error to accountActivityWatcher ");
for(Object ob : os) {
System.out.println("accountActivityWatcher " + ob.toString());
}
}
};
/**
* Basic constructor
*
* @param mAccount The mAccount to be monitor
* @param mContext This app mContext
*/
public AccountActivityWatcher(GeneralCoinAccount mAccount, Context mContext) {
//String serverUrl = InsightApiConstants.protocol + "://" + InsightApiConstants.getAddress(mAccount.getCoin()) + ":" + InsightApiConstants.getPort(mAccount.getCoin()) + "/"+InsightApiConstants.getRawPath(mAccount.getCoin())+"/mSocket.io/";
String serverUrl = InsightApiConstants.sProtocolSocketIO + "://" + InsightApiConstants.getAddress(mAccount.getCryptoCoin()) + ":" + InsightApiConstants.getPort(mAccount.getCryptoCoin()) + "/";
this.mAccount = mAccount;
this.mContext = mContext;
System.out.println("accountActivityWatcher " + serverUrl);
try {
IO.Options opts = new IO.Options();
System.out.println("accountActivityWatcher default path " + opts.path);
this.mSocket = IO.socket(serverUrl);
this.mSocket.on(Socket.EVENT_CONNECT, onConnect);
this.mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
this.mSocket.on(Socket.EVENT_ERROR, onError);
this.mSocket.on(Socket.EVENT_CONNECT_ERROR, onError);
this.mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onError);
this.mSocket.on(InsightApiConstants.sChangeAddressRoom, onAddressTransaction);
} catch (URISyntaxException e) {
//TODO change exception handler
e.printStackTrace();
}
}
/**
* Add an address to be monitored, it can be used after the connect
* @param address The String address to monitor
*/
public void addAddress(String address) {
mWatchAddress.add(address);
if (this.mSocket.connected()) {
mSocket.emit(InsightApiConstants.sSubscribeEmmit, InsightApiConstants.sChangeAddressRoom, new String[]{address});
}
}
/**
* Connects the Socket
*/
public void connect() {
//TODO change to use log
System.out.println("accountActivityWatcher connecting");
try{
this.mSocket.connect();
}catch(Exception e){
//TODO change exception handler
System.out.println("accountActivityWatcher exception " + e.getMessage());
}
}
/**
* Disconnects the Socket
*/
public void disconnect() {this.mSocket.disconnect();}
}

View file

@ -0,0 +1,83 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import android.content.Context;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Txi;
import cy.agorise.crystalwallet.models.GeneralCoinAccount;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Broadcast a transaction, using the InsightApi
*
*/
public class BroadcastTransaction extends Thread implements Callback<Txi> {
/**
* The rawTX as Hex String
*/
private String mRawTx;
/**
* The serviceGenerator to call
*/
private InsightApiServiceGenerator mServiceGenerator;
/**
* This app context, used to save on the DB
*/
private Context mContext;
/**
* The account who sign the transaction
*/
private GeneralCoinAccount mAccount;
/**
* Basic Consturctor
* @param RawTx The RawTX in Hex String
* @param account The account who signs the transaction
* @param context This app context
*/
public BroadcastTransaction(String RawTx, GeneralCoinAccount account, Context context){
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
this.mContext = context;
this.mRawTx = RawTx;
this.mAccount = account;
}
/**
* Handles the response of the call
*
*/
@Override
public void onResponse(Call<Txi> call, Response<Txi> response) {
if (response.isSuccessful()) {
//TODO invalidated send
//TODO call getTransactionData
GetTransactionData trData = new GetTransactionData(response.body().txid,this.mAccount,this.mContext);
trData.start();
} else {
System.out.println("SENDTEST: not succesful " + response.message());
//TODO change how to handle invalid transaction
}
}
/**
* Handles the failures of the call
*/
@Override
public void onFailure(Call<Txi> call, Throwable t) {
//TODO change how to handle invalid transaction
System.out.println("SENDTEST: sendError " + t.getMessage() );
}
/**
* Starts the call of the service
*/
@Override
public void run() {
InsightApiService service = this.mServiceGenerator.getService(InsightApiService.class);
Call<Txi> broadcastTransaction = service.broadcastTransaction(InsightApiConstants.getPath(this.mAccount.getCryptoCoin()),this.mRawTx);
broadcastTransaction.enqueue(this);
}
}

View file

@ -0,0 +1,74 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import com.google.gson.JsonObject;
import java.io.IOException;
import cy.agorise.crystalwallet.enums.CryptoCoin;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Get the estimete fee amount from an insight api server.
* This class gets the rate of the fee for a giving coin in about to block for a transaction to be
* confirmated.
*
* This ammount is giving as amount of currency / kbytes, as example btc / kbytes
*
*/
public abstract class GetEstimateFee {
//TODO add a funciton to get the rate of a specific port
/**
* The funciton to get the rate for the transaction be included in the next 2 blocks
* @param coin The coin to get the rate
* @return The rate number (coin/kbytes)
* @throws IOException If the server answer null, or the rate couldn't be calculated
*/
public static long getEstimateFee(final CryptoCoin coin) throws IOException {
String serverUrl = InsightApiConstants.sProtocol + "://"
+ InsightApiConstants.getAddress(coin) + "/";
InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl);
InsightApiService service = serviceGenerator.getService(InsightApiService.class);
Call<JsonObject> call = service.estimateFee(InsightApiConstants.getPath(coin));
final Object SYNC = new Object();
final JsonObject answer = new JsonObject();
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
synchronized (SYNC) {
answer.addProperty("answer",
(long) (response.body().get("2").getAsDouble()* Math.pow(10, coin.getPrecision())));
SYNC.notifyAll();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
synchronized (SYNC) {
SYNC.notifyAll();
}
}
});
synchronized (SYNC){
for(int i = 0; i < 6; i++) {
try {
SYNC.wait(5000);
} catch (InterruptedException e) {
// this interruption never rises
}
if(answer.get("answer")!=null){
break;
}
}
}
if(answer.get("answer")==null){
throw new IOException("");
}
return (long) (answer.get("answer").getAsDouble());
}
}

View file

@ -0,0 +1,207 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import android.content.Context;
import android.util.Log;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.AddressTxi;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Txi;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Vin;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Vout;
import cy.agorise.crystalwallet.models.GTxIO;
import cy.agorise.crystalwallet.models.GeneralCoinAccount;
import cy.agorise.crystalwallet.models.GeneralCoinAddress;
import cy.agorise.crystalwallet.models.GeneralTransaction;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Get all the transaction data of the addresses of an account
*
*/
public class GetTransactionByAddress extends Thread implements Callback<AddressTxi> {
/**
* The account to be query
*/
private GeneralCoinAccount mAccount;
/**
* The list of address to query
*/
private List<GeneralCoinAddress> mAddresses = new ArrayList<>();
/**
* The serviceGenerator to call
*/
private InsightApiServiceGenerator mServiceGenerator;
/**
* This app context, used to save on the DB
*/
private Context mContext;
/**
* Basic consturcotr
* @param account The account to be query
* @param context This app context
*/
public GetTransactionByAddress(GeneralCoinAccount account, Context context) {
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
this.mAccount = account;
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
this.mContext = context;
}
/**
* add an address to be query
* @param address the address to be query
*/
public void addAddress(GeneralCoinAddress address) {
this.mAddresses.add(address);
}
/**
* Handle the response
* @param call The call with the addresTxi object
* @param response the response status object
*/
@Override
public void onResponse(Call<AddressTxi> call, Response<AddressTxi> response) {
if (response.isSuccessful()) {
boolean changed = false;
AddressTxi addressTxi = response.body();
for (Txi txi : addressTxi.items) {
GeneralCoinAccount tempAccount = null;
GeneralTransaction transaction = new GeneralTransaction();
transaction.setAccount(this.mAccount);
transaction.setTxid(txi.txid);
transaction.setBlock(txi.blockheight);
transaction.setDate(new Date(txi.time * 1000));
transaction.setFee((long) (txi.fee * Math.pow(10,this.mAccount.getCryptoCoin().getPrecision())));
transaction.setConfirm(txi.confirmations);
transaction.setType(this.mAccount.getCryptoCoin());
transaction.setBlockHeight(txi.blockheight);
for (Vin vin : txi.vin) {
GTxIO input = new GTxIO();
input.setAmount((long) (vin.value * Math.pow(10,this.mAccount.getCryptoCoin().getPrecision())));
input.setTransaction(transaction);
input.setOut(true);
input.setType(this.mAccount.getCryptoCoin());
String addr = vin.addr;
input.setAddressString(addr);
input.setIndex(vin.n);
input.setScriptHex(vin.scriptSig.hex);
input.setOriginalTxid(vin.txid);
for (GeneralCoinAddress address : this.mAddresses) {
if (address.getAddressString(this.mAccount.getNetworkParam()).equals(addr)) {
input.setAddress(address);
tempAccount = address.getAccount();
if (!address.hasTransactionOutput(input, this.mAccount.getNetworkParam())) {
address.getTransactionOutput().add(input);
}
changed = true;
}
}
transaction.getTxInputs().add(input);
}
for (Vout vout : txi.vout) {
if(vout.scriptPubKey.addresses == null || vout.scriptPubKey.addresses.length <= 0){
// The address is null, this must be a memo
String hex = vout.scriptPubKey.hex;
int opReturnIndex = hex.indexOf("6a");
if(opReturnIndex >= 0) {
byte[] memoBytes = new byte[Integer.parseInt(hex.substring(opReturnIndex+2,opReturnIndex+4),16)];
for(int i = 0; i < memoBytes.length;i++){
memoBytes[i] = Byte.parseByte(hex.substring(opReturnIndex+4+(i*2),opReturnIndex+6+(i*2)),16);
}
transaction.setMemo(new String(memoBytes));
}
}else {
GTxIO output = new GTxIO();
output.setAmount((long) (vout.value * Math.pow(10, this.mAccount.getCryptoCoin().getPrecision())));
output.setTransaction(transaction);
output.setOut(false);
output.setType(this.mAccount.getCryptoCoin());
String addr = vout.scriptPubKey.addresses[0];
output.setAddressString(addr);
output.setIndex(vout.n);
output.setScriptHex(vout.scriptPubKey.hex);
for (GeneralCoinAddress address : this.mAddresses) {
if (address.getAddressString(this.mAccount.getNetworkParam()).equals(addr)) {
output.setAddress(address);
tempAccount = address.getAccount();
if (!address.hasTransactionInput(output, this.mAccount.getNetworkParam())) {
address.getTransactionInput().add(output);
}
changed = true;
}
}
transaction.getTxOutputs().add(output);
}
}
if(txi.txlock && txi.confirmations< this.mAccount.getCryptoNet().getConfirmationsNeeded()){
transaction.setConfirm(this.mAccount.getCryptoNet().getConfirmationsNeeded());
}
//TODO database
/*SCWallDatabase db = new SCWallDatabase(this.mContext);
long idTransaction = db.getGeneralTransactionId(transaction);
if (idTransaction == -1) {
db.putGeneralTransaction(transaction);
} else {
transaction.setId(idTransaction);
db.updateGeneralTransaction(transaction);
}*/
if (tempAccount != null && transaction.getConfirm() < this.mAccount.getCryptoNet().getConfirmationsNeeded()) {
new GetTransactionData(transaction.getTxid(), tempAccount, this.mContext, true).start();
}
for (GeneralCoinAddress address : this.mAddresses) {
if (address.updateTransaction(transaction)) {
break;
}
}
}
if(changed) {
this.mAccount.balanceChange();
}
}
}
/**
* Failure of the call
* @param call The call object
* @param t The reason for the failure
*/
@Override
public void onFailure(Call<AddressTxi> call, Throwable t) {
Log.e("GetTransactionByAddress", "Error in json format");
}
/**
* Function to start the insight api call
*/
@Override
public void run() {
if (this.mAddresses.size() > 0) {
StringBuilder addressToQuery = new StringBuilder();
for (GeneralCoinAddress address : this.mAddresses) {
addressToQuery.append(address.getAddressString(this.mAccount.getNetworkParam())).append(",");
}
addressToQuery.deleteCharAt(addressToQuery.length() - 1);
InsightApiService service = this.mServiceGenerator.getService(InsightApiService.class);
Call<AddressTxi> addressTxiCall = service.getTransactionByAddress(InsightApiConstants.getPath(this.mAccount.getCryptoCoin()),addressToQuery.toString());
addressTxiCall.enqueue(this);
}
}
}

View file

@ -0,0 +1,196 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import android.content.Context;
import java.util.Date;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Txi;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Vin;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Vout;
import cy.agorise.crystalwallet.models.GTxIO;
import cy.agorise.crystalwallet.models.GeneralCoinAccount;
import cy.agorise.crystalwallet.models.GeneralCoinAddress;
import cy.agorise.crystalwallet.models.GeneralTransaction;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* CThis class retrieve the data of a single transaction
*/
public class GetTransactionData extends Thread implements Callback<Txi> {
/**
* The account to be query
*/
private final GeneralCoinAccount mAccount;
/**
* The transaction txid to be query
*/
private String mTxId;
/**
* The serviceGenerator to call
*/
private InsightApiServiceGenerator mServiceGenerator;
/**
* This app context, used to save on the DB
*/
private Context mContext;
/**
* If has to wait for another confirmation
*/
private boolean mMustWait = false;
/**
* Constructor used to query for a transaction with unknown confirmations
* @param txid The txid of the transaciton to be query
* @param account The account to be query
* @param context This app Context
*/
public GetTransactionData(String txid, GeneralCoinAccount account, Context context) {
this(txid, account, context, false);
}
/**
* Consturctor to be used qhen the confirmations of the transaction are known
* @param txid The txid of the transaciton to be query
* @param account The account to be query
* @param context This app Context
* @param mustWait If there is less confirmation that needed
*/
public GetTransactionData(String txid, GeneralCoinAccount account, Context context, boolean mustWait) {
String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/";
this.mAccount = account;
this.mTxId= txid;
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
this.mContext = context;
this.mMustWait = mustWait;
}
/**
* Function to start the insight api call
*/
@Override
public void run() {
if (this.mMustWait) {
//We are waiting for confirmation
try {
Thread.sleep(InsightApiConstants.sWaitTime);
} catch (InterruptedException ignored) {
//TODO this exception never rises
}
}
InsightApiService service = this.mServiceGenerator.getService(InsightApiService.class);
Call<Txi> txiCall = service.getTransaction(InsightApiConstants.getPath(this.mAccount.getCryptoCoin()),this.mTxId);
txiCall.enqueue(this);
}
@Override
public void onResponse(Call<Txi> call, Response<Txi> response) {
if (response.isSuccessful()) {
Txi txi = response.body();
GeneralTransaction transaction = new GeneralTransaction();
transaction.setAccount(this.mAccount);
transaction.setTxid(txi.txid);
transaction.setBlock(txi.blockheight);
transaction.setDate(new Date(txi.time * 1000));
transaction.setFee((long) (txi.fee * Math.pow(10,this.mAccount.getCryptoCoin().getPrecision())));
transaction.setConfirm(txi.confirmations);
transaction.setType(this.mAccount.getCryptoCoin());
transaction.setBlockHeight(txi.blockheight);
for (Vin vin : txi.vin) {
GTxIO input = new GTxIO();
input.setAmount((long) (vin.value * Math.pow(10,this.mAccount.getCryptoCoin().getPrecision())));
input.setTransaction(transaction);
input.setOut(true);
input.setType(this.mAccount.getCryptoCoin());
String addr = vin.addr;
input.setAddressString(addr);
input.setIndex(vin.n);
input.setScriptHex(vin.scriptSig.hex);
input.setOriginalTxid(vin.txid);
for (GeneralCoinAddress address : this.mAccount.getAddresses()) {
if (address.getAddressString(this.mAccount.getNetworkParam()).equals(addr)) {
input.setAddress(address);
if (!address.hasTransactionOutput(input, this.mAccount.getNetworkParam())) {
address.getTransactionOutput().add(input);
}
}
}
transaction.getTxInputs().add(input);
}
for (Vout vout : txi.vout) {
if(vout.scriptPubKey.addresses == null || vout.scriptPubKey.addresses.length <= 0){
// The address is null, this must be a memo
String hex = vout.scriptPubKey.hex;
int opReturnIndex = hex.indexOf("6a");
if(opReturnIndex >= 0) {
byte[] memoBytes = new byte[Integer.parseInt(hex.substring(opReturnIndex+2,opReturnIndex+4),16)];
for(int i = 0; i < memoBytes.length;i++){
memoBytes[i] = Byte.parseByte(hex.substring(opReturnIndex+4+(i*2),opReturnIndex+6+(i*2)),16);
}
transaction.setMemo(new String(memoBytes));
System.out.println("Memo read : " + transaction.getMemo()); //TODO log this line
}
}else {
GTxIO output = new GTxIO();
output.setAmount((long) (vout.value * Math.pow(10, this.mAccount.getCryptoCoin().getPrecision())));
output.setTransaction(transaction);
output.setOut(false);
output.setType(this.mAccount.getCryptoCoin());
String addr = vout.scriptPubKey.addresses[0];
output.setAddressString(addr);
output.setIndex(vout.n);
output.setScriptHex(vout.scriptPubKey.hex);
for (GeneralCoinAddress address : this.mAccount.getAddresses()) {
if (address.getAddressString(this.mAccount.getNetworkParam()).equals(addr)) {
output.setAddress(address);
if (!address.hasTransactionInput(output, this.mAccount.getNetworkParam())) {
address.getTransactionInput().add(output);
}
}
}
transaction.getTxOutputs().add(output);
}
}
// This is for features like dash instantSend
if(txi.txlock && txi.confirmations< this.mAccount.getCryptoNet().getConfirmationsNeeded()){
transaction.setConfirm(this.mAccount.getCryptoNet().getConfirmationsNeeded());
}
//TODO database
/*SCWallDatabase db = new SCWallDatabase(this.mContext);
long idTransaction = db.getGeneralTransactionId(transaction);
if (idTransaction == -1) {
db.putGeneralTransaction(transaction);
} else {
transaction.setId(idTransaction);
db.updateGeneralTransaction(transaction);
}*/
this.mAccount.updateTransaction(transaction);
this.mAccount.balanceChange();
if (transaction.getConfirm() < this.mAccount.getCryptoNet().getConfirmationsNeeded()) {
//If transaction weren't confirmed, add the transaction to watch for change on the confirmations
new GetTransactionData(this.mTxId, this.mAccount, this.mContext, true).start();
}
}
}
/**
* TODO handle the failure response
* @param call the Call object
* @param t the reason of the failure
*/
@Override
public void onFailure(Call<Txi> call, Throwable t) {
}
}

View file

@ -0,0 +1,116 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import java.util.HashMap;
import cy.agorise.crystalwallet.enums.CryptoCoin;
/**
* Class holds all constant related to the Insight Api
*
*/
abstract class InsightApiConstants {
/**
* Protocol of the insight api calls
*/
static final String sProtocol = "https";
/**
* Protocol of the insigiht api Socket.IO connection
*/
static final String sProtocolSocketIO = "http";
/**
* Contains each url information for each coin
*/
private static final HashMap<CryptoCoin,AddressPort> sServerAddressPort = new HashMap<>();
/**
* Insight api Socket.IO new transaction by address notification
*/
static final String sChangeAddressRoom = "bitcoind/addresstxid";
/**
* Socket.io subscribe command
*/
static final String sSubscribeEmmit = "subscribe";
/**
* Tag used in the response of the address transaction notification
*/
static final String sTxTag = "txid";
/**
* Wait time to check for confirmations
*/
static long sWaitTime = (30 * 1000); //wait 1 minute
//Filled the serverAddressPort maps with static data
static{
//serverAddressPort.put(Coin.BITCOIN,new AddressPort("fr.blockpay.ch",3002,"node/btc/testnet","insight-api"));
sServerAddressPort.put(CryptoCoin.BITCOIN,new AddressPort("fr.blockpay.ch",3003,"node/btc/testnet","insight-api"));
//serverAddressPort.put(Coin.BITCOIN_TEST,new AddressPort("fr.blockpay.ch",3003,"node/btc/testnet","insight-api"));
sServerAddressPort.put(CryptoCoin.LITECOIN,new AddressPort("fr.blockpay.ch",3009,"node/ltc","insight-lite-api"));
sServerAddressPort.put(CryptoCoin.DASH,new AddressPort("fr.blockpay.ch",3005,"node/dash","insight-api-dash"));
sServerAddressPort.put(CryptoCoin.DOGECOIN,new AddressPort("fr.blockpay.ch",3006,"node/dogecoin","insight-api"));
}
/**
* Get the insight api server address
* @param coin The coin of the API to find
* @return The String address of the server, can be a name or the IP
*/
static String getAddress(CryptoCoin coin){
return sServerAddressPort.get(coin).mServerAddress;
}
/**
* Get the port of the server Insight API
* @param coin The coin of the API to find
* @return The server number port
*/
static int getPort(CryptoCoin coin){
return sServerAddressPort.get(coin).mPort;
}
/**
* Get the url path of the server Insight API
* @param coin The coin of the API to find
* @return The path of the Insight API
*/
static String getPath(CryptoCoin coin){
return sServerAddressPort.get(coin).mPath + "/" + sServerAddressPort.get(coin).mInsightPath;
}
/**
* Contains all the url info neccessary to connects to the insight api
*/
private static class AddressPort{
/**
* The server address
*/
final String mServerAddress;
/**
* The port used in the Socket.io
*/
final int mPort;
/**
* The path of the coin server
*/
final String mPath;
/**
* The path of the insight api
*/
final String mInsightPath;
/**
* Constructor
* @param serverAddress The server address of the Insight API
* @param port the port number of the Insight API
* @param path the path to the Insight API before the last /
* @param insightPath the path after the last / of the Insight API
*/
AddressPort(String serverAddress, int port, String path, String insightPath) {
this.mServerAddress = serverAddress;
this.mPort = port;
this.mPath = path;
this.mInsightPath = insightPath;
}
}
}

View file

@ -0,0 +1,52 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import com.google.gson.JsonObject;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.AddressTxi;
import cy.agorise.crystalwallet.apigenerator.insightapi.models.Txi;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
/**
* Holds each call to the insigh api server
*/
interface InsightApiService {
/**
* The query for the info of a single transaction
* @param path The path of the insight api without the server address
* @param txid the transasction to be query
*/
@GET("{path}/tx/{txid}")
Call<Txi> getTransaction(@Path(value = "path", encoded = true) String path, @Path(value = "txid", encoded = true) String txid);
/**
* The query for the transasctions of multiples addresses
* @param path The path of the insight api without the server address
* @param addrs the addresses to be query each separated with a ","
*/
@GET("{path}/addrs/{addrs}/txs")
Call<AddressTxi> getTransactionByAddress(@Path(value = "path", encoded = true) String path, @Path(value = "addrs", encoded = true) String addrs);
/**
* Broadcast Transaction
* @param path The path of the insight api without the server address
* @param rawtx the rawtx to send in Hex String
*/
@FormUrlEncoded
@POST("{path}/tx/send")
Call<Txi> broadcastTransaction(@Path(value = "path", encoded = true) String path, @Field("rawtx") String rawtx);
/**
* Get the estimate rate fee for a coin in the Insight API
* @param path The path of the insight api without the server address
*/
@GET("{path}/utils/estimatefee?nbBlocks=2")
Call<JsonObject> estimateFee(@Path(value = "path", encoded = true) String path);
}

View file

@ -0,0 +1,130 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Generatir fir tge okhttp connection of the Insight API
* TODO finish documentation
*/
class InsightApiServiceGenerator {
/**
* Tag used for logging
*/
public static String TAG = "InsightApiServiceGenerator";
/**
* The complete uri to connect to the insight api, this change from coin to coin
*/
private static String sApiBaseUrl;
/**
* Loggin interceptor
*/
private static HttpLoggingInterceptor sLogging;
/**
* Http builder
*/
private static OkHttpClient.Builder sClientBuilder;
/**
* Builder for the retrofit class
*/
private static Retrofit.Builder sBuilder;
/**
*
*/
private static HashMap<Class<?>, Object> sServices;
/**
* Constructor, using the url of a insigth api coin
* @param apiBaseUrl The complete url to the server of the insight api
*/
InsightApiServiceGenerator(String apiBaseUrl) {
sApiBaseUrl= apiBaseUrl;
sLogging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
sClientBuilder = new OkHttpClient.Builder().addInterceptor(sLogging);
sBuilder = new Retrofit.Builder().baseUrl(sApiBaseUrl).addConverterFactory(GsonConverterFactory.create());
sServices = new HashMap<>();
}
/**
*
* @param klass
* @param thing
* @param <T>
*/
private static <T> void setService(Class<T> klass, T thing) {
sServices.put(klass, thing);
}
/**
*
* @param serviceClass
* @param <T>
* @return
*/
public <T> T getService(Class<T> serviceClass) {
T service = serviceClass.cast(sServices.get(serviceClass));
if (service == null) {
service = createService(serviceClass);
setService(serviceClass, service);
}
return service;
}
/**
*
* @param serviceClass
* @param <S>
* @return
*/
private static <S> S createService(Class<S> serviceClass) {
sClientBuilder.interceptors().add(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Request original = chain.request();
okhttp3.Request.Builder requestBuilder = original.newBuilder().method(original.method(), original.body());
okhttp3.Request request = requestBuilder.build();
return chain.proceed(request);
}
});
sClientBuilder.readTimeout(5, TimeUnit.MINUTES);
sClientBuilder.connectTimeout(5, TimeUnit.MINUTES);
OkHttpClient client = sClientBuilder.build();
Retrofit retrofit = sBuilder.client(client).build();
return retrofit.create(serviceClass);
}
/**
*
* @return
*/
public static InsightApiService Create() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.interceptors().add(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Request original = chain.request();
// Customize the request
okhttp3.Request request = original.newBuilder().method(original.method(), original.body()).build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(sApiBaseUrl).client(client).build();
return retrofit.create(InsightApiService.class);
}
}

View file

@ -0,0 +1,24 @@
package cy.agorise.crystalwallet.apigenerator.insightapi.models;
/**
* Represents the address txi of a insishgt api response
*/
public class AddressTxi {
/**
* The total number of items
*/
public int totalItems;
/**
* The start index of the current txi
*/
public int from;
/**
* the last index of the current txi
*/
public int to;
/**
* The arrays of txi of this response
*/
public Txi[] items;
}

View file

@ -0,0 +1,24 @@
package cy.agorise.crystalwallet.apigenerator.insightapi.models;
/**
* The transasction Script public keym is used to validate
*/
public class ScriptPubKey {
/**
* The code to validate in hex
*/
public String hex;
/**
* the code to validate this transaction
*/
public String asm;
/**
* the acoin address involved
*/
public String[] addresses;
/**
* The type of the hash
*/
public String type;
}

View file

@ -0,0 +1,16 @@
package cy.agorise.crystalwallet.apigenerator.insightapi.models;
/**
* Reprensents the Script signature of an trnasaction Input
*/
public class ScriptSig {
/**
* The hex
*/
public String hex;
/**
* the asm
*/
public String asm;
}

View file

@ -0,0 +1,69 @@
package cy.agorise.crystalwallet.apigenerator.insightapi.models;
/**
* Represents one transaction input of the insight API
*/
public class Txi {
/**
* The id of this transaction
*/
public String txid;
/**
* the version number of this transaction
*/
public int version;
/**
* Time to hold this transaction
*/
public long locktime;
/**
* The array of the transaction inputs
*/
public Vin[] vin;
/**
* the array of the transactions outputs
*/
public Vout[] vout;
/**
* this block hash
*/
public String blockhash;
/**
* The blockheight where this transaction belongs, if 0 this transactions hasn't be included in any block yet
*/
public int blockheight;
/**
* Number of confirmations
*/
public int confirmations;
/**
* The time of the first broadcast fo this transaction
*/
public long time;
/**
* The time which this transaction was included
*/
public long blocktime;
/**
* Total value to transactions outputs
*/
public double valueOut;
/**
* The size in bytes
*/
public int size;
/**
* Total value of transactions inputs
*/
public double valueIn;
/**
* Fee of this transaction has to be valueIn - valueOut
*/
public double fee;
/**
* This is only for dash, is the instantsend state
*/
public boolean txlock=false;
}

View file

@ -0,0 +1,44 @@
package cy.agorise.crystalwallet.apigenerator.insightapi.models;
/**
* This represents a transaction input
*/
public class Vin {
/**
* The original transaction id where this transaction is an output
*/
public String txid;
/**
*
*/
public int vout;
/**
* Sequence fo the transaction
*/
public long sequence;
/**
* Order of the transasction input on the transasction
*/
public int n;
/**
* The script signature
*/
public ScriptSig scriptSig;
/**
* The addr of this transaction
*/
public String addr;
/**
* Value in satoshi
*/
public long valueSat;
/**
* Calue of this transaction
*/
public double value;
/**
*
*/
public String doubleSpentTxID;
}

View file

@ -0,0 +1,32 @@
package cy.agorise.crystalwallet.apigenerator.insightapi.models;
/**
* Represents a Transasction output
*/
public class Vout {
/**
* The amount of coin
*/
public double value;
/**
* the order of this transaciton output on the transaction
*/
public int n;
/**
* The script public key
*/
public ScriptPubKey scriptPubKey;
/**
* If this transaciton output was spent what txid it belongs
*/
public String spentTxId;
/**
* The index on the transaction that this transaction was spent
*/
public String spentIndex;
/**
* The block height of the transaction this output was spent
*/
public String spentHeight;
}

View file

@ -0,0 +1,166 @@
package cy.agorise.crystalwallet.models;
import cy.agorise.crystalwallet.enums.CryptoCoin;
/**
* General Coin Transaction Input/Output
*
* This class represent each Input or Output Transaction of a General Coin Transaction
*
* Created by henry on 06/02/2017.
*/
public class GTxIO {
/**
* The id on the database
*/
private long mId = -1;
/**
* The Coin type of this transaction
*/
private CryptoCoin mType;
/**
* The index on the transaction Input/Output
*/
private int mIndex;
/**
* The address that this transaction Input/Output belongs
*/
private GeneralCoinAddress mAddress;
/**
* The transaction that this Input/Output belongs
*/
private GeneralTransaction mTransaction;
/**
* The amount
*/
private long mAmount;
/**