copay/js/services/backupService.js

47 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-06-16 12:46:17 -07:00
'use strict';
2014-06-18 09:01:50 -07:00
var BackupService = function(notification) {
this.notifications = notification;
};
2014-06-16 12:46:17 -07:00
BackupService.prototype.getName = function(wallet) {
return (wallet.name ? (wallet.name + '-') : '') + wallet.id;
2014-06-16 12:46:17 -07:00
};
2014-07-17 12:53:38 -07:00
BackupService.prototype.download = function(wallet, scope) {
2014-06-16 12:46:17 -07:00
var ew = wallet.toEncryptedObj();
2014-07-07 13:09:24 -07:00
var partial = !wallet.publicKeyRing.isComplete();
var walletName = this.getName(wallet) + (partial ? '-Partial' : '');
var filename = walletName + '-keybackup.json.aes';
var notify = partial ? 'Partial backup created' : 'Backup created';
this.notifications.success(notify, 'Encrypted backup file saved.');
2014-06-16 12:46:17 -07:00
var blob = new Blob([ew], {
type: 'text/plain;charset=utf-8'
});
// show a native save dialog if we are in the shell
// and pass the wallet to the shell to convert to node Buffer
if (window.cshell) {
return window.cshell.send('backup:download', {
name: walletName,
wallet: ew
});
}
2014-07-17 12:53:38 -07:00
// throw an email intent if we are in the mobile version
if (window.xwalk && scope) {
var name = wallet.name ? wallet.name + ' ' : '';
var partial = partial ? 'Partial ' : '';
var subject = 'Copay - ' + name + 'Wallet ' + partial + 'Backup';
var body = 'This is the encrypted backup of the wallet ' + wallet.id + ':\n\n' + ew;
scope.mobileBackupURI = encodeURI('mailto:?subject=' + subject + '&body=' + body);
return;
}
2014-06-16 12:46:17 -07:00
// otherwise lean on the browser implementation
saveAs(blob, filename);
};
angular.module('copayApp.services').service('backupService', BackupService);