Merge pull request #3407 from gabrielbazan7/feat/exportMetaData2

Incluiding address book and transaction history cache in export file
This commit is contained in:
Gustavo Maximiliano Cortez 2015-11-11 19:12:32 -03:00
commit 173bf4b13f
4 changed files with 169 additions and 54 deletions

View File

@ -45,6 +45,14 @@
</form> </form>
<h4></h4> <h4></h4>
<ul class="no-bullet m0">
<li>
<switch id="no-metaData" name="metaData" ng-model="metaData" class="green right"></switch>
<span translate>Include address book and history cache</span>
</li>
</ul>
<ul class="no-bullet m0" ng-show="index.canSign"> <ul class="no-bullet m0" ng-show="index.canSign">
<li> <li>
<switch id="no-sign" name="noSign" ng-model="noSign" class="green right"></switch> <switch id="no-sign" name="noSign" ng-model="noSign" class="green right"></switch>

View File

@ -1,62 +1,127 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('backupController', angular.module('copayApp.controllers').controller('backupController',
function($rootScope, $scope, $timeout, backupService, profileService, isMobile, notification, go, gettext, gettextCatalog) { function($rootScope, $scope, $timeout, $log, backupService, storageService, profileService, isMobile, notification, go, gettext, gettextCatalog) {
this.error = null; var self = this;
this.success = null;
self.error = null;
self.success = null;
$scope.metaData = true;
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
this.isEncrypted = fc.isPrivKeyEncrypted(); self.isEncrypted = fc.isPrivKeyEncrypted();
this.downloadWalletBackup = function() { self.downloadWalletBackup = function() {
var self = this; self.getMetaData($scope.metaData, function(err, txsFromLocal, localAddressBook) {
var opts = {
noSign: $scope.noSign,
};
backupService.walletDownload(this.password, opts, function(err) {
if (err) { if (err) {
self.error = true; self.error = true;
return ; return;
} }
$rootScope.$emit('Local/BackupDone'); var opts = {
notification.success(gettext('Success'), gettext('Encrypted export file saved')); noSign: $scope.noSign,
go.walletHome(); historyCache: txsFromLocal,
addressBook: localAddressBook
};
backupService.walletDownload(self.password, opts, function(err) {
if (err) {
self.error = true;
return;
}
notification.success(gettext('Success'), gettext('Encrypted export file saved'));
go.walletHome();
});
}); });
}; };
this.getBackup = function() { self.getMetaData = function(metaData, cb) {
var opts = { if (metaData == false) return cb();
noSign: $scope.noSign, self.getHistoryCache(function(err, txsFromLocal) {
}; if (err) return cb(err);
var ew = backupService.walletExport(this.password, opts); self.getAddressbook(function(err, localAddressBook) {
if (!ew) { if (err) return cb(err);
this.error = true;
} else {
this.error = false;
}
return ew;
};
this.viewWalletBackup = function() { return cb(null, txsFromLocal, localAddressBook)
});
});
}
self.getHistoryCache = function(cb) {
storageService.getTxHistory(fc.credentials.walletId, function(err, txs) {
if (err) return cb(err);
var localTxs = [];
try {
localTxs = JSON.parse(txs);
} catch (ex) {
$log.warn(ex);
}
if (!localTxs[0]) return cb(null, null);
return cb(null, localTxs);
});
}
self.getAddressbook = function(cb) {
storageService.getAddressbook(fc.credentials.network, function(err, addressBook) {
if (err) return cb(err);
var localAddressBook = [];
try {
localAddressBook = JSON.parse(addressBook);
} catch (ex) {
$log.warn(ex);
}
return cb(null, localAddressBook);
});
}
self.getBackup = function(cb) {
self.getMetaData(function(err, txsFromLocal, localAddressBook) {
if (err) {
self.error = true;
return cb(null);
}
var opts = {
noSign: $scope.noSign,
historyCache: txsFromLocal,
addressBook: localAddressBook
};
var ew = backupService.walletExport(self.password, opts);
if (!ew) {
self.error = true;
} else {
self.error = false;
}
return cb(ew);
});
}
self.viewWalletBackup = function() {
var self = this; var self = this;
$timeout(function() { $timeout(function() {
var ew = self.getBackup(); self.getBackup(function(backup) {
if (!ew) return; var ew = backup;
self.backupWalletPlainText = ew; if (!ew) return;
$rootScope.$emit('Local/BackupDone'); self.backupWalletPlainText = ew;
});
}, 100); }, 100);
}; };
this.copyWalletBackup = function() { self.copyWalletBackup = function() {
var ew = this.getBackup(); self.getBackup(function(backup) {
if (!ew) return; var ew = backup;
window.cordova.plugins.clipboard.copy(ew); if (!ew) return;
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard')); window.cordova.plugins.clipboard.copy(ew);
$rootScope.$emit('Local/BackupDone'); window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
});
}; };
this.sendWalletBackup = function() { self.sendWalletBackup = function() {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
if (isMobile.Android() || isMobile.Windows()) { if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true; window.ignoreMobilePause = true;
@ -66,19 +131,20 @@ angular.module('copayApp.controllers').controller('backupController',
if (fc.alias) { if (fc.alias) {
name = fc.alias + ' [' + name + ']'; name = fc.alias + ' [' + name + ']';
} }
var ew = this.getBackup(); self.getBackup(function(backup) {
if (!ew) return; var ew = backup;
if (!ew) return;
if( $scope.noSign) if ($scope.noSign)
name = name + '(No Private Key)'; name = name + '(No Private Key)';
var properties = { var properties = {
subject: 'Copay Wallet Backup: ' + name, subject: 'Copay Wallet Backup: ' + name,
body: 'Here is the encrypted backup of the wallet ' + name + ': \n\n' + ew + '\n\n To import this backup, copy all text between {...}, including the symbols {}', body: 'Here is the encrypted backup of the wallet ' + name + ': \n\n' + ew + '\n\n To import this backup, copy all text between {...}, including the symbols {}',
isHtml: false isHtml: false
}; };
$rootScope.$emit('Local/BackupDone'); window.plugin.email.open(properties);
window.plugin.email.open(properties); });
}; };
}); });

View File

@ -53,6 +53,14 @@ angular.module('copayApp.services')
return cb(); return cb();
}; };
root.addMetadata = function(b, opts) {
b = JSON.parse(b);
if (opts.historyCache) b.historyCache = opts.historyCache;
if (opts.addressBook) b.addressBook = opts.addressBook;
return JSON.stringify(b);
}
root.walletExport = function(password, opts) { root.walletExport = function(password, opts) {
if (!password) { if (!password) {
return null; return null;
@ -61,6 +69,8 @@ angular.module('copayApp.services')
try { try {
opts = opts || {}; opts = opts || {};
var b = fc.export(opts); var b = fc.export(opts);
if (opts.historyCache || opts.addressBook) b = root.addMetadata(b, opts);
var e = sjcl.encrypt(password, b, { var e = sjcl.encrypt(password, b, {
iter: 10000 iter: 10000
}); });
@ -82,4 +92,4 @@ angular.module('copayApp.services')
_download(ew, filename, cb) _download(ew, filename, cb)
}; };
return root; return root;
}); });

View File

@ -11,7 +11,7 @@ angular.module('copayApp.services')
root.focusedClient = null; root.focusedClient = null;
root.walletClients = {}; root.walletClients = {};
root.Utils = bwcService.getUtils(); root.Utils = bwcService.getUtils();
root.formatAmount = function(amount) { root.formatAmount = function(amount) {
var config = configService.getSync().wallet.settings; var config = configService.getSync().wallet.settings;
if (config.unitCode == 'sat') return amount; if (config.unitCode == 'sat') return amount;
@ -276,8 +276,8 @@ angular.module('copayApp.services')
// check if exist // check if exist
if (lodash.find(root.profile.credentials, { if (lodash.find(root.profile.credentials, {
'walletId': walletData.walletId 'walletId': walletData.walletId
})) { })) {
return cb(gettext('Cannot join the same wallet more that once')); return cb(gettext('Cannot join the same wallet more that once'));
} }
} catch (ex) { } catch (ex) {
@ -337,6 +337,25 @@ angular.module('copayApp.services')
}); });
}; };
root.setMetaData = function(walletClient, addressBook, historyCache, cb) {
storageService.getAddressbook(walletClient.credentials.network, function(err, localAddressBook) {
var localAddressBook1 = {};
try {
localAddressBook1 = JSON.parse(localAddressBook);
} catch (ex) {
$log.warn(ex);
}
var mergeAddressBook = lodash.merge(addressBook, localAddressBook1);
storageService.setAddressbook(walletClient.credentials.network, JSON.stringify(addressBook), function(err) {
if (err) return cb(err);
storageService.setTxHistory(JSON.stringify(historyCache), walletClient.credentials.walletId, function(err) {
if (err) return cb(err);
return cb(null);
});
});
});
}
root._addWalletClient = function(walletClient, opts, cb) { root._addWalletClient = function(walletClient, opts, cb) {
var walletId = walletClient.credentials.walletId; var walletId = walletClient.credentials.walletId;
@ -383,7 +402,19 @@ angular.module('copayApp.services')
} catch (err) { } catch (err) {
return cb(gettext('Could not import. Check input file and password')); return cb(gettext('Could not import. Check input file and password'));
} }
root._addWalletClient(walletClient, opts, cb);
str = JSON.parse(str);
var addressBook = str.addressBook || {};
var historyCache = str.historyCache ||  [];
root._addWalletClient(walletClient, opts, function(err, walletId) {
if (err) return cb(err);
root.setMetaData(walletClient, addressBook, historyCache, function(error) {
if (error) console.log(error);
return cb(err, walletId);
});
});
}; };
root.importExtendedPrivateKey = function(xPrivKey, opts, cb) { root.importExtendedPrivateKey = function(xPrivKey, opts, cb) {