From 119f4ea12b2fdd738febcd74c1b334e13378bcf0 Mon Sep 17 00:00:00 2001 From: henry Date: Tue, 17 Oct 2017 22:55:42 -0400 Subject: [PATCH] - Import Account - Create Account (miss seed) - Created bitshares faucet api generator - Created GrapheneAccount constructor and info loader - Added funcion to daos --- .../BitsharesFaucetApiGenerator.java | 16 +++- .../apigenerator/GrapheneApiGenerator.java | 28 ++++-- .../dao/CryptoNetAccountDao.java | 3 + .../dao/GrapheneAccountInfoDao.java | 2 +- .../manager/BitsharesAccountManager.java | 87 +++++++++++++------ .../models/CryptoCoinBalance.java | 16 ++-- .../models/CryptoNetAccount.java | 10 +++ .../crystalwallet/models/GrapheneAccount.java | 39 +++++++++ .../models/GrapheneAccountInfo.java | 16 +++- 9 files changed, 172 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/BitsharesFaucetApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/BitsharesFaucetApiGenerator.java index 1620297..2a54f3a 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/BitsharesFaucetApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/BitsharesFaucetApiGenerator.java @@ -4,5 +4,19 @@ package cy.agorise.crystalwallet.apigenerator; * Created by henry on 15/10/2017. */ -public class BitsharesFaucetApiGenerator { +public abstract class BitsharesFaucetApiGenerator { + + /** + * Class to register a new Bitshares Account + * + * @param accountName The name of the Account to be register + * @param ownerKey The owner key public address + * @param activeKey The active key public address + * @param memoKey the memo key public address + * @return The bitshares id of the registered account, or null + */ + public static String registerBitsharesAccount(String accountName, String ownerKey, String activeKey, String memoKey){ + //TODO faucet function + return null; + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index e955041..a3e967d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -1,5 +1,6 @@ package cy.agorise.crystalwallet.apigenerator; +import android.app.Application; import android.arch.lifecycle.LiveData; import android.content.Context; @@ -7,7 +8,9 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.manager.BitsharesAccountManager; import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoNetBalance; @@ -295,8 +298,10 @@ public abstract class GrapheneApiGenerator { thread.start(); } - public static void subscribeBitsharesAccount(long accountId, final String accountBitsharesId, Context context){ - final LiveData> balances = CrystalDatabase.getAppDatabase(context).cryptoCoinBalanceDao().getBalancesFromAccount(accountId); + public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId, final Context context){ + CrystalDatabase db = CrystalDatabase.getAppDatabase(context); + final LiveData> balances = db.cryptoCoinBalanceDao().getBalancesFromAccount(accountId); + final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao(); SubscriptionListener balanceListener = new SubscriptionListener() { @Override public ObjectType getInterestObjectType() { @@ -313,12 +318,19 @@ public abstract class GrapheneApiGenerator { if(balanceUpdate.owner.equals(accountBitsharesId)){ boolean find = false; for(CryptoCoinBalance balance : balances.getValue()){ + + } + if(!find){ + CryptoCoinBalance balance = new CryptoCoinBalance(); + + balanceDao.insertCryptoCoinBalance(balance); } //TODO balance function - //TODO refresh transactions + BitsharesAccountManager.refreshAccountTransactions(accountId,context); } } } + } } }; @@ -337,8 +349,8 @@ public abstract class GrapheneApiGenerator { } public static void getAccountBalance(final long accountId, final String accountGrapheneId, final Context context){ - - + CrystalDatabase db = CrystalDatabase.getAppDatabase(context); + final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao(); WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), null, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { @@ -347,9 +359,11 @@ public abstract class GrapheneApiGenerator { CryptoCoinBalance ccBalance = new CryptoCoinBalance(); ccBalance.setAccountId(accountId); ccBalance.setBalance(balance.getAmount().longValue()); + //TODO find asset + //ccBalance.setCryptoCurrency(); - //TODO cryptocyrrency - CrystalDatabase.getAppDatabase(context).cryptoCoinBalanceDao().insertCryptoCoinBalance(ccBalance); + + balanceDao.insertCryptoCoinBalance(ccBalance); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java index 5a2e0f6..1e2b18e 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CryptoNetAccountDao.java @@ -23,6 +23,9 @@ public interface CryptoNetAccountDao { @Query("SELECT * FROM crypto_net_account") List getAll(); + @Query("SELECT * FROM crypto_net_account WHERE id = :accountId") + LiveData getById( long accountId); + @Query("SELECT * FROM crypto_net_account WHERE crypto_net = 'BITSHARES'") LiveData> getBitsharesAccounts(); diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/GrapheneAccountInfoDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/GrapheneAccountInfoDao.java index 3923722..ef15d29 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/GrapheneAccountInfoDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/GrapheneAccountInfoDao.java @@ -23,7 +23,7 @@ public interface GrapheneAccountInfoDao { LiveData> getAll(); @Query("SELECT * FROM graphene_account WHERE crypto_net_account_id = :cryptoNetAccountId") - LiveData getGrapheneAccountInfo(int cryptoNetAccountId); + LiveData getGrapheneAccountInfo(long cryptoNetAccountId); @Insert(onConflict = OnConflictStrategy.REPLACE) public long[] insertGrapheneAccountInfo(GrapheneAccountInfo... accounts); 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 c8f5dea..c12c6c1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -13,6 +13,7 @@ import java.util.Objects; import cy.agorise.crystalwallet.apigenerator.ApiRequest; import cy.agorise.crystalwallet.apigenerator.ApiRequestListener; +import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequest; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestsListener; @@ -20,12 +21,14 @@ import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendReque import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.CryptoNet; import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.GrapheneAccount; +import cy.agorise.crystalwallet.models.GrapheneAccountInfo; import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.AssetAmount; @@ -36,6 +39,7 @@ import cy.agorise.graphenej.Transaction; import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.models.AccountProperties; import cy.agorise.graphenej.models.HistoricalTransfer; +import cy.agorise.graphenej.objects.Memo; import cy.agorise.graphenej.operations.TransferOperationBuilder; /** @@ -45,10 +49,24 @@ import cy.agorise.graphenej.operations.TransferOperationBuilder; public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetInfoRequestsListener { @Override public CryptoNetAccount createAccountFromSeed(CryptoNetAccount account, Context context) { - //TODO generate account keys - //TODO register account faucet api - //TODO save account on DB - //TODO subscribe account + if(account instanceof GrapheneAccount) { + + GrapheneAccount grapheneAccount = (GrapheneAccount) account; + + BitsharesFaucetApiGenerator.registerBitsharesAccount(grapheneAccount.getName(), + new Address(grapheneAccount.getOwnerKey(),"BTS").toString(), + new Address(grapheneAccount.getActiveKey(),"BTS").toString(), + new Address(grapheneAccount.getMemoKey(),"BTS").toString()); + + CrystalDatabase db = CrystalDatabase.getAppDatabase(context); + db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount); + db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount)); + + GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); + this.refreshAccountTransactions(account.getId(), context); + GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); + return grapheneAccount; + } return null; } @@ -63,10 +81,14 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI }else if(grapheneAccount.getName() == null){ grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); } - //TODO grapaheneAccount null, error fetching + if(grapheneAccount == null) { + //TODO grapaheneAccount null, error fetching + return null; + } CrystalDatabase db = CrystalDatabase.getAppDatabase(context); - //TODO save account on DB + db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount); + db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount)); GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); this.refreshAccountTransactions(account.getId(), context); @@ -80,12 +102,18 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI public void loadAccountFromDB(CryptoNetAccount account, Context context) { if(account instanceof GrapheneAccount){ GrapheneAccount grapheneAccount = (GrapheneAccount) account; + CrystalDatabase db = CrystalDatabase.getAppDatabase(context); + grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getGrapheneAccountInfo(account.getId()).getValue()); if(grapheneAccount.getAccountId() == null){ grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); }else if(grapheneAccount.getName() == null){ grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); } - //TODO grapaheneAccount null, error fetching + + if(grapheneAccount == null) { + //TODO grapaheneAccount null, error fetching + return; + } GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); this.refreshAccountTransactions(account.getId(),context); @@ -163,19 +191,21 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){ Asset feeAsset = new Asset(sendRequest.getFeeAsset()); + UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId()); + UserAccount toUserAccount = new UserAccount(sendRequest.getToAccount()); TransferOperationBuilder builder = new TransferOperationBuilder() - .setSource(new UserAccount(sendRequest.getSourceAccount().getAccountId())) - .setDestination(new UserAccount(sendRequest.getToAccount())) + .setSource(fromUserAccount) + .setDestination(toUserAccount) .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getBaseAmount()), new Asset(sendRequest.getBaseAsset()))) .setFee(new AssetAmount(UnsignedLong.valueOf(sendRequest.getFeeAmount()), feeAsset)); - //TODO memo + if(sendRequest.getMemo() != null) { + //builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes())); + //TODO memo + } ArrayList operationList = new ArrayList(); operationList.add(builder.build()); - //TODO get privateKey with seed model - LiveData seed = CrystalDatabase.getAppDatabase(sendRequest.getContext()).accountSeedDao().findById(sendRequest.getSourceAccount().getSeedId()); - - ECKey privateKey = new BrainKey(seed.getValue().getMasterSeed(),0).getPrivateKey(); + ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(); Transaction transaction = new Transaction(privateKey, null, operationList); ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { @@ -237,22 +267,29 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI return listener.account; } - public void refreshAccountTransactions(final long idAccount, Context context){ - final CrystalDatabase db = CrystalDatabase.getAppDatabase(context); - final LiveData> transactions = db.transactionDao().getByIdAccount(idAccount); - final LiveData account = null; - //TODO find account - int start = transactions.getValue().size(); - int limit = 50; - int stop = start + limit; + public static void refreshAccountTransactions(long idAccount, Context context){ + CrystalDatabase db = CrystalDatabase.getAppDatabase(context); + LiveData> transactions = db.transactionDao().getByIdAccount(idAccount); + LiveData account = db.cryptoNetAccountDao().getById(idAccount); + if(account.getValue().getCryptoNet() == CryptoNet.BITSHARES) { - ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start,stop,limit,account.getValue(),db)); + GrapheneAccount grapheneAccount = new GrapheneAccount(account.getValue()); + + grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getGrapheneAccountInfo(idAccount).getValue()); - GrapheneApiGenerator.getAccountTransaction(account.getValue().getName(),start,stop,limit,transactionRequest); + int start = transactions.getValue().size(); + int limit = 50; + int stop = start + limit; + + ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db)); + + + GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest); + } } - private class TransactionRequestListener implements ApiRequestListener{ + private static class TransactionRequestListener implements ApiRequestListener{ int start; int stop; diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java index a5e1844..0f0a5c6 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java @@ -34,8 +34,8 @@ public class CryptoCoinBalance { @ColumnInfo(name="account_id") private long mAccountId; - @ColumnInfo(name = "coin") - private CryptoCoin mCoin; + @ColumnInfo(name = "crypto_currency_id") + private long mCryptoCurrencyId; @ColumnInfo(name = "balance") private long mBalance; @@ -56,12 +56,12 @@ public class CryptoCoinBalance { this.mAccountId = accountId; } - public CryptoCoin getCoin() { - return mCoin; + public long getCryptoCurrency() { + return mCryptoCurrencyId; } - public void setCoin(CryptoCoin coin) { - this.mCoin = coin; + public void setCryptoCurrency(long cryptoCurrencyId) { + this.mCryptoCurrencyId = cryptoCurrencyId; } public long getBalance() { @@ -76,7 +76,7 @@ public class CryptoCoinBalance { @Override public boolean areItemsTheSame( @NonNull CryptoCoinBalance oldBalance, @NonNull CryptoCoinBalance newBalance) { - return oldBalance.getCoin() == newBalance.getCoin(); + return oldBalance.getCryptoCurrency() == newBalance.getCryptoCurrency(); } @Override public boolean areContentsTheSame( @@ -94,7 +94,7 @@ public class CryptoCoinBalance { if (mAccountId != that.mAccountId) return false; if (mBalance != that.mBalance) return false; - return mCoin == that.mCoin; + return mCryptoCurrencyId == that.mCryptoCurrencyId; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java index d78eb95..f72cb2f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java @@ -47,6 +47,16 @@ public class CryptoNetAccount { @ColumnInfo(name = "crypto_net") private CryptoNet mCryptoNet; + public CryptoNetAccount() { + } + + public CryptoNetAccount(long mId, long mSeedId, int mAccountIndex, CryptoNet mCryptoNet) { + this.mId = mId; + this.mSeedId = mSeedId; + this.mAccountIndex = mAccountIndex; + this.mCryptoNet = mCryptoNet; + } + public long getId() { return mId; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccount.java b/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccount.java index 85ad344..46d4d74 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccount.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccount.java @@ -1,6 +1,9 @@ package cy.agorise.crystalwallet.models; +import org.bitcoinj.core.ECKey; + /** + * * Created by henry on 24/9/2017. */ @@ -10,6 +13,18 @@ public class GrapheneAccount extends CryptoNetAccount { protected String name; protected String accountId; + public GrapheneAccount() { + } + + public GrapheneAccount(CryptoNetAccount account) { + super(account.getId(),account.getSeedId(),account.getAccountIndex(),account.getCryptoNet()); + } + + public void loadInfo(GrapheneAccountInfo info){ + this.name = info.getName(); + this.accountId = info.getAccountId(); + } + public String getName() { return name; } @@ -25,4 +40,28 @@ public class GrapheneAccount extends CryptoNetAccount { public void setAccountId(String accountId) { this.accountId = accountId; } + + /** + * Return the owner key, generates from the seed if it has not been generated. null if it can't be generated + */ + public ECKey getOwnerKey(){ + //TODO implement + return null; + } + + /** + * Return the active key, generates from the seed if it has not been generated. null if it can't be generated + */ + public ECKey getActiveKey(){ + //TODO implement + return null; + } + + /** + * Return the memo key, generates from the seed if it has not been generated. null if it can't be generated + */ + public ECKey getMemoKey(){ + //TODO implement + return null; + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccountInfo.java b/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccountInfo.java index 9178161..98e9fed 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccountInfo.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccountInfo.java @@ -17,7 +17,7 @@ import android.arch.persistence.room.Index; public class GrapheneAccountInfo { @ColumnInfo(name = "crypto_net_account_id") - protected String cryptoNetAccountId; + protected long cryptoNetAccountId; @ColumnInfo(name = "account_name") protected String name; @@ -25,11 +25,21 @@ public class GrapheneAccountInfo { @ColumnInfo(name = "account_id") protected String accountId; - public String getCryptoNetAccountId() { + public GrapheneAccountInfo(long cryptoNetAccountId) { + this.cryptoNetAccountId = cryptoNetAccountId; + } + + public GrapheneAccountInfo(GrapheneAccount account) { + this.cryptoNetAccountId = account.getId(); + this.name = account.getName(); + this.accountId = account.getAccountId(); + } + + public long getCryptoNetAccountId() { return cryptoNetAccountId; } - public void setCryptoNetAccountId(String cryptoNetAccountId) { + public void setCryptoNetAccountId(long cryptoNetAccountId) { this.cryptoNetAccountId = cryptoNetAccountId; }