Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Uncompressed Public Key Compact Signatures #1250

Merged
merged 3 commits into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/crypto/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ Signature.prototype.set = function(obj) {
};

Signature.fromCompact = function(buf) {
$.checkArgument(BufferUtil.isBuffer(buf), 'Argument is expected to be a Buffer');

var sig = new Signature();
//TODO: handle uncompressed pubkeys

var compressed = true;
var i = buf.slice(0, 1)[0] - 27 - 4;
if (i < 0) {
compressed = false;
i = i + 4;
}

var b2 = buf.slice(1, 33);
var b3 = buf.slice(33, 65);

Expand Down Expand Up @@ -142,8 +149,9 @@ Signature.prototype.toCompact = function(i, compressed) {
}

var val = i + 27 + 4;
if (compressed === false)
if (compressed === false) {
val = val - 4;
}
var b1 = new Buffer([val]);
var b2 = this.r.toBuffer({
size: 32
Expand Down
12 changes: 12 additions & 0 deletions test/crypto/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ describe('Signature', function() {
var sig = Signature.fromCompact(compressed);
sig.r.cmp(BN.Zero).should.equal(0);
sig.s.cmp(BN.Zero).should.equal(0);
sig.compressed.should.equal(true);
});

it('should create a signature from an uncompressed signature', function() {
var sigHexaStr = '1cd5e61ab5bfd0d1450997894cb1a53e917f89d82eb43f06fa96f32c96e061aec12fc1188e8b' +
'0dc553a2588be2b5b68dbbd7f092894aa3397786e9c769c5348dc6';
var sig = Signature.fromCompact(new Buffer(sigHexaStr, 'hex'));
var r = 'd5e61ab5bfd0d1450997894cb1a53e917f89d82eb43f06fa96f32c96e061aec1';
var s = '2fc1188e8b0dc553a2588be2b5b68dbbd7f092894aa3397786e9c769c5348dc6';
sig.r.toString('hex').should.equal(r);
sig.s.toString('hex').should.equal(s);
sig.compressed.should.equal(false);
});

});
Expand Down