Added status code to the file request. TODO create more status code

Change the FileBackup to bakcup multipls Bitshares accounts and to use the Status Code
This commit is contained in:
henry 2018-03-29 23:16:43 -04:00
parent 6c5fd9de37
commit ea2adab087
5 changed files with 84 additions and 46 deletions

View file

@ -22,6 +22,9 @@ public interface AccountSeedDao {
@Query("SELECT * FROM account_seed") @Query("SELECT * FROM account_seed")
LiveData<List<AccountSeed>> getAll(); LiveData<List<AccountSeed>> getAll();
@Query("SELECT * FROM account_seed")
List<AccountSeed> getAllNoLiveData();
@Query("SELECT * FROM account_seed WHERE id = :id") @Query("SELECT * FROM account_seed WHERE id = :id")
LiveData<AccountSeed> findByIdLiveData(long id); LiveData<AccountSeed> findByIdLiveData(long id);

View file

@ -1,8 +1,6 @@
package cy.agorise.crystalwallet.manager; package cy.agorise.crystalwallet.manager;
import android.app.Activity;
import android.os.Environment; import android.os.Environment;
import android.util.Log;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
@ -10,10 +8,11 @@ import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import cy.agorise.crystalwallet.application.CrystalApplication;
import cy.agorise.crystalwallet.dao.CrystalDatabase; 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.CreateBackupRequest;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest;
import cy.agorise.crystalwallet.requestmanagers.FileServiceRequest; import cy.agorise.crystalwallet.requestmanagers.FileServiceRequest;
import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestsListener; import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestsListener;
import cy.agorise.graphenej.BrainKey; import cy.agorise.graphenej.BrainKey;
@ -40,56 +39,69 @@ public class FileBackupManager implements FileServiceRequestsListener {
public void createBackupBinFile(CreateBackupRequest request) public void createBackupBinFile(CreateBackupRequest request)
{ {
CrystalDatabase db = CrystalDatabase.getAppDatabase(request.getContext()); CrystalDatabase db = CrystalDatabase.getAppDatabase(request.getContext());
db.cryptoNetAccountDao().getAllCryptoNetAccountBySeed(request.getSeed().getId()); List<BitsharesSeedName> seedNames = new ArrayList<>();
List<AccountSeed> seeds = db.accountSeedDao().getAllNoLiveData();
/*TinyDB tinyDB = new TinyDB(myActivity); for(AccountSeed seed : seeds) {
ArrayList<AccountDetails> accountDetails = tinyDB.getListObject(myActivity.getResources().getString(R.string.pref_wallet_accounts), AccountDetails.class); List<CryptoNetAccount> accounts = db.cryptoNetAccountDao().getAllCryptoNetAccountBySeed(seed.getId());
String _brnKey = getBrainKey(accountDetails); for(CryptoNetAccount account : accounts){
String _accountName = getAccountName(accountDetails); if(account.getCryptoNet().equals(CryptoNet.BITSHARES)){
String _pinCode = getPin(accountDetails); seedNames.add(new BitsharesSeedName(account.getName(),seed.getMasterSeed()));
}
getBinBytesFromBrainkey(_pinCode, _brnKey, _accountName);*/ }
} }
public void getBinBytesFromBrainkey(String pin, String brnKey, String accountName, CreateBackupRequest request) { getBinBytesFromBrainkey(seedNames,request); //TODO make funcion for non-bitshares accounts
BrainKey brainKey = new BrainKey(brnKey, 0); }
public void getBinBytesFromBrainkey(List<BitsharesSeedName> bitsharesSeedNames, CreateBackupRequest request) {
try { try {
ArrayList<Wallet> wallets = new ArrayList<>(); ArrayList<Wallet> wallets = new ArrayList<>();
ArrayList<LinkedAccount> accounts = new ArrayList<>(); ArrayList<LinkedAccount> accounts = new ArrayList<>();
ArrayList<PrivateKeyBackup> keys = new ArrayList<>(); ArrayList<PrivateKeyBackup> keys = new ArrayList<>();
String fileName = null; //TODO choice a good name, now we use the first bitshares account as the bin backup
Wallet wallet = new Wallet(accountName, brainKey.getBrainKey(), brainKey.getSequenceNumber(), Chains.BITSHARES.CHAIN_ID, pin); 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); wallets.add(wallet);
PrivateKeyBackup keyBackup = new PrivateKeyBackup(brainKey.getPrivateKey().getPrivKeyBytes(), PrivateKeyBackup keyBackup = new PrivateKeyBackup(brainKey.getPrivateKey().getPrivKeyBytes(),
brainKey.getSequenceNumber(), brainKey.getSequenceNumber(), wallet.getEncryptionKey(pin)); brainKey.getSequenceNumber(), brainKey.getSequenceNumber(), wallet.getEncryptionKey(request.getPassword()));
keys.add(keyBackup); keys.add(keyBackup);
LinkedAccount linkedAccount = new LinkedAccount(bitsharesSeedName.accountName, Chains.BITSHARES.CHAIN_ID);
LinkedAccount linkedAccount = new LinkedAccount(accountName, Chains.BITSHARES.CHAIN_ID);
accounts.add(linkedAccount); accounts.add(linkedAccount);
}
WalletBackup backup = new WalletBackup(wallets, keys, accounts); WalletBackup backup = new WalletBackup(wallets, keys, accounts);
byte[] results = FileBin.serializeWalletBackup(backup, pin); byte[] results = FileBin.serializeWalletBackup(backup, request.getPassword());
List<Integer> resultFile = new ArrayList<>(); List<Integer> resultFile = new ArrayList<>();
for(byte result: results){ for(byte result: results){
resultFile.add(result & 0xff); resultFile.add(result & 0xff);
} }
saveBinContentToFile(resultFile, accountName, request); saveBinContentToFile(resultFile, fileName, request);
} }
catch (Exception e) { catch (Exception e) {
request.setStatus(CreateBackupRequest.StatusCode.FAILED);
//TODO error exception //TODO error exception
} }
} }
static void saveBinContentToFile(List<Integer> content, String _accountName, CreateBackupRequest request ) static void saveBinContentToFile(List<Integer> content, String fileName, CreateBackupRequest request )
{ {
String folder = Environment.getExternalStorageDirectory() + File.separator + "Crystal"; //TODO make constant 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); 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<Integer> content, CreateBackupRequest request) private static boolean saveBinFile (String filePath , List<Integer> content, CreateBackupRequest request)
@ -124,4 +136,15 @@ public class FileBackupManager implements FileServiceRequestsListener {
return success; return success;
} }
public class BitsharesSeedName{
String accountName;
String accountSeed;
public BitsharesSeedName(String accountName, String accountSeed) {
this.accountName = accountName;
this.accountSeed = accountSeed;
}
}
} }

View file

@ -11,22 +11,34 @@ import cy.agorise.crystalwallet.models.AccountSeed;
public class CreateBackupRequest extends FileServiceRequest { public class CreateBackupRequest extends FileServiceRequest {
private AccountSeed seed; public enum StatusCode{
PROCESSING(0),
enum StatusCode{ OK(1),
FAILED(2);
protected long code;
StatusCode(long code) {
this.code = code;
} }
public CreateBackupRequest(Context context, Activity activity, AccountSeed seed) { public long getCode() {
super(context, activity); return code;
this.seed = seed; }
} }
public AccountSeed getSeed() { private StatusCode status = StatusCode.PROCESSING;
return seed;
public CreateBackupRequest(Context context, String password) {
super(context,password);
} }
public void setStatus(StatusCode status) {
this.status = status;
this._fireOnCarryOutEvent();
}
public StatusCode getStatus() {
return status;
}
} }

View file

@ -10,11 +10,11 @@ import android.content.Context;
public abstract class FileServiceRequest { public abstract class FileServiceRequest {
protected Context context; 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.context = context;
this.activity = activity; this.password = password;
} }
protected FileServiceRequestListener listener; protected FileServiceRequestListener listener;
@ -27,8 +27,8 @@ public abstract class FileServiceRequest {
return context; return context;
} }
public Activity getActivity() { public String getPassword() {
return activity; return password;
} }
protected void _fireOnCarryOutEvent(){ protected void _fireOnCarryOutEvent(){

View file

@ -9,7 +9,7 @@ import android.content.Context;
public class ImportBackupRequest extends FileServiceRequest { public class ImportBackupRequest extends FileServiceRequest {
public ImportBackupRequest(Context context, Activity activity) { public ImportBackupRequest(Context context, String password) {
super(context, activity); super(context, password);
} }
} }