From 9fe97dc855d6c1e8302c38a19311f5da3b6d682a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Jul 2016 06:03:48 -0700 Subject: [PATCH] script: fromOptions. --- lib/bcoin/script.js | 57 +++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index fe5d20a0..234aa306 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -56,17 +56,17 @@ Witness.prototype.__defineSetter__('length', function(length) { */ Witness.prototype.fromOptions = function fromOptions(options) { - var items = options.items; + var items; + + assert(options, 'Witness data is required.'); + + items = options.items; if (!items) items = options; - assert(Array.isArray(items)); - - // Duplicate: - // items = items.slice(); - - this.items = items; + if (items) + this.fromArray(items); return this; }; @@ -1150,34 +1150,28 @@ Script.prototype.__defineSetter__('length', function(length) { */ Script.prototype.fromOptions = function fromOptions(options) { - var code, raw; + assert(options, 'Script data is required.'); - if (Buffer.isBuffer(options)) { - raw = options; - } else if (Array.isArray(options)) { - code = options; - } else { - code = options.code; - raw = options.raw; + if (Buffer.isBuffer(options)) + return this.fromRaw(options); + + if (Array.isArray(options)) + return this.fromArray(options); + + if (options.raw) { + if (!options.code) + return this.fromRaw(options.raw); + assert(Buffer.isBuffer(options.raw)); + this.raw = options.raw; } - if (code) { - code = Script.parseArray(code); - // Duplicate: - // code = code.slice(); + if (options.code) { + if (!options.raw) + return this.fromArray(options.code); + assert(Array.isArray(options.code)); + this.code = options.code; } - if (!raw) - raw = Script.encode(code); - else if (!code) - code = Script.decode(raw); - - assert(Array.isArray(code)); - assert(Buffer.isBuffer(raw)); - - this.raw = raw; - this.code = code; - return this; }; @@ -4553,6 +4547,9 @@ Script.parseArray = function parseArray(code) { assert(Array.isArray(code)); + if (code.length === 0) + return code; + if (code[0] instanceof Opcode) return code;