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>
<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">
<li>
<switch id="no-sign" name="noSign" ng-model="noSign" class="green right"></switch>

View File

@ -1,62 +1,127 @@
'use strict';
angular.module('copayApp.controllers').controller('backupController',
function($rootScope, $scope, $timeout, backupService, profileService, isMobile, notification, go, gettext, gettextCatalog) {
this.error = null;
this.success = null;
function($rootScope, $scope, $timeout, $log, backupService, storageService, profileService, isMobile, notification, go, gettext, gettextCatalog) {
var self = this;
self.error = null;
self.success = null;
$scope.metaData = true;
var fc = profileService.focusedClient;
this.isEncrypted = fc.isPrivKeyEncrypted();
self.isEncrypted = fc.isPrivKeyEncrypted();
this.downloadWalletBackup = function() {
var self = this;
var opts = {
noSign: $scope.noSign,
};
backupService.walletDownload(this.password, opts, function(err) {
self.downloadWalletBackup = function() {
self.getMetaData($scope.metaData, function(err, txsFromLocal, localAddressBook) {
if (err) {
self.error = true;
return ;
return;
}
$rootScope.$emit('Local/BackupDone');
notification.success(gettext('Success'), gettext('Encrypted export file saved'));
go.walletHome();
var opts = {
noSign: $scope.noSign,
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() {
var opts = {
noSign: $scope.noSign,
};
self.getMetaData = function(metaData, cb) {
if (metaData == false) return cb();
self.getHistoryCache(function(err, txsFromLocal) {
if (err) return cb(err);
var ew = backupService.walletExport(this.password, opts);
if (!ew) {
this.error = true;
} else {
this.error = false;
}
return ew;
};
self.getAddressbook(function(err, localAddressBook) {
if (err) return cb(err);
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;
$timeout(function() {
var ew = self.getBackup();
if (!ew) return;
self.backupWalletPlainText = ew;
$rootScope.$emit('Local/BackupDone');
self.getBackup(function(backup) {
var ew = backup;
if (!ew) return;
self.backupWalletPlainText = ew;
});
}, 100);
};
this.copyWalletBackup = function() {
var ew = this.getBackup();
if (!ew) return;
window.cordova.plugins.clipboard.copy(ew);
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
$rootScope.$emit('Local/BackupDone');
self.copyWalletBackup = function() {
self.getBackup(function(backup) {
var ew = backup;
if (!ew) return;
window.cordova.plugins.clipboard.copy(ew);
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
});
};
this.sendWalletBackup = function() {
self.sendWalletBackup = function() {
var fc = profileService.focusedClient;
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
@ -66,19 +131,20 @@ angular.module('copayApp.controllers').controller('backupController',
if (fc.alias) {
name = fc.alias + ' [' + name + ']';
}
var ew = this.getBackup();
if (!ew) return;
self.getBackup(function(backup) {
var ew = backup;
if (!ew) return;
if( $scope.noSign)
name = name + '(No Private Key)';
if ($scope.noSign)
name = name + '(No Private Key)';
var properties = {
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 {}',
isHtml: false
};
$rootScope.$emit('Local/BackupDone');
window.plugin.email.open(properties);
var properties = {
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 {}',
isHtml: false
};
window.plugin.email.open(properties);
});
};
});

View File

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

View File

@ -11,7 +11,7 @@ angular.module('copayApp.services')
root.focusedClient = null;
root.walletClients = {};
root.Utils = bwcService.getUtils();
root.Utils = bwcService.getUtils();
root.formatAmount = function(amount) {
var config = configService.getSync().wallet.settings;
if (config.unitCode == 'sat') return amount;
@ -276,8 +276,8 @@ angular.module('copayApp.services')
// check if exist
if (lodash.find(root.profile.credentials, {
'walletId': walletData.walletId
})) {
'walletId': walletData.walletId
})) {
return cb(gettext('Cannot join the same wallet more that once'));
}
} 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) {
var walletId = walletClient.credentials.walletId;
@ -383,7 +402,19 @@ angular.module('copayApp.services')
} catch (err) {
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) {