Wallet/src/js/controllers/export.js

160 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.controllers').controller('exportController',
2016-08-18 19:23:58 -03:00
function($rootScope, $scope, $timeout, $log, lodash, backupService, walletService, storageService, profileService, platformInfo, notification, go, gettext, gettextCatalog, $state, $stateParams) {
var prevState;
2016-06-01 16:07:25 -03:00
var isWP = platformInfo.isWP;
var isAndroid = platformInfo.isAndroid;
2016-08-18 19:23:58 -03:00
var wallet = profileService.getWallet($stateParams.walletId);
$scope.isEncrypted = wallet.isPrivKeyEncrypted();
$scope.isCordova = platformInfo.isCordova;
$scope.isSafari = platformInfo.isSafari;
2016-06-16 12:18:11 -03:00
$scope.error = null;
2016-08-18 19:23:58 -03:00
$scope.init = function() {
2016-06-30 15:59:57 -03:00
$scope.supported = true;
2016-06-29 17:04:40 -03:00
$scope.exportQR = false;
2016-06-29 15:18:49 -03:00
$scope.noSignEnabled = false;
$scope.showAdvanced = false;
2016-08-18 19:23:58 -03:00
$scope.wallet = wallet;
$scope.canSign = wallet.canSign();
2016-06-16 12:18:11 -03:00
2016-08-18 19:23:58 -03:00
walletService.getEncodedWalletInfo(wallet, function(err, code) {
if (err) {
2016-08-18 19:23:58 -03:00
$log.warn(err);
return $state.go('wallet.preferencesAdvanced')
}
2016-06-16 12:18:11 -03:00
2016-08-18 19:23:58 -03:00
if (!code)
$scope.supported = false;
else
$scope.exportWalletInfo = code;
2016-06-16 12:18:11 -03:00
2016-08-18 19:23:58 -03:00
$timeout(function() {
$scope.$apply();
}, 1);
});
};
2016-06-16 14:43:10 -03:00
2016-06-29 17:04:40 -03:00
/*
EXPORT WITHOUT PRIVATE KEY - PENDING
2016-06-29 15:18:49 -03:00
$scope.noSignEnabledChange = function() {
$scope.exportWalletInfo = encodeWalletInfo();
$timeout(function() {
$scope.$apply();
}, 1);
};
2016-06-29 17:04:40 -03:00
*/
2016-06-29 15:18:49 -03:00
$scope.$on('$destroy', function() {
2016-08-18 19:23:58 -03:00
walletService.lock(wallet);
});
$scope.downloadWalletBackup = function() {
2016-06-17 11:46:40 -03:00
$scope.getAddressbook(function(err, localAddressBook) {
if (err) {
$scope.error = true;
return;
}
var opts = {
2016-05-20 09:50:44 -03:00
noSign: $scope.noSignEnabled,
addressBook: localAddressBook
};
backupService.walletDownload($scope.password, opts, function(err) {
if (err) {
$scope.error = true;
return;
}
notification.success(gettext('Success'), gettext('Encrypted export file saved'));
go.walletHome();
});
});
};
$scope.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);
});
2016-06-16 12:18:11 -03:00
};
$scope.getBackup = function(cb) {
2016-06-17 11:46:40 -03:00
$scope.getAddressbook(function(err, localAddressBook) {
if (err) {
$scope.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,
addressBook: localAddressBook
};
var ew = backupService.walletExport($scope.password, opts);
if (!ew) {
$scope.error = true;
} else {
$scope.error = false;
}
2015-11-10 12:34:03 -03:00
return cb(ew);
});
2016-06-16 12:18:11 -03:00
};
$scope.viewWalletBackup = function() {
$timeout(function() {
$scope.getBackup(function(backup) {
2015-11-10 12:34:03 -03:00
var ew = backup;
if (!ew) return;
$scope.backupWalletPlainText = ew;
2015-11-10 12:34:03 -03:00
});
}, 100);
};
$scope.copyWalletBackup = function() {
$scope.getBackup(function(backup) {
2015-11-10 12:34:03 -03:00
var ew = backup;
if (!ew) return;
window.cordova.plugins.clipboard.copy(ew);
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
});
};
$scope.sendWalletBackup = function() {
window.plugins.toast.showShortCenter(gettextCatalog.getString('Preparing backup...'));
2016-08-18 19:23:58 -03:00
var name = (wallet.credentials.walletName || wallet.credentials.walletId);
if (wallet.alias) {
name = wallet.alias + ' [' + name + ']';
}
$scope.getBackup(function(backup) {
2015-11-10 12:34:03 -03:00
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
});