From 3d4c90f8eb40925925755ef697e5733cc3e33a4c Mon Sep 17 00:00:00 2001 From: henry Date: Sun, 12 Nov 2017 23:04:06 -0400 Subject: [PATCH] Added the get asset by name Update equivalent value url. If we are going to use the testnet the equivalent value has to be retrieved from the main net. Update docs and code --- .../manager/BitsharesAccountManager.java | 46 +++++++------------ .../crystalwallet/models/GrapheneAccount.java | 43 +++++++++++++---- .../models/GrapheneAccountInfo.java | 19 ++++++++ 3 files changed, 71 insertions(+), 37 deletions(-) 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 6470540..193c01b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -1,18 +1,15 @@ package cy.agorise.crystalwallet.manager; -import android.arch.lifecycle.LiveData; import android.content.Context; import com.google.common.primitives.UnsignedLong; -import org.bitcoinj.core.Block; import org.bitcoinj.core.ECKey; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.TimeZone; import cy.agorise.crystalwallet.apigenerator.ApiRequest; @@ -28,11 +25,9 @@ import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAcc import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.TransactionDao; import cy.agorise.crystalwallet.enums.CryptoNet; -import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAssetInfo; 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; @@ -47,7 +42,6 @@ import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.models.AccountProperties; import cy.agorise.graphenej.models.BlockHeader; import cy.agorise.graphenej.models.HistoricalTransfer; -import cy.agorise.graphenej.objects.Memo; import cy.agorise.graphenej.operations.TransferOperationBuilder; /** @@ -62,11 +56,10 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI if(account instanceof GrapheneAccount) { GrapheneAccount grapheneAccount = (GrapheneAccount) account; - String btsIdAccount = BitsharesFaucetApiGenerator.registerBitsharesAccount(grapheneAccount.getName(), - new Address(grapheneAccount.getOwnerKey(),"BTS").toString(), - new Address(grapheneAccount.getActiveKey(),"BTS").toString(), - new Address(grapheneAccount.getMemoKey(),"BTS").toString(),GrapheneApiGenerator.url); + new Address(grapheneAccount.getOwnerKey(context),"BTS").toString(), + new Address(grapheneAccount.getActiveKey(context),"BTS").toString(), + new Address(grapheneAccount.getMemoKey(context),"BTS").toString(),GrapheneApiGenerator.url); if(btsIdAccount !=null) { grapheneAccount.setAccountId(btsIdAccount); @@ -76,7 +69,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI db.grapheneAccountInfoDao().insertGrapheneAccountInfo(new GrapheneAccountInfo(grapheneAccount)); GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); - this.refreshAccountTransactions(grapheneAccount.getId(), context); + BitsharesAccountManager.refreshAccountTransactions(grapheneAccount.getId(), context); GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); return grapheneAccount; } @@ -91,7 +84,6 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI GrapheneAccount grapheneAccount = (GrapheneAccount) account; if(grapheneAccount.getAccountId() == null){ - System.out.println("Importing without accountid"); GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName()); if(fetch == null) { //TODO grapaheneAccount null, error fetching @@ -99,7 +91,6 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } grapheneAccount.setAccountId(fetch.getAccountId()); }else if(grapheneAccount.getName() == null){ - System.out.println("Importing without accountname"); GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId()); if(fetch == null) { //TODO grapaheneAccount null, error fetching @@ -108,14 +99,12 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI grapheneAccount.setName(fetch.getName()); } - System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId()); - 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); + BitsharesAccountManager.refreshAccountTransactions(account.getId(), context); GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(), grapheneAccount.getAccountId(), context); return grapheneAccount; } @@ -124,14 +113,12 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI @Override public void loadAccountFromDB(CryptoNetAccount account, Context context) { - System.out.println("On loadAccount from db"); if(account instanceof GrapheneAccount){ GrapheneAccount grapheneAccount = (GrapheneAccount) account; CrystalDatabase db = CrystalDatabase.getAppDatabase(context); GrapheneAccountInfo info = db.grapheneAccountInfoDao().getByAccountId(account.getId()); grapheneAccount.loadInfo(info); if(grapheneAccount.getAccountId() == null){ - System.out.println("On loadAccount null id"); GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName()); if(fetch != null){ info.setAccountId(fetch.getAccountId()); @@ -139,7 +126,6 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info); } }else if(grapheneAccount.getName() == null){ - System.out.println("On loadAccount null name"); GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId()); if(fetch != null) { info.setName(fetch.getName()); @@ -148,15 +134,13 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } } - if(grapheneAccount == null) { + if(grapheneAccount.getName() == null || grapheneAccount.getAccountId() == null) { //TODO grapaheneAccount null, error fetching return; } - System.out.println("On loadAccount no nulls " + grapheneAccount.getName() + " " + grapheneAccount.getAccountId()); - GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); - this.refreshAccountTransactions(account.getId(),context); + BitsharesAccountManager.refreshAccountTransactions(account.getId(),context); GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); } } @@ -264,7 +248,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI ArrayList operationList = new ArrayList(); operationList.add(builder.build()); - ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(); + ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext()); Transaction transaction = new Transaction(privateKey, null, operationList); ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { @@ -301,13 +285,17 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI synchronized (SYNC){ try { SYNC.wait(100); - } catch (InterruptedException e) {} + } catch (InterruptedException ignore) {} } } return listener.account; } + /** + * Gets account info by its name + * @param grapheneName The name of the account to retrieve + */ private GrapheneAccount getAccountInfoByName(String grapheneName){ final Object SYNC = new Object(); long timeout = 60000; @@ -323,7 +311,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI synchronized (SYNC){ try { SYNC.wait(100); - } catch (InterruptedException e) {} + } catch (InterruptedException ignore) {} } } @@ -437,7 +425,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI GrapheneApiGenerator.getAssetById(assets,assetRequest); synchronized (SYNC){ - try {SYNC.wait(3000);} catch (InterruptedException e) {} + try {SYNC.wait(3000);} catch (InterruptedException ignore) {} } info = db.bitsharesAssetDao().getBitsharesAssetInfoById(transfer.getOperation().getAssetAmount().getAsset().getObjectId()); } @@ -472,7 +460,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI /** * Gets the current change from two assets */ - private static void getEquivalentValue(final CryptoNetEquivalentRequest request){ + private void getEquivalentValue(final CryptoNetEquivalentRequest request){ if(request.getFromCurrency() instanceof BitsharesAsset && request.getToCurrency() instanceof BitsharesAsset) { BitsharesAsset fromAsset = (BitsharesAsset) request.getFromCurrency(); BitsharesAsset toAsset = (BitsharesAsset) request.getToCurrency(); @@ -502,7 +490,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI GrapheneAccount account; - public AccountIdOrNameListener(Object SYNC) { + AccountIdOrNameListener(Object SYNC) { this.SYNC = SYNC; } 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 46d4d74..16dcef9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccount.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccount.java @@ -1,7 +1,13 @@ package cy.agorise.crystalwallet.models; +import android.content.Context; + import org.bitcoinj.core.ECKey; +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.SeedType; +import cy.agorise.graphenej.BrainKey; + /** * * Created by henry on 24/9/2017. @@ -44,24 +50,45 @@ public class GrapheneAccount extends CryptoNetAccount { /** * 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 + public ECKey getOwnerKey(Context context){ + AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId()).getValue(); + if(seed == null) return null; + if(seed.getType().equals(SeedType.BRAINKEY)){ + return new BrainKey(seed.getMasterSeed(),0).getPrivateKey(); + }else{ + //TODO implement slip48 + 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; + public ECKey getActiveKey(Context context){ + AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId()).getValue(); + if(seed == null) + return null; + if(seed.getType().equals(SeedType.BRAINKEY)){ + return new BrainKey(seed.getMasterSeed(),0).getPrivateKey(); + }else{ + //TODO implement slip48 + 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; + public ECKey getMemoKey(Context context){ + AccountSeed seed = CrystalDatabase.getAppDatabase(context).accountSeedDao().findById(this.getSeedId()).getValue(); + if(seed == null) + return null; + if(seed.getType().equals(SeedType.BRAINKEY)){ + return new BrainKey(seed.getMasterSeed(),0).getPrivateKey(); + }else{ + //TODO implement slip48 + 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 98e9fed..382242b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccountInfo.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GrapheneAccountInfo.java @@ -6,6 +6,8 @@ import android.arch.persistence.room.ForeignKey; import android.arch.persistence.room.Index; /** + * This represents the additional info of a bitshares account + * * Created by henry on 24/9/2017. */ @@ -16,19 +18,36 @@ import android.arch.persistence.room.Index; childColumns = "crypto_net_account_id")) public class GrapheneAccountInfo { + /** + * The database id of the cryptonetAccount + */ @ColumnInfo(name = "crypto_net_account_id") protected long cryptoNetAccountId; + /** + * The account name + */ @ColumnInfo(name = "account_name") protected String name; + /** + * The bitshares id of this account + */ @ColumnInfo(name = "account_id") protected String accountId; + /** + * Baisc constructor + * @param cryptoNetAccountId The database ud of the CryptoNetAccount + */ public GrapheneAccountInfo(long cryptoNetAccountId) { this.cryptoNetAccountId = cryptoNetAccountId; } + /** + * Constructor used to save in the database + * @param account a complete graphene account with its info + */ public GrapheneAccountInfo(GrapheneAccount account) { this.cryptoNetAccountId = account.getId(); this.name = account.getName();