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-17 07:33:43 -07:00
|
|
|
};
|
2014-06-16 12:46:17 -07:00
|
|
|
|
|
|
|
BackupService.prototype.getName = function(wallet) {
|
|
|
|
return (wallet.name ? (wallet.name+'-') : '') + wallet.id;
|
|
|
|
};
|
|
|
|
|
|
|
|
BackupService.prototype.download = function(wallet) {
|
|
|
|
var ew = wallet.toEncryptedObj();
|
|
|
|
var timestamp = +(new Date());
|
|
|
|
var walletName = this.getName(wallet);
|
2014-06-16 12:54:50 -07:00
|
|
|
var filename = walletName + '-' + timestamp + '-keybackup.json.aes';
|
2014-06-17 07:33:43 -07:00
|
|
|
this.notifications.success('Backup created', '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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// otherwise lean on the browser implementation
|
|
|
|
saveAs(blob, filename);
|
|
|
|
};
|
|
|
|
|
|
|
|
BackupService.prototype.sendEmail = function(email, wallet) {
|
|
|
|
var ew = wallet.toEncryptedObj();
|
|
|
|
var body = ew;
|
|
|
|
var subject = this.getName(wallet);
|
|
|
|
var href = 'mailto:' + email + '?' + 'subject=[Copay Backup] ' + subject + '&' + 'body=' + body;
|
|
|
|
|
|
|
|
if (window.cshell) {
|
|
|
|
return window.cshell.send('backup:email', href);
|
|
|
|
}
|
|
|
|
|
|
|
|
var newWin = window.open(href, '_blank', 'scrollbars=yes,resizable=yes,width=10,height=10');
|
|
|
|
if (newWin) {
|
|
|
|
setTimeout(function() {
|
|
|
|
newWin.close();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-17 07:33:43 -07:00
|
|
|
angular.module('copayApp.services').service('backupService', BackupService);
|