|
|
@ -208,35 +208,66 @@ Script.prototype.isScriptHashIn = function() { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Adds a script element at the start of the script. |
|
|
|
* @param {*} obj a string, number, Opcode, Bufer, or object to add |
|
|
|
* @returns {Script} this script instance |
|
|
|
*/ |
|
|
|
Script.prototype.prepend = function(obj) { |
|
|
|
this._addByType(obj, true); |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Adds a script element to the end of the script. |
|
|
|
* |
|
|
|
* @param {*} obj a string, number, Opcode, Bufer, or object to add |
|
|
|
* @returns {Script} this script instance |
|
|
|
* |
|
|
|
*/ |
|
|
|
Script.prototype.add = function(obj) { |
|
|
|
this._addByType(obj, false); |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Script.prototype._addByType = function(obj, prepend) { |
|
|
|
if (typeof obj === 'string') { |
|
|
|
this._addOpcode(obj); |
|
|
|
this._addOpcode(obj, prepend); |
|
|
|
} else if (typeof obj === 'number') { |
|
|
|
this._addOpcode(obj); |
|
|
|
this._addOpcode(obj, prepend); |
|
|
|
} else if (obj.constructor && obj.constructor.name && obj.constructor.name === 'Opcode') { |
|
|
|
this._addOpcode(obj); |
|
|
|
this._addOpcode(obj, prepend); |
|
|
|
} else if (Buffer.isBuffer(obj)) { |
|
|
|
this._addBuffer(obj); |
|
|
|
this._addBuffer(obj, prepend); |
|
|
|
} else if (typeof obj === 'object') { |
|
|
|
this.chunks.push(obj); |
|
|
|
this._insertAtPosition(obj, prepend); |
|
|
|
} else { |
|
|
|
throw new Error('Invalid script chunk'); |
|
|
|
} |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Script.prototype._addOpcode = function(opcode) { |
|
|
|
Script.prototype._insertAtPosition = function(op, prepend) { |
|
|
|
if (prepend) { |
|
|
|
this.chunks.unshift(op); |
|
|
|
} else { |
|
|
|
this.chunks.push(op); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
Script.prototype._addOpcode = function(opcode, prepend) { |
|
|
|
var op; |
|
|
|
if (typeof opcode === 'number') { |
|
|
|
this.chunks.push(opcode); |
|
|
|
op = opcode; |
|
|
|
} else if (opcode.constructor && opcode.constructor.name && opcode.constructor.name === 'Opcode') { |
|
|
|
this.chunks.push(opcode.toNumber()); |
|
|
|
op = opcode.toNumber(); |
|
|
|
} else { |
|
|
|
this.chunks.push(Opcode(opcode).toNumber()); |
|
|
|
op = Opcode(opcode).toNumber(); |
|
|
|
} |
|
|
|
this._insertAtPosition(op, prepend); |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Script.prototype._addBuffer = function(buf) { |
|
|
|
Script.prototype._addBuffer = function(buf, prepend) { |
|
|
|
var opcodenum; |
|
|
|
var len = buf.length; |
|
|
|
if (buf.length > 0 && buf.length < Opcode.map.OP_PUSHDATA1) { |
|
|
@ -250,11 +281,11 @@ Script.prototype._addBuffer = function(buf) { |
|
|
|
} else { |
|
|
|
throw new Error('You can\'t push that much data'); |
|
|
|
} |
|
|
|
this.chunks.push({ |
|
|
|
this._insertAtPosition({ |
|
|
|
buf: buf, |
|
|
|
len: len, |
|
|
|
opcodenum: opcodenum |
|
|
|
}); |
|
|
|
}, prepend); |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|