fixed SignatureHash tests!!!!!

This commit is contained in:
Manuel Araoz 2014-04-03 15:44:15 -03:00
parent 3886bfe923
commit 1119b6f9e7
2 changed files with 25 additions and 7 deletions

View File

@ -376,6 +376,7 @@ Script.prototype.findAndDelete = function(chunk) {
if (Buffer.isBuffer(this.chunks[i]) && if (Buffer.isBuffer(this.chunks[i]) &&
buffertools.compare(this.chunks[i], chunk) === 0) { buffertools.compare(this.chunks[i], chunk) === 0) {
this.chunks.splice(i, 1); this.chunks.splice(i, 1);
i--;
dirty = true; dirty = true;
} }
} }
@ -383,6 +384,7 @@ Script.prototype.findAndDelete = function(chunk) {
for (var i = 0, l = this.chunks.length; i < l; i++) { for (var i = 0, l = this.chunks.length; i < l; i++) {
if (this.chunks[i] === chunk) { if (this.chunks[i] === chunk) {
this.chunks.splice(i, 1); this.chunks.splice(i, 1);
i--;
dirty = true; dirty = true;
} }
} }

View File

@ -331,14 +331,18 @@ TransactionSignatureSerializer.prototype.serializeOutput = function(nOutput) {
this.bytes.varint(out.s.length); this.bytes.varint(out.s.length);
this.bytes.put(out.s); this.bytes.put(out.s);
} }
console.log('after output '+nOutput+': '+buffertools.toHex(this.bytes.buffer()));
}; };
// serialize the script // serialize the script
TransactionSignatureSerializer.prototype.serializeScriptCode = function() { TransactionSignatureSerializer.prototype.serializeScriptCode = function() {
console.log('scriptCode='+this.scriptCode);
this.scriptCode.findAndDelete(OP_CODESEPARATOR); this.scriptCode.findAndDelete(OP_CODESEPARATOR);
console.log('scriptCode='+this.scriptCode);
this.bytes.varint(this.scriptCode.buffer.length); this.bytes.varint(this.scriptCode.buffer.length);
console.log('after varint: '+buffertools.toHex(this.bytes.buffer()));
this.bytes.put(this.scriptCode.buffer); this.bytes.put(this.scriptCode.buffer);
console.log('after script: '+buffertools.toHex(this.bytes.buffer()));
}; };
// serialize an input of txTo // serialize an input of txTo
@ -348,6 +352,7 @@ TransactionSignatureSerializer.prototype.serializeInput = function(nInput) {
// Serialize the prevout // Serialize the prevout
this.bytes.put(this.txTo.ins[nInput].o); this.bytes.put(this.txTo.ins[nInput].o);
console.log('after prevout: '+buffertools.toHex(this.bytes.buffer()));
// Serialize the script // Serialize the script
if (nInput !== this.nIn) { if (nInput !== this.nIn) {
@ -363,6 +368,7 @@ TransactionSignatureSerializer.prototype.serializeInput = function(nInput) {
} else { } else {
this.bytes.word32le(this.txTo.ins[nInput].q); this.bytes.word32le(this.txTo.ins[nInput].q);
} }
console.log('after input '+nInput+': '+buffertools.toHex(this.bytes.buffer()));
}; };
@ -371,22 +377,28 @@ TransactionSignatureSerializer.prototype.serializeInput = function(nInput) {
TransactionSignatureSerializer.prototype.serialize = function() { TransactionSignatureSerializer.prototype.serialize = function() {
// serialize nVersion // serialize nVersion
this.bytes.word32le(this.txTo.version); this.bytes.word32le(this.txTo.version);
console.log(buffertools.toHex(this.bytes.buffer())+' after version');
// serialize vin // serialize vin
var nInputs = this.anyoneCanPay ? 1 : this.txTo.ins.length; var nInputs = this.anyoneCanPay ? 1 : this.txTo.ins.length;
console.log('nInputs '+nInputs);
this.bytes.varint(nInputs); this.bytes.varint(nInputs);
console.log(buffertools.toHex(this.bytes.buffer())+' after nInputs');
for (var nInput = 0; nInput < nInputs; nInput++) { for (var nInput = 0; nInput < nInputs; nInput++) {
this.serializeInput(nInput); this.serializeInput(nInput);
} }
console.log(buffertools.toHex(this.bytes.buffer())+' after inputs');
// serialize vout // serialize vout
var nOutputs = this.hashNone ? 0 : (this.hashSingle ? this.nIn + 1 : this.txTo.outs.length); var nOutputs = this.hashNone ? 0 : (this.hashSingle ? this.nIn + 1 : this.txTo.outs.length);
this.bytes.varint(nOutputs); this.bytes.varint(nOutputs);
console.log(buffertools.toHex(this.bytes.buffer())+' after nOutputs');
console.log('nOutputs = '+nOutputs);
for (var nOutput = 0; nOutput < nOutputs; nOutput++) { for (var nOutput = 0; nOutput < nOutputs; nOutput++) {
this.serializeOutput(nOutput); this.serializeOutput(nOutput);
} }
console.log(buffertools.toHex(this.bytes.buffer())+' after outputs');
// serialize nLockTime // serialize nLockTime
this.bytes.word32le(this.txTo.lock_time); this.bytes.word32le(this.txTo.lock_time);
console.log(buffertools.toHex(this.bytes.buffer())+' after lock_time');
}; };
TransactionSignatureSerializer.prototype.buffer = function() { TransactionSignatureSerializer.prototype.buffer = function() {
@ -417,14 +429,18 @@ Transaction.prototype.hashForSignature =
var hashBuf = new Put().word32le(hashType).buffer(); var hashBuf = new Put().word32le(hashType).buffer();
buffer = Buffer.concat([buffer, hashBuf]); buffer = Buffer.concat([buffer, hashBuf]);
var bth = buffertools.toHex(buffer); var bth = buffertools.toHex(buffer);
//console.log('tx sig b ' + bth); console.log('tx sig b ' + bth);
var expected = '907c2bc503ade11cc3b04eb2918b6f547b0630ab569273824748c87ea14b0696526c66ba740200000000fd1f9bdd4ef073c7afc4ae00da8a66f429c917a0081ad1e1dabce28d373eab81d8628de80200000000ad042b5f25efb33beec9f3364e8a9139e8439d9d7e26529c3c30b6c3fd89f8684cfd68ea0200000000599ac2fe02a526ed040000000008535300516352515164370e010000000003006300ab2ec2291fe51c6f'; var expected = 'f40a750701b5788174aef79788716f96af779d7959147a0c2e0e5bfb6c2dba2df5b4b978940300000004005163acffffffff0445e6fd0200000000096aac536365526a526aa6546b000000000008acab656a6552535141a0fd010000000000c897ea030000000008526500ab526a6a631b39dba395e20496';
//console.log('expected '+expected); var rawtx = 'f40a750702af06efff3ea68e5d56e42bc41cdb8b6065c98f1221fe04a325a898cb61f3d7ee030000000363acacffffffffb5788174aef79788716f96af779d7959147a0c2e0e5bfb6c2dba2df5b4b97894030000000965510065535163ac6affffffff0445e6fd0200000000096aac536365526a526aa6546b000000000008acab656a6552535141a0fd010000000000c897ea030000000008526500ab526a6a631b39dba3';
console.log('expected '+expected);
for (var i=0; i<expected.length/2;i++) { for (var i=0; i<expected.length/2;i++) {
var byt = expected.substring(i*2, i*2+2); var byt = expected.substring(i*2, i*2+2);
var rbyt = bth.substring(i*2, i*2+2); var rbyt = bth.substring(i*2, i*2+2);
//if (byt !== rbyt) throw new Error(byt +'!='+rbyt+' on pos '+i); var interest = buffertools.toHex(this.serialize()) === rawtx;
//console.log(byt+ ' OK at '+i); if (interest) {
if (byt !== rbyt) throw new Error(byt +'!='+rbyt+' on pos '+i);
//console.log(byt+ ' OK at '+i);
}
} }
return buffertools.reverse(util.twoSha256(buffer)); return buffertools.reverse(util.twoSha256(buffer));