From ac95b6651109ac89d7cfafbd725cb5af98e05016 Mon Sep 17 00:00:00 2001 From: hvarona Date: Thu, 25 Oct 2018 00:22:13 -0400 Subject: [PATCH] Added the import rrequest with only the mnemonic words of the seed --- .../manager/BitsharesAccountManager.java | 75 ++++++++++++++++++- .../ImportBitsharesAccountRequest.java | 25 ------- 2 files changed, 74 insertions(+), 26 deletions(-) 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..8d47661 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) { + 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 */ diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java index cf83d4b..14113b2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBitsharesAccountRequest.java @@ -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; }