Browse Source

working towards script interpreting

patch-2
Manuel Araoz 10 years ago
parent
commit
898bdac05f
  1. 17
      lib/script_interpreter.js
  2. 24
      test/script_interpreter.js

17
lib/script_interpreter.js

@ -1,5 +1,7 @@
'use strict';
var _ = require('lodash');
var Script = require('./script');
var Opcode = require('./opcode');
var BN = require('./crypto/bn');
@ -8,6 +10,7 @@ var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Signature = require('./crypto/signature');
var PublicKey = require('./publickey');
var Transaction = require('./transaction');
/**
* Bitcoin transactions contain scripts. Each input has a script called the
@ -198,7 +201,7 @@ ScriptInterpreter.prototype.step = function() {
var fRequireMinimal = (this.flags & ScriptInterpreter.SCRIPT_VERIFY_MINIMALDATA) != 0;
//bool fExec = !count(vfExec.begin(), vfExec.end(), false);
var fExec = !(this.vfExec.indexOf(false) + 1);
var fExec = (this.vfExec.indexOf(false) !== -1);
//
// Read instruction
@ -206,9 +209,8 @@ ScriptInterpreter.prototype.step = function() {
var chunk = this.script.chunks[this.pc];
this.pc++;
var opcodenum = chunk.opcodenum;
if (typeof opcodenum === 'undefined') {
this.errstr = 'SCRIPT_ERR_BAD_OPCODE';
return false;
if (_.isUndefined(opcodenum)) {
opcodenum = chunk;
}
if (chunk.buf && chunk.buf.length > ScriptInterpreter.MAX_SCRIPT_ELEMENT_SIZE) {
this.errstr = 'SCRIPT_ERR_PUSH_SIZE';
@ -1016,7 +1018,6 @@ ScriptInterpreter.prototype.step = function() {
}
this.stack.pop();
// stack.push_back(fSuccess ? vchTrue : vchFalse);
this.stack.push(fSuccess ? ScriptInterpreter.true : ScriptInterpreter.false);
if (opcodenum === Opcode.OP_CHECKMULTISIGVERIFY) {
@ -1042,6 +1043,12 @@ ScriptInterpreter.prototype.step = function() {
* Translated from bitcoind's VerifyScript
*/
ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin, flags) {
if (_.isUndefined(tx)) {
tx = new Transaction();
}
if (_.isUndefined(nin)) {
nin = 0;
}
this.set({
script: scriptSig,
tx: tx,

24
test/script_interpreter.js

@ -54,24 +54,28 @@ describe('ScriptInterpreter', function() {
describe('#verify', function() {
it('should verify or unverify these trivial scripts from script_valid.json', function() {
var verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_1'), Transaction(), 0);
it('should verify these trivial scripts', function() {
var verified;
var si = ScriptInterpreter();
verified = si.verify(Script('OP_1'), Script('OP_1'));
console.log(si.errstr);
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_0'), Transaction(), 0);
var
verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_0'));
verified.should.equal(false);
verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_1'), Transaction(), 0);
verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_1'));
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script('OP_CODESEPARATOR'), Script('OP_1'), Transaction(), 0);
verified = ScriptInterpreter().verify(Script('OP_CODESEPARATOR'), Script('OP_1'));
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script(''), Script('OP_DEPTH OP_0 OP_EQUAL'), Transaction(), 0);
verified = ScriptInterpreter().verify(Script(''), Script('OP_DEPTH OP_0 OP_EQUAL'));
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script('OP_1 OP_2'), Script('OP_2 OP_EQUALVERIFY OP_1 OP_EQUAL'), Transaction(), 0);
verified = ScriptInterpreter().verify(Script('OP_1 OP_2'), Script('OP_2 OP_EQUALVERIFY OP_1 OP_EQUAL'));
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script('9 0x000000000000000010'), Script(''), Transaction(), 0);
verified = ScriptInterpreter().verify(Script('9 0x000000000000000010'), Script(''));
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_15 OP_ADD OP_16 OP_EQUAL'), Transaction(), 0);
verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_15 OP_ADD OP_16 OP_EQUAL'));
verified.should.equal(true);
verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_IF OP_VER OP_ELSE OP_1 OP_ENDIF'), Transaction(), 0);
verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_IF OP_VER OP_ELSE OP_1 OP_ENDIF'));
verified.should.equal(true);
});

Loading…
Cancel
Save