From 7befdd42074888018524f1f23aa4a6227432c6e0 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Tue, 11 Sep 2018 21:40:25 -0400 Subject: [PATCH] - FileBackupManager and ImportSeedActivity no longer add the seed to the database. Now they call the ValidateImportBitsharesAccountRequest to do so. - Also, the ImportSeedActivity tells the user the reason in case of a failed import. --- .../activities/ImportSeedActivity.java | 2 +- .../manager/BitsharesAccountManager.java | 29 +++++++++++++++++-- .../manager/FileBackupManager.java | 7 +++-- ...ValidateImportBitsharesAccountRequest.java | 28 +++++++++++++++++- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java index db7c445..21844fe 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java @@ -114,7 +114,7 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator if (this.importSeedValidator.isValid()) { final ValidateImportBitsharesAccountRequest validatorRequest = - new ValidateImportBitsharesAccountRequest(etAccountName.getText().toString(), etSeedWords.getText().toString()); + new ValidateImportBitsharesAccountRequest(etAccountName.getText().toString(), etSeedWords.getText().toString(), getApplicationContext(), true); validatorRequest.setListener(new CryptoNetInfoRequestListener() { @Override 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 5514263..c0d5194 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -19,6 +19,7 @@ import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator; 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.models.BitsharesAccountNameCache; import cy.agorise.crystalwallet.models.seed.BIP39; import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest; @@ -246,6 +247,9 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI */ private void validateImportAccount(final ValidateImportBitsharesAccountRequest importRequest){ //TODO check internet and server status + final CrystalDatabase db = CrystalDatabase.getAppDatabase(importRequest.getContext()); + final AccountSeedDao accountSeedDao = db.accountSeedDao(); + ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() { @Override public void success(Object answer, int idPetition) { @@ -259,7 +263,8 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI if((new Address(activeKey.getKey(),"BTS")).toString().equals(bk.getPublicAddress("BTS").toString())){ importRequest.setSeedType(SeedType.BRAINKEY); importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED); - return; + + break; } } BIP39 bip39 = new BIP39(-1, importRequest.getMnemonic()); @@ -267,9 +272,29 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI if((new Address(activeKey.getKey(),"BTS")).toString().equals(new Address(ECKey.fromPublicOnly(bip39.getBitsharesActiveKey(0).getPubKey())).toString())){ importRequest.setSeedType(SeedType.BIP39); importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED); - return; + break; } } + + if ((importRequest.getStatus() == ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)){ + if (importRequest.addAccountIfValid()) { + AccountSeed seed = new AccountSeed(); + seed.setName(importRequest.getAccountName()); + seed.setType(importRequest.getSeedType()); + seed.setMasterSeed(importRequest.getMnemonic()); + long idSeed = accountSeedDao.insertAccountSeed(seed); + if (idSeed >= 0) { + GrapheneAccount account = new GrapheneAccount(); + account.setCryptoNet(CryptoNet.BITSHARES); + account.setAccountIndex(0); + account.setSeedId(idSeed); + account.setName(importRequest.getAccountName()); + importAccountFromSeed(account, importRequest.getContext()); + } + } + return; + } + importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.BAD_SEED); } importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.PETITION_FAILED); 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 5417dd9..b0944dd 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java @@ -234,14 +234,14 @@ public class FileBackupManager implements FileServiceRequestsListener { final AccountSeedDao accountSeedDao = db.accountSeedDao(); for(BitsharesSeedName seedName : seedNames) { final ValidateImportBitsharesAccountRequest validatorRequest = - new ValidateImportBitsharesAccountRequest(seedName.accountName, seedName.accountSeed); + new ValidateImportBitsharesAccountRequest(seedName.accountName, seedName.accountSeed, request.getContext(), true); validatorRequest.setListener(new CryptoNetInfoRequestListener() { @Override public void onCarryOut() { if (!validatorRequest.getStatus().equals(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)) { request.setStatus(ImportBackupRequest.StatusCode.FAILED); // TODO reason bad seed } else { - AccountSeed seed = new AccountSeed(); + /*AccountSeed seed = new AccountSeed(); seed.setName(validatorRequest.getAccountName()); seed.setType(validatorRequest.getSeedType()); seed.setMasterSeed(validatorRequest.getMnemonic()); @@ -257,7 +257,8 @@ public class FileBackupManager implements FileServiceRequestsListener { request.setStatus(ImportBackupRequest.StatusCode.SUCCEEDED); }else{ request.setStatus(ImportBackupRequest.StatusCode.FAILED); //TODO reason couldn't insert seed - } + }*/ + request.setStatus(ImportBackupRequest.StatusCode.SUCCEEDED); } } }); 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 81c3aa4..244bf3b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ValidateImportBitsharesAccountRequest.java @@ -1,5 +1,7 @@ package cy.agorise.crystalwallet.requestmanagers; +import android.content.Context; + import cy.agorise.crystalwallet.enums.CryptoCoin; import cy.agorise.crystalwallet.enums.SeedType; @@ -36,6 +38,11 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest */ private final String mnemonic; + /** + * True - the account must be added if the accountName and mnemonic are correct + */ + private boolean addAccountIfValid = false; + /** * If this seed is BIP39 or Brainkey */ @@ -46,10 +53,21 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest */ private StatusCode status = StatusCode.NOT_STARTED; - public ValidateImportBitsharesAccountRequest(String accountName, String mnemonic){ + private Context context; + + public ValidateImportBitsharesAccountRequest(String accountName, String mnemonic, Context context){ super(CryptoCoin.BITSHARES); this.accountName = accountName; this.mnemonic = mnemonic; + this.context = context; + } + + public ValidateImportBitsharesAccountRequest(String accountName, String mnemonic, Context context, boolean addAccountIfValid){ + super(CryptoCoin.BITSHARES); + this.accountName = accountName; + this.mnemonic = mnemonic; + this.addAccountIfValid = addAccountIfValid; + this.context = context; } public void validate(){ @@ -70,6 +88,14 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest return seedType; } + public Context getContext() { + return context; + } + + public boolean addAccountIfValid(){ + return this.addAccountIfValid; + } + public void setSeedType(SeedType seedType) { this.seedType = seedType; }