trying to fix OP_CHECKSIG script evaluation
This commit is contained in:
parent
6a0512e1bf
commit
9aa6152f25
@ -879,9 +879,8 @@ ScriptInterpreter.prototype.step = function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Drop the signature, since there's no way for a signature to sign itself
|
// Drop the signature, since there's no way for a signature to sign itself
|
||||||
console.log(subscript.toString());
|
var tmpScript = Script().add(bufSig);
|
||||||
subscript.findAndDelete(Script().add(bufSig));
|
subscript.findAndDelete(tmpScript);
|
||||||
console.log(subscript.toString());
|
|
||||||
|
|
||||||
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
|
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
|
||||||
// serror is set
|
// serror is set
|
||||||
@ -890,11 +889,12 @@ ScriptInterpreter.prototype.step = function() {
|
|||||||
|
|
||||||
var fSuccess;
|
var fSuccess;
|
||||||
try {
|
try {
|
||||||
var sig = Signature().fromTxFormat(bufSig);
|
var sig = Signature.fromTxFormat(bufSig);
|
||||||
var pubkey = PublicKey().fromBuffer(bufPubkey, false);
|
var pubkey = PublicKey.fromBuffer(bufPubkey, false);
|
||||||
fSuccess = this.tx.verify(sig, pubkey, this.nin, subscript);
|
fSuccess = this.tx.verify(sig, pubkey, this.nin, subscript);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//invalid sig or pubkey
|
//invalid sig or pubkey
|
||||||
|
console.log('FALSEEEEEEEEEEEEEEEEee ' + e);
|
||||||
fSuccess = false;
|
fSuccess = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -980,8 +980,8 @@ ScriptInterpreter.prototype.step = function() {
|
|||||||
|
|
||||||
var fOk;
|
var fOk;
|
||||||
try {
|
try {
|
||||||
var sig = Signature().fromTxFormat(bufSig);
|
var sig = Signature.fromTxFormat(bufSig);
|
||||||
var pubkey = PublicKey().fromBuffer(bufPubkey, false);
|
var pubkey = PublicKey.fromBuffer(bufPubkey, false);
|
||||||
fOk = this.tx.verify(sig, pubkey, this.nin, subscript);
|
fOk = this.tx.verify(sig, pubkey, this.nin, subscript);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//invalid sig or pubkey
|
//invalid sig or pubkey
|
||||||
@ -1088,13 +1088,13 @@ ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (this.stack.length === 0) {
|
if (this.stack.length === 0) {
|
||||||
this.errstr = 'SCRIPT_ERR_EVAL_FALSE';
|
this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_RESULT';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf = this.stack[this.stack.length - 1];
|
var buf = this.stack[this.stack.length - 1];
|
||||||
if (!ScriptInterpreter.castToBool(buf)) {
|
if (!ScriptInterpreter.castToBool(buf)) {
|
||||||
this.errstr = 'SCRIPT_ERR_EVAL_FALSE';
|
this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_STACK';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1131,12 +1131,12 @@ ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (stackCopy.length === 0) {
|
if (stackCopy.length === 0) {
|
||||||
this.errstr = 'SCRIPT_ERR_EVAL_FALSE';
|
this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_P2SH_STACK';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ScriptInterpreter.castToBool(stackCopy[stackCopy.length - 1])) {
|
if (!ScriptInterpreter.castToBool(stackCopy[stackCopy.length - 1])) {
|
||||||
this.errstr = 'SCRIPT_ERR_EVAL_FALSE';
|
this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_P2SH_STACK';
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -84,6 +84,10 @@ function sighash(transaction, sighashType, inputNumber, subscript) {
|
|||||||
.write(txcopy.toBuffer())
|
.write(txcopy.toBuffer())
|
||||||
.writeInt32LE(sighashType)
|
.writeInt32LE(sighashType)
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
|
console.log('actual:');
|
||||||
|
console.log(buf.toString('hex'));
|
||||||
|
console.log('expected:');
|
||||||
|
console.log('01000000019ce5586f04dd407719ab7e2ed3583583b9022f29652702cfac5ed082013461fe0000000043410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8acffffffff010000000000000000000000000001000000');
|
||||||
return BufferReader(Hash.sha256sha256(buf)).readReverse();
|
return BufferReader(Hash.sha256sha256(buf)).readReverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,6 +101,10 @@ function sign(transaction, keypair, nhashtype, nin, subscript) {
|
|||||||
function verify(transaction, sig, pubkey, nin, subscript) {
|
function verify(transaction, sig, pubkey, nin, subscript) {
|
||||||
var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript);
|
var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript);
|
||||||
hashbuf = new BufferReader(hashbuf).readReverse();
|
hashbuf = new BufferReader(hashbuf).readReverse();
|
||||||
|
console.log('actual:');
|
||||||
|
console.log(hashbuf.toString('hex'));
|
||||||
|
console.log('expected:');
|
||||||
|
console.log('f4a222b692e7f86c299f878c4b981242238f49b467b8d990219fbf5cfc0838cd');
|
||||||
return ECDSA.verify(hashbuf, sig, pubkey, 'little');
|
return ECDSA.verify(hashbuf, sig, pubkey, 'little');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -383,4 +383,11 @@ Transaction.prototype.isValidSignature = function(signature) {
|
|||||||
return this.inputs[signature.inputIndex].isValidSignature(self, signature);
|
return this.inputs[signature.inputIndex].isValidSignature(self, signature);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {bool} whether the signature is valid for this transaction input
|
||||||
|
*/
|
||||||
|
Transaction.prototype.verify = function(sig, pubkey, nin, subscript) {
|
||||||
|
return Sighash.verify(this, sig, pubkey, nin, subscript);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Transaction;
|
module.exports = Transaction;
|
||||||
|
|||||||
@ -240,8 +240,39 @@ describe('ScriptInterpreter', function() {
|
|||||||
var scriptPubkey = Script.fromBitcoindString(vector[1]);
|
var scriptPubkey = Script.fromBitcoindString(vector[1]);
|
||||||
var flags = getFlags(vector[2]);
|
var flags = getFlags(vector[2]);
|
||||||
|
|
||||||
var spendtx = Transaction();
|
var hashbuf = new Buffer(32);
|
||||||
|
hashbuf.fill(0);
|
||||||
|
var credtx = Transaction();
|
||||||
|
//credtx.addTxin(hashbuf, 0xffffffff, Script('OP_0 OP_0'), 0xffffffff);
|
||||||
|
credtx.inputs.push(new Transaction.Input({
|
||||||
|
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
|
outputIndex: 0xffffffff,
|
||||||
|
sequenceNumber: 0xffffffff,
|
||||||
|
script: Script('OP_0 OP_0')
|
||||||
|
}));
|
||||||
|
//credtx.addTxout(BN(0), scriptPubkey);
|
||||||
|
credtx._addOutput(new Transaction.Output({
|
||||||
|
script: scriptPubkey,
|
||||||
|
satoshis: 0
|
||||||
|
}));
|
||||||
|
var idbuf = credtx.id;
|
||||||
|
//console.log('idbuf: '+idbuf);
|
||||||
|
//console.log('expef: 9ce5586f04dd407719ab7e2ed3583583b9022f29652702cfac5ed082013461fe');
|
||||||
|
|
||||||
|
|
||||||
|
var spendtx = Transaction();
|
||||||
|
//spendtx.addTxin(idbuf, 0, scriptSig, 0xffffffff);
|
||||||
|
spendtx.inputs.push(new Transaction.Input({
|
||||||
|
prevTxId: idbuf.toString('hex'),
|
||||||
|
outputIndex: 0,
|
||||||
|
sequenceNumber: 0xffffffff,
|
||||||
|
script: scriptSig
|
||||||
|
}));
|
||||||
|
//spendtx.addTxout(BN(0), Script());
|
||||||
|
credtx._addOutput(new Transaction.Output({
|
||||||
|
script: Script(),
|
||||||
|
satoshis: 0
|
||||||
|
}));
|
||||||
var interp = ScriptInterpreter();
|
var interp = ScriptInterpreter();
|
||||||
console.log(scriptSig.toString() + ' ' + scriptPubkey.toString());
|
console.log(scriptSig.toString() + ' ' + scriptPubkey.toString());
|
||||||
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user