isOpReturn

standard OP_RETURN scripts contain either just an OP_RETURN or an OP_RETURN
followed by a single pushdata OP with not more than 40 bytes.
This commit is contained in:
Ryan X. Charles 2014-09-19 21:59:19 -07:00
parent ffdfe0ce83
commit 27fbdb42ad
2 changed files with 36 additions and 0 deletions

View File

@ -137,6 +137,8 @@ Script.prototype.fromString = function(str) {
throw new Error('Invalid script');
}
} else if (opcodenum === Opcode.map.OP_PUSHDATA1 || opcodenum === Opcode.map.OP_PUSHDATA2 || opcodenum === Opcode.map.OP_PUSHDATA4) {
if (tokens[i + 2].slice(0, 2) != '0x')
throw new Error('Pushdata data must start with 0x');
this.chunks.push({
buf: new Buffer(tokens[i + 2].slice(2), 'hex'),
len: parseInt(tokens[i + 1]),
@ -171,4 +173,18 @@ Script.prototype.toString = function() {
return str.substr(0, str.length - 1);
};
Script.prototype.isOpReturn = function() {
if (this.chunks[0] === Opcode('OP_RETURN').toNumber()
&&
(this.chunks.length === 1
||
(this.chunks.length === 2
&& this.chunks[1].buf
&& this.chunks[1].buf.length <= 40
&& this.chunks[1].length === this.chunks.len)))
return true;
else
return false;
};
module.exports = Script;

View File

@ -196,4 +196,24 @@ describe('Script', function() {
});
describe('#isOpReturn', function() {
it('should know this is a (blank) OP_RETURN script', function() {
Script('OP_RETURN').isOpReturn().should.equal(true);
});
it('should know this is an OP_RETURN script', function() {
var buf = new Buffer(40);
buf.fill(0);
Script('OP_RETURN 40 0x' + buf.toString('hex')).isOpReturn().should.equal(true);
});
it('should know this is not an OP_RETURN script', function() {
var buf = new Buffer(40);
buf.fill(0);
Script('OP_CHECKMULTISIG 40 0x' + buf.toString('hex')).isOpReturn().should.equal(false);
});
});
});