diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/InsightApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/InsightApiGenerator.java index 35b0f71..7a5505f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/InsightApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/InsightApiGenerator.java @@ -1,54 +1,50 @@ package cy.agorise.crystalwallet.apigenerator; -import android.content.Context; - import java.util.HashMap; +import cy.agorise.crystalwallet.apigenerator.insightapi.AddressesActivityWatcher; import cy.agorise.crystalwallet.apigenerator.insightapi.BroadcastTransaction; import cy.agorise.crystalwallet.apigenerator.insightapi.GetEstimateFee; import cy.agorise.crystalwallet.apigenerator.insightapi.GetTransactionByAddress; -import cy.agorise.crystalwallet.apigenerator.insightapi.GetTransactionData; -import cy.agorise.crystalwallet.enums.CryptoNet; +import cy.agorise.crystalwallet.enums.CryptoCoin; import cy.agorise.crystalwallet.network.CryptoNetManager; public class InsightApiGenerator { - private static HashMap transactionGetters = new HashMap(); - private static HashMap transacitonFollowers = new HashMap(); + private static HashMap transactionGetters = new HashMap(); + private static HashMap transactionFollowers = new HashMap(); /** * Fecth all the transaciton for a giving address - * @param cryptoNet the crypto net of the address + * @param cryptoCoin the crypto net of the address * @param address The address String * @param request the request api to response * @param subscribe If needs to follow the address (Real time) */ - public static void getTransactionFromAddress(CryptoNet cryptoNet, String address, + public static void getTransactionFromAddress(CryptoCoin cryptoCoin, String address, ApiRequest request, boolean subscribe){ - if(!transactionGetters.containsKey(cryptoNet)){ - transactionGetters.put(cryptoNet,new GetTransactionByAddress(cryptoNet,CryptoNetManager.getURL(cryptoNet))); + if(!transactionGetters.containsKey(cryptoCoin)){ + transactionGetters.put(cryptoCoin,new GetTransactionByAddress(cryptoCoin,CryptoNetManager.getURL(cryptoCoin.getCryptoNet()))); + } + 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.get(cryptoCoin).addAddress(address); + transactionFollowers.get(cryptoCoin).connect(); } - transactionGetters.get(cryptoNet).addAddress(address); - //TODO process request - } - - /** - * Funciton used for unconfirmed transactions - * @param cryptoNet - * @param txid - * @param context - */ - public static void followTransaction(CryptoNet cryptoNet, String txid, Context context){ - } /** * Broadcast an insight api transaction - * @param cryptoNet The cryptoNet of the transaction + * @param cryptoCoin The cryptoNet of the transaction * @param rawtx the transaction to be broadcasted */ - public static void broadcastTransaction(CryptoNet cryptoNet, String rawtx, final ApiRequest request){ - BroadcastTransaction bTransaction = new BroadcastTransaction(rawtx, CryptoNetManager.getURL(cryptoNet), "api", new BroadcastTransaction.BroadCastTransactionListener() { + public static void broadcastTransaction(CryptoCoin cryptoCoin, String rawtx, final ApiRequest request){ + BroadcastTransaction bTransaction = new BroadcastTransaction(rawtx, + CryptoNetManager.getURL(cryptoCoin.getCryptoNet()), "api", new BroadcastTransaction.BroadCastTransactionListener() { @Override public void onSuccess() { request.getListener().success(true,request.getId()); @@ -64,13 +60,15 @@ public class InsightApiGenerator { request.getListener().fail(request.getId()); } }); + bTransaction.start(); } /** * Fetch the estimated fee for a transaction */ - public static void getEstimateFee(CryptoNet cryptoNet, final ApiRequest request){ - GetEstimateFee.getEstimateFee(CryptoNetManager.getURL(cryptoNet), new GetEstimateFee.estimateFeeListener() { + public static void getEstimateFee(CryptoCoin cryptoCoin, final ApiRequest request){ + GetEstimateFee.getEstimateFee(CryptoNetManager.getURL(cryptoCoin.getCryptoNet()), + new GetEstimateFee.estimateFeeListener() { @Override public void estimateFee(long value) { request.listener.success(value,request.getId()); diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AddressesActivityWatcher.java similarity index 79% rename from app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java rename to app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AddressesActivityWatcher.java index 1745efb..719c412 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AddressesActivityWatcher.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import cy.agorise.crystalwallet.enums.CryptoCoin; import cy.agorise.crystalwallet.models.GeneralCoinAccount; import io.socket.client.IO; import io.socket.client.Socket; @@ -24,12 +25,9 @@ import io.socket.emitter.Emitter; * */ -public class AccountActivityWatcher { +public class AddressesActivityWatcher { - /** - * The mAccount to be monitor - */ - private final GeneralCoinAccount mAccount; + private final CryptoCoin cryptoCoin; /** * The list of address to monitor */ @@ -38,10 +36,6 @@ public class AccountActivityWatcher { * the Socket.IO */ private Socket mSocket; - /** - * This app mContext, used to save on the DB - */ - private final Context mContext; private final String mServerUrl; @@ -55,9 +49,9 @@ public class AccountActivityWatcher { try { System.out.println("Receive accountActivtyWatcher " + os[0].toString() ); String txid = ((JSONObject) os[0]).getString(InsightApiConstants.sTxTag); - new GetTransactionData(txid, mAccount, mServerUrl, mContext).start(); + new GetTransactionData(txid, mServerUrl, cryptoCoin).start(); } catch (JSONException ex) { - Logger.getLogger(AccountActivityWatcher.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(AddressesActivityWatcher.class.getName()).log(Level.SEVERE, null, ex); } } }; @@ -84,7 +78,9 @@ public class AccountActivityWatcher { private final Emitter.Listener onDisconnect = new Emitter.Listener() { @Override public void call(Object... os) { - System.out.println("Disconnected to accountActivityWatcher"); + try { + Thread.sleep(60000); + } catch (InterruptedException ignore) {} mSocket.connect(); } }; @@ -99,19 +95,20 @@ public class AccountActivityWatcher { for(Object ob : os) { System.out.println("accountActivityWatcher " + ob.toString()); } + try { + Thread.sleep(60000); + } catch (InterruptedException ignore) {} + mSocket.connect(); } }; /** * Basic constructor * - * @param mAccount The mAccount to be monitor - * @param mContext This app mContext */ - public AccountActivityWatcher(String serverUrl, GeneralCoinAccount mAccount, Context mContext) { + public AddressesActivityWatcher(String serverUrl, CryptoCoin cryptoCoin) { this.mServerUrl = serverUrl; - this.mAccount = mAccount; - this.mContext = mContext; + this.cryptoCoin = cryptoCoin; try { this.mSocket = IO.socket(serverUrl); this.mSocket.on(Socket.EVENT_CONNECT, onConnect); @@ -141,13 +138,11 @@ public class AccountActivityWatcher { * 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()); + if(this.mSocket == null || !this.mSocket.connected()) { + this.mSocket.connect(); + } + }catch(Exception ignore){ } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java index 853d1f7..5ca3894 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java @@ -16,6 +16,7 @@ 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; @@ -41,14 +42,14 @@ public class GetTransactionByAddress extends Thread implements Callback { - /** - * The account to be query - */ - private final GeneralCoinAccount mAccount; /** * The transaction txid to be query */ @@ -32,10 +30,6 @@ public class GetTransactionData extends Thread implements Callback { * The serviceGenerator to call */ private InsightApiServiceGenerator mServiceGenerator; - /** - * This app context, used to save on the DB - */ - private Context mContext; private String mServerUrl; /** @@ -43,30 +37,28 @@ public class GetTransactionData extends Thread implements Callback { */ private boolean mMustWait = false; + private CryptoCoin cryptoCoin; + /** * 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,String serverUrl, Context context) { - this(txid, account, serverUrl, context, false); + public GetTransactionData(String txid, String serverUrl, CryptoCoin cryptoCoin) { + this(txid, serverUrl, cryptoCoin, 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,String serverUrl, Context context, boolean mustWait) { + public GetTransactionData(String txid, String serverUrl, CryptoCoin cryptoCoin, boolean mustWait) { this.mServerUrl = serverUrl; - this.mAccount = account; this.mTxId= txid; this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl); - this.mContext = context; this.mMustWait = mustWait; + this.cryptoCoin = cryptoCoin; } /** @@ -91,97 +83,12 @@ public class GetTransactionData extends Thread implements Callback { @Override public void onResponse(Call call, Response 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()) { + 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.mAccount, this.mServerUrl, this.mContext, true).start(); + new GetTransactionData(this.mTxId, this.mServerUrl, this.cryptoCoin, true).start(); } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java index 8f33755..5b62286 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitcoinTransactionDao.java @@ -2,8 +2,11 @@ package cy.agorise.crystalwallet.dao; import android.arch.lifecycle.LiveData; import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; import android.arch.persistence.room.Query; +import cy.agorise.crystalwallet.models.BitcoinTransaction; import cy.agorise.crystalwallet.models.BitcoinTransactionExtended; /** @@ -14,4 +17,10 @@ public interface BitcoinTransactionDao { @Query("SELECT * FROM crypto_coin_transaction cct, bitcoin_transaction bt WHERE bt.crypto_coin_transaction_id = cct.id") LiveData getAll(); + + @Query("SELECT * FROM bitcoin_transaction bt WHERE bt.tx_id = :txid") + BitcoinTransaction getByTxid(String txid); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public long[] insertBitcoinTransaction(BitcoinTransaction... transactions); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java index fb0fc48..ec5edb2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -21,6 +21,7 @@ import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperationBuilder; import cy.agorise.crystalwallet.application.constant.BitsharesConstant; import cy.agorise.crystalwallet.dao.AccountSeedDao; +import cy.agorise.crystalwallet.enums.CryptoCoin; import cy.agorise.crystalwallet.models.BitsharesAccountNameCache; import cy.agorise.crystalwallet.models.seed.BIP39; import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest; @@ -220,24 +221,26 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI */ @Override public void onNewRequest(CryptoNetInfoRequest request) { - if (request instanceof ValidateImportBitsharesAccountRequest){ - this.validateImportAccount((ValidateImportBitsharesAccountRequest) request); - } else if (request instanceof ValidateExistBitsharesAccountRequest){ - this.validateExistAcccount((ValidateExistBitsharesAccountRequest) request); - } else if (request instanceof ValidateBitsharesSendRequest){ - this.validateSendRequest((ValidateBitsharesSendRequest) request); - }else if (request instanceof CryptoNetEquivalentRequest){ - this.getEquivalentValue((CryptoNetEquivalentRequest) request); - }else if (request instanceof ValidateCreateBitsharesAccountRequest){ - this.validateCreateAccount((ValidateCreateBitsharesAccountRequest) request); - }else if (request instanceof ValidateBitsharesLTMUpgradeRequest){ - this.validateLTMAccountUpgrade((ValidateBitsharesLTMUpgradeRequest) request); - }else if (request instanceof GetBitsharesAccountNameCacheRequest){ - this.getBitsharesAccountNameCacheRequest((GetBitsharesAccountNameCacheRequest) request); - }else{ + if(request.getCoin().equals(CryptoCoin.BITSHARES)) { + if (request instanceof ValidateImportBitsharesAccountRequest) { + this.validateImportAccount((ValidateImportBitsharesAccountRequest) request); + } else if (request instanceof ValidateExistBitsharesAccountRequest) { + this.validateExistAcccount((ValidateExistBitsharesAccountRequest) request); + } else if (request instanceof ValidateBitsharesSendRequest) { + this.validateSendRequest((ValidateBitsharesSendRequest) request); + } else if (request instanceof CryptoNetEquivalentRequest) { + this.getEquivalentValue((CryptoNetEquivalentRequest) request); + } else if (request instanceof ValidateCreateBitsharesAccountRequest) { + this.validateCreateAccount((ValidateCreateBitsharesAccountRequest) request); + } else if (request instanceof ValidateBitsharesLTMUpgradeRequest) { + this.validateLTMAccountUpgrade((ValidateBitsharesLTMUpgradeRequest) request); + } else if (request instanceof GetBitsharesAccountNameCacheRequest) { + this.getBitsharesAccountNameCacheRequest((GetBitsharesAccountNameCacheRequest) request); + } else { - //TODO not implemented - System.out.println("Error request not implemented " + request.getClass().getName()); + //TODO not implemented + System.out.println("Error request not implemented " + request.getClass().getName()); + } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java index ed2222c..8beb806 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -12,7 +12,9 @@ import org.bitcoinj.crypto.HDKeyDerivation; import org.bitcoinj.script.Script; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.HashMap; import java.util.List; import cy.agorise.crystalwallet.apigenerator.ApiRequest; @@ -23,7 +25,9 @@ import cy.agorise.crystalwallet.apigenerator.insightapi.GetTransactionData; 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.dao.CrystalDatabase; import cy.agorise.crystalwallet.enums.CryptoCoin; +import cy.agorise.crystalwallet.models.BitcoinTransaction; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.GTxIO; import cy.agorise.crystalwallet.models.GeneralCoinAccount; @@ -36,7 +40,27 @@ import cy.agorise.graphenej.Util; public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInfoRequestsListener { - CryptoCoin cryptoCoin; + static HashMap generalAccountManagers = new HashMap(); + + private static CryptoCoin[] SUPPORTED_COINS = new CryptoCoin[]{ + CryptoCoin.BITCOIN, + CryptoCoin.BITCOIN_TEST, + CryptoCoin.DASH, + CryptoCoin.LITECOIN + } ; + + final CryptoCoin cryptoCoin; + final Context context; + + public static GeneralAccountManager getAccountManager(CryptoCoin coin){ + return generalAccountManagers.get(coin); + } + + public GeneralAccountManager(CryptoCoin cryptoCoin, Context context) { + this.cryptoCoin = cryptoCoin; + this.context = context; + generalAccountManagers.put(cryptoCoin,this); + } @Override public void createAccountFromSeed(CryptoNetAccount account, ManagerRequest request, Context context) { @@ -55,6 +79,14 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf @Override public void onNewRequest(CryptoNetInfoRequest request) { + if(Arrays.asList(SUPPORTED_COINS).contains(request.getCoin())){ + if(request instanceof GeneralAccountSendRequest){ + this.send((GeneralAccountSendRequest)request); + }else{ + System.out.println("Invalid " +this.cryptoCoin.getLabel() + " request "); + } + + } } @@ -63,29 +95,35 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf * @param txi */ public void processTxi(Txi txi){ + CrystalDatabase db = CrystalDatabase.getAppDatabase(this.context); + BitcoinTransaction btTransaction = db.bitcoinTransactionDao().getByTxid(txi.txid); + if(btTransaction != null){ + btTransaction.setConfirmations(txi.confirmations); + db.bitcoinTransactionDao().insertBitcoinTransaction(btTransaction); + }else { - 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,cryptoCoin.getPrecision()))); - transaction.setConfirm(txi.confirmations); - transaction.setType(cryptoCoin); - transaction.setBlockHeight(txi.blockheight); - for (Vin vin : txi.vin) { - GTxIO input = new GTxIO(); - input.setAmount((long) (vin.value * Math.pow(10,cryptoCoin.getPrecision()))); - input.setTransaction(transaction); - input.setOut(true); - input.setType(cryptoCoin); - String addr = vin.addr; - input.setAddressString(addr); - input.setIndex(vin.n); - input.setScriptHex(vin.scriptSig.hex); - input.setOriginalTxid(vin.txid); + 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, cryptoCoin.getPrecision()))); + transaction.setConfirm(txi.confirmations); + transaction.setType(cryptoCoin); + transaction.setBlockHeight(txi.blockheight); + + for (Vin vin : txi.vin) { + GTxIO input = new GTxIO(); + input.setAmount((long) (vin.value * Math.pow(10, cryptoCoin.getPrecision()))); + input.setTransaction(transaction); + input.setOut(true); + input.setType(cryptoCoin); + 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); @@ -96,31 +134,31 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf } } }*/ - transaction.getTxInputs().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); + 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)); } - transaction.setMemo(new String(memoBytes)); - } - }else { - GTxIO output = new GTxIO(); - output.setAmount((long) (vout.value * Math.pow(10, cryptoCoin.getPrecision()))); - output.setTransaction(transaction); - output.setOut(false); - output.setType(cryptoCoin); - String addr = vout.scriptPubKey.addresses[0]; - output.setAddressString(addr); - output.setIndex(vout.n); - output.setScriptHex(vout.scriptPubKey.hex); + } else { + GTxIO output = new GTxIO(); + output.setAmount((long) (vout.value * Math.pow(10, cryptoCoin.getPrecision()))); + output.setTransaction(transaction); + output.setOut(false); + output.setType(cryptoCoin); + 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); @@ -133,13 +171,13 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf } }*/ - transaction.getTxOutputs().add(output); + transaction.getTxOutputs().add(output); + } } - } - if(txi.txlock && txi.confirmations< cryptoCoin.getCryptoNet().getConfirmationsNeeded()){ - transaction.setConfirm(cryptoCoin.getCryptoNet().getConfirmationsNeeded()); - } - //TODO database + if (txi.txlock && txi.confirmations < cryptoCoin.getCryptoNet().getConfirmationsNeeded()) { + transaction.setConfirm(cryptoCoin.getCryptoNet().getConfirmationsNeeded()); + } + //TODO database /*SCWallDatabase db = new SCWallDatabase(this.mContext); long idTransaction = db.getGeneralTransactionId(transaction); if (idTransaction == -1) { @@ -158,13 +196,14 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf break; } }*/ + } } public void send(final GeneralAccountSendRequest request){ //TODO check server connection //TODO validate to address - InsightApiGenerator.getEstimateFee(request.getAccount().getCryptoNet(),new ApiRequest(1, new ApiRequestListener() { + InsightApiGenerator.getEstimateFee(request.getAccount().getCryptoCoin(),new ApiRequest(1, new ApiRequestListener() { @Override public void success(Object answer, int idPetition) { Transaction tx = new Transaction(request.getAccount().getNetworkParam()); @@ -233,7 +272,7 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf tx.addSignedInput(outPoint, script, utxo.getAddress().getKey(), Transaction.SigHash.ALL, true); } - InsightApiGenerator.broadcastTransaction(request.getAccount().getCryptoNet(),Util.bytesToHex(tx.bitcoinSerialize()),new ApiRequest(1, new ApiRequestListener() { + InsightApiGenerator.broadcastTransaction(request.getAccount().getCryptoCoin(),Util.bytesToHex(tx.bitcoinSerialize()),new ApiRequest(1, new ApiRequestListener() { @Override public void success(Object answer, int idPetition) { request.setStatus(GeneralAccountSendRequest.StatusCode.SUCCEEDED); diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CryptoNetInfoRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CryptoNetInfoRequest.java index f45a5b1..23f88f8 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CryptoNetInfoRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CryptoNetInfoRequest.java @@ -21,13 +21,6 @@ public abstract class CryptoNetInfoRequest { */ protected CryptoNetInfoRequestListener listener; - - - - - - - protected CryptoNetInfoRequest(CryptoCoin coin){ this.coin = coin; } @@ -43,4 +36,7 @@ public abstract class CryptoNetInfoRequest { CryptoNetInfoRequests.getInstance().removeRequest(this); } + public CryptoCoin getCoin() { + return coin; + } }