add Script#isPushOnly()

This commit is contained in:
Manuel Araoz 2014-12-10 12:35:01 -03:00
parent 5bdda7c95b
commit b61bd6f76a
2 changed files with 27 additions and 6 deletions

View File

@ -1,16 +1,15 @@
'use strict';
var _ = require('lodash');
var bu = require('./util/buffer');
var Address = require('./address');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Errors = require('./errors');
var Hash = require('./crypto/hash');
var Opcode = require('./opcode');
var PublicKey = require('./publickey');
var PublicKey = require('./publickey');
var Hash = require('./crypto/hash');
var bu = require('./util/buffer');
var _ = require('lodash');
/**
* A bitcoin transaction script. Each transaction's inputs and outputs
@ -304,6 +303,17 @@ Script.prototype.isDataOut = function() {
this.chunks[1].length === this.chunks.len)));
};
/**
* @returns true if the script is only composed of data pushing
* opcodes or small int opcodes (OP_0, OP_1, ..., OP_16)
*/
Script.prototype.isPushOnly = function() {
return _.every(this.chunks, function(chunk) {
var opcodenum = chunk.opcodenum;
return !_.isUndefined(opcodenum) || chunk <= Opcode.map.OP_16;
});
};
Script.types = {};
Script.types.UNKNOWN = 'Unknown';
@ -453,7 +463,7 @@ Script.prototype.removeCodeseparators = function() {
* @returns a new Multisig output script for given public keys,
* 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 {number} m - amount of required signatures to spend the output
*/
Script.buildMultisigOut = function(pubkeys, m) {
var s = new Script();
@ -513,7 +523,7 @@ Script.buildDataOut = function(data) {
};
/**
* @param {Script} script - the redeemScript for the new p2sh output
* @param {Script} script - the redeemScript for the new p2sh output
* @returns Script new pay to script hash script for given script
*/
Script.buildScriptHashOut = function(script) {

View File

@ -301,6 +301,17 @@ describe('Script', function() {
});
});
describe('#isPushOnly', function() {
it('should know these scripts are or aren\'t push only', function() {
Script('OP_NOP 1 0x01').isPushOnly().should.equal(false);
Script('OP_0').isPushOnly().should.equal(true);
Script('OP_0 OP_RETURN').isPushOnly().should.equal(false);
Script('OP_PUSHDATA1 5 0x1010101010').isPushOnly().should.equal(true);
// like bitcoind, we regard OP_RESERVED as being "push only"
Script('OP_RESERVED').isPushOnly().should.equal(true);
});
});
describe('#classify', function() {
it('should classify public key hash out', function() {
Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000 OP_EQUALVERIFY OP_CHECKSIG').classify().should.equal(Script.types.PUBKEYHASH_OUT);