Browse Source

Merge pull request #694 from maraoz/add/interpreter

add Script#isPushOnly(), for script evaluation
patch-2
Esteban Ordano 10 years ago
parent
commit
7c7db732a2
  1. 2
      .jshintrc
  2. 22
      lib/script.js
  3. 11
      test/script.js

2
.jshintrc

@ -25,7 +25,7 @@
"maxparams": 4, // Maximum number of parameters for a function "maxparams": 4, // Maximum number of parameters for a function
"maxstatements": 15, // Maximum number of statements in a function "maxstatements": 15, // Maximum number of statements in a function
"maxcomplexity": 4, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity) "maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity)
"maxdepth": 4, // Maximum depth of nested control structures "maxdepth": 4, // Maximum depth of nested control structures
"maxlen": 120, // Maximum number of cols in a line "maxlen": 120, // Maximum number of cols in a line

22
lib/script.js

@ -1,16 +1,15 @@
'use strict'; 'use strict';
var _ = require('lodash');
var bu = require('./util/buffer');
var Address = require('./address'); var Address = require('./address');
var BufferReader = require('./encoding/bufferreader'); var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter'); var BufferWriter = require('./encoding/bufferwriter');
var Errors = require('./errors');
var Hash = require('./crypto/hash'); var Hash = require('./crypto/hash');
var Opcode = require('./opcode'); var Opcode = require('./opcode');
var PublicKey = require('./publickey'); var PublicKey = require('./publickey');
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 * 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))); 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 = {};
Script.types.UNKNOWN = 'Unknown'; Script.types.UNKNOWN = 'Unknown';
@ -453,7 +463,7 @@ Script.prototype.removeCodeseparators = function() {
* @returns a new Multisig output script for given public keys, * @returns a new Multisig output script for given public keys,
* 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
*/ */
Script.buildMultisigOut = function(pubkeys, m) { Script.buildMultisigOut = function(pubkeys, m) {
var s = new Script(); 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 * @returns Script new pay to script hash script for given script
*/ */
Script.buildScriptHashOut = function(script) { Script.buildScriptHashOut = function(script) {

11
test/script.js

@ -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() { describe('#classify', function() {
it('should classify public key hash out', 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); Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000 OP_EQUALVERIFY OP_CHECKSIG').classify().should.equal(Script.types.PUBKEYHASH_OUT);

Loading…
Cancel
Save