Added creator's signature to address book entry. Re-factory somethings and improve the merge of them

This commit is contained in:
Gustavo Cortez 2014-07-07 01:33:39 -03:00
commit 313fcd4808
4 changed files with 168 additions and 81 deletions

View file

@ -186,23 +186,23 @@ angular.module('copayApp.controllers').controller('SendController',
}, 500);
};
$scope.deleteAddressBook = function(addressBook) {
var w = $rootScope.wallet;
$timeout(function() {
var errorMsg;
try {
w.deleteAddressBook(addressBook);
} catch (e) {
errorMsg = e.message;
}
$scope.signAddressBook = {};
if (errorMsg) {
notification.error('Error', errorMsg);
} else {
notification.success('Success', 'Entry removed successfully');
}
$rootScope.$digest();
}, 500);
$scope.checkSignAddressBook = function(key) {
if (key) {
$timeout(function() {
var w = $rootScope.wallet;
var sign = w.verifySignAddressBook(key);
$scope.signAddressBook[key] = sign;
}, 10);
}
};
$scope.toggleAddressBookEntry = function(key) {
if (key) {
var w = $rootScope.wallet;
w.toggleAddressBookEntry(key);
}
};
$scope.copyAddress = function(address) {
@ -250,11 +250,11 @@ angular.module('copayApp.controllers').controller('SendController',
if (errorMsg) {
notification.error('Error', errorMsg);
} else {
$scope.checkSignAddressBook(entry.address);
notification.success('Success', 'New entry has been created');
}
$rootScope.$digest();
}, 500);
$anchorScroll();
// reset fields
$scope.newaddress = $scope.newlabel = null;
});

View file

@ -146,11 +146,6 @@ Wallet.prototype._handleAddressBook = function(senderId, data, isInbound) {
if (!this.addressBook[key]) {
this.addressBook[key] = rcv[key];
hasChange = true;
} else {
if (rcv[key].createdTs > this.addressBook[key].createdTs) {
this.addressBook[key] = rcv[key];
hasChange = true;
}
}
}
if (hasChange) {
@ -244,7 +239,6 @@ Wallet.prototype.getMyCopayerIdPriv = function() {
return this.privateKey.getIdPriv(); //copayer idpriv is hex of a private key
};
Wallet.prototype.getSecret = function() {
var pubkeybuf = new Buffer(this.getMyCopayerId(), 'hex');
var str = Base58Check.encode(pubkeybuf);
@ -838,21 +832,42 @@ Wallet.prototype._checkAddressBook = function(key) {
Wallet.prototype.setAddressBook = function(key, label) {
this._checkAddressBook(key);
var copayerId = this.getMyCopayerId();
var ts = Date.now();
var payload = {
address: key,
label: label,
copayerId: copayerId,
createdTs: ts
};
var addressbook = {
createdTs: Date.now(),
copayerId: this.getMyCopayerId(),
label: label
hidden: false,
createdTs: ts,
copayerId: copayerId,
label: label,
signature: this.signObject(payload)
};
this.addressBook[key] = addressbook;
this.sendAddressBook();
this.store();
};
Wallet.prototype.deleteAddressBook = function(key) {
Wallet.prototype.verifySignAddressBook = function(key) {
if (key) {
this.addressBook[key].copayerId = -1;
this.addressBook[key].createdTs = Date.now();
this.sendAddressBook();
var signature = this.addressBook[key].signature;
var payload = {
address: key,
label: this.addressBook[key].label,
copayerId: this.addressBook[key].copayerId,
createdTs: this.addressBook[key].createdTs
};
return this.verifySignedObject(payload, signature);
}
}
Wallet.prototype.toggleAddressBookEntry = function(key) {
if (key) {
this.addressBook[key].hidden = !this.addressBook[key].hidden;
this.store();
}
};
@ -867,4 +882,19 @@ Wallet.prototype.offerBackup = function() {
this.store();
};
Wallet.prototype.signObject = function(payload) {
var key = new bitcore.Key();
key.private = new Buffer(this.getMyCopayerIdPriv(), 'hex');
key.regenerateSync();
var sign = bitcore.Message.sign(JSON.stringify(payload), key);
return sign.toString('hex');
}
Wallet.prototype.verifySignedObject = function(payload, signature) {
var pubkey = new Buffer(this.getMyCopayerId(), 'hex');
var sign = new Buffer(signature, 'hex');
var v = bitcore.Message.verifyWithPubKey(pubkey, JSON.stringify(payload), sign);
return v;
}
module.exports = require('soop')(Wallet);