From ab8fff71e2552b76f012e21ce9bac54111f9359d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Jul 2016 14:46:16 -0700 Subject: [PATCH] script compilation. --- lib/bcoin/script.js | 74 +++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 07265cbe..b2744101 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -2486,7 +2486,10 @@ Script.isCode = function isCode(raw) { Script.prototype.fromPubkey = function fromPubkey(key) { assert(Buffer.isBuffer(key) && key.length >= 33); - return this.fromArray([key, opcodes.OP_CHECKSIG]); + this.push(key); + this.push(opcodes.OP_CHECKSIG); + this.compile(); + return this; }; /** @@ -2507,13 +2510,13 @@ Script.fromPubkey = function fromPubkey(key) { Script.prototype.fromPubkeyhash = function fromPubkeyhash(hash) { assert(Buffer.isBuffer(hash) && hash.length === 20); - return this.fromArray([ - opcodes.OP_DUP, - opcodes.OP_HASH160, - hash, - opcodes.OP_EQUALVERIFY, - opcodes.OP_CHECKSIG - ]); + this.push(opcodes.OP_DUP); + this.push(opcodes.OP_HASH160); + this.push(hash); + this.push(opcodes.OP_EQUALVERIFY); + this.push(opcodes.OP_CHECKSIG); + this.compile(); + return this; }; /** @@ -2535,7 +2538,6 @@ Script.fromPubkeyhash = function fromPubkeyhash(hash) { */ Script.prototype.fromMultisig = function fromMultisig(m, n, keys) { - var code = []; var i; assert(utils.isNumber(m) && utils.isNumber(n)); @@ -2546,15 +2548,16 @@ Script.prototype.fromMultisig = function fromMultisig(m, n, keys) { keys = utils.sortKeys(keys); - code.push(m + 0x50); + this.push(m + 0x50); for (i = 0; i < keys.length; i++) - code.push(keys[i]); + this.push(keys[i]); - code.push(n + 0x50); - code.push(opcodes.OP_CHECKMULTISIG); + this.push(n + 0x50); + this.push(opcodes.OP_CHECKMULTISIG); + this.compile(); - return this.fromArray(code); + return this; }; /** @@ -2577,11 +2580,11 @@ Script.fromMultisig = function fromMultisig(m, n, keys) { Script.prototype.fromScripthash = function fromScripthash(hash) { assert(Buffer.isBuffer(hash) && hash.length === 20); - return this.fromArray([ - opcodes.OP_HASH160, - hash, - opcodes.OP_EQUAL - ]); + this.push(opcodes.OP_HASH160); + this.push(hash); + this.push(opcodes.OP_EQUAL); + this.compile(); + return this; }; /** @@ -2603,10 +2606,10 @@ Script.fromScripthash = function fromScripthash(hash) { Script.prototype.fromNulldata = function fromNulldata(flags) { assert(Buffer.isBuffer(flags)); assert(flags.length <= constants.script.MAX_OP_RETURN, 'Nulldata too large.'); - return this.fromArray([ - opcodes.OP_RETURN, - flags - ]); + this.push(opcodes.OP_RETURN); + this.push(flags); + this.compile(); + return this; }; /** @@ -2629,7 +2632,10 @@ Script.fromNulldata = function fromNulldata(flags) { Script.prototype.fromProgram = function fromProgram(version, data) { assert(utils.isNumber(version) && version >= 0 && version <= 16); assert(Buffer.isBuffer(data) && data.length >= 2 && data.length <= 40); - return this.fromArray([version === 0 ? 0 : version + 0x50, data]); + this.push(version === 0 ? 0 : version + 0x50); + this.push(data); + this.compile(); + return this; }; /** @@ -2686,7 +2692,6 @@ Script.fromAddress = function fromAddress(address) { */ Script.prototype.fromCommitment = function fromCommitment(hash, flags) { - var code = []; var p; if (typeof flags === 'string') @@ -2698,13 +2703,15 @@ Script.prototype.fromCommitment = function fromCommitment(hash, flags) { p.writeU32BE(0xaa21a9ed); p.writeHash(hash); - code.push(opcodes.OP_RETURN); - code.push(p.render()); + this.push(opcodes.OP_RETURN); + this.push(p.render()); if (flags) - code.push(flags); + this.push(flags); - return this.fromArray(code); + this.compile(); + + return this; }; /** @@ -4740,13 +4747,16 @@ Opcode.from = function from(data) { if (typeof data === 'number') return Opcode.fromOp(data); - if (bn.isBN(data)) - return Opcode.fromNumber(data); + if (Buffer.isBuffer(data)) + return Opcode.fromData(data); if (typeof data === 'string') return Opcode.fromString(data, 'utf8'); - return Opcode.fromData(data); + if (bn.isBN(data)) + return Opcode.fromNumber(data); + + assert(false, 'Bad data for opcode.'); }; /**