add sighash tests
This commit is contained in:
parent
72570719d8
commit
b020fe1e80
|
@ -445,15 +445,7 @@ Transaction.prototype.hashForSignature =
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone transaction
|
// Clone transaction
|
||||||
var txTmp = new Transaction();
|
var txTmp = new Transaction(this);
|
||||||
this.ins.forEach(function(txin, i) {
|
|
||||||
txTmp.ins.push(new TransactionIn(txin));
|
|
||||||
});
|
|
||||||
this.outs.forEach(function(txout) {
|
|
||||||
txTmp.outs.push(new TransactionOut(txout));
|
|
||||||
});
|
|
||||||
txTmp.version = this.version;
|
|
||||||
txTmp.lock_time = this.lock_time;
|
|
||||||
|
|
||||||
// In case concatenating two scripts ends up with two codeseparators,
|
// In case concatenating two scripts ends up with two codeseparators,
|
||||||
// or an extra one at the end, this prevents all those possible
|
// or an extra one at the end, this prevents all those possible
|
||||||
|
@ -505,10 +497,13 @@ Transaction.prototype.hashForSignature =
|
||||||
} else {
|
} else {
|
||||||
var outsLen;
|
var outsLen;
|
||||||
if (hashTypeMode === SIGHASH_SINGLE) {
|
if (hashTypeMode === SIGHASH_SINGLE) {
|
||||||
// TODO: Untested
|
|
||||||
if (inIndex >= txTmp.outs.length) {
|
if (inIndex >= txTmp.outs.length) {
|
||||||
throw new Error("Transaction.hashForSignature(): SIGHASH_SINGLE " +
|
// bug present in bitcoind which must be also present in bitcore
|
||||||
"no corresponding txout found - out of bounds");
|
// Transaction.hashForSignature(): SIGHASH_SINGLE
|
||||||
|
// no corresponding txout found - out of bounds
|
||||||
|
var ret = new Buffer(1);
|
||||||
|
ret.writeUInt8(1, 0);
|
||||||
|
return ret; // return 1 bug
|
||||||
}
|
}
|
||||||
outsLen = inIndex + 1;
|
outsLen = inIndex + 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -9,9 +9,20 @@ var bitcore = bitcore || require('../bitcore');
|
||||||
var Transaction = bitcore.Transaction;
|
var Transaction = bitcore.Transaction;
|
||||||
var Script = bitcore.Script;
|
var Script = bitcore.Script;
|
||||||
var Opcode = bitcore.Opcode;
|
var Opcode = bitcore.Opcode;
|
||||||
|
var util = bitcore.util;
|
||||||
|
var Put = bitcore.Put;
|
||||||
|
var Put = require('bufferput');
|
||||||
|
var buffertools = require('buffertools');
|
||||||
|
|
||||||
|
var seed = 1;
|
||||||
|
// seedable pseudo-random function
|
||||||
|
var random = function() {
|
||||||
|
var x = Math.sin(seed++) * 10000;
|
||||||
|
return x - Math.floor(x);
|
||||||
|
};
|
||||||
|
|
||||||
var randInt = function(low, high) {
|
var randInt = function(low, high) {
|
||||||
return Math.floor(Math.random() * (high - low + 1) + low);
|
return Math.floor(random() * (high - low + 1) + low);
|
||||||
};
|
};
|
||||||
var randUIntN = function(nBits) {
|
var randUIntN = function(nBits) {
|
||||||
return randInt(0, Math.pow(2, nBits));
|
return randInt(0, Math.pow(2, nBits));
|
||||||
|
@ -20,7 +31,7 @@ var randUInt32 = function() {
|
||||||
return randUIntN(32);
|
return randUIntN(32);
|
||||||
};
|
};
|
||||||
var randBool = function() {
|
var randBool = function() {
|
||||||
return Math.random() < 0.5;
|
return random() < 0.5;
|
||||||
};
|
};
|
||||||
var hexAlphabet = '0123456789abcdef';
|
var hexAlphabet = '0123456789abcdef';
|
||||||
var randHex = function() {
|
var randHex = function() {
|
||||||
|
@ -37,7 +48,7 @@ var randTxHash = function() {
|
||||||
return randHexN(64);
|
return randHexN(64);
|
||||||
};
|
};
|
||||||
var randPick = function(list) {
|
var randPick = function(list) {
|
||||||
return list[randInt(0, list.length-1)];
|
return list[randInt(0, list.length - 1)];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,8 +56,8 @@ var opList = Opcode.asList();
|
||||||
|
|
||||||
var randomScript = function() {
|
var randomScript = function() {
|
||||||
var s = new Script();
|
var s = new Script();
|
||||||
var ops = randInt(0,10);
|
var ops = randInt(0, 10);
|
||||||
for (var i=0; i<ops; i++) {
|
for (var i = 0; i < ops; i++) {
|
||||||
var op = randPick(opList);
|
var op = randPick(opList);
|
||||||
s.writeOp(Opcode.map[op]);
|
s.writeOp(Opcode.map[op]);
|
||||||
}
|
}
|
||||||
|
@ -70,8 +81,8 @@ var randomTx = function(single) {
|
||||||
tx.ins.push(txin);
|
tx.ins.push(txin);
|
||||||
}
|
}
|
||||||
for (i = 0; i < outsN; i++) {
|
for (i = 0; i < outsN; i++) {
|
||||||
var txout = Transaction.Out({
|
var txout = new Transaction.Out({
|
||||||
value: randInt(0,100000000),
|
value: new Buffer(8),
|
||||||
script: randomScript().serialize()
|
script: randomScript().serialize()
|
||||||
});
|
});
|
||||||
tx.outs.push(txout);
|
tx.outs.push(txout);
|
||||||
|
@ -79,13 +90,136 @@ var randomTx = function(single) {
|
||||||
return tx;
|
return tx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var signatureHashOld = function(tx, script, inIndex, hashType) {
|
||||||
|
if (+inIndex !== inIndex ||
|
||||||
|
inIndex < 0 || inIndex >= tx.ins.length) {
|
||||||
|
throw new Error('Input index "' + inIndex + '" invalid or out of bounds ' +
|
||||||
|
'(' + tx.ins.length + ' inputs)');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clone transaction
|
||||||
|
var txTmp = new Transaction();
|
||||||
|
tx.ins.forEach(function(txin) {
|
||||||
|
txTmp.ins.push(new Transaction.In(txin));
|
||||||
|
});
|
||||||
|
tx.outs.forEach(function(txout) {
|
||||||
|
txTmp.outs.push(new Transaction.Out(txout));
|
||||||
|
});
|
||||||
|
txTmp.version = tx.version;
|
||||||
|
txTmp.lock_time = tx.lock_time;
|
||||||
|
|
||||||
|
// In case concatenating two scripts ends up with two codeseparators,
|
||||||
|
// or an extra one at the end, this prevents all those possible
|
||||||
|
// incompatibilities.
|
||||||
|
script.findAndDelete(Opcode.map.OP_CODESEPARATOR);
|
||||||
|
|
||||||
|
// Get mode portion of hashtype
|
||||||
|
var hashTypeMode = hashType & 0x1f;
|
||||||
|
|
||||||
|
// Generate modified transaction data for hash
|
||||||
|
var bytes = (new Put());
|
||||||
|
bytes.word32le(tx.version);
|
||||||
|
|
||||||
|
// Serialize inputs
|
||||||
|
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
|
||||||
|
// Blank out all inputs except current one, not recommended for open
|
||||||
|
// transactions.
|
||||||
|
bytes.varint(1);
|
||||||
|
bytes.put(tx.ins[inIndex].o);
|
||||||
|
bytes.varint(script.buffer.length);
|
||||||
|
bytes.put(script.buffer);
|
||||||
|
bytes.word32le(tx.ins[inIndex].q);
|
||||||
|
} else {
|
||||||
|
bytes.varint(tx.ins.length);
|
||||||
|
for (var i = 0, l = tx.ins.length; i < l; i++) {
|
||||||
|
var txin = tx.ins[i];
|
||||||
|
bytes.put(txin.o);
|
||||||
|
|
||||||
|
// Current input's script gets set to the script to be signed, all others
|
||||||
|
// get blanked.
|
||||||
|
if (inIndex === i) {
|
||||||
|
bytes.varint(script.buffer.length);
|
||||||
|
bytes.put(script.buffer);
|
||||||
|
} else {
|
||||||
|
bytes.varint(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hashTypeMode === Transaction.SIGHASH_NONE && inIndex !== i) {
|
||||||
|
bytes.word32le(0);
|
||||||
|
} else {
|
||||||
|
bytes.word32le(tx.ins[i].q);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize outputs
|
||||||
|
if (hashTypeMode === Transaction.SIGHASH_NONE) {
|
||||||
|
bytes.varint(0);
|
||||||
|
} else {
|
||||||
|
var outsLen;
|
||||||
|
if (hashTypeMode === Transaction.SIGHASH_SINGLE) {
|
||||||
|
if (inIndex >= txTmp.outs.length) {
|
||||||
|
// bug present in bitcoind which must be also present in bitcore
|
||||||
|
// Transaction.hashForSignature(): SIGHASH_SINGLE
|
||||||
|
// no corresponding txout found - out of bounds
|
||||||
|
var ret = new Buffer(1);
|
||||||
|
ret.writeUInt8(1, 0);
|
||||||
|
return ret; // return 1 bug
|
||||||
|
}
|
||||||
|
outsLen = inIndex + 1;
|
||||||
|
} else {
|
||||||
|
outsLen = tx.outs.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes.varint(outsLen);
|
||||||
|
for (var i = 0; i < outsLen; i++) {
|
||||||
|
if (hashTypeMode === Transaction.SIGHASH_SINGLE && i !== inIndex) {
|
||||||
|
// Zero all outs except the one we want to keep
|
||||||
|
bytes.put(util.INT64_MAX);
|
||||||
|
bytes.varint(0);
|
||||||
|
} else {
|
||||||
|
bytes.put(tx.outs[i].v);
|
||||||
|
bytes.varint(tx.outs[i].s.length);
|
||||||
|
bytes.put(tx.outs[i].s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes.word32le(tx.lock_time);
|
||||||
|
|
||||||
|
var buffer = bytes.buffer();
|
||||||
|
|
||||||
|
// Append hashType
|
||||||
|
buffer = Buffer.concat([buffer, new Buffer([parseInt(hashType), 0, 0, 0])]);
|
||||||
|
|
||||||
|
return util.twoSha256(buffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
describe('Transaction sighash (#hashForSignature)', function() {
|
describe('Transaction sighash (#hashForSignature)', function() {
|
||||||
for (var i = 0; i < 10; i++) {
|
for (var i = 0; i < 250; i++) {
|
||||||
it('should hash correctly random tx #'+(i+1), function() {
|
it('should hash correctly random tx #' + (i + 1), function() {
|
||||||
var tx = randomTx();
|
var tx = randomTx();
|
||||||
console.log(tx);
|
var l = tx.ins.length;
|
||||||
tx.hashForSignature(script, inIndex, hashType);
|
for (var i = 0; i < l; i++) {
|
||||||
return;
|
var script = randomScript();
|
||||||
|
var hashType = randUInt32();
|
||||||
|
var h = buffertools.toHex(tx.hashForSignature(script, i, hashType));
|
||||||
|
var oh = buffertools.toHex(signatureHashOld(tx, script, i, hashType));
|
||||||
|
h.should.equal(oh);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue