From fd531de123084be6bd76a6474d5091679562e1c6 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 9 Dec 2014 17:30:52 -0300 Subject: [PATCH] Update opcode and script transactions --- lib/opcode.js | 5 +++++ lib/script.js | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/opcode.js b/lib/opcode.js index 75e8120..80af9f9 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -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 */ diff --git a/lib/script.js b/lib/script.js index df418f2..ed45d86 100644 --- a/lib/script.js +++ b/lib/script.js @@ -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);