verify signed messages

...and fix bug where i (recover param) was stored incorrectly
This commit is contained in:
Ryan X. Charles 2014-08-22 16:15:44 -07:00
parent 3b3ebb0458
commit 6176ad4a98
5 changed files with 33 additions and 6 deletions

View File

@ -43,6 +43,8 @@ Address.prototype.fromString = function(str) {
}
this.hashbuf = buf.slice(1);
return this;
}
Address.prototype.isValid = function() {

View File

@ -5,6 +5,7 @@ var Pubkey = require('./pubkey');
var BufferWriter = require('./bufferwriter');
var Hash = require('./hash');
var Address = require('./address');
var Signature = require('./signature');
var Message = function Message(messagebuf, key, sig, address, verified) {
if (!(this instanceof Message))
@ -60,7 +61,6 @@ Message.prototype.verify = function() {
ecdsa.key.pubkey = ecdsa.sig2pubkey();
if (!ecdsa.verify()) {
console.log(ecdsa.sigError());
this.verified = false;
return this;
}
@ -75,14 +75,14 @@ Message.prototype.verify = function() {
return this;
};
/*
Message.verify = function(messagebuf, sigstr, address) {
var sigbuf = new Buffer(sigstr, 'base64');
var message = new Message();
message.messagebuf = messagebuf;
message.sig = Signature().fromCompressed(sigbuf);
message.address = address;
return message.verify().verified;
};
*/
module.exports = Message;

View File

@ -12,7 +12,7 @@ var Signature = function Signature(r, s, i) {
};
Signature.prototype.fromCompressed = function(buf) {
var i = buf.slice(0, 1)[0];
var i = buf.slice(0, 1)[0] - 27 - 4; //TODO: handle uncompressed pubkeys
var b2 = buf.slice(1, 33);
var b3 = buf.slice(33, 65);
@ -26,17 +26,23 @@ Signature.prototype.fromCompressed = function(buf) {
this.i = i;
this.r = BN().fromBuffer(b2);
this.s = BN().fromBuffer(b3);
return this;
};
Signature.prototype.fromDER = function(buf) {
var obj = Signature.parseDER(buf);
this.r = obj.r;
this.s = obj.s;
return this;
};
Signature.prototype.fromString = function(str) {
var buf = new Buffer(str, 'hex');
this.fromDER(buf);
return this;
};
Signature.parseDER = function(buf) {
@ -101,7 +107,7 @@ Signature.prototype.toCompressed = function(i) {
if (!(i === 0 || i === 1 || i === 2 || i === 3))
throw new Error('i must be equal to 0, 1, 2, or 3');
var b1 = new Buffer([i]);
var b1 = new Buffer([i + 27 + 4]); //TODO: handle uncompressed pubkeys
var b2 = this.r.toBuffer({size: 32});
var b3 = this.s.toBuffer({size: 32});
return Buffer.concat([b1, b2, b3]);

View File

@ -58,4 +58,23 @@ describe('Message', function() {
});
describe('@verify', function() {
var messagebuf = new Buffer('this is my message');
var key = Key().fromRandom();
it('should verify a signed message', function() {
var sigstr = Message.sign(messagebuf, key);
var addr = Address().fromPubkey(key.pubkey);
Message.verify(messagebuf, sigstr, addr).should.equal(true);
});
it('should verify this known good signature', function() {
var addrstr = '1CKTmxj6DjGrGTfbZzVxnY4Besbv8oxSZb';
var address = Address().fromString(addrstr);
var sigstr = 'IOrTlbNBI0QO990xOw4HAjnvRl/1zR+oBMS6HOjJgfJqXp/1EnFrcJly0UcNelqJNIAH4f0abxOZiSpYmenMH4M=';
Message.verify(messagebuf, sigstr, address);
});
});
});

View File

@ -15,7 +15,7 @@ describe('Signature', function() {
var blank = new Buffer(32);
blank.fill(0);
var compressed = Buffer.concat([
new Buffer([0]),
new Buffer([0 + 27 + 4]),
blank,
blank
]);