Browse Source

fixed Script parse tests for all scripts (valid and invalid)

patch-2
Manuel Araoz 11 years ago
parent
commit
dc56cb8d45
  1. 5
      Script.js
  2. 2
      ScriptInterpreter.js
  3. 2
      test/test.Script.js
  4. 19
      util/util.js

5
Script.js

@ -270,13 +270,14 @@ function spec(b) {
var split = s.split(' '); var split = s.split(' ');
for (var i = 0; i < split.length; i++) { for (var i = 0; i < split.length; i++) {
var word = split[i]; var word = split[i];
if (word === '') continue;
if (word.length > 2 && word.substring(0, 2) === '0x') { if (word.length > 2 && word.substring(0, 2) === '0x') {
// raw hex value // raw hex value
//console.log('hex value'); //console.log('hex value');
chunks.push(new Buffer(word.substring(2, word.length), 'hex')); chunks.push(new Buffer(word.substring(2, word.length), 'hex'));
} else { } else {
var opcode = Opcode.map['OP_' + word]; var opcode = Opcode.map['OP_' + word];
if (opcode) { if (typeof opcode !== 'undefined') {
// op code in string form // op code in string form
//console.log('opcode'); //console.log('opcode');
chunks.push(opcode); chunks.push(opcode);
@ -297,7 +298,7 @@ function spec(b) {
} }
chunks.push(new Buffer(hex,'hex')); chunks.push(new Buffer(hex,'hex'));
} else { } else {
throw new Error('Could not parse word from script: ' +word); throw new Error('Could not parse word "' +word+'" from script "'+s+'"');
} }
} }
} }

2
ScriptInterpreter.js

@ -393,6 +393,8 @@ function spec(b) {
// (x1 x2 - bool) // (x1 x2 - bool)
var v1 = this.stackTop(2); var v1 = this.stackTop(2);
var v2 = this.stackTop(1); var v2 = this.stackTop(1);
console.log(v1);
console.log(v2);
var value = buffertools.compare(v1, v2) === 0; var value = buffertools.compare(v1, v2) === 0;
// OP_NOTEQUAL is disabled because it would be too easy to say // OP_NOTEQUAL is disabled because it would be too easy to say

2
test/test.Script.js

@ -84,7 +84,7 @@ describe('Script', function() {
}); });
}); });
test_data.dataScriptValid.forEach(function(datum) { test_data.dataScriptAll.forEach(function(datum) {
if (datum.length < 2) throw new Error('Invalid test data'); if (datum.length < 2) throw new Error('Invalid test data');
var human = datum[0] + ' ' + datum[1]; var human = datum[0] + ' ' + datum[1];
it('should parse script from human readable ' + human, function() { it('should parse script from human readable ' + human, function() {

19
util/util.js

@ -111,18 +111,27 @@ var intTo64Bits = function(integer) {
lo: (integer & 0xFFFFFFFF) >>> 0 lo: (integer & 0xFFFFFFFF) >>> 0
}; };
}; };
var fitsIn32Bits = function(integer) { var fitsInNBits = function(integer, n) {
// TODO: make this efficient!!! // TODO: make this efficient!!!
return integer.toString(2).replace('-','').length < 32; return integer.toString(2).replace('-','').length < n;
} }
exports.intToBuffer = function(integer) { exports.intToBuffer = function(integer) {
if (fitsIn32Bits(integer)) { var data = null;
var data = new Buffer(4); if (fitsInNBits(integer, 8)) {
data = new Buffer(1);
data.writeInt8(integer, 0);
return data;
} else if (fitsInNBits(integer, 16)) {
data = new Buffer(2);
data.writeInt16LE(integer, 0);
return data;
} else if (fitsInNBits(integer, 32)) {
data = new Buffer(4);
data.writeInt32LE(integer, 0); data.writeInt32LE(integer, 0);
return data; return data;
} else { } else {
var x = intTo64Bits(integer); var x = intTo64Bits(integer);
var data = new Buffer(8); data = new Buffer(8);
data.writeInt32LE(x.hi, 0); // high part contains sign information (signed) data.writeInt32LE(x.hi, 0); // high part contains sign information (signed)
data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer
return data; return data;

Loading…
Cancel
Save