- 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.
This commit is contained in:
Javier Varona 2018-09-11 21:40:25 -04:00
parent fc3fe7c92c
commit 7befdd4207
4 changed files with 59 additions and 7 deletions

View file

@ -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

View file

@ -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);

View file

@ -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);
}
}
});

View file

@ -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;
}