From bc77fabba91a71ed59d679fafb5f315c228972d7 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Fri, 25 Apr 2014 17:34:24 -0300 Subject: [PATCH 1/4] Added import angularjs controller --- index.html | 9 ++++++++- js/app.js | 4 +++- js/controllers/import.js | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 js/controllers/import.js diff --git a/index.html b/index.html index 1ab1d737c..b9ed6d77d 100644 --- a/index.html +++ b/index.html @@ -150,7 +150,7 @@
Create a new wallet
- Import from file + Import from file @@ -159,6 +159,12 @@ + + + diff --git a/js/app.js b/js/app.js index bcfce8f2c..bf845c194 100644 --- a/js/app.js +++ b/js/app.js @@ -15,7 +15,8 @@ var copayApp = window.copayApp = angular.module('copay',[ 'copay.controllerUtils', 'copay.setup', 'copay.directives', - 'copay.video' + 'copay.video', + 'copay.import' ]); angular.module('copay.header', []); @@ -30,4 +31,5 @@ angular.module('copay.setup', []); angular.module('copay.socket', []); angular.module('copay.directives', []); angular.module('copay.video', []); +angular.module('copay.import', []); diff --git a/js/controllers/import.js b/js/controllers/import.js new file mode 100644 index 000000000..306fd5aa4 --- /dev/null +++ b/js/controllers/import.js @@ -0,0 +1,6 @@ +'use strict'; + +angular.module('copay.import').controller('ImportController', + function($scope, $rootScope) { + $scope.title = 'Import a backup'; + }); From 05f41a8e46fbf2494680e6a22558d85889ed4609 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Fri, 25 Apr 2014 17:34:38 -0300 Subject: [PATCH 2/4] added import route --- js/routes.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/js/routes.js b/js/routes.js index 3970dd047..1206f5192 100644 --- a/js/routes.js +++ b/js/routes.js @@ -14,6 +14,10 @@ angular templateUrl: 'signin.html', validate: false }) + .when('/import', { + templateUrl: 'import.html', + validate: false + }) .when('/setup', { templateUrl: 'setup.html', validate: false From 3746a9b878e716bc4183339d2ce264d86c432069 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Fri, 25 Apr 2014 19:11:56 -0300 Subject: [PATCH 3/4] added a new directive for import backups --- index.html | 4 ++++ js/directives.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index b9ed6d77d..dbea93755 100644 --- a/index.html +++ b/index.html @@ -162,6 +162,10 @@ diff --git a/js/directives.js b/js/directives.js index 9dd48091c..580434df8 100644 --- a/js/directives.js +++ b/js/directives.js @@ -75,5 +75,15 @@ angular.module('copay.directives') } } }) -; + .directive('ngFileSelect',function() { + return { + link: function($scope, el) { + el.bind('change', function(e) { + $scope.file = (e.srcElement || e.target).files[0]; + console.log('file directive', $scope.file); + $scope.getFile(); + }); + } + } + }); From 69e1bb8e373ebf4d81bfef15db0a45a36dbbe7f6 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Fri, 25 Apr 2014 19:12:13 -0300 Subject: [PATCH 4/4] added the import method --- js/controllers/import.js | 18 +++++++++++++++++- js/models/core/WalletFactory.js | 31 +++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/js/controllers/import.js b/js/controllers/import.js index 306fd5aa4..43c3376c2 100644 --- a/js/controllers/import.js +++ b/js/controllers/import.js @@ -1,6 +1,22 @@ 'use strict'; angular.module('copay.import').controller('ImportController', - function($scope, $rootScope) { + function($scope, $rootScope, walletFactory, controllerUtils) { $scope.title = 'Import a backup'; + + $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 + var obj = JSON.parse(evt.target.result); + $rootScope.wallet = walletFactory.fromObj(obj); + + controllerUtils.startNetwork($rootScope.wallet); + } + }; + + reader.readAsBinaryString($scope.file); + }; }); diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 7a2972cbe..86c863d9c 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -51,16 +51,11 @@ WalletFactory.prototype._checkRead = function(walletId) { return ret?true:false; }; -WalletFactory.prototype.read = function(walletId) { - if (! this._checkRead(walletId)) - return false; - - var s = this.storage; - var opts = s.get(walletId, 'opts'); - opts.id = walletId; - opts.publicKeyRing = new PublicKeyRing.fromObj(s.get(walletId, 'publicKeyRing')); - opts.txProposals = new TxProposals.fromObj(s.get(walletId, 'txProposals')); - opts.privateKey = new PrivateKey.fromObj(s.get(walletId, 'privateKey')); +WalletFactory.prototype.fromObj = function(obj) { + var opts = obj.opts; + opts.publicKeyRing = new PublicKeyRing.fromObj(obj.publicKeyRing); + opts.txProposals = new TxProposals.fromObj(obj.txProposals); + opts.privateKey = new PrivateKey.fromObj(obj.privateKey); opts.storage = this.storage; opts.network = this.network; opts.blockchain = this.blockchain; @@ -76,6 +71,22 @@ WalletFactory.prototype.read = function(walletId) { // No really an error, just to be sure. } this.log('### WALLET OPENED:', w.id); + + return w; +}; + +WalletFactory.prototype.read = function(walletId) { + if (! this._checkRead(walletId)) + return false; + + var s = this.storage; + var opts = s.get(walletId, 'opts'); + opts.id = walletId; + opts.publicKeyRing = s.get(walletId, 'publicKeyRing'); + opts.txProposals = s.get(walletId, 'txProposals'); + opts.privateKey = s.get(walletId, 'privateKey'); + + w = this.formObj(opts); return w; };