From 2838b3ecd2c524e4170de7f4487a8e9f86f6a4f8 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 17 Apr 2014 18:02:20 -0300 Subject: [PATCH] added support for the wallet backups --- bower.json | 3 ++- index.html | 7 ++--- js/controllers/backup.js | 58 +++++++++++++++++++++++++++++++++++++--- js/models/core/Wallet.js | 45 ++++++++++++++++++++++++------- 4 files changed, 97 insertions(+), 16 deletions(-) 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 1b8f93c30..d833911d2 100644 --- a/index.html +++ b/index.html @@ -288,19 +288,19 @@

{{title}}

- +

Backup to email

@@ -326,6 +326,7 @@ + diff --git a/js/controllers/backup.js b/js/controllers/backup.js index e851e602c..565e88682 100644 --- a/js/controllers/backup.js +++ b/js/controllers/backup.js @@ -1,13 +1,65 @@ 'use strict'; angular.module('copay.backup').controller('BackupController', - function($scope, $rootScope, $location) { - + function($scope, $rootScope, $location, $window, $timeout) { if (!$rootScope.wallet.id) { $location.path('signin'); } - $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 094016358..5969a34f0 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -122,6 +122,17 @@ Wallet.prototype._handleNetworkChange = function(newPeer) { this.sendTxProposals(newPeer); }; +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; @@ -136,20 +147,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);