diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/AccountSeedDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/AccountSeedDao.java index 16790d1..c808d90 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/AccountSeedDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/AccountSeedDao.java @@ -22,6 +22,9 @@ public interface AccountSeedDao { @Query("SELECT * FROM account_seed") LiveData> getAll(); + @Query("SELECT * FROM account_seed") + List getAllNoLiveData(); + @Query("SELECT * FROM account_seed WHERE id = :id") LiveData findByIdLiveData(long id); diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java index 67ff6d3..1345509 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java @@ -1,8 +1,6 @@ package cy.agorise.crystalwallet.manager; -import android.app.Activity; import android.os.Environment; -import android.util.Log; import java.io.BufferedOutputStream; import java.io.File; @@ -10,10 +8,11 @@ import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; -import cy.agorise.crystalwallet.application.CrystalApplication; import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.CryptoNet; +import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.requestmanagers.CreateBackupRequest; -import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; import cy.agorise.crystalwallet.requestmanagers.FileServiceRequest; import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestsListener; import cy.agorise.graphenej.BrainKey; @@ -40,56 +39,69 @@ public class FileBackupManager implements FileServiceRequestsListener { public void createBackupBinFile(CreateBackupRequest request) { CrystalDatabase db = CrystalDatabase.getAppDatabase(request.getContext()); - db.cryptoNetAccountDao().getAllCryptoNetAccountBySeed(request.getSeed().getId()); + List seedNames = new ArrayList<>(); + List seeds = db.accountSeedDao().getAllNoLiveData(); + for(AccountSeed seed : seeds) { + List accounts = db.cryptoNetAccountDao().getAllCryptoNetAccountBySeed(seed.getId()); + for(CryptoNetAccount account : accounts){ + if(account.getCryptoNet().equals(CryptoNet.BITSHARES)){ + seedNames.add(new BitsharesSeedName(account.getName(),seed.getMasterSeed())); + } + } + } - /*TinyDB tinyDB = new TinyDB(myActivity); - ArrayList accountDetails = tinyDB.getListObject(myActivity.getResources().getString(R.string.pref_wallet_accounts), AccountDetails.class); - String _brnKey = getBrainKey(accountDetails); - String _accountName = getAccountName(accountDetails); - String _pinCode = getPin(accountDetails); - - getBinBytesFromBrainkey(_pinCode, _brnKey, _accountName);*/ + getBinBytesFromBrainkey(seedNames,request); //TODO make funcion for non-bitshares accounts } - public void getBinBytesFromBrainkey(String pin, String brnKey, String accountName, CreateBackupRequest request) { - BrainKey brainKey = new BrainKey(brnKey, 0); + public void getBinBytesFromBrainkey(List bitsharesSeedNames, CreateBackupRequest request) { + try { ArrayList wallets = new ArrayList<>(); ArrayList accounts = new ArrayList<>(); ArrayList keys = new ArrayList<>(); - - Wallet wallet = new Wallet(accountName, brainKey.getBrainKey(), brainKey.getSequenceNumber(), Chains.BITSHARES.CHAIN_ID, pin); - wallets.add(wallet); - - PrivateKeyBackup keyBackup = new PrivateKeyBackup(brainKey.getPrivateKey().getPrivKeyBytes(), - brainKey.getSequenceNumber(), brainKey.getSequenceNumber(), wallet.getEncryptionKey(pin)); - keys.add(keyBackup); - - LinkedAccount linkedAccount = new LinkedAccount(accountName, Chains.BITSHARES.CHAIN_ID); - accounts.add(linkedAccount); + String fileName = null; //TODO choice a good name, now we use the first bitshares account as the bin backup + for(BitsharesSeedName bitsharesSeedName : bitsharesSeedNames) { + if(fileName == null){ + fileName = bitsharesSeedName.accountName; + } + BrainKey brainKey = new BrainKey(bitsharesSeedName.accountSeed, 0); //TODO chain to use BIP39 + //TODO adapt CHAIN ID + Wallet wallet = new Wallet(bitsharesSeedName.accountName, brainKey.getBrainKey(), brainKey.getSequenceNumber(), Chains.BITSHARES.CHAIN_ID, request.getPassword()); + wallets.add(wallet); + PrivateKeyBackup keyBackup = new PrivateKeyBackup(brainKey.getPrivateKey().getPrivKeyBytes(), + brainKey.getSequenceNumber(), brainKey.getSequenceNumber(), wallet.getEncryptionKey(request.getPassword())); + keys.add(keyBackup); + LinkedAccount linkedAccount = new LinkedAccount(bitsharesSeedName.accountName, Chains.BITSHARES.CHAIN_ID); + accounts.add(linkedAccount); + } WalletBackup backup = new WalletBackup(wallets, keys, accounts); - byte[] results = FileBin.serializeWalletBackup(backup, pin); + byte[] results = FileBin.serializeWalletBackup(backup, request.getPassword()); List resultFile = new ArrayList<>(); for(byte result: results){ resultFile.add(result & 0xff); } - saveBinContentToFile(resultFile, accountName, request); + saveBinContentToFile(resultFile, fileName, request); } catch (Exception e) { - + request.setStatus(CreateBackupRequest.StatusCode.FAILED); //TODO error exception } } - static void saveBinContentToFile(List content, String _accountName, CreateBackupRequest request ) + static void saveBinContentToFile(List content, String fileName, CreateBackupRequest request ) { String folder = Environment.getExternalStorageDirectory() + File.separator + "Crystal"; //TODO make constant - String path = folder + File.separator + _accountName + ".bin"; + String path = folder + File.separator + fileName + ".bin"; boolean success = saveBinFile(path,content,request); - //TODO handle sucess + if(success) { + request.setStatus(CreateBackupRequest.StatusCode.OK); + }else{ + request.setStatus(CreateBackupRequest.StatusCode.FAILED); + } + } private static boolean saveBinFile (String filePath , List content, CreateBackupRequest request) @@ -124,4 +136,15 @@ public class FileBackupManager implements FileServiceRequestsListener { return success; } + + public class BitsharesSeedName{ + String accountName; + String accountSeed; + + public BitsharesSeedName(String accountName, String accountSeed) { + this.accountName = accountName; + this.accountSeed = accountSeed; + } + } + } diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java index 28942b4..8194745 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java @@ -11,22 +11,34 @@ import cy.agorise.crystalwallet.models.AccountSeed; public class CreateBackupRequest extends FileServiceRequest { - private AccountSeed seed; - - enum StatusCode{ + public enum StatusCode{ + PROCESSING(0), + OK(1), + FAILED(2); + protected long code; + StatusCode(long code) { + this.code = code; + } + public long getCode() { + return code; + } } - public CreateBackupRequest(Context context, Activity activity, AccountSeed seed) { - super(context, activity); - this.seed = seed; + private StatusCode status = StatusCode.PROCESSING; + + public CreateBackupRequest(Context context, String password) { + super(context,password); } - public AccountSeed getSeed() { - return seed; + public void setStatus(StatusCode status) { + this.status = status; + this._fireOnCarryOutEvent(); } - + public StatusCode getStatus() { + return status; + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java index b6c0af3..33332b8 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java @@ -10,11 +10,11 @@ import android.content.Context; public abstract class FileServiceRequest { protected Context context; - protected Activity activity; + protected String password; - protected FileServiceRequest(Context context, Activity activity) { + protected FileServiceRequest(Context context, String password) { this.context = context; - this.activity = activity; + this.password = password; } protected FileServiceRequestListener listener; @@ -27,8 +27,8 @@ public abstract class FileServiceRequest { return context; } - public Activity getActivity() { - return activity; + public String getPassword() { + return password; } protected void _fireOnCarryOutEvent(){ diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java index d63841f..2f824bd 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java @@ -9,7 +9,7 @@ import android.content.Context; public class ImportBackupRequest extends FileServiceRequest { - public ImportBackupRequest(Context context, Activity activity) { - super(context, activity); + public ImportBackupRequest(Context context, String password) { + super(context, password); } }