fixing tests for new script internals

This commit is contained in:
Manuel Araoz 2014-12-11 10:27:20 -03:00
parent 898bdac05f
commit 016bc6e3ed
3 changed files with 43 additions and 40 deletions

View File

@ -87,7 +87,9 @@ Script.fromBuffer = function(buffer) {
opcodenum: opcodenum opcodenum: opcodenum
}); });
} else { } else {
script.chunks.push(opcodenum); script.chunks.push({
opcodenum: opcodenum
});
} }
} }
@ -99,13 +101,9 @@ Script.prototype.toBuffer = function() {
for (var i = 0; i < this.chunks.length; i++) { for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i]; var chunk = this.chunks[i];
var opcodenum; var opcodenum = chunk.opcodenum;
if (typeof chunk === 'number') { bw.writeUInt8(chunk.opcodenum);
opcodenum = chunk; if (chunk.buf) {
bw.writeUInt8(opcodenum);
} else {
opcodenum = chunk.opcodenum;
bw.writeUInt8(chunk.opcodenum);
if (opcodenum < Opcode.map.OP_PUSHDATA1) { if (opcodenum < Opcode.map.OP_PUSHDATA1) {
bw.write(chunk.buf); bw.write(chunk.buf);
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) { } else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
@ -163,7 +161,9 @@ Script.fromString = function(str) {
}); });
i = i + 3; i = i + 3;
} else { } else {
script.chunks.push(opcodenum); script.chunks.push({
opcodenum: opcodenum
});
i = i + 1; i = i + 1;
} }
} }
@ -175,23 +175,25 @@ Script.prototype.toString = function() {
for (var i = 0; i < this.chunks.length; i++) { for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i]; var chunk = this.chunks[i];
var opcodenum; var opcodenum = chunk.opcodenum;
if (typeof chunk === 'number') { if (!chunk.buf) {
opcodenum = chunk; if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
str = str + Opcode(opcodenum).toString() + ' '; str = str + ' ' + Opcode(opcodenum).toString();
} else { } else {
opcodenum = chunk.opcodenum; str = str + ' ' + '0x' + opcodenum.toString(16);
if (opcodenum === Opcode.map.OP_PUSHDATA1 ||
opcodenum === Opcode.map.OP_PUSHDATA2 ||
opcodenum === Opcode.map.OP_PUSHDATA4) {
str = str + Opcode(opcodenum).toString() + ' ';
} }
str = str + chunk.len + ' '; } else {
str = str + '0x' + chunk.buf.toString('hex') + ' '; if (opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) {
str = str + ' ' + Opcode(opcodenum).toString();
}
str = str + ' ' + chunk.len;
str = str + ' ' + '0x' + chunk.buf.toString('hex');
} }
} }
return str.substr(0, str.length - 1); return str.substr(1);
}; };
// script classification methods // script classification methods
@ -200,11 +202,14 @@ Script.prototype.toString = function() {
* @returns true if this is a pay to pubkey hash output script * @returns true if this is a pay to pubkey hash output script
*/ */
Script.prototype.isPublicKeyHashOut = function() { Script.prototype.isPublicKeyHashOut = function() {
return this.chunks[0] === Opcode('OP_DUP').toNumber() && return this.chunks[0] && this.chunks[0].opcodenum === Opcode.OP_DUP &&
this.chunks[1] === Opcode('OP_HASH160').toNumber() && this.chunks[1] &&
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
this.chunks[2].buf && this.chunks[2].buf &&
this.chunks[3] === Opcode('OP_EQUALVERIFY').toNumber() && this.chunks[3] &&
this.chunks[4] === Opcode('OP_CHECKSIG').toNumber(); this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
this.chunks[4] &&
this.chunks[4].opcodenum === Opcode.OP_CHECKSIG;
}; };
/** /**
@ -301,7 +306,7 @@ Script.prototype.isMultisigIn = function() {
* @returns true if this is an OP_RETURN data script * @returns true if this is an OP_RETURN data script
*/ */
Script.prototype.isDataOut = function() { Script.prototype.isDataOut = function() {
return (this.chunks[0] === Opcode('OP_RETURN').toNumber() && return (this.chunks[0].opcodenum === Opcode('OP_RETURN').toNumber() &&
(this.chunks.length === 1 || (this.chunks.length === 1 ||
(this.chunks.length === 2 && (this.chunks.length === 2 &&
this.chunks[1].buf && this.chunks[1].buf &&
@ -470,7 +475,7 @@ Script.prototype.removeCodeseparators = function() {
* requiring m of those public keys to spend * requiring m of those public keys to spend
* @param {PublicKey[]} pubkeys - list of all public keys controlling the output * @param {PublicKey[]} pubkeys - list of all public keys controlling the output
* @param {number} m - amount of required signatures to spend the output * @param {number} m - amount of required signatures to spend the output
* @param {Object} [opts] - Several options: * @param {Object} [opts] - Several options:
* - noSorting: defaults to false, if true, don't sort the given * - noSorting: defaults to false, if true, don't sort the given
* public keys before creating the script * public keys before creating the script
*/ */

View File

@ -22,7 +22,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
}); });
it('should parse this buffer containing another OP code', function() { it('should parse this buffer containing another OP code', function() {
@ -30,7 +30,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber(); buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
}); });
it('should parse this buffer containing three bytes of data', function() { it('should parse this buffer containing three bytes of data', function() {
@ -75,9 +75,9 @@ describe('Script', function() {
buf[buf.length - 1] = Opcode('OP_0').toNumber(); buf[buf.length - 1] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3); script.chunks.length.should.equal(3);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
script.chunks[1].buf.toString('hex').should.equal('010203'); script.chunks[1].buf.toString('hex').should.equal('010203');
script.chunks[2].should.equal(buf[buf.length - 1]); script.chunks[2].opcodenum.should.equal(buf[buf.length - 1]);
}); });
}); });
@ -89,7 +89,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
script.toBuffer().toString('hex').should.equal(buf.toString('hex')); script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
}); });
@ -98,7 +98,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber(); buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
script.toBuffer().toString('hex').should.equal(buf.toString('hex')); script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
}); });
@ -148,9 +148,9 @@ describe('Script', function() {
buf[buf.length - 1] = Opcode('OP_0').toNumber(); buf[buf.length - 1] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3); script.chunks.length.should.equal(3);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
script.chunks[1].buf.toString('hex').should.equal('010203'); script.chunks[1].buf.toString('hex').should.equal('010203');
script.chunks[2].should.equal(buf[buf.length - 1]); script.chunks[2].opcodenum.should.equal(buf[buf.length - 1]);
script.toBuffer().toString('hex').should.equal(buf.toString('hex')); script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
}); });
@ -182,9 +182,9 @@ describe('Script', function() {
buf[buf.length - 1] = Opcode('OP_0').toNumber(); buf[buf.length - 1] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3); script.chunks.length.should.equal(3);
script.chunks[0].should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
script.chunks[1].buf.toString('hex').should.equal('010203'); script.chunks[1].buf.toString('hex').should.equal('010203');
script.chunks[2].should.equal(buf[buf.length - 1]); script.chunks[2].opcodenum.should.equal(buf[buf.length - 1]);
script.toString().toString('hex').should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0'); script.toString().toString('hex').should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0');
}); });

View File

@ -58,9 +58,7 @@ describe('ScriptInterpreter', function() {
var verified; var verified;
var si = ScriptInterpreter(); var si = ScriptInterpreter();
verified = si.verify(Script('OP_1'), Script('OP_1')); verified = si.verify(Script('OP_1'), Script('OP_1'));
console.log(si.errstr);
verified.should.equal(true); verified.should.equal(true);
var
verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_0')); verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_0'));
verified.should.equal(false); verified.should.equal(false);
verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_1')); verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_1'));