Added the import rrequest with only the mnemonic words of the seed

This commit is contained in:
hvarona 2018-10-25 00:22:13 -04:00
parent c216dfa209
commit ac95b66511
2 changed files with 74 additions and 26 deletions

View file

@ -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) {
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.setName(userAccount.getName());
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) {
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.setName(userAccount.getName());
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
*/

View file

@ -28,21 +28,11 @@ public class ImportBitsharesAccountRequest extends CryptoNetInfoRequest {
PETITION_FAILED
}
/**
* The name of the account
*/
private String accountName;
/**
* The mnemonic words
*/
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
*/
@ -57,16 +47,13 @@ public class ImportBitsharesAccountRequest extends CryptoNetInfoRequest {
public ImportBitsharesAccountRequest(String mnemonic, Context context){
super(CryptoCoin.BITSHARES);
this.accountName = "";
this.mnemonic = mnemonic;
this.context = context;
}
public ImportBitsharesAccountRequest(String mnemonic, Context context, boolean addAccountIfValid){
super(CryptoCoin.BITSHARES);
this.accountName = "";
this.mnemonic = mnemonic;
this.addAccountIfValid = addAccountIfValid;
this.context = context;
}
@ -76,10 +63,6 @@ public class ImportBitsharesAccountRequest extends CryptoNetInfoRequest {
}
}
public String getAccountName() {
return accountName;
}
public String getMnemonic() {
return mnemonic;
}
@ -92,14 +75,6 @@ public class ImportBitsharesAccountRequest extends CryptoNetInfoRequest {
return context;
}
public boolean addAccountIfValid(){
return this.addAccountIfValid;
}
public void setAccountName(String accountName){
this.accountName = accountName;
}
public void setSeedType(SeedType seedType) {
this.seedType = seedType;
}