From c995f039c2aa2a30bccabc4a11d6ef3838b71eac Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 1 May 2014 18:32:22 -0300 Subject: [PATCH 1/6] added encryption backup support --- js/models/core/Wallet.js | 5 +++++ js/models/core/WalletFactory.js | 6 ++++++ js/models/storage/LocalEncrypted.js | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 7cfc6033d..cec091c8c 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -268,6 +268,11 @@ Wallet.fromObj = function(o, storage, network, blockchain) { return w; }; +Wallet.prototype.toEncryptedObj = function() { + var walletObj = this.toObj(); + return this.storage.export(walletObj); +}; + Wallet.prototype.sendTxProposals = function(recipients) { this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals); diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index a757c340a..8ffbc8403 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -68,6 +68,12 @@ WalletFactory.prototype.fromObj = function(obj) { return w; }; +WalletFactory.prototype.fromEncryptedObj = function(base64, password) { + this.storage._setPassphrase(password); + var walletObj = this.storage.import(base64); + return this.fromObj(walletObj); +}; + WalletFactory.prototype.read = function(walletId) { if (! this._checkRead(walletId)) return false; diff --git a/js/models/storage/LocalEncrypted.js b/js/models/storage/LocalEncrypted.js index 14a05ad12..cfeb1d2c5 100644 --- a/js/models/storage/LocalEncrypted.js +++ b/js/models/storage/LocalEncrypted.js @@ -157,4 +157,14 @@ Storage.prototype.clearAll = function() { localStorage.clear(); }; +Storage.prototype.export = function(obj) { + var encryptedObj = this._encryptObj(obj); + return encryptedObj; +}; + +Storage.prototype.import = function(base64) { + var decryptedObj = this._decryptObj(base64); + return decryptedObj; +}; + module.exports = require('soop')(Storage); From 76df93ab10f4ee52ca0b1e179d6b065aab0467d0 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 1 May 2014 18:33:36 -0300 Subject: [PATCH 2/6] new backup/import encrypted methods --- index.html | 4 +++- js/controllers/backup.js | 2 +- js/controllers/import.js | 15 ++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 972943ea6..542a75b53 100644 --- a/index.html +++ b/index.html @@ -187,7 +187,9 @@

{{title}}

- + + +
diff --git a/js/controllers/backup.js b/js/controllers/backup.js index 5c468fe24..03c67f2bf 100644 --- a/js/controllers/backup.js +++ b/js/controllers/backup.js @@ -8,7 +8,7 @@ angular.module('copay.backup').controller('BackupController', // TODO: get the real encrypted wallet. var _getEncryptedWallet = function() { - var wallet = JSON.stringify($rootScope.wallet.toObj()); + var wallet = $rootScope.wallet.toEncryptedObj(); return wallet; }; diff --git a/js/controllers/import.js b/js/controllers/import.js index 43c3376c2..2d5630915 100644 --- a/js/controllers/import.js +++ b/js/controllers/import.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copay.import').controller('ImportController', - function($scope, $rootScope, walletFactory, controllerUtils) { + function($scope, $rootScope, walletFactory, controllerUtils, Passphrase) { $scope.title = 'Import a backup'; $scope.getFile = function() { @@ -10,13 +10,18 @@ angular.module('copay.import').controller('ImportController', // If we use onloadend, we need to check the readyState. reader.onloadend = function(evt) { if (evt.target.readyState == FileReader.DONE) { // DONE == 2 - var obj = JSON.parse(evt.target.result); - $rootScope.wallet = walletFactory.fromObj(obj); - + var encryptedObj = evt.target.result; + var passphrase = Passphrase.getBase64($scope.password); + $rootScope.wallet = walletFactory.fromEncryptedObj(encryptedObj, passphrase); controllerUtils.startNetwork($rootScope.wallet); } }; - reader.readAsBinaryString($scope.file); + $scope.import = function() { + if ($scope.password != '') { + reader.readAsBinaryString($scope.file); + } + }; + }; }); From b79caed7fcd5c601c23608e12365854cff69ec2b Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 1 May 2014 18:39:12 -0300 Subject: [PATCH 3/6] added backup via email --- js/controllers/backup.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/js/controllers/backup.js b/js/controllers/backup.js index 03c67f2bf..5ea246b3a 100644 --- a/js/controllers/backup.js +++ b/js/controllers/backup.js @@ -40,11 +40,10 @@ angular.module('copay.backup').controller('BackupController', } 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. + + 'body=' + body; var newWin = $window.open(href, '_blank', 'scrollbars=yes,resizable=yes,width=10,height=10'); From 960b64affe60b1ccbdcc0b3568f6d63e8e6b95c8 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 1 May 2014 19:08:35 -0300 Subject: [PATCH 4/6] added import from text backup --- index.html | 3 +++ js/controllers/import.js | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 542a75b53..71a065a0a 100644 --- a/index.html +++ b/index.html @@ -188,7 +188,10 @@

{{title}}

+
Select a backup file
+
Or just pate the backup text here
+
diff --git a/js/controllers/import.js b/js/controllers/import.js index 2d5630915..27510d60c 100644 --- a/js/controllers/import.js +++ b/js/controllers/import.js @@ -4,6 +4,13 @@ angular.module('copay.import').controller('ImportController', function($scope, $rootScope, walletFactory, controllerUtils, Passphrase) { $scope.title = 'Import a backup'; + + var _importBackup = function(encryptedObj) { + var passphrase = Passphrase.getBase64($scope.password); + $rootScope.wallet = walletFactory.fromEncryptedObj(encryptedObj, passphrase); + controllerUtils.startNetwork($rootScope.wallet); + }; + $scope.getFile = function() { var reader = new FileReader(); @@ -11,17 +18,18 @@ angular.module('copay.import').controller('ImportController', reader.onloadend = function(evt) { if (evt.target.readyState == FileReader.DONE) { // DONE == 2 var encryptedObj = evt.target.result; - var passphrase = Passphrase.getBase64($scope.password); - $rootScope.wallet = walletFactory.fromEncryptedObj(encryptedObj, passphrase); - controllerUtils.startNetwork($rootScope.wallet); + _importBackup(encryptedObj); } }; + }; - $scope.import = function() { - if ($scope.password != '') { + $scope.import = function() { + if ($scope.password != '') { + if ($scope.backupText != '') { + _importBackup($scope.backupText); + } else { reader.readAsBinaryString($scope.file); } - }; - + } }; }); From 850d88e4587934c893f93048a5e14f9ed246ea5f Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 1 May 2014 19:12:17 -0300 Subject: [PATCH 5/6] fixed typo issue --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 71a065a0a..f160a81f2 100644 --- a/index.html +++ b/index.html @@ -190,7 +190,7 @@
Select a backup file
-
Or just pate the backup text here
+
Or just paste the backup text here
From fc6a24c6b101dd101b84760fff892404d0bc00c5 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Thu, 1 May 2014 19:39:01 -0300 Subject: [PATCH 6/6] added import from backup text --- index.html | 2 +- js/controllers/import.js | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index f160a81f2..9994794a6 100644 --- a/index.html +++ b/index.html @@ -187,7 +187,7 @@

{{title}}

- +
Select a backup file
Or just paste the backup text here
diff --git a/js/controllers/import.js b/js/controllers/import.js index 27510d60c..d04fd54fc 100644 --- a/js/controllers/import.js +++ b/js/controllers/import.js @@ -4,7 +4,7 @@ angular.module('copay.import').controller('ImportController', function($scope, $rootScope, walletFactory, controllerUtils, Passphrase) { $scope.title = 'Import a backup'; - + var reader = new FileReader(); var _importBackup = function(encryptedObj) { var passphrase = Passphrase.getBase64($scope.password); $rootScope.wallet = walletFactory.fromEncryptedObj(encryptedObj, passphrase); @@ -12,8 +12,6 @@ angular.module('copay.import').controller('ImportController', }; $scope.getFile = function() { - var reader = new FileReader(); - // If we use onloadend, we need to check the readyState. reader.onloadend = function(evt) { if (evt.target.readyState == FileReader.DONE) { // DONE == 2 @@ -24,8 +22,8 @@ angular.module('copay.import').controller('ImportController', }; $scope.import = function() { - if ($scope.password != '') { - if ($scope.backupText != '') { + if ($scope.password) { + if ($scope.backupText) { _importBackup($scope.backupText); } else { reader.readAsBinaryString($scope.file);