From 7cdf559fd7aae425eaea17a905ee493525800e82 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Mon, 7 Jul 2014 10:58:43 -0300 Subject: [PATCH] Fixes typos: Matias's suggestions. Added more tests --- js/controllers/send.js | 12 ++++++------ js/models/core/Wallet.js | 22 ++++++++++++++-------- test/test.Wallet.js | 16 +++++++++++----- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index ed679e6b1..dc9518351 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -186,15 +186,15 @@ angular.module('copayApp.controllers').controller('SendController', }, 500); }; - $scope.signAddressBook = {}; + $scope.addressbook = {}; - $scope.checkSignAddressBook = function(key) { + $scope.verifyAddressbookSignature = function(key) { if (key) { $timeout(function() { var w = $rootScope.wallet; - var sign = w.verifySignAddressBook(key); - $scope.signAddressBook[key] = sign; - if (!sign) { + var signature = w.verfifyAddressbookSignature(key); + $scope.addressbook[key] = signature; + if (!signature) { notification.error('Wrong signature', 'Entry of Addressbooks was deleted'); } }, 10); @@ -253,7 +253,7 @@ angular.module('copayApp.controllers').controller('SendController', if (errorMsg) { notification.error('Error', errorMsg); } else { - $scope.checkSignAddressBook(entry.address); + $scope.verifyAddressbookSignature(entry.address); notification.success('Success', 'New entry has been created'); } $rootScope.$digest(); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 486a9ad2c..7bd375377 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -840,19 +840,19 @@ Wallet.prototype.setAddressBook = function(key, label) { copayerId: copayerId, createdTs: ts }; - var addressbook = { + var newEntry = { hidden: false, createdTs: ts, copayerId: copayerId, label: label, - signature: this.signObject(payload) + signature: this.signJson(payload) }; - this.addressBook[key] = addressbook; + this.addressBook[key] = newEntry; this.sendAddressBook(); this.store(); }; -Wallet.prototype.verifySignAddressBook = function(key) { +Wallet.prototype.verfifyAddressbookSignature = function(key) { if (key) { var signature = this.addressBook[key].signature; var payload = { @@ -861,13 +861,16 @@ Wallet.prototype.verifySignAddressBook = function(key) { copayerId: this.addressBook[key].copayerId, createdTs: this.addressBook[key].createdTs }; - var sign = this.verifySignedObject(payload, signature); - if (!sign) { + var isVerified = this.verifySignedObject(payload, signature); + if (!isVerified) { // remove wrong signed entry delete this.addressBook[key]; this.store(); } - return sign; + return isVerified; + } + else { + throw new Error('Key is required'); } } @@ -876,6 +879,9 @@ Wallet.prototype.toggleAddressBookEntry = function(key) { this.addressBook[key].hidden = !this.addressBook[key].hidden; this.store(); } + else { + throw new Error('Key is required'); + } }; Wallet.prototype.isReady = function() { @@ -888,7 +894,7 @@ Wallet.prototype.offerBackup = function() { this.store(); }; -Wallet.prototype.signObject = function(payload) { +Wallet.prototype.signJson = function(payload) { var key = new bitcore.Key(); key.private = new Buffer(this.getMyCopayerIdPriv(), 'hex'); key.regenerateSync(); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index c172a1e93..d42aa6707 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -795,6 +795,9 @@ describe('Wallet model', function() { w.addressBook[key].hidden.should.equal(true); w.toggleAddressBookEntry(key); w.addressBook[key].hidden.should.equal(false); + (function() { + w.toggleAddressBookEntry(); + }).should.throw(); }); it('handle network addressBook correctly', function() { @@ -827,7 +830,7 @@ describe('Wallet model', function() { copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03', createdTs: 1403102115 }; - should.exist(w.signObject(payload)); + should.exist(w.signJson(payload)); }); it('should verify signed object', function() { @@ -838,7 +841,7 @@ describe('Wallet model', function() { copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03', createdTs: 1403102115, }; - var signature = w.signObject(data); + var signature = w.signJson(data); w.verifySignedObject(data, signature).should.equal(true); data.label = 'Another'; @@ -860,13 +863,16 @@ describe('Wallet model', function() { createdTs: payload.createdTs, copayerId: payload.copayerId, label: payload.label, - signature: w.signObject(payload) + signature: w.signJson(payload) }; w.addressBook[key] = addressbook; - w.verifySignAddressBook(key).should.equal(true); + w.verfifyAddressbookSignature(key).should.equal(true); w.addressBook[key].label = 'Another'; - w.verifySignAddressBook(key).should.equal(false); + w.verfifyAddressbookSignature(key).should.equal(false); + (function() { + w.verfifyAddressbookSignature(); + }).should.throw(); }); });