Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.
Closed
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
36 changes: 25 additions & 11 deletions lib/transaction/sighash.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@ var SIGHASH_SINGLE_BUG = '000000000000000000000000000000000000000000000000000000
var BITS_64_ON = 'ffffffffffffffff';

/**
* Returns a buffer of length 32 bytes with the hash that needs to be signed
* for OP_CHECKSIG.
*
* @name Signing.sighash
* @param {Transaction} transaction the transaction to sign
* @param {number} sighashType the type of the hash
* @param {number} inputNumber the input index for the signature
* @param {Script} subscript the script that will be signed
* Returns the preimage to be signed
*/
var sighash = function sighash(transaction, sighashType, inputNumber, subscript) {
var sighashPreimage = function sighash(transaction, sighashType, inputNumber, subscript) {
var Transaction = require('./transaction');
var Input = require('./input');

Expand Down Expand Up @@ -79,11 +72,31 @@ var sighash = function sighash(transaction, sighashType, inputNumber, subscript)
if (sighashType & Signature.SIGHASH_ANYONECANPAY) {
txcopy.inputs = [txcopy.inputs[inputNumber]];
}

var buf = new BufferWriter()
return new BufferWriter()
.write(txcopy.toBuffer())
.writeInt32LE(sighashType)
.toBuffer();
};

/**
* Returns a buffer of length 32 bytes with the hash that needs to be signed
* for OP_CHECKSIG.
*
* @name Signing.sighash
* @param {Transaction} transaction the transaction to sign
* @param {number} sighashType the type of the hash
* @param {number} inputNumber the input index for the signature
* @param {Script} subscript the script that will be signed
*/
var sighash = function sighash(transaction, sighashType, inputNumber, subscript) {
if ((sighashType & 31) === Signature.SIGHASH_SINGLE) {
// The SIGHASH_SINGLE bug.
// https://bitcointalk.org/index.php?topic=260595.0
if (inputNumber >= transaction.outputs.length) {
return new Buffer(SIGHASH_SINGLE_BUG, 'hex');
}
}
var buf = sighashPreimage(transaction, sighashType, inputNumber, subscript);
var ret = Hash.sha256sha256(buf);
ret = new BufferReader(ret).readReverse();
return ret;
Expand Down Expand Up @@ -132,5 +145,6 @@ function verify(transaction, signature, publicKey, inputIndex, subscript) {
module.exports = {
sighash: sighash,
sign: sign,
sighashPreimage: sighashPreimage,
verify: verify
};
23 changes: 21 additions & 2 deletions test/transaction/sighash.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var bitcore = require('../../');
var Script = bitcore.Script;
var Transaction = bitcore.Transaction;
var sighash = Transaction.sighash;
var BufferUtils = bitcore.util.buffer;
var Signature = bitcore.crypto.Signature;

var vectors_sighash = require('../data/sighash.json');

Expand All @@ -28,10 +30,27 @@ describe('sighash', function() {
var tx = new Transaction(txbuf);

//make sure transacion to/from buffer is isomorphic
tx.uncheckedSerialize().should.equal(txbuf.toString('hex'));
tx.uncheckedSerialize().should.equal(vector[0]);

//sighash ought to be correct
sighash.sighash(tx, nhashtype, nin, subscript).toString('hex').should.equal(sighashbuf.toString('hex'));
BufferUtils.equal(sighash.sighash(tx, nhashtype, nin, subscript), sighashbuf).should.be.true;
});
});

it('sighashPreimage handles correctly the SIGHASH_SINGLE bug', function() {

// Use the first transaction from vectors_sighash as template
var faketx = new Transaction(vectors_sighash[1][0]);

// Clear all outputs
faketx.outputs = [];
var script = new Script();

// Calculate preimage with the SIGHASH_SINGLE bug
var preimage = sighash.sighashPreimage(faketx, Signature.SIGHASH_SINGLE, 0, script)

var SIGHASH_SINGLE_BUG_RESULT = '0000000000000000000000000000000000000000000000000000000000000001';

preimage.toString('hex').should.equal(SIGHASH_SINGLE_BUG_RESULT)
});
});