-
Notifications
You must be signed in to change notification settings - Fork 1k
Conversation
Related + some Test cases: bitpay/bitcore#1269 |
I see, so the issue is that the sighash is reversed when it's signed but not when generating k, and leading to discrepancies between libraries in the situations that "little" endian is used. Also verified against libsecp256k1 and with this PR it will pass without errors: var assert = require('assert');
var bitcore = require('bitcore-lib');
var message = new Buffer('52204d20fd0131ae1afd173fd80a3a746d2dcc0cddced8c9dc3d61cc7ab6e966', 'hex');
var reversed = new Buffer('66e9b67acc613ddcc9d8cedd0ccc2d6d743a0ad83f17fd1aae3101fd204d2052', 'hex');
var keyBuffer = new Buffer('16f243e962c59e71e54189e67e66cf2440a1334514c09c00ddcc21632bac9808', 'hex');
var key = bitcore.PrivateKey.fromBuffer(keyBuffer);
var signature1 = bitcore.crypto.ECDSA.sign(message, key).toBuffer().toString('hex');
var signature2 = bitcore.crypto.ECDSA.sign(message, key, 'little').toBuffer().toString('hex');
var secp256k1 = require('secp256k1');
var signature3 = secp256k1.sign(message, keyBuffer);
var signature4 = secp256k1.sign(reversed, keyBuffer);
assert.equal(secp256k1.signatureExport(signature3.signature).toString('hex'), signature1);
assert.equal(secp256k1.signatureExport(signature4.signature).toString('hex'), signature2); |
@@ -88,9 +88,10 @@ ECDSA.prototype.deterministicK = function(badrs) { | |||
var x = this.privkey.bn.toBuffer({ | |||
size: 32 | |||
}); | |||
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00]), x, this.hashbuf]), k); | |||
var hashbuf = this.endian === 'little' ? [].reverse.call(new Buffer(this.hashbuf)) : this.hashbuf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to use BufferUtil.reverse()
here, since we have benchmarks around it, and use it elsewhere.
LGTM, except nit from above. I think this is also backwards compatible and should be able to include in |
LGTM. Agree with @braydonf 's suggestion. Great Job @fanatid and @braydonf checking it. @crwatkins this will make Copay's signatures to match other libraries (as mentioned in bitcoin-dot-org/Bitcoin.org#888). |
Updated. |
Thanks! Landed in |
This bug gives different signatures for same transactions. This means that you got different txIds for same data.
/cc @killerstorm