Skip to content

Commit

Permalink
fixed Script parse tests for all scripts (valid and invalid)
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Mar 7, 2014
1 parent e83590f commit dc56cb8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,14 @@ function spec(b) {
var split = s.split(' ');
for (var i = 0; i < split.length; i++) {
var word = split[i];
if (word === '') continue;
if (word.length > 2 && word.substring(0, 2) === '0x') {
// raw hex value
//console.log('hex value');
chunks.push(new Buffer(word.substring(2, word.length), 'hex'));
} else {
var opcode = Opcode.map['OP_' + word];
if (opcode) {
if (typeof opcode !== 'undefined') {
// op code in string form
//console.log('opcode');
chunks.push(opcode);
Expand All @@ -297,7 +298,7 @@ function spec(b) {
}
chunks.push(new Buffer(hex,'hex'));
} else {
throw new Error('Could not parse word from script: ' +word);
throw new Error('Could not parse word "' +word+'" from script "'+s+'"');
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions ScriptInterpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ function spec(b) {
// (x1 x2 - bool)
var v1 = this.stackTop(2);
var v2 = this.stackTop(1);
console.log(v1);
console.log(v2);
var value = buffertools.compare(v1, v2) === 0;

// OP_NOTEQUAL is disabled because it would be too easy to say
Expand Down
2 changes: 1 addition & 1 deletion test/test.Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
var human = datum[0] + ' ' + datum[1];
it('should parse script from human readable ' + human, function() {
Expand Down
19 changes: 14 additions & 5 deletions util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,27 @@ var intTo64Bits = function(integer) {
lo: (integer & 0xFFFFFFFF) >>> 0
};
};
var fitsIn32Bits = function(integer) {
var fitsInNBits = function(integer, n) {
// TODO: make this efficient!!!
return integer.toString(2).replace('-','').length < 32;
return integer.toString(2).replace('-','').length < n;
}
exports.intToBuffer = function(integer) {
if (fitsIn32Bits(integer)) {
var data = new Buffer(4);
var data = null;
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);
return data;
} else {
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.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer
return data;
Expand Down

0 comments on commit dc56cb8

Please sign in to comment.