|
|
@ -182,32 +182,40 @@ Script.fromString = function(str) { |
|
|
|
return script; |
|
|
|
}; |
|
|
|
|
|
|
|
Script.prototype.toString = function() { |
|
|
|
Script.prototype._chunkToString = function(chunk) { |
|
|
|
var opcodenum = chunk.opcodenum; |
|
|
|
var str = ''; |
|
|
|
for (var i = 0; i < this.chunks.length; i++) { |
|
|
|
var chunk = this.chunks[i]; |
|
|
|
var opcodenum = chunk.opcodenum; |
|
|
|
if (!chunk.buf) { |
|
|
|
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') { |
|
|
|
str = str + ' ' + Opcode(opcodenum).toString(); |
|
|
|
} else { |
|
|
|
var numstr = opcodenum.toString(16); |
|
|
|
if (numstr.length % 2 !== 0) { |
|
|
|
numstr = '0' + numstr; |
|
|
|
} |
|
|
|
str = str + ' ' + '0x' + numstr; |
|
|
|
} |
|
|
|
if (!chunk.buf) { |
|
|
|
// no data chunk
|
|
|
|
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') { |
|
|
|
str = str + ' ' + Opcode(opcodenum).toString(); |
|
|
|
} else { |
|
|
|
if (opcodenum === Opcode.OP_PUSHDATA1 || |
|
|
|
opcodenum === Opcode.OP_PUSHDATA2 || |
|
|
|
opcodenum === Opcode.OP_PUSHDATA4) { |
|
|
|
str = str + ' ' + Opcode(opcodenum).toString(); |
|
|
|
} |
|
|
|
str = str + ' ' + chunk.len; |
|
|
|
if (chunk.len > 0) { |
|
|
|
str = str + ' ' + '0x' + chunk.buf.toString('hex'); |
|
|
|
var numstr = opcodenum.toString(16); |
|
|
|
if (numstr.length % 2 !== 0) { |
|
|
|
numstr = '0' + numstr; |
|
|
|
} |
|
|
|
str = str + ' ' + '0x' + numstr; |
|
|
|
} |
|
|
|
} else { |
|
|
|
// data chunk
|
|
|
|
if (opcodenum === Opcode.OP_PUSHDATA1 || |
|
|
|
opcodenum === Opcode.OP_PUSHDATA2 || |
|
|
|
opcodenum === Opcode.OP_PUSHDATA4) { |
|
|
|
str = str + ' ' + Opcode(opcodenum).toString(); |
|
|
|
} |
|
|
|
str = str + ' ' + chunk.len; |
|
|
|
if (chunk.len > 0) { |
|
|
|
str = str + ' ' + '0x' + chunk.buf.toString('hex'); |
|
|
|
} |
|
|
|
} |
|
|
|
return str; |
|
|
|
}; |
|
|
|
|
|
|
|
Script.prototype.toString = function() { |
|
|
|
var str = ''; |
|
|
|
for (var i = 0; i < this.chunks.length; i++) { |
|
|
|
var chunk = this.chunks[i]; |
|
|
|
str += this._chunkToString(chunk); |
|
|
|
} |
|
|
|
|
|
|
|
return str.substr(1); |
|
|
@ -480,10 +488,10 @@ Script.prototype._addByType = function(obj, prepend) { |
|
|
|
this._addOpcode(obj, prepend); |
|
|
|
} else if (BufferUtil.isBuffer(obj)) { |
|
|
|
this._addBuffer(obj, prepend); |
|
|
|
} else if (typeof obj === 'object') { |
|
|
|
this._insertAtPosition(obj, prepend); |
|
|
|
} else if (obj instanceof Script) { |
|
|
|
this.chunks = this.chunks.concat(obj.chunks); |
|
|
|
} else if (typeof obj === 'object') { |
|
|
|
this._insertAtPosition(obj, prepend); |
|
|
|
} else { |
|
|
|
throw new Error('Invalid script chunk'); |
|
|
|
} |
|
|
@ -598,6 +606,8 @@ Script.buildP2SHMultisigIn = function(pubkeys, threshold, signatures, opts) { |
|
|
|
var s = new Script(); |
|
|
|
s.add(Opcode.OP_0); |
|
|
|
_.each(signatures, function(signature) { |
|
|
|
$.checkArgument(BufferUtil.isBuffer(signature), 'Signatures must be an array of Buffers'); |
|
|
|
// TODO: allow signatures to be an array of Signature objects
|
|
|
|
s.add(signature); |
|
|
|
}); |
|
|
|
s.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer()); |
|
|
|