Fixes and re-factory

This commit is contained in:
Gustavo Cortez 2014-07-07 20:01:50 -03:00
parent c00deb25f9
commit 14cea0362f
2 changed files with 14 additions and 18 deletions

View File

@ -140,9 +140,11 @@ Wallet.prototype._handleAddressBook = function(senderId, data, isInbound) {
var hasChange;
for (var key in rcv) {
if (!this.addressBook[key]) {
this.addressBook[key] = rcv[key];
var isVerified = this.verifyAddressbookSignature(senderId, key);
hasChange = true;
var isVerified = this.verifyAddressbookEntry(rcv[key], senderId, key);
if (isVerified) {
this.addressBook[key] = rcv[key];
hasChange = true;
}
}
}
if (hasChange) {
@ -854,22 +856,16 @@ Wallet.prototype.setAddressBook = function(key, label) {
this.store();
};
Wallet.prototype.verifyAddressbookSignature = function(senderId, key) {
Wallet.prototype.verifyAddressbookEntry = function(rcvEntry, senderId, key) {
if (!key) throw new Error('Keys are required');
var signature = this.addressBook[key].signature;
var signature = rcvEntry.signature;
var payload = {
address: key,
label: this.addressBook[key].label,
copayerId: this.addressBook[key].copayerId,
createdTs: this.addressBook[key].createdTs
label: rcvEntry.label,
copayerId: rcvEntry.copayerId,
createdTs: rcvEntry.createdTs
};
var isVerified = this.verifySignedJson(senderId, payload, signature);
if (!isVerified) {
// remove wrong signed entry
delete this.addressBook[key];
this.store();
}
return isVerified;
return this.verifySignedJson(senderId, payload, signature);
}
Wallet.prototype.toggleAddressBookEntry = function(key) {

View File

@ -891,11 +891,11 @@ describe('Wallet model', function() {
signature: "3046022100d4cdefef66ab8cea26031d5df03a38fc9ec9b09b0fb31d3a26b6e204918e9e78022100ecdbbd889ec99ea1bfd471253487af07a7fa7c0ac6012ca56e10e66f335e4586"
};
w.verifyAddressbookSignature(pubKey, key).should.equal(true);
w.verifyAddressbookEntry(w.addressBook[key], pubKey, key).should.equal(true);
w.addressBook[key].label = 'Another';
w.verifyAddressbookSignature(pubKey, key).should.equal(false);
w.verifyAddressbookEntry(w.addressBook[key], pubKey, key).should.equal(false);
(function() {
w.verifyAddressbookSignature();
w.verifyAddressbookEntry();
}).should.throw();
});