Merge pull request #85 from colkito/feature/wallet-backup-support

Feature/wallet backup support
This commit is contained in:
Gustavo Maximiliano Cortez 2014-04-17 18:21:56 -03:00
commit d34dfc746c
4 changed files with 97 additions and 15 deletions

View File

@ -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": "*"
}
}

View File

@ -325,19 +325,19 @@ missing. Ask your copayers to join your session: <b>{{$root.wallet.network.peerI
<h2>{{title}}</h2>
<div class="row text-center">
<div class="large-4 columns">
<a href="/" class="panel box-backup">
<a class="panel box-backup" ng-click="download()">
<i class="fi-download size-72"></i>
<p> Download File </p>
</a>
</div>
<div class="large-4 columns">
<a href="/" class="panel box-backup">
<a class="panel box-backup" ng-click="dropbox()">
<i class="fi-social-dropbox size-72"></i>
<p> Backup to Dropbox </p>
</a>
</div>
<div class="large-4 columns">
<a href="/" class="panel box-backup">
<a class="panel box-backup" ng-click="email()">
<i class="fi-mail size-72"></i>
<p> Backup to email </p>
</a>
@ -364,6 +364,7 @@ missing. Ask your copayers to join your session: <b>{{$root.wallet.network.peerI
<script src="lib/peerjs/peer.js"></script>
<script src="lib/bitcore.js"></script>
<script src="lib/crypto-js/rollups/aes.js"></script>
<script src="lib/file-saver/FileSaver.js"></script>
<script src="lib/socket.io.js"></script>
<script src="js/copayBundle.js"></script>

View File

@ -1,8 +1,7 @@
'use strict';
angular.module('copay.backup').controller('BackupController',
function($scope, $rootScope, $location, Socket, controllerUtils) {
function($scope, $rootScope, $location, $window, $timeout, Socket, controllerUtils) {
if (!$rootScope.wallet || !$rootScope.wallet.id) {
$location.path('signin');
}
@ -12,4 +11,58 @@ angular.module('copay.backup').controller('BackupController',
}
$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);
}
}
}
};
});

View File

@ -122,6 +122,17 @@ Wallet.prototype._handleNetworkChange = function(newPeer) {
this.emit('refresh');
};
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;
@ -143,20 +154,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);