Merge pull request #197 from colkito/feature/import-backups

Feature/import backups
This commit is contained in:
Matias Alejo Garcia 2014-04-26 11:31:58 -03:00
commit d08d0aa875
6 changed files with 73 additions and 13 deletions

View file

@ -154,7 +154,7 @@
<div ng-show="wallets.length">
<a ng-click="create()" ng-disabled="loading" loading="Creating">Create a new wallet</a>
</div>
<a class="right" ng-click="import()">Import from file</a>
<a class="right" ng-href="#import">Import from file</a>
</div>
</div>
@ -163,6 +163,16 @@
</div>
</script>
<script type="text/ng-template" id="import.html">
<div ng-controller="ImportController">
<h3>{{title}}</h3>
<div class="large-6 columns">
<input type="file" class="form-control" placeholder="Select a backup file" ng-model="backupFile" autofocus="" ng-file-select>
</div>
</div>
</script>
<script type="text/ng-template" id="setup.html">
<div ng-controller="SetupController">
<div data-alert class="alert-box info round" ng-show="loading">
@ -535,6 +545,7 @@
<script src="js/controllers/backup.js"></script>
<script src="js/controllers/signin.js"></script>
<script src="js/controllers/setup.js"></script>
<script src="js/controllers/import.js"></script>
<script src="js/init.js"></script>
</body>

View file

@ -16,7 +16,8 @@ var copayApp = window.copayApp = angular.module('copay',[
'copay.controllerUtils',
'copay.setup',
'copay.directives',
'copay.video'
'copay.video',
'copay.import'
]);
angular.module('copay.header', []);
@ -32,4 +33,5 @@ angular.module('copay.setup', []);
angular.module('copay.socket', []);
angular.module('copay.directives', []);
angular.module('copay.video', []);
angular.module('copay.import', []);

22
js/controllers/import.js Normal file
View file

@ -0,0 +1,22 @@
'use strict';
angular.module('copay.import').controller('ImportController',
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);
};
});

View file

@ -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();
});
}
}
});

View file

@ -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;
};

View file

@ -14,6 +14,10 @@ angular
templateUrl: 'signin.html',
validate: false
})
.when('/import', {
templateUrl: 'import.html',
validate: false
})
.when('/setup', {
templateUrl: 'setup.html',
validate: false