Merge pull request #689 from eordano/feature/scriptOpcodeUpdate

Feature/script opcode update
This commit is contained in:
Manuel Aráoz 2014-12-09 18:18:19 -03:00
commit 6727342202
2 changed files with 35 additions and 6 deletions

View File

@ -1,5 +1,7 @@
'use strict';
var _ = require('lodash');
function Opcode(num) {
if (!(this instanceof Opcode)) {
return new Opcode(num);
@ -203,6 +205,9 @@ for (var k in Opcode.map) {
}
}
// Easier access to opcodes
_.extend(Opcode, Opcode.map);
/**
* @returns true if opcode is one of OP_0, OP_1, ..., OP_16
*/

View File

@ -1,11 +1,16 @@
'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 Hash = require('./crypto/hash');
var bu = require('./util/buffer');
var PublicKey = require('./publickey');
/**
* A bitcoin transaction script. Each transaction's inputs and outputs
@ -25,6 +30,8 @@ var Script = function Script(from) {
if (bu.isBuffer(from)) {
return Script.fromBuffer(from);
} else if (from instanceof Script) {
return Script.fromBuffer(from.toBuffer());
} else if (typeof from === 'string') {
return Script.fromString(from);
} else if (typeof from !== 'undefined') {
@ -182,8 +189,6 @@ Script.prototype.toString = function() {
return str.substr(0, str.length - 1);
};
// script classification methods
/**
@ -341,6 +346,7 @@ Script.prototype.classify = function() {
* @returns true if script is one of the known types
*/
Script.prototype.isStandard = function() {
// TODO: Add BIP62 compliance
return this.classify() !== Script.types.UNKNOWN;
};
@ -430,6 +436,16 @@ Script.prototype._addBuffer = function(buf, prepend) {
return this;
};
Script.prototype.removeCodeseparators = function() {
var chunks = [];
for (var i = 0; i < this.chunks.length; i++) {
if (this.chunks[i] !== Opcode.map.OP_CODESEPARATOR) {
chunks.push(this.chunks[i]);
}
}
this.chunks = chunks;
return this;
};
// high level script builder methods
@ -459,6 +475,8 @@ Script.buildMultisigOut = function(pubkeys, m) {
Script.buildPublicKeyHashOut = function(to) {
if (to instanceof PublicKey) {
to = to.toAddress();
} else if (_.isString(to)) {
to = new Address(to);
}
var s = new Script();
s.add(Opcode('OP_DUP'))
@ -495,8 +513,8 @@ Script.buildDataOut = function(data) {
};
/**
* @returns a new pay to script hash script for given script
* @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) {
var s = new Script();
@ -506,9 +524,15 @@ Script.buildScriptHashOut = function(script) {
return s;
};
/**
* @returns Script an empty script
*/
Script.empty = function() {
return new Script();
};
/**
* @returns a new pay to script hash script that pays to this script
* @returns Script a new pay to script hash script that pays to this script
*/
Script.prototype.toScriptHashOut = function() {
return Script.buildScriptHashOut(this);