From 7a463c9a9da98acf3a24e7988a17933052da40d8 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 16 Oct 2014 10:18:37 -0300 Subject: [PATCH] wallet and profile backup working on the UX --- js/controllers/manage.js | 6 +++++- js/controllers/more.js | 2 +- js/models/Identity.js | 5 ++--- js/models/Wallet.js | 2 +- js/services/backupService.js | 42 ++++++++++++++++++++---------------- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/js/controllers/manage.js b/js/controllers/manage.js index 821804c90..20710ac7c 100644 --- a/js/controllers/manage.js +++ b/js/controllers/manage.js @@ -1,3 +1,7 @@ 'use strict'; -angular.module('copayApp.controllers').controller('ManageController', function($scope, $rootScope, $location, controllerUtils) { +angular.module('copayApp.controllers').controller('ManageController', function($scope, $rootScope, $location, controllerUtils, backupService) { + + $scope.downloadBackup = function() { + backupService.profileDownload($rootScope.iden); + }; }); diff --git a/js/controllers/more.js b/js/controllers/more.js index b25e33288..494e794ea 100644 --- a/js/controllers/more.js +++ b/js/controllers/more.js @@ -74,7 +74,7 @@ angular.module('copayApp.controllers').controller('MoreController', } $scope.downloadBackup = function() { - backupService.download(w); + backupService.walletDownload(w); } $scope.viewBackup = function() { diff --git a/js/models/Identity.js b/js/models/Identity.js index 2bd2d4c7b..2a0472ab4 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -39,7 +39,6 @@ function Identity(email, password, opts) { this.walletDefaults = opts.walletDefaults || {}; this.version = opts.version || version; - this.email = email; // open wallets this.openWallets = []; @@ -401,7 +400,7 @@ Identity.prototype.createWallet = function(opts, cb) { }); opts.publicKeyRing.addCopayer( opts.privateKey.deriveBIP45Branch().extendedPublicKeyString(), - opts.nickname || this.email + opts.nickname || this.profile.getName() ); log.debug('\t### PublicKeyRing Initialized'); @@ -632,7 +631,7 @@ Identity.prototype.joinWallet = function(opts, cb) { walletOpts.network = joinNetwork; walletOpts.privateKey = privateKey; - walletOpts.nickname = opts.nickname || this.email; + walletOpts.nickname = opts.nickname || this.profile.getName(); if (opts.password) walletOpts.password = opts.password; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 840b8a2f9..bc86c2de1 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -1171,7 +1171,7 @@ Wallet.fromObj = function(o, readOpts) { */ Wallet.prototype.toEncryptedObj = function() { var walletObj = this.toObj(); - return this.storage.export(walletObj); + return this.storage.encrypt(walletObj); }; /** diff --git a/js/services/backupService.js b/js/services/backupService.js index 58b033d2f..874bef919 100644 --- a/js/services/backupService.js +++ b/js/services/backupService.js @@ -5,32 +5,16 @@ var BackupService = function(notification) { this.notifications = notification; }; -BackupService.prototype.getName = function(wallet) { - return (wallet.name ? (wallet.name + '-') : '') + wallet.id; -}; - BackupService.prototype.getCopayer = function(wallet) { return wallet.totalCopayers > 1 ? wallet.getMyCopayerNickname() : ''; }; -BackupService.prototype.getBackup = function(wallet) { - return wallet.toEncryptedObj(); -}; - -BackupService.prototype.getFilename = function(wallet) { - var walletName = this.getName(wallet); - var copayerName = this.getCopayer(wallet); - return (copayerName ? copayerName + '-' : '') + walletName + '-keybackup.json.aes'; -}; - -BackupService.prototype.download = function(wallet) { - var ew = this.getBackup(wallet); - var filename = this.getFilename(wallet); - - this.notifications.success('Backup created', 'Encrypted backup file saved'); +BackupService.prototype._download = function(ew, walletName, filename) { var blob = new Blob([ew], { type: 'text/plain;charset=utf-8' }); + + // show a native save dialog if we are in the shell // and pass the wallet to the shell to convert to node Buffer if (window.cshell) { @@ -49,9 +33,29 @@ BackupService.prototype.download = function(wallet) { attachments: ['base64:' + filename + '//' + btoa(ew)] }); } + this.notifications.success('Backup created', 'Encrypted backup file saved'); // otherwise lean on the browser implementation saveAs(blob, filename); }; + +BackupService.prototype.walletDownload = function(wallet) { + var ew = wallet.toEncryptedObj(); + var walletName = wallet.getName(); + var copayerName = this.getCopayer(wallet); + var filename = (copayerName ? copayerName + '-' : '') + walletName + '-keybackup.json.aes'; + this._download(ew, walletName, filename) +}; + +BackupService.prototype.profileDownload = function(iden) { + var ew = iden.toEncryptedObj(); + var name = iden.profile.getName(); + var filename = name + '-profile.json'; + this._download(ew, name, filename) +}; + + + + angular.module('copayApp.services').service('backupService', BackupService);