Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge d9c892d into 987d1a9
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanxcharles committed Oct 19, 2018
2 parents 987d1a9 + d9c892d commit 14a0e9d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/script/script.js
Expand Up @@ -146,10 +146,23 @@ Script.fromASM = function(str) {

if (_.isUndefined(opcodenum)) {
var buf = Buffer.from(tokens[i], 'hex');
var opcodenum;
var len = buf.length
if (len >= 0 && len < Opcode.OP_PUSHDATA1) {
opcodenum = len;
} else if (len < Math.pow(2, 8)) {
opcodenum = Opcode.OP_PUSHDATA1;
} else if (len < Math.pow(2, 16)) {
opcodenum = Opcode.OP_PUSHDATA2;
} else if (len < Math.pow(2, 32)) {
opcodenum = Opcode.OP_PUSHDATA4;
} else {
throw new Error('You can\'t push that much data');
}
script.chunks.push({
buf: buf,
len: buf.length,
opcodenum: buf.length
opcodenum: opcodenum
});
i = i + 1;
} else if (opcodenum === Opcode.OP_PUSHDATA1 ||
Expand Down Expand Up @@ -462,13 +475,13 @@ Script.prototype.isMultisigIn = function() {
* @returns {boolean} true if this is a valid standard OP_RETURN output
*/
Script.prototype.isDataOut = function() {
return this.chunks.length >= 1 &&
var step1 = this.chunks.length >= 1 &&
this.chunks[0].opcodenum === Opcode.OP_RETURN &&
(this.chunks.length === 1 ||
(this.chunks.length === 2 &&
this.chunks[1].buf &&
this.chunks[1].buf.length <= Script.OP_RETURN_STANDARD_SIZE &&
this.chunks[1].length === this.chunks.len));
this.toBuffer().length <= 223 // 223 instead of 220 because (+1 for OP_RETURN, +2 for the pushdata opcodes)
if (!step1) return false
var chunks = this.chunks.slice(1)
var script2 = new Script({chunks})
return script2.isPushOnly()
};

/**
Expand Down Expand Up @@ -497,7 +510,10 @@ Script.prototype.getData = function() {
*/
Script.prototype.isPushOnly = function() {
return _.every(this.chunks, function(chunk) {
return chunk.opcodenum <= Opcode.OP_16;
return chunk.opcodenum <= Opcode.OP_16 ||
chunk.opcodenum === Opcode.OP_PUSHDATA1 ||
chunk.opcodenum === Opcode.OP_PUSHDATA2 ||
chunk.opcodenum === Opcode.OP_PUSHDATA4;
});
};

Expand Down
18 changes: 18 additions & 0 deletions test/script/script.js
Expand Up @@ -198,6 +198,19 @@ describe('Script', function() {
script.chunks[3].opcodenum.should.equal(Opcode.OP_EQUALVERIFY);
script.chunks[4].opcodenum.should.equal(Opcode.OP_CHECKSIG);
});

it('should parse this known problematic script in ASM', function () {
var asm = 'OP_RETURN 026d02 0568656c6c6f';
var script = Script.fromASM(asm);
script.toASM().should.equal(asm);
});

it('should parse this long script in ASM', function () {
var asm = 'OP_RETURN 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
var script = Script.fromASM(asm);
script.chunks[1].opcodenum.should.equal(Opcode.OP_PUSHDATA1)
script.toASM().should.equal(asm);
});
});

describe('#fromString', function() {
Expand Down Expand Up @@ -265,11 +278,16 @@ describe('Script', function() {
Script('OP_RETURN').isDataOut().should.equal(true);
});

it('validates that this two part OP_RETURN is standard', function() {
Script.fromASM('OP_RETURN 026d02 0568656c6c6f').isDataOut().should.equal(true);
});

it('validates that this 40-byte OP_RETURN is standard', function() {
var buf = new Buffer(40);
buf.fill(0);
Script('OP_RETURN 40 0x' + buf.toString('hex')).isDataOut().should.equal(true);
});

it('validates that this 80-byte OP_RETURN is standard', function() {
var buf = new Buffer(80);
buf.fill(0);
Expand Down

0 comments on commit 14a0e9d

Please sign in to comment.