Added path to the insigiht api

Added Bitcoin Server Verifier
Added Bitcoin url and db currency
This commit is contained in:
hvarona 2018-10-30 22:16:27 -04:00
parent 960dd67394
commit 6c0d936b67
15 changed files with 160 additions and 42 deletions

View file

@ -637,7 +637,7 @@ public abstract class GrapheneApiGenerator {
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
CryptoCurrency baseCurrency = cryptoCurrencyDao.getByName(baseAssetName);
CryptoCurrency baseCurrency = cryptoCurrencyDao.getByName(baseAssetName,CryptoNet.BITSHARES.name());
BitsharesAssetInfo info = null;
if(baseCurrency != null){
info = db.bitsharesAssetDao().getBitsharesAssetInfo(baseCurrency.getId());

View file

@ -14,6 +14,8 @@ public class InsightApiGenerator {
private static HashMap<CryptoCoin,GetTransactionByAddress> transactionGetters = new HashMap();
private static HashMap<CryptoCoin,AddressesActivityWatcher> transactionFollowers = new HashMap();
private static final String PATH = "api";
/**
* Fecth all the transaciton for a giving address
* @param cryptoCoin the crypto net of the address
@ -22,13 +24,13 @@ public class InsightApiGenerator {
*/
public static void getTransactionFromAddress(CryptoCoin cryptoCoin, String address, boolean subscribe){
if(!transactionGetters.containsKey(cryptoCoin)){
transactionGetters.put(cryptoCoin,new GetTransactionByAddress(cryptoCoin,CryptoNetManager.getURL(cryptoCoin.getCryptoNet())));
transactionGetters.put(cryptoCoin,new GetTransactionByAddress(cryptoCoin,CryptoNetManager.getURL(cryptoCoin.getCryptoNet()),PATH));
}
transactionGetters.get(cryptoCoin).addAddress(address);
transactionGetters.get(cryptoCoin).start();
if(subscribe){
if(!transactionFollowers.containsKey(cryptoCoin)){
transactionFollowers.put(cryptoCoin,new AddressesActivityWatcher(CryptoNetManager.getURL(cryptoCoin.getCryptoNet()),cryptoCoin));
transactionFollowers.put(cryptoCoin,new AddressesActivityWatcher(CryptoNetManager.getURL(cryptoCoin.getCryptoNet()),PATH,cryptoCoin));
}
transactionFollowers.get(cryptoCoin).addAddress(address);
transactionFollowers.get(cryptoCoin).connect();
@ -42,7 +44,7 @@ public class InsightApiGenerator {
*/
public static void broadcastTransaction(CryptoCoin cryptoCoin, String rawtx, final ApiRequest request){
BroadcastTransaction bTransaction = new BroadcastTransaction(rawtx,
CryptoNetManager.getURL(cryptoCoin.getCryptoNet()), "api", new BroadcastTransaction.BroadCastTransactionListener() {
CryptoNetManager.getURL(cryptoCoin.getCryptoNet()), PATH, new BroadcastTransaction.BroadCastTransactionListener() {
@Override
public void onSuccess() {
request.getListener().success(true,request.getId());

View file

@ -38,6 +38,7 @@ public class AddressesActivityWatcher {
private Socket mSocket;
private final String mServerUrl;
private final String mPath;
/**
* Handles the address/transaction notification.
@ -49,7 +50,7 @@ public class AddressesActivityWatcher {
try {
System.out.println("Receive accountActivtyWatcher " + os[0].toString() );
String txid = ((JSONObject) os[0]).getString(InsightApiConstants.sTxTag);
new GetTransactionData(txid, mServerUrl, cryptoCoin).start();
new GetTransactionData(txid, mServerUrl, mPath, cryptoCoin).start();
} catch (JSONException ex) {
Logger.getLogger(AddressesActivityWatcher.class.getName()).log(Level.SEVERE, null, ex);
}
@ -106,7 +107,8 @@ public class AddressesActivityWatcher {
* Basic constructor
*
*/
public AddressesActivityWatcher(String serverUrl, CryptoCoin cryptoCoin) {
public AddressesActivityWatcher(String serverUrl, String path, CryptoCoin cryptoCoin) {
this.mPath = path;
this.mServerUrl = serverUrl;
this.cryptoCoin = cryptoCoin;
try {

View file

@ -20,6 +20,8 @@ import retrofit2.Response;
public abstract class GetEstimateFee {
private static String PATH = "api";
/**
* The funciton to get the rate for the transaction be included in the next 2 blocks
* @param serverUrl The url of the insight server
@ -29,12 +31,11 @@ public abstract class GetEstimateFee {
try {
InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl);
InsightApiService service = serviceGenerator.getService(InsightApiService.class);
Call<JsonObject> call = service.estimateFee(serverUrl);
final JsonObject answer = new JsonObject();
Call<JsonObject> call = service.estimateFee(PATH);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
listener.estimateFee((long) (answer.get("answer").getAsDouble()));
listener.estimateFee((long) (response.body().get("2").getAsDouble()));
}

View file

@ -0,0 +1,43 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class GetGenesisBlock {
private static String PATH = "api";
public GetGenesisBlock(String serverUrl, final genesisBlockListener listener) {
try {
InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl);
InsightApiService service = serviceGenerator.getService(InsightApiService.class);
Call<JsonObject> call = service.genesisBlock(PATH);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
try {
listener.genesisBlock(response.body().get("blockHash").getAsString());
}catch(Exception e){
e.printStackTrace();
listener.fail();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
listener.fail();
}
});
}catch(Exception e){
listener.fail();
}
}
public interface genesisBlockListener{
void genesisBlock(String value);
void fail();
}
}

View file

@ -1,27 +1,14 @@
package cy.agorise.crystalwallet.apigenerator.insightapi;
import android.content.Context;
import android.util.Log;
import com.idescout.sql.SqlScoutServer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cy.agorise.crystalwallet.apigenerator.InsightApiGenerator;
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.enums.CryptoCoin;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.manager.GeneralAccountManager;
import cy.agorise.crystalwallet.models.CryptoCurrency;
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;
@ -41,7 +28,9 @@ public class GetTransactionByAddress extends Thread implements Callback<AddressT
*/
private InsightApiServiceGenerator mServiceGenerator;
private String serverUrl;
private String mServerUrl;
private String mPath;
private CryptoCoin cryptoNet;
private boolean inProcess = false;
@ -49,9 +38,10 @@ public class GetTransactionByAddress extends Thread implements Callback<AddressT
/**
* Basic consturcotr
*/
public GetTransactionByAddress(CryptoCoin cryptoNet, String serverUrl) {
public GetTransactionByAddress(CryptoCoin cryptoNet, String serverUrl,String path) {
this.mPath = path;
this.cryptoNet = cryptoNet;
this.serverUrl = serverUrl;
this.mServerUrl = serverUrl;
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
}
@ -107,7 +97,7 @@ public class GetTransactionByAddress extends Thread implements Callback<AddressT
}
addressToQuery.deleteCharAt(addressToQuery.length() - 1);
InsightApiService service = this.mServiceGenerator.getService(InsightApiService.class);
Call<AddressTxi> addressTxiCall = service.getTransactionByAddress(this.serverUrl,addressToQuery.toString());
Call<AddressTxi> addressTxiCall = service.getTransactionByAddress(this.mPath,addressToQuery.toString());
addressTxiCall.enqueue(this);
}
}

View file

@ -32,6 +32,8 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
private InsightApiServiceGenerator mServiceGenerator;
private String mServerUrl;
private String mPath;
/**
* If has to wait for another confirmation
*/
@ -43,8 +45,8 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
* Constructor used to query for a transaction with unknown confirmations
* @param txid The txid of the transaciton to be query
*/
public GetTransactionData(String txid, String serverUrl, CryptoCoin cryptoCoin) {
this(txid, serverUrl, cryptoCoin, false);
public GetTransactionData(String txid, String serverUrl, String path, CryptoCoin cryptoCoin) {
this(txid, serverUrl, path, cryptoCoin, false);
}
@ -53,7 +55,8 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
* @param txid The txid of the transaciton to be query
* @param mustWait If there is less confirmation that needed
*/
public GetTransactionData(String txid, String serverUrl, CryptoCoin cryptoCoin, boolean mustWait) {
public GetTransactionData(String txid, String serverUrl, String path, CryptoCoin cryptoCoin, boolean mustWait) {
this.mPath = path;
this.mServerUrl = serverUrl;
this.mTxId= txid;
this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl);
@ -76,7 +79,7 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
}
InsightApiService service = this.mServiceGenerator.getService(InsightApiService.class);
Call<Txi> txiCall = service.getTransaction(this.mServerUrl,this.mTxId);
Call<Txi> txiCall = service.getTransaction(this.mPath,this.mTxId);
txiCall.enqueue(this);
}
@ -88,7 +91,7 @@ public class GetTransactionData extends Thread implements Callback<Txi> {
GeneralAccountManager.getAccountManager(this.cryptoCoin).processTxi(txi);
if (txi.confirmations < this.cryptoCoin.getCryptoNet().getConfirmationsNeeded()) {
//If transaction weren't confirmed, add the transaction to watch for change on the confirmations
new GetTransactionData(this.mTxId, this.mServerUrl, this.cryptoCoin, true).start();
new GetTransactionData(this.mTxId, this.mServerUrl, this.mPath, this.cryptoCoin, true).start();
}
}
}

View file

@ -49,4 +49,7 @@ interface InsightApiService {
@GET("{path}/utils/estimatefee?nbBlocks=2")
Call<JsonObject> estimateFee(@Path(value = "path", encoded = true) String path);
@GET("{path}/block-index/0")
Call<JsonObject> genesisBlock(@Path(value = "path", encoded = true) String path);
}

View file

@ -17,6 +17,7 @@ import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence;
import cy.agorise.crystalwallet.models.GeneralSetting;
import cy.agorise.crystalwallet.network.CryptoNetManager;
@ -49,6 +50,13 @@ public class CrystalApplication extends Application {
//This is for testing the equivalent values on the testnet TODO remove
public static BitsharesAsset bitEURAsset = new BitsharesAsset("EUR",4,"1.3.120",BitsharesAsset.Type.SMART_COIN);
public static final String BITCOIN_SERVER_URLS[] ={
"https://insight.bitpay.com/"
};
public static final CryptoCurrency BITCOIN_CURRENCY = new CryptoCurrency("BTC",CryptoNet.BITCOIN,8);
@Override
public void onCreate() {
super.onCreate();
@ -62,10 +70,10 @@ public class CrystalApplication extends Application {
//This is for testing the equivalent values on the testnet TODO remove
if(db.bitsharesAssetDao().getBitsharesAssetInfoById(bitEURAsset.getBitsharesId())== null){
if(db.cryptoCurrencyDao().getByName(bitEURAsset.getName())== null){
if(db.cryptoCurrencyDao().getByName(bitEURAsset.getName(),bitEURAsset.getCryptoNet().name())== null){
db.cryptoCurrencyDao().insertCryptoCurrency(bitEURAsset);
}
long idCurrency = db.cryptoCurrencyDao().getByName(bitEURAsset.getName()).getId();
long idCurrency = db.cryptoCurrencyDao().getByName(bitEURAsset.getName(),bitEURAsset.getCryptoNet().name()).getId();
BitsharesAssetInfo info = new BitsharesAssetInfo(bitEURAsset);
info.setCryptoCurrencyId(idCurrency);
db.bitsharesAssetDao().insertBitsharesAssetInfo(info);
@ -74,10 +82,10 @@ public class CrystalApplication extends Application {
//This is for testing the equivalent values on the testnet TODO remove
if(db.bitsharesAssetDao().getBitsharesAssetInfoById(bitUSDAsset.getBitsharesId())== null){
if(db.cryptoCurrencyDao().getByName(bitUSDAsset.getName())== null){
if(db.cryptoCurrencyDao().getByName(bitUSDAsset.getName(),bitUSDAsset.getCryptoNet().name())== null){
db.cryptoCurrencyDao().insertCryptoCurrency(bitUSDAsset);
}
long idCurrency = db.cryptoCurrencyDao().getByName(bitUSDAsset.getName()).getId();
long idCurrency = db.cryptoCurrencyDao().getByName(bitUSDAsset.getName(),bitUSDAsset.getCryptoNet().name()).getId();
BitsharesAssetInfo info = new BitsharesAssetInfo(bitUSDAsset);
info.setCryptoCurrencyId(idCurrency);
db.bitsharesAssetDao().insertBitsharesAssetInfo(info);
@ -93,6 +101,14 @@ public class CrystalApplication extends Application {
// TODO and hoop over the urls if no connection can be established
CryptoNetManager.addCryptoNetURL(CryptoNet.BITSHARES,BITSHARES_URL);
//Adding Bitcoin info
CryptoNetManager.addCryptoNetURL(CryptoNet.BITCOIN,BITCOIN_SERVER_URLS);
if(db.cryptoCurrencyDao().getByName(BITCOIN_CURRENCY.getName(),BITCOIN_CURRENCY.getCryptoNet().name())== null){
db.cryptoCurrencyDao().insertCryptoCurrency(BITCOIN_CURRENCY);
}
GeneralSetting generalSettingPreferredLanguage = db.generalSettingDao().getSettingByName(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE);
if (generalSettingPreferredLanguage != null) {

View file

@ -74,10 +74,10 @@ public abstract class BitsharesConstant {
public static void addSmartCoins(Context context){
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
for(BitsharesAsset smartcoin : SMARTCOINS){
if(db.cryptoCurrencyDao().getByName(smartcoin.getName())== null){
if(db.cryptoCurrencyDao().getByName(smartcoin.getName(),CryptoNet.BITSHARES.name())== null){
db.cryptoCurrencyDao().insertCryptoCurrency(smartcoin);
}
long idCurrency = db.cryptoCurrencyDao().getByName(smartcoin.getName()).getId();
long idCurrency = db.cryptoCurrencyDao().getByName(smartcoin.getName(),CryptoNet.BITSHARES.name()).getId();
BitsharesAssetInfo info = new BitsharesAssetInfo(smartcoin);
info.setCryptoCurrencyId(idCurrency);
db.bitsharesAssetDao().insertBitsharesAssetInfo(info);

View file

@ -35,8 +35,8 @@ public interface CryptoCurrencyDao {
@Query("SELECT * FROM crypto_currency WHERE name = :name")
LiveData<CryptoCurrency> getLiveDataByName(String name);
@Query("SELECT * FROM crypto_currency WHERE name = :name")
CryptoCurrency getByName(String name);
@Query("SELECT * FROM crypto_currency WHERE name = :name and crypto_net = :cryptoNet")
CryptoCurrency getByName(String name, String cryptoNet);
@Insert(onConflict = OnConflictStrategy.IGNORE)
public long[] insertCryptoCurrency(CryptoCurrency... currencies);

View file

@ -130,7 +130,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
long[] idAccount = db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
grapheneAccount.setId(idAccount[0]);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
//subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
}
@Override
@ -148,7 +148,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
//subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
}
@Override
@ -160,7 +160,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount));
//subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context);
}
}
}

View file

@ -45,6 +45,15 @@ public class CryptoCurrency {
@ColumnInfo(name = "precision")
private int mPrecision;
public CryptoCurrency() {
}
public CryptoCurrency(String name, CryptoNet cryptoNet, int precision) {
this.mName = name;
this.mCryptoNet = cryptoNet;
this.mPrecision = precision;
}
public long getId() {
return mId;
}

View file

@ -0,0 +1,46 @@
package cy.agorise.crystalwallet.network;
import cy.agorise.crystalwallet.apigenerator.insightapi.GetGenesisBlock;
import cy.agorise.crystalwallet.enums.CryptoCoin;
public class BitcoinCryptoNetVerifier extends CryptoNetVerifier{
final CryptoCoin cryptoCoin;
public BitcoinCryptoNetVerifier(CryptoCoin cryptoCoin) {
this.cryptoCoin = cryptoCoin;
}
@Override
public void checkURL(final String url) {
final long startTime = System.currentTimeMillis();
GetGenesisBlock genesisBloc = new GetGenesisBlock(url, new GetGenesisBlock.genesisBlockListener() {
@Override
public void genesisBlock(String value) {
if(cryptoCoin.getParameters()!= null){
if(value.equals(cryptoCoin.getParameters().getGenesisBlock().getHashAsString())){
CryptoNetManager.verifiedCryptoNetURL(cryptoCoin.getCryptoNet(), url, System.currentTimeMillis() - startTime);
}
//TODO bad genesis block
}else{
CryptoNetManager.verifiedCryptoNetURL(cryptoCoin.getCryptoNet(), url, System.currentTimeMillis() - startTime);
}
}
@Override
public void fail() {
//TODO failed
}
});
}
@Override
public String getChainId() {
if(cryptoCoin == null || cryptoCoin.getParameters()== null) {
return null;
}
return cryptoCoin.getParameters().getGenesisBlock().getHashAsString();
}
}

View file

@ -1,5 +1,6 @@
package cy.agorise.crystalwallet.network;
import cy.agorise.crystalwallet.enums.CryptoCoin;
import cy.agorise.crystalwallet.enums.CryptoNet;
/**
@ -15,6 +16,8 @@ public abstract class CryptoNetVerifier {
static CryptoNetVerifier getNetworkVerify(CryptoNet cryptoNet){
if(cryptoNet.getLabel().equals(CryptoNet.BITSHARES.getLabel())){
return new BitsharesCryptoNetVerifier();
}else if(cryptoNet.getLabel().equals(CryptoNet.BITCOIN.getLabel())){
return new BitcoinCryptoNetVerifier(CryptoCoin.BITCOIN);
}
return null;
}