From 8541843e14462d57f90ad46d1a1a16bb39ad714e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Jul 2016 06:19:57 -0700 Subject: [PATCH] script.fromString. --- lib/bcoin/script.js | 90 ++++++++++++++++++++++++++++----------------- test/script-test.js | 4 +- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 234aa306..07265cbe 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -535,6 +535,32 @@ Witness.fromRaw = function fromRaw(data, enc) { return new Witness().fromRaw(data); }; +/** + * Inject items from string. + * @private + * @param {String|String[]} items + */ + +Witness.prototype.fromString = function fromString(items) { + var i; + + if (!Array.isArray(items)) { + assert(typeof items === 'string'); + + items = items.trim(); + + if (items.length === 0) + return this; + + items = items.split(/\s+/); + } + + for (i = 0; i < items.length; i++) + this.items.push(new Buffer(items[i], 'hex')); + + return this; +}; + /** * Parse a test script/array * string into a witness object. _Must_ @@ -546,24 +572,7 @@ Witness.fromRaw = function fromRaw(data, enc) { */ Witness.fromString = function fromString(items) { - var i, result; - - if (!Array.isArray(items)) { - if (typeof items !== 'string') - return new Witness(); - - items = items.trim().split(/\s+/); - } - - if (items.length === 0) - return new Witness(); - - result = new Array(items.length); - - for (i = 0; i < items.length; i++) - result[i] = new Buffer(items[i], 'hex'); - - return new Witness(result); + return new Witness().fromString(items); }; /** @@ -2677,23 +2686,25 @@ Script.fromAddress = function fromAddress(address) { */ Script.prototype.fromCommitment = function fromCommitment(hash, flags) { + var code = []; var p; - if (!flags) - flags = 'mined by bcoin'; - if (typeof flags === 'string') flags = new Buffer(flags, 'utf8'); + assert(!flags || Buffer.isBuffer(flags)); + p = new BufferWriter(); p.writeU32BE(0xaa21a9ed); p.writeHash(hash); - return this.fromArray([ - opcodes.OP_RETURN, - p.render(), - flags - ]); + code.push(opcodes.OP_RETURN); + code.push(p.render()); + + if (flags) + code.push(flags); + + return this.fromArray(code); }; /** @@ -4021,25 +4032,24 @@ Script.getWitnessSigops = function getWitnessSigops(input, output, witness, flag }; /** - * Parse a bitcoind test script - * string into a script object. + * Inject properties from bitcoind test string. + * @private * @param {String} items - Script string. - * @returns {Script} * @throws Parse error. */ -Script.fromString = function fromString(code) { +Script.prototype.fromString = function fromString(code) { var i, op, symbol, p; - if (typeof code !== 'string') - return new Script(); + assert(typeof code === 'string'); code = code.trim(); if (code.length === 0) - return new Script(); + return this; code = code.split(/\s+/); + p = new BufferWriter(); for (i = 0; i < code.length; i++) { @@ -4074,7 +4084,19 @@ Script.fromString = function fromString(code) { p.writeU8(opcodes[symbol]); } - return new Script(p.render()); + return this.fromRaw(p.render()); +}; + +/** + * Parse a bitcoind test script + * string into a script object. + * @param {String} items - Script string. + * @returns {Script} + * @throws Parse error. + */ + +Script.fromString = function fromString(code) { + return new Script().fromString(code); }; /** diff --git a/test/script-test.js b/test/script-test.js index 2525c0a5..87bdac9b 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -272,7 +272,7 @@ describe('Script', function() { scripts.forEach(function(data) { // ["Format is: [[wit...]?, scriptSig, scriptPubKey, flags, expected_scripterror, ... comments]"], - var witness = Array.isArray(data[0]) ? data.shift() : null; + var witness = Array.isArray(data[0]) ? data.shift() : []; var input = data[0] ? data[0].trim() : data[0] || ''; var output = data[1] ? data[1].trim() : data[1] || ''; var flags = data[2] ? data[2].trim().split(/,\s*/) : []; @@ -288,7 +288,7 @@ describe('Script', function() { comments += ' (' + expected + ')'; - if (witness) + if (witness.length !== 0) amount = witness.pop() * 100000000; witness = bcoin.witness.fromString(witness);