From 49ac6dfcf487f7033037eabdc6884cd2dd7c5658 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Mon, 7 Jul 2014 18:35:44 -0300 Subject: [PATCH] Hold on backup page until all copayers made a full backup --- index.html | 13 ++++++++----- js/controllers/header.js | 4 ++-- js/models/core/PublicKeyRing.js | 34 +++++++++++++++++++++++++++++++++ js/models/core/Wallet.js | 8 ++++---- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index cac7012c4..ec93c6212 100644 --- a/index.html +++ b/index.html @@ -140,10 +140,11 @@
- +
diff --git a/js/controllers/header.js b/js/controllers/header.js index 1b61dc518..1a7c8fe66 100644 --- a/js/controllers/header.js +++ b/js/controllers/header.js @@ -87,10 +87,10 @@ angular.module('copayApp.controllers').controller('HeaderController', window.onbeforeunload = undefined; }); - $scope.backupAndOpen = function() { + $scope.backup = function() { var w = $rootScope.wallet; - w.offerBackup(); backupService.download(w); + w.setBackupReady(); }; $scope.getVideoURL = function(copayer) { diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index ea56d20f8..8bf663d6d 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -30,6 +30,7 @@ function PublicKeyRing(opts) { this.publicKeysCache = opts.publicKeysCache || {}; this.nicknameFor = opts.nicknameFor || {}; this.copayerIds = []; + this.copayersBackup = opts.copayersBackup || []; this.addressToPath = {}; } @@ -59,6 +60,7 @@ PublicKeyRing.prototype.toObj = function() { requiredCopayers: this.requiredCopayers, totalCopayers: this.totalCopayers, indexes: AddressIndex.serialize(this.indexes), + copayersBackup: this.copayersBackup, copayersExtPubKeys: this.copayersHK.map(function(b) { return b.extendedPublicKeyString(); @@ -350,12 +352,30 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) { return hasChanged; }; +PublicKeyRing.prototype.setBackupReady = function(copayerId) { + if (this.isBackupReady()) return false; + + var cid = this.myCopayerId(); + this.copayersBackup.push(cid); + return true; +} + +PublicKeyRing.prototype.isBackupReady = function(copayerId) { + var cid = this.myCopayerId(); + return this.copayersBackup.indexOf(cid) != -1; +} + +PublicKeyRing.prototype.isFullyBackup = function(copayerId) { + return this.copayersBackup.length == this.totalCopayers; +} + PublicKeyRing.prototype.merge = function(inPKR, ignoreId) { this._checkInPKR(inPKR, ignoreId); var hasChanged = false; hasChanged |= this.mergeIndexes(inPKR.indexes); hasChanged |= this._mergePubkeys(inPKR); + hasChanged |= this.mergeBackups(inPKR.copayersBackup); return !!hasChanged; }; @@ -372,4 +392,18 @@ PublicKeyRing.prototype.mergeIndexes = function(indexes) { return !!hasChanged } +PublicKeyRing.prototype.mergeBackups = function(backups) { + var self = this; + var hasChanged = false; + + backups.forEach(function(cid) { + var isNew = self.copayersBackup.indexOf(cid) == -1; + if (isNew) self.copayersBackup.push(cid); + hasChanged |= isNew; + }); + + return !!hasChanged +} + + module.exports = require('soop')(PublicKeyRing); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 5e0be4c44..fa7c8be57 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -879,13 +879,13 @@ Wallet.prototype.toggleAddressBookEntry = function(key) { }; Wallet.prototype.isReady = function() { - var ret = this.publicKeyRing.isComplete() && this.backupOffered; + var ret = this.publicKeyRing.isComplete() && this.publicKeyRing.isFullyBackup(); return ret; }; -Wallet.prototype.offerBackup = function() { - this.backupOffered = true; - this.store(); +Wallet.prototype.setBackupReady = function() { + this.publicKeyRing.setBackupReady(); + this.emit('publicKeyRingUpdated', false); }; Wallet.prototype.signJson = function(payload) {