diff --git a/bower.json b/bower.json index cdd5f0b9c..950d050b1 100644 --- a/bower.json +++ b/bower.json @@ -14,6 +14,7 @@ "angular-mocks": "~1.2.14", "mocha": "~1.18.2", "chai": "~1.9.1", - "crypto-js": "http://crypto-js.googlecode.com/files/CryptoJS%20v3.1.2.zip" + "crypto-js": "http://crypto-js.googlecode.com/files/CryptoJS%20v3.1.2.zip", + "file-saver": "*" } } diff --git a/index.html b/index.html index e896e0805..c30530d91 100644 --- a/index.html +++ b/index.html @@ -325,19 +325,19 @@ missing. Ask your copayers to join your session: {{$root.wallet.network.peerI

{{title}}

- +

Backup to email

@@ -364,6 +364,7 @@ missing. Ask your copayers to join your session: {{$root.wallet.network.peerI + diff --git a/js/controllers/backup.js b/js/controllers/backup.js index 3f78d1ad5..1a46faa9f 100644 --- a/js/controllers/backup.js +++ b/js/controllers/backup.js @@ -1,8 +1,7 @@ 'use strict'; angular.module('copay.backup').controller('BackupController', - function($scope, $rootScope, $location, Socket, controllerUtils) { - + function($scope, $rootScope, $location, $window, $timeout, Socket, controllerUtils) { if (!$rootScope.wallet || !$rootScope.wallet.id) { $location.path('signin'); } @@ -12,4 +11,58 @@ angular.module('copay.backup').controller('BackupController', } $scope.title = 'Backup'; + + var filename = $rootScope.wallet.id + '.json.aes'; + + // TODO: get the real encrypted wallet. + var _getEncryptedWallet = function() { + var wallet = JSON.stringify($rootScope.wallet.toObj()); + return wallet; + }; + + var _getWalletBlob = function() { + var wallet = _getEncryptedWallet(); + var blob = new Blob([wallet], {type: 'text/plain;charset=utf-8'}); + + return blob; + }; + + $scope.download = function() { + var blob = _getWalletBlob(); + saveAs(blob, filename); + }; + + $scope.dropbox = function() { + var blob = _getWalletBlob(); + var url = $window.URL.createObjectURL(blob); + + // TODO: send the blob to Dropbox, (if we can...) + }; + + $scope.email = function() { + var email = prompt('Please enter your email addres.'); + var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; + + if (!email.match(mailformat)) { + alert('Enter a valid email address'); + } else { + if (email && email !== '') { + var body = _getEncryptedWallet(); + console.log(body); + var subject = 'Copay Backup'; + var href = 'mailto:' + email + '?' + + 'subject=' + subject + '&' + + 'body=' + 'body'; // TODO: Uncomment this when get the real encrypted wallet. + + var newWin = $window.open(href, '_blank', 'scrollbars=yes,resizable=yes,width=10,height=10'); + + if (newWin) { + $timeout(function() { + newWin.close(); + }, 1000); + } + } + } + }; + }); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index bc488b483..69cb7ab2e 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -122,6 +122,17 @@ Wallet.prototype._handleNetworkChange = function(newPeer) { this.emit('refresh'); }; +Wallet.prototype._optsToObj = function () { + var obj = { + id: this.id, + spendUnconfirmed: this.spendUnconfirmed, + requiredCopayers: this.requiredCopayers, + totalCopayers: this.totalCopayers, + }; + + return obj; +}; + Wallet.prototype.netStart = function() { var self = this; var net = this.network; @@ -143,20 +154,36 @@ Wallet.prototype.netStart = function() { Wallet.prototype.store = function() { this.log('[Wallet.js.135:store:]'); //TODO - this.storage.set(this.id,'opts', { - id: this.id, - spendUnconfirmed: this.spendUnconfirmed, - requiredCopayers: this.requiredCopayers, - totalCopayers: this.totalCopayers, - }); - this.storage.set(this.id,'publicKeyRing', this.publicKeyRing.toObj()); - this.storage.set(this.id,'txProposals', this.txProposals.toObj()); - this.storage.set(this.id,'privateKey', this.privateKey.toObj()); + var wallet = this.toObj(); + this.storage.setFromObj(this.id, wallet); this.log('[Wallet.js.146] EMIT REFRESH'); //TODO this.emit('refresh'); + }; +Wallet.prototype.toObj = function() { + var optsObj = this._optsToObj(); + var walletObj = { + opts: optsObj, + publicKeyring: this.publicKeyRing.toObj(), + txProposals: this.txProposals.toObj(), + privateKey: this.privateKey.toObj() + }; + + return walletObj; +}; + +Wallet.fromObj = function(wallet) { + var opts = wallet.opts; + opts['publicKeyRing'] = this.publicKeyring.fromObj(wallet.publicKeyRing); + opts['txProposals'] = this.txProposal.fromObj(wallet.txProposals); + opts['privateKey'] = this.privateKey.fromObj(wallet.privateKey); + + var w = new Wallet(opts); + + return w; +}; Wallet.prototype.sendTxProposals = function(recipients) { this.log('### SENDING txProposals TO:', recipients||'All', this.txProposals);