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
});
} 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++) {
var chunk = this.chunks[i];
var opcodenum;
if (typeof chunk === 'number') {
opcodenum = chunk;
bw.writeUInt8(opcodenum);
} else {
opcodenum = chunk.opcodenum;
bw.writeUInt8(chunk.opcodenum);
var opcodenum = chunk.opcodenum;
bw.writeUInt8(chunk.opcodenum);
if (chunk.buf) {
if (opcodenum < Opcode.map.OP_PUSHDATA1) {
bw.write(chunk.buf);
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
@ -163,7 +161,9 @@ Script.fromString = function(str) {
});
i = i + 3;
} else {
script.chunks.push(opcodenum);
script.chunks.push({
opcodenum: opcodenum
});
i = i + 1;
}
}
@ -175,23 +175,25 @@ Script.prototype.toString = function() {
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
var opcodenum;
if (typeof chunk === 'number') {
opcodenum = chunk;
str = str + Opcode(opcodenum).toString() + ' ';
} else {
opcodenum = chunk.opcodenum;
if (opcodenum === Opcode.map.OP_PUSHDATA1 ||
opcodenum === Opcode.map.OP_PUSHDATA2 ||
opcodenum === Opcode.map.OP_PUSHDATA4) {
str = str + Opcode(opcodenum).toString() + ' ';
var opcodenum = chunk.opcodenum;
if (!chunk.buf) {
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
str = str + ' ' + Opcode(opcodenum).toString();
} else {
str = str + ' ' + '0x' + opcodenum.toString(16);
}
str = str + chunk.len + ' ';
str = str + '0x' + chunk.buf.toString('hex') + ' ';
} else {
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
@ -200,11 +202,14 @@ Script.prototype.toString = function() {
* @returns true if this is a pay to pubkey hash output script
*/
Script.prototype.isPublicKeyHashOut = function() {
return this.chunks[0] === Opcode('OP_DUP').toNumber() &&
this.chunks[1] === Opcode('OP_HASH160').toNumber() &&
return this.chunks[0] && this.chunks[0].opcodenum === Opcode.OP_DUP &&
this.chunks[1] &&
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
this.chunks[2].buf &&
this.chunks[3] === Opcode('OP_EQUALVERIFY').toNumber() &&
this.chunks[4] === Opcode('OP_CHECKSIG').toNumber();
this.chunks[3] &&
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
*/
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 === 2 &&
this.chunks[1].buf &&
@ -470,7 +475,7 @@ Script.prototype.removeCodeseparators = function() {
* requiring m of those public keys to spend
* @param {PublicKey[]} pubkeys - list of all public keys controlling 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
* public keys before creating the script
*/

View File

@ -22,7 +22,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf);
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() {
@ -30,7 +30,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
var script = Script.fromBuffer(buf);
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() {
@ -75,9 +75,9 @@ describe('Script', function() {
buf[buf.length - 1] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf);
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[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();
var script = Script.fromBuffer(buf);
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'));
});
@ -98,7 +98,7 @@ describe('Script', function() {
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
var script = Script.fromBuffer(buf);
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'));
});
@ -148,9 +148,9 @@ describe('Script', function() {
buf[buf.length - 1] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf);
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[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'));
});
@ -182,9 +182,9 @@ describe('Script', function() {
buf[buf.length - 1] = Opcode('OP_0').toNumber();
var script = Script.fromBuffer(buf);
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[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');
});

View File

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