This commit is contained in:
Javier Varona 2018-04-25 22:44:08 -04:00
commit df60afb203
6 changed files with 62 additions and 53 deletions

View file

@ -589,7 +589,14 @@ public abstract class GrapheneApiGenerator {
} }
} }
public static void getEquivalenValue(String baseAssetName, final List<BitsharesAsset> 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<BitsharesAsset> quoteAssets, final Context context){
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao(); final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao(); final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();

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

@ -4,7 +4,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin;
import cy.agorise.crystalwallet.enums.SeedType; 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 * return true if the account exist, and the mnemonic (brainkey provide is for that account
* Created by Henry Varona on 1/10/2017. * Created by Henry Varona on 1/10/2017.
@ -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

@ -3,6 +3,7 @@ package cy.agorise.crystalwallet.service;
import android.arch.lifecycle.LifecycleService; import android.arch.lifecycle.LifecycleService;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator;
@ -29,8 +30,15 @@ public class EquivalencesThread extends Thread{
super.run(); super.run();
while(this.keepLoadingEquivalences){ while(this.keepLoadingEquivalences){
List<BitsharesAsset> queryAsset = new ArrayList<>();
for(BitsharesAsset asset : bitsharesAssets){
if(!asset.getName().equals(fromAsset)){
queryAsset.add(asset);
}
}
try { 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"); Log.i("Equivalences Thread", "In loop");
Thread.sleep(300000); Thread.sleep(300000);
} catch (InterruptedException e) { } catch (InterruptedException e) {

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