|
|
@ -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); |
|
|
|