* Script * Represents a input or output script. * @alias module:script.Script * @property {Array} code - Parsed script code. * @property {Buffer?} raw - Serialized script. * @property {Number} length - Number of parsed opcodes.
| 45 | */ |
| 46 | |
| 47 | class Script { |
| 48 | /** |
| 49 | * Create a script. |
| 50 | * @constructor |
| 51 | * @param {Buffer|Array|Object} code |
| 52 | */ |
| 53 | |
| 54 | constructor(options) { |
| 55 | this.raw = EMPTY_BUFFER; |
| 56 | this.code = []; |
| 57 | |
| 58 | if (options) |
| 59 | this.fromOptions(options); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get length. |
| 64 | * @returns {Number} |
| 65 | */ |
| 66 | |
| 67 | get length() { |
| 68 | return this.code.length; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set length. |
| 73 | * @param {Number} value |
| 74 | */ |
| 75 | |
| 76 | set length(value) { |
| 77 | this.code.length = value; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Inject properties from options object. |
| 82 | * @private |
| 83 | * @param {Object} options |
| 84 | */ |
| 85 | |
| 86 | fromOptions(options) { |
| 87 | assert(options, 'Script data is required.'); |
| 88 | |
| 89 | if (Buffer.isBuffer(options)) |
| 90 | return this.fromRaw(options); |
| 91 | |
| 92 | if (Array.isArray(options)) |
| 93 | return this.fromArray(options); |
| 94 | |
| 95 | if (options.raw) { |
| 96 | if (!options.code) |
| 97 | return this.fromRaw(options.raw); |
| 98 | assert(Buffer.isBuffer(options.raw), 'Raw must be a Buffer.'); |
| 99 | this.raw = options.raw; |
| 100 | } |
| 101 | |
| 102 | if (options.code) { |
| 103 | if (!options.raw) |
| 104 | return this.fromArray(options.code); |
no outgoing calls
no test coverage detected