From e3fb99f1eb6e07789270f3098be19dfe68e9fd9e Mon Sep 17 00:00:00 2001 From: henry Date: Mon, 23 Apr 2018 21:06:17 -0400 Subject: [PATCH 1/2] Change validate import to have the status code like other request --- .../manager/BitsharesAccountManager.java | 15 ++-- .../manager/FileBackupManager.java | 4 +- ...ValidateImportBitsharesAccountRequest.java | 68 +++++++++---------- ...tsharesAccountMnemonicValidationField.java | 7 +- 4 files changed, 44 insertions(+), 50 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 d095a0f..8173aca 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -237,43 +237,41 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI * Process the import account request */ private void validateImportAccount(final ValidateImportBitsharesAccountRequest importRequest){ + //TODO check internet and server status ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() { @Override public void success(Object answer, int idPetition) { - importRequest.setAccountExists(true); ApiRequest getAccountInfo = new ApiRequest(1,new ApiRequestListener(){ @Override public void success(Object answer, int idPetition) { if(answer != null && answer instanceof AccountProperties) { AccountProperties prop = (AccountProperties) answer; - //TODO change the way to compare keys BrainKey bk = new BrainKey(importRequest.getMnemonic(), 0); for(PublicKey activeKey : prop.owner.getKeyAuthList()){ if((new Address(activeKey.getKey(),"BTS")).toString().equals(bk.getPublicAddress("BTS").toString())){ - System.out.println("Mnemonic brainkey correct"); importRequest.setSeedType(SeedType.BRAINKEY); - importRequest.setMnemonicIsCorrect(true); + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED); return; } } BIP39 bip39 = new BIP39(-1, importRequest.getMnemonic()); for(PublicKey activeKey : prop.active.getKeyAuthList()){ if((new Address(activeKey.getKey(),"BTS")).toString().equals(new Address(ECKey.fromPublicOnly(bip39.getBitsharesActiveKey(0).getPubKey())).toString())){ - System.out.println("Mnemonic BIP39 correct"); importRequest.setSeedType(SeedType.BIP39); - importRequest.setMnemonicIsCorrect(true); + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED); return; } } - System.out.println("Mnemonic incorrect"); - importRequest.setMnemonicIsCorrect(false); + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.BAD_SEED); } + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.PETITION_FAILED); } @Override public void fail(int idPetition) { // + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.NO_ACCOUNT_DATA); } }); GrapheneApiGenerator.getAccountById((String)answer,getAccountInfo); @@ -282,6 +280,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI @Override public void fail(int idPetition) { // + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.ACCOUNT_DOESNT_EXIST); } }); diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java index a799081..5417dd9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java @@ -238,7 +238,7 @@ public class FileBackupManager implements FileServiceRequestsListener { validatorRequest.setListener(new CryptoNetInfoRequestListener() { @Override public void onCarryOut() { - if (!validatorRequest.getMnemonicIsCorrect()) { + if (!validatorRequest.getStatus().equals(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)) { request.setStatus(ImportBackupRequest.StatusCode.FAILED); // TODO reason bad seed } else { AccountSeed seed = new AccountSeed(); @@ -254,8 +254,6 @@ public class FileBackupManager implements FileServiceRequestsListener { account.setName(validatorRequest.getAccountName()); BitsharesAccountManager bManger = new BitsharesAccountManager(); bManger.importAccountFromSeed(account,request.getContext()); - - request.setStatus(ImportBackupRequest.StatusCode.SUCCEEDED); }else{ request.setStatus(ImportBackupRequest.StatusCode.FAILED); //TODO reason couldn't insert seed diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java index 7a8419d..19b449b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java @@ -12,53 +12,48 @@ import cy.agorise.crystalwallet.enums.SeedType; public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest { + /** + * The status code of this request + */ + public enum StatusCode{ + NOT_STARTED, + SUCCEEDED, + NO_INTERNET, + NO_SERVER_CONNECTION, + ACCOUNT_DOESNT_EXIST, + BAD_SEED, + NO_ACCOUNT_DATA, + PETITION_FAILED + } + /** * The name of the account */ - private String accountName; + private final String accountName; /** * The mnemonic words */ - private String mnemonic; + private final String mnemonic; /** - * Indicates if the account exist + * If this seed is BIP39 or Brainkey */ - private Boolean accountExists; - /** - * Indicates if the mnemonic provided belongs to that account - */ - private Boolean mnemonicIsCorrect; - private SeedType seedType; + /** + * The status of this request + */ + private StatusCode status = StatusCode.NOT_STARTED; + public ValidateImportBitsharesAccountRequest(String accountName, String mnemonic){ super(CryptoCoin.BITSHARES); this.accountName = accountName; this.mnemonic = mnemonic; } - public void setAccountExists(boolean value){ - this.accountExists = value; - this.validate(); - } - - public void setMnemonicIsCorrect(boolean value){ - this.mnemonicIsCorrect = value; - this.validate(); - } - - public boolean getAccountExists(){ - return this.accountExists; - } - - public boolean getMnemonicIsCorrect(){ - return this.mnemonicIsCorrect; - } - public void validate(){ - if ((this.accountExists != null) && (this.mnemonicIsCorrect != null)){ + if (!(this.status.equals(StatusCode.NOT_STARTED))){ this._fireOnCarryOutEvent(); } } @@ -67,18 +62,10 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest return accountName; } - public void setAccountName(String accountName) { - this.accountName = accountName; - } - public String getMnemonic() { return mnemonic; } - public void setMnemonic(String mnemonic) { - this.mnemonic = mnemonic; - } - public SeedType getSeedType() { return seedType; } @@ -86,4 +73,13 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest public void setSeedType(SeedType seedType) { this.seedType = seedType; } + + public void setStatus(StatusCode status) { + this.status = status; + this._fireOnCarryOutEvent(); + } + + public StatusCode getStatus() { + return status; + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountMnemonicValidationField.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountMnemonicValidationField.java index f4ad77c..cbbbe72 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountMnemonicValidationField.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/validators/validationfields/BitsharesAccountMnemonicValidationField.java @@ -35,11 +35,12 @@ public class BitsharesAccountMnemonicValidationField extends ValidationField { request.setListener(new CryptoNetInfoRequestListener() { @Override public void onCarryOut() { - if (!request.getMnemonicIsCorrect()){ + if(request.getStatus().equals(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)){ + setValidForValue(mixedValue, true); + }else{ + //TODO handle error request setMessageForValue(mixedValue,validator.getContext().getResources().getString(R.string.error_invalid_account)); setValidForValue(mixedValue, false); - } else { - setValidForValue(mixedValue, true); } } }); From e5bbce9fcdbc26cdff1afa0032c076c285783978 Mon Sep 17 00:00:00 2001 From: henry Date: Mon, 23 Apr 2018 21:13:48 -0400 Subject: [PATCH 2/2] Remove the search for the equivalence of the same asset --- .../apigenerator/GrapheneApiGenerator.java | 9 ++++++++- .../ValidateImportBitsharesAccountRequest.java | 2 +- .../crystalwallet/service/EquivalencesThread.java | 10 +++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) 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 6edebed..3312030 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -589,7 +589,14 @@ public abstract class GrapheneApiGenerator { } } - public static void getEquivalenValue(String baseAssetName, final List quoteAssets, final Context context){ + /** + * Retrieves the equivalent value from a list of assets to a base asset + * + * @param baseAssetName The base asset to use + * @param quoteAssets The list of quotes assets to query + * @param context The Context of this Application + */ + public static void getEquivalentValue(String baseAssetName, final List quoteAssets, final Context context){ CrystalDatabase db = CrystalDatabase.getAppDatabase(context); final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao(); final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao(); diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java index 19b449b..81c3aa4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java @@ -4,7 +4,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin; import cy.agorise.crystalwallet.enums.SeedType; /** - * Imports a bitsahres accounts, + * Imports a bitshares accounts, * * return true if the account exist, and the mnemonic (brainkey provide is for that account * Created by Henry Varona on 1/10/2017. diff --git a/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java b/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java index 263a0cf..12a446c 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/EquivalencesThread.java @@ -3,6 +3,7 @@ package cy.agorise.crystalwallet.service; import android.arch.lifecycle.LifecycleService; import android.util.Log; +import java.util.ArrayList; import java.util.List; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; @@ -29,8 +30,15 @@ public class EquivalencesThread extends Thread{ super.run(); while(this.keepLoadingEquivalences){ + List queryAsset = new ArrayList<>(); + for(BitsharesAsset asset : bitsharesAssets){ + if(!asset.getName().equals(fromAsset)){ + queryAsset.add(asset); + } + } try { - GrapheneApiGenerator.getEquivalenValue(fromAsset, bitsharesAssets, this.service); + //GrapheneApiGenerator.getEquivalentValue(fromAsset, bitsharesAssets, this.service); + GrapheneApiGenerator.getEquivalentValue(fromAsset, queryAsset, this.service); Log.i("Equivalences Thread", "In loop"); Thread.sleep(300000); } catch (InterruptedException e) {