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

View File

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