Wallet/src/js/controllers/export.js

156 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.controllers').controller('exportController',
2016-06-01 16:07:25 -03:00
function($rootScope, $scope, $timeout, $log, backupService, storageService, profileService, platformInfo, notification, go, gettext, gettextCatalog) {
var self = this;
2016-06-01 16:07:25 -03:00
var isWP = platformInfo.isWP;
var isAndroid = platformInfo.isAndroid;
self.error = null;
self.success = null;
2016-05-20 09:50:44 -03:00
$scope.metaDataEnabled = true;
var fc = profileService.focusedClient;
self.isEncrypted = fc.isPrivKeyEncrypted();
self.downloadWalletBackup = function() {
2016-05-20 09:50:44 -03:00
self.getMetaData($scope.metaDataEnabled, function(err, txsFromLocal, localAddressBook) {
if (err) {
self.error = true;
return;
}
var opts = {
2016-05-20 09:50:44 -03:00
noSign: $scope.noSignEnabled,
historyCache: txsFromLocal,
addressBook: localAddressBook
};
backupService.walletDownload(self.password, opts, function(err) {
if (err) {
self.error = true;
return;
}
2015-11-27 20:47:23 -03:00
$rootScope.$emit('Local/BackupDone');
notification.success(gettext('Success'), gettext('Encrypted export file saved'));
go.walletHome();
});
});
};
2015-11-11 18:54:41 -03:00
self.getMetaData = function(metaData, cb) {
if (metaData == false) return cb();
self.getHistoryCache(function(err, txsFromLocal) {
2015-11-10 12:49:12 -03:00
if (err) return cb(err);
self.getAddressbook(function(err, localAddressBook) {
2015-11-10 12:49:12 -03:00
if (err) return cb(err);
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);
}
2015-11-10 12:49:12 -03:00
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);
}
2015-11-10 12:49:12 -03:00
return cb(null, localAddressBook);
});
}
2015-11-10 12:34:03 -03:00
self.getBackup = function(cb) {
2016-05-20 09:50:44 -03:00
self.getMetaData($scope.metaDataEnabled, function(err, txsFromLocal, localAddressBook) {
if (err) {
self.error = true;
2015-11-10 12:34:03 -03:00
return cb(null);
}
var opts = {
2016-05-20 09:50:44 -03:00
noSign: $scope.noSignEnabled,
historyCache: txsFromLocal,
addressBook: localAddressBook
};
var ew = backupService.walletExport(self.password, opts);
if (!ew) {
self.error = true;
} else {
self.error = false;
2015-11-27 20:47:23 -03:00
$rootScope.$emit('Local/BackupDone');
}
2015-11-10 12:34:03 -03:00
return cb(ew);
});
}
self.viewWalletBackup = function() {
var self = this;
$timeout(function() {
2015-11-10 12:34:03 -03:00
self.getBackup(function(backup) {
var ew = backup;
if (!ew) return;
self.backupWalletPlainText = ew;
});
}, 100);
};
self.copyWalletBackup = function() {
2015-11-10 12:34:03 -03:00
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'));
});
};
self.sendWalletBackup = function() {
var fc = profileService.focusedClient;
window.plugins.toast.showShortCenter(gettextCatalog.getString('Preparing backup...'));
var name = (fc.credentials.walletName || fc.credentials.walletId);
if (fc.alias) {
name = fc.alias + ' [' + name + ']';
}
2015-11-10 12:34:03 -03:00
self.getBackup(function(backup) {
var ew = backup;
if (!ew) return;
2016-05-20 09:50:44 -03:00
if ($scope.noSignEnabled)
2015-11-10 12:34:03 -03:00
name = name + '(No Private Key)';
var subject = 'Copay Wallet Backup: ' + name;
var 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 {}';
window.plugins.socialsharing.shareViaEmail(
body,
subject,
null, // TO: must be null or an array
null, // CC: must be null or an array
null, // BCC: must be null or an array
null, // FILES: can be null, a string, or an array
function() {},
function() {}
);
2015-11-10 12:34:03 -03:00
});
};
2015-11-10 12:34:03 -03:00
});