Change validate import to have the status code like other request

This commit is contained in:
henry 2018-04-23 21:06:17 -04:00
parent 032cc6fae5
commit e3fb99f1eb
4 changed files with 44 additions and 50 deletions

View file

@ -237,43 +237,41 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
* Process the import account request * Process the import account request
*/ */
private void validateImportAccount(final ValidateImportBitsharesAccountRequest importRequest){ private void validateImportAccount(final ValidateImportBitsharesAccountRequest importRequest){
//TODO check internet and server status
ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() { ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() {
@Override @Override
public void success(Object answer, int idPetition) { public void success(Object answer, int idPetition) {
importRequest.setAccountExists(true);
ApiRequest getAccountInfo = new ApiRequest(1,new ApiRequestListener(){ ApiRequest getAccountInfo = new ApiRequest(1,new ApiRequestListener(){
@Override @Override
public void success(Object answer, int idPetition) { public void success(Object answer, int idPetition) {
if(answer != null && answer instanceof AccountProperties) { if(answer != null && answer instanceof AccountProperties) {
AccountProperties prop = (AccountProperties) answer; AccountProperties prop = (AccountProperties) answer;
//TODO change the way to compare keys
BrainKey bk = new BrainKey(importRequest.getMnemonic(), 0); BrainKey bk = new BrainKey(importRequest.getMnemonic(), 0);
for(PublicKey activeKey : prop.owner.getKeyAuthList()){ for(PublicKey activeKey : prop.owner.getKeyAuthList()){
if((new Address(activeKey.getKey(),"BTS")).toString().equals(bk.getPublicAddress("BTS").toString())){ if((new Address(activeKey.getKey(),"BTS")).toString().equals(bk.getPublicAddress("BTS").toString())){
System.out.println("Mnemonic brainkey correct");
importRequest.setSeedType(SeedType.BRAINKEY); importRequest.setSeedType(SeedType.BRAINKEY);
importRequest.setMnemonicIsCorrect(true); importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED);
return; return;
} }
} }
BIP39 bip39 = new BIP39(-1, importRequest.getMnemonic()); BIP39 bip39 = new BIP39(-1, importRequest.getMnemonic());
for(PublicKey activeKey : prop.active.getKeyAuthList()){ for(PublicKey activeKey : prop.active.getKeyAuthList()){
if((new Address(activeKey.getKey(),"BTS")).toString().equals(new Address(ECKey.fromPublicOnly(bip39.getBitsharesActiveKey(0).getPubKey())).toString())){ 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.setSeedType(SeedType.BIP39);
importRequest.setMnemonicIsCorrect(true); importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED);
return; return;
} }
} }
System.out.println("Mnemonic incorrect"); importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.BAD_SEED);
importRequest.setMnemonicIsCorrect(false);
} }
importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.PETITION_FAILED);
} }
@Override @Override
public void fail(int idPetition) { public void fail(int idPetition) {
// //
importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.NO_ACCOUNT_DATA);
} }
}); });
GrapheneApiGenerator.getAccountById((String)answer,getAccountInfo); GrapheneApiGenerator.getAccountById((String)answer,getAccountInfo);
@ -282,6 +280,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
@Override @Override
public void fail(int idPetition) { public void fail(int idPetition) {
// //
importRequest.setStatus(ValidateImportBitsharesAccountRequest.StatusCode.ACCOUNT_DOESNT_EXIST);
} }
}); });

View file

@ -238,7 +238,7 @@ public class FileBackupManager implements FileServiceRequestsListener {
validatorRequest.setListener(new CryptoNetInfoRequestListener() { validatorRequest.setListener(new CryptoNetInfoRequestListener() {
@Override @Override
public void onCarryOut() { public void onCarryOut() {
if (!validatorRequest.getMnemonicIsCorrect()) { if (!validatorRequest.getStatus().equals(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)) {
request.setStatus(ImportBackupRequest.StatusCode.FAILED); // TODO reason bad seed request.setStatus(ImportBackupRequest.StatusCode.FAILED); // TODO reason bad seed
} else { } else {
AccountSeed seed = new AccountSeed(); AccountSeed seed = new AccountSeed();
@ -254,8 +254,6 @@ public class FileBackupManager implements FileServiceRequestsListener {
account.setName(validatorRequest.getAccountName()); account.setName(validatorRequest.getAccountName());
BitsharesAccountManager bManger = new BitsharesAccountManager(); BitsharesAccountManager bManger = new BitsharesAccountManager();
bManger.importAccountFromSeed(account,request.getContext()); bManger.importAccountFromSeed(account,request.getContext());
request.setStatus(ImportBackupRequest.StatusCode.SUCCEEDED); request.setStatus(ImportBackupRequest.StatusCode.SUCCEEDED);
}else{ }else{
request.setStatus(ImportBackupRequest.StatusCode.FAILED); //TODO reason couldn't insert seed request.setStatus(ImportBackupRequest.StatusCode.FAILED); //TODO reason couldn't insert seed

View file

@ -12,53 +12,48 @@ import cy.agorise.crystalwallet.enums.SeedType;
public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest { 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 * The name of the account
*/ */
private String accountName; private final String accountName;
/** /**
* The mnemonic words * 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; private SeedType seedType;
/**
* The status of this request
*/
private StatusCode status = StatusCode.NOT_STARTED;
public ValidateImportBitsharesAccountRequest(String accountName, String mnemonic){ public ValidateImportBitsharesAccountRequest(String accountName, String mnemonic){
super(CryptoCoin.BITSHARES); super(CryptoCoin.BITSHARES);
this.accountName = accountName; this.accountName = accountName;
this.mnemonic = mnemonic; 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(){ public void validate(){
if ((this.accountExists != null) && (this.mnemonicIsCorrect != null)){ if (!(this.status.equals(StatusCode.NOT_STARTED))){
this._fireOnCarryOutEvent(); this._fireOnCarryOutEvent();
} }
} }
@ -67,18 +62,10 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest
return accountName; return accountName;
} }
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getMnemonic() { public String getMnemonic() {
return mnemonic; return mnemonic;
} }
public void setMnemonic(String mnemonic) {
this.mnemonic = mnemonic;
}
public SeedType getSeedType() { public SeedType getSeedType() {
return seedType; return seedType;
} }
@ -86,4 +73,13 @@ public class ValidateImportBitsharesAccountRequest extends CryptoNetInfoRequest
public void setSeedType(SeedType seedType) { public void setSeedType(SeedType seedType) {
this.seedType = seedType; this.seedType = seedType;
} }
public void setStatus(StatusCode status) {
this.status = status;
this._fireOnCarryOutEvent();
}
public StatusCode getStatus() {
return status;
}
} }

View file

@ -35,11 +35,12 @@ public class BitsharesAccountMnemonicValidationField extends ValidationField {
request.setListener(new CryptoNetInfoRequestListener() { request.setListener(new CryptoNetInfoRequestListener() {
@Override @Override
public void onCarryOut() { 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)); setMessageForValue(mixedValue,validator.getContext().getResources().getString(R.string.error_invalid_account));
setValidForValue(mixedValue, false); setValidForValue(mixedValue, false);
} else {
setValidForValue(mixedValue, true);
} }
} }
}); });