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 7ac8255..56603fe 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/ImportSeedActivity.java @@ -28,7 +28,7 @@ import cy.agorise.crystalwallet.dialogs.material.PositiveResponse; import cy.agorise.crystalwallet.dialogs.material.QuestionDialog; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequestListener; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequests; -import cy.agorise.crystalwallet.requestmanagers.ValidateImportBitsharesAccountRequest; +import cy.agorise.crystalwallet.requestmanagers.ImportBitsharesAccountRequest; import cy.agorise.crystalwallet.viewmodels.AccountSeedViewModel; import cy.agorise.crystalwallet.viewmodels.validators.ImportSeedValidator; import cy.agorise.crystalwallet.viewmodels.validators.UIValidatorListener; @@ -210,6 +210,7 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator clearErrors(); } }); + /* etAccountName.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -224,14 +225,14 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator @Override public void afterTextChanged(Editable s) { - /* - * Validate that PINs are equals - * */ + // + // Validate that PINs are equals + // validatePINS(); - /* - * If all is ready to continue enable the button, contrarie case disable it - * */ + // + // If all is ready to continue enable the button, contrarie case disable it + // if(allFieldsAreOK()){ enableCreate(); } @@ -240,6 +241,7 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator } } }); + */ accountSeedViewModel = ViewModelProviders.of(this).get(AccountSeedViewModel.class); importSeedValidator = new ImportSeedValidator(this.getApplicationContext(),etPin,etPinConfirmation,etAccountName,etSeedWords); @@ -285,8 +287,8 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator boolean complete = false; if( etPin.getText().toString().trim().compareTo("")!=0 && etPinConfirmation.getText().toString().trim().compareTo("")!=0 && - etSeedWords.getText().toString().trim().compareTo("")!=0 && - etAccountName.getText().toString().trim().compareTo("")!=0){ + etSeedWords.getText().toString().trim().compareTo("")!=0 /*&& + etAccountName.getText().toString().trim().compareTo("")!=0*/){ if(pinsOK){ complete = true; } @@ -311,11 +313,11 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator void afterSeedWordsChanged(Editable editable) { this.importSeedValidator.validate(); } - @OnTextChanged(value = R.id.etAccountName, + /*@OnTextChanged(value = R.id.etAccountName, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) void afterAccountNameChanged(Editable editable) { this.importSeedValidator.validate(); - } + }*/ @OnClick(R.id.btnCancel) public void cancel(){ @@ -356,11 +358,11 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator /* * Validate mnemonic with the server * */ - /*final ValidateImportBitsharesAccountRequest request = new ValidateImportBitsharesAccountRequest(etAccountName.getText().toString().trim(),etSeedWords.getText().toString().trim(),activity); + final ImportBitsharesAccountRequest request = new ImportBitsharesAccountRequest(etSeedWords.getText().toString().trim(),activity); request.setListener(new CryptoNetInfoRequestListener() { @Override public void onCarryOut() { - if(request.getStatus().equals(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)){ + if(request.getStatus().equals(ImportBitsharesAccountRequest.StatusCode.SUCCEEDED)){ //Correct @@ -376,7 +378,7 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator } } }); - CryptoNetInfoRequests.getInstance().addRequest(request);*/ + CryptoNetInfoRequests.getInstance().addRequest(request); } }); @@ -391,8 +393,8 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator final ImportSeedActivity thisActivity = this; - final ValidateImportBitsharesAccountRequest validatorRequest = - new ValidateImportBitsharesAccountRequest(etAccountName.getText().toString(), etSeedWords.getText().toString(), getApplicationContext(), true); + final ImportBitsharesAccountRequest validatorRequest = + new ImportBitsharesAccountRequest(etSeedWords.getText().toString(), getApplicationContext(), true); validatorRequest.setListener(new CryptoNetInfoRequestListener() { @Override @@ -403,7 +405,7 @@ public class ImportSeedActivity extends AppCompatActivity implements UIValidator * */ crystalLoading.dismiss(); - if (!validatorRequest.getStatus().equals(ValidateImportBitsharesAccountRequest.StatusCode.SUCCEEDED)) { + if (!validatorRequest.getStatus().equals(ImportBitsharesAccountRequest.StatusCode.SUCCEEDED)) { switch (validatorRequest.getStatus()){ case PETITION_FAILED: 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 ec5edb2..4baf5d9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -28,6 +28,7 @@ import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequestsListener; import cy.agorise.crystalwallet.requestmanagers.GetBitsharesAccountNameCacheRequest; +import cy.agorise.crystalwallet.requestmanagers.ImportBitsharesAccountRequest; import cy.agorise.crystalwallet.requestmanagers.ValidateBitsharesLTMUpgradeRequest; import cy.agorise.crystalwallet.requestmanagers.ValidateBitsharesSendRequest; import cy.agorise.crystalwallet.requestmanagers.ValidateCreateBitsharesAccountRequest; @@ -222,7 +223,9 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI @Override public void onNewRequest(CryptoNetInfoRequest request) { if(request.getCoin().equals(CryptoCoin.BITSHARES)) { - if (request instanceof ValidateImportBitsharesAccountRequest) { + if (request instanceof ImportBitsharesAccountRequest) { + this.importAccount((ImportBitsharesAccountRequest) request); + } else if (request instanceof ValidateImportBitsharesAccountRequest) { this.validateImportAccount((ValidateImportBitsharesAccountRequest) request); } else if (request instanceof ValidateExistBitsharesAccountRequest) { this.validateExistAcccount((ValidateExistBitsharesAccountRequest) request); @@ -244,6 +247,76 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } } + private void importAccount(final ImportBitsharesAccountRequest importRequest){ + final CrystalDatabase db = CrystalDatabase.getAppDatabase(importRequest.getContext()); + final AccountSeedDao accountSeedDao = db.accountSeedDao(); + ApiRequest getAccountNamesBK = new ApiRequest(0, new ApiRequestListener() { + @Override + public void success(Object answer, int idPetition) { + if(answer != null && importRequest.getStatus().equals(ImportBitsharesAccountRequest.StatusCode.NOT_STARTED)) { + UserAccount userAccount = (UserAccount) answer; + importRequest.setSeedType(SeedType.BRAINKEY); + importRequest.setStatus(ImportBitsharesAccountRequest.StatusCode.SUCCEEDED); + + AccountSeed seed = new AccountSeed(); + seed.setName(userAccount.getName()); + 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.setAccountId(userAccount.getObjectId()); + importAccountFromSeed(account, importRequest.getContext()); + } + } + } + + @Override + public void fail(int idPetition) { + //importRequest.setStatus(ImportBitsharesAccountRequest.StatusCode.PETITION_FAILED); + } + }); + + ApiRequest getAccountNamesBP39 = new ApiRequest(0, new ApiRequestListener() { + @Override + public void success(Object answer, int idPetition) { + if(answer != null && importRequest.getStatus().equals(ImportBitsharesAccountRequest.StatusCode.NOT_STARTED)) { + UserAccount userAccount = (UserAccount) answer; + importRequest.setSeedType(SeedType.BIP39); + importRequest.setStatus(ImportBitsharesAccountRequest.StatusCode.SUCCEEDED); + + AccountSeed seed = new AccountSeed(); + seed.setName(userAccount.getName()); + 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.setAccountId(userAccount.getObjectId()); + importAccountFromSeed(account, importRequest.getContext()); + } + } + } + + @Override + public void fail(int idPetition) { + //importRequest.setStatus(ImportBitsharesAccountRequest.StatusCode.PETITION_FAILED); + } + }); + + BrainKey bk = new BrainKey(importRequest.getMnemonic(), 0); + BIP39 bip39 = new BIP39(-1, importRequest.getMnemonic()); + GrapheneApiGenerator.getAccountByOwnerOrActiveAddress(bk.getPublicAddress("BTS"),getAccountNamesBK); + GrapheneApiGenerator.getAccountByOwnerOrActiveAddress(new Address(ECKey.fromPublicOnly(bip39.getBitsharesActiveKey(0).getPubKey())),getAccountNamesBP39); + + } + /** * Process the import account request */ diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java new file mode 100644 index 0000000..14113b2 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java @@ -0,0 +1,90 @@ +package cy.agorise.crystalwallet.requestmanagers; + +import android.content.Context; + +import cy.agorise.crystalwallet.enums.CryptoCoin; +import cy.agorise.crystalwallet.enums.SeedType; + +/** + * Imports a bitshares accounts, + * + * return true if the account exist, and the mnemonic (brainkey provide is for that account + * Created by Henry Varona on 10/24/2018. + */ + +public class ImportBitsharesAccountRequest 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 mnemonic words + */ + private final String mnemonic; + + /** + * If this seed is BIP39 or Brainkey + */ + private SeedType seedType; + + /** + * The status of this request + */ + private StatusCode status = StatusCode.NOT_STARTED; + + private Context context; + + public ImportBitsharesAccountRequest(String mnemonic, Context context){ + super(CryptoCoin.BITSHARES); + this.mnemonic = mnemonic; + this.context = context; + } + + public ImportBitsharesAccountRequest(String mnemonic, Context context, boolean addAccountIfValid){ + super(CryptoCoin.BITSHARES); + this.mnemonic = mnemonic; + this.context = context; + } + + public void validate(){ + if (!(this.status.equals(StatusCode.NOT_STARTED))){ + this._fireOnCarryOutEvent(); + } + } + + public String getMnemonic() { + return mnemonic; + } + + public SeedType getSeedType() { + return seedType; + } + + public Context getContext() { + return context; + } + + 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/res/layout/import_seed.xml b/app/src/main/res/layout/import_seed.xml index 59e5f6e..5b29d02 100644 --- a/app/src/main/res/layout/import_seed.xml +++ b/app/src/main/res/layout/import_seed.xml @@ -84,8 +84,9 @@ android:layout_width="330dp" android:layout_height="wrap_content" android:layout_below="@+id/etSeedWordsLayout" + android:layout_centerHorizontal="true" android:layout_marginTop="8dp" - android:layout_centerHorizontal="true"> + android:visibility="gone"> + android:singleLine="true" + android:visibility="gone" />