Fixes typos: Matias's suggestions. Added more tests

This commit is contained in:
Gustavo Cortez 2014-07-07 10:58:43 -03:00
commit 7cdf559fd7
3 changed files with 31 additions and 19 deletions

View file

@ -186,15 +186,15 @@ angular.module('copayApp.controllers').controller('SendController',
}, 500); }, 500);
}; };
$scope.signAddressBook = {}; $scope.addressbook = {};
$scope.checkSignAddressBook = function(key) { $scope.verifyAddressbookSignature = function(key) {
if (key) { if (key) {
$timeout(function() { $timeout(function() {
var w = $rootScope.wallet; var w = $rootScope.wallet;
var sign = w.verifySignAddressBook(key); var signature = w.verfifyAddressbookSignature(key);
$scope.signAddressBook[key] = sign; $scope.addressbook[key] = signature;
if (!sign) { if (!signature) {
notification.error('Wrong signature', 'Entry of Addressbooks was deleted'); notification.error('Wrong signature', 'Entry of Addressbooks was deleted');
} }
}, 10); }, 10);
@ -253,7 +253,7 @@ angular.module('copayApp.controllers').controller('SendController',
if (errorMsg) { if (errorMsg) {
notification.error('Error', errorMsg); notification.error('Error', errorMsg);
} else { } else {
$scope.checkSignAddressBook(entry.address); $scope.verifyAddressbookSignature(entry.address);
notification.success('Success', 'New entry has been created'); notification.success('Success', 'New entry has been created');
} }
$rootScope.$digest(); $rootScope.$digest();

View file

@ -840,19 +840,19 @@ Wallet.prototype.setAddressBook = function(key, label) {
copayerId: copayerId, copayerId: copayerId,
createdTs: ts createdTs: ts
}; };
var addressbook = { var newEntry = {
hidden: false, hidden: false,
createdTs: ts, createdTs: ts,
copayerId: copayerId, copayerId: copayerId,
label: label, label: label,
signature: this.signObject(payload) signature: this.signJson(payload)
}; };
this.addressBook[key] = addressbook; this.addressBook[key] = newEntry;
this.sendAddressBook(); this.sendAddressBook();
this.store(); this.store();
}; };
Wallet.prototype.verifySignAddressBook = function(key) { Wallet.prototype.verfifyAddressbookSignature = function(key) {
if (key) { if (key) {
var signature = this.addressBook[key].signature; var signature = this.addressBook[key].signature;
var payload = { var payload = {
@ -861,13 +861,16 @@ Wallet.prototype.verifySignAddressBook = function(key) {
copayerId: this.addressBook[key].copayerId, copayerId: this.addressBook[key].copayerId,
createdTs: this.addressBook[key].createdTs createdTs: this.addressBook[key].createdTs
}; };
var sign = this.verifySignedObject(payload, signature); var isVerified = this.verifySignedObject(payload, signature);
if (!sign) { if (!isVerified) {
// remove wrong signed entry // remove wrong signed entry
delete this.addressBook[key]; delete this.addressBook[key];
this.store(); 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.addressBook[key].hidden = !this.addressBook[key].hidden;
this.store(); this.store();
} }
else {
throw new Error('Key is required');
}
}; };
Wallet.prototype.isReady = function() { Wallet.prototype.isReady = function() {
@ -888,7 +894,7 @@ Wallet.prototype.offerBackup = function() {
this.store(); this.store();
}; };
Wallet.prototype.signObject = function(payload) { Wallet.prototype.signJson = function(payload) {
var key = new bitcore.Key(); var key = new bitcore.Key();
key.private = new Buffer(this.getMyCopayerIdPriv(), 'hex'); key.private = new Buffer(this.getMyCopayerIdPriv(), 'hex');
key.regenerateSync(); key.regenerateSync();

View file

@ -795,6 +795,9 @@ describe('Wallet model', function() {
w.addressBook[key].hidden.should.equal(true); w.addressBook[key].hidden.should.equal(true);
w.toggleAddressBookEntry(key); w.toggleAddressBookEntry(key);
w.addressBook[key].hidden.should.equal(false); w.addressBook[key].hidden.should.equal(false);
(function() {
w.toggleAddressBookEntry();
}).should.throw();
}); });
it('handle network addressBook correctly', function() { it('handle network addressBook correctly', function() {
@ -827,7 +830,7 @@ describe('Wallet model', function() {
copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03', copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03',
createdTs: 1403102115 createdTs: 1403102115
}; };
should.exist(w.signObject(payload)); should.exist(w.signJson(payload));
}); });
it('should verify signed object', function() { it('should verify signed object', function() {
@ -838,7 +841,7 @@ describe('Wallet model', function() {
copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03', copayerId: '026a55261b7c898fff760ebe14fd22a71892295f3b49e0ca66727bc0a0d7f94d03',
createdTs: 1403102115, createdTs: 1403102115,
}; };
var signature = w.signObject(data); var signature = w.signJson(data);
w.verifySignedObject(data, signature).should.equal(true); w.verifySignedObject(data, signature).should.equal(true);
data.label = 'Another'; data.label = 'Another';
@ -860,13 +863,16 @@ describe('Wallet model', function() {
createdTs: payload.createdTs, createdTs: payload.createdTs,
copayerId: payload.copayerId, copayerId: payload.copayerId,
label: payload.label, label: payload.label,
signature: w.signObject(payload) signature: w.signJson(payload)
}; };
w.addressBook[key] = addressbook; w.addressBook[key] = addressbook;
w.verifySignAddressBook(key).should.equal(true); w.verfifyAddressbookSignature(key).should.equal(true);
w.addressBook[key].label = 'Another'; w.addressBook[key].label = 'Another';
w.verifySignAddressBook(key).should.equal(false); w.verfifyAddressbookSignature(key).should.equal(false);
(function() {
w.verfifyAddressbookSignature();
}).should.throw();
}); });
}); });