diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 46a074db..b4da3244 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -126,7 +126,7 @@ Block.prototype.getCommitmentHash = function getCommitmentHash() { var leaves = []; var i, witnessNonce, witnessRoot, commitmentHash; - witnessNonce = this.txs[0].inputs[0].witness[0]; + witnessNonce = this.txs[0].inputs[0].witness.items[0]; if (!witnessNonce) return; diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 1c23e360..f7956ba6 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -9,7 +9,6 @@ var bcoin = require('../bcoin'); var utils = bcoin.utils; var assert = utils.assert; var constants = bcoin.protocol.constants; -var Script = bcoin.script; /** * Input @@ -22,9 +21,9 @@ function Input(options, tx) { assert(typeof options.script !== 'string'); this.prevout = options.prevout; - this.script = options.script || new Script([]); + this.script = options.script || new bcoin.script([]); this.sequence = options.sequence == null ? 0xffffffff : options.sequence; - this.witness = options.witness || []; + this.witness = options.witness || new bcoin.script.witness([]); this._size = options._size || 0; this._offset = options._offset || 0; this._witnessSize = options._witnessSize || 0; @@ -58,8 +57,8 @@ Input.prototype.getType = function getType() { if (this._type) return this._type; - if (this.witness.length > 0) - type = bcoin.script.getInputType(this.witness, null, true); + if (this.witness.items.length > 0) + type = this.witness.getInputType(); if (!type || type === 'unknown') type = this.script.getInputType(); @@ -79,14 +78,14 @@ Input.prototype.getRedeem = function getRedeem() { type = this.getType(); if (type === 'witnessscripthash') - return bcoin.script.getRedeem(this.witness); + return this.witness.getRedeem(); if (type === 'scripthash') { redeem = this.script.getRedeem(); if (!redeem) return; if (redeem.isWitnessScripthash()) - return bcoin.script.getRedeem(this.witness); + return this.witness.getRedeem(); return redeem; } }; @@ -117,8 +116,8 @@ Input.prototype.getAddress = function getAddress() { if (this._address) return this._address; - if (this.witness.length > 0) - address = bcoin.script.getInputAddress(this.witness, null, true); + if (this.witness.items.length > 0) + address = this.witness.getInputAdddress(); if (!address) address = this.script.getInputAddress(); @@ -227,7 +226,7 @@ Input.prototype.toJSON = function toJSON() { }, output: this.output ? this.output.toJSON() : null, script: utils.toHex(this.script.encode()), - witness: utils.toHex(bcoin.protocol.framer.witness(this.witness)), + witness: utils.toHex(this.witness.encode()), sequence: this.sequence }; }; @@ -239,8 +238,8 @@ Input._fromJSON = function _fromJSON(json) { index: json.prevout.index }, output: json.output ? bcoin.coin._fromJSON(json.output) : null, - script: new Script(new Buffer(json.script, 'hex')), - witness: bcoin.protocol.parser.parseWitness(new Buffer(json.witness, 'hex')), + script: new bcoin.script(new Buffer(json.script, 'hex')), + witness: new bcoin.script.witness(new Buffer(json.witness, 'hex')), sequence: json.sequence }; }; @@ -274,7 +273,7 @@ Input.fromRaw = function fromRaw(data, enc) { Input.prototype.toExtended = function toExtended(enc) { var input = bcoin.protocol.framer.input(this); - var witness = bcoin.protocol.framer.witness(this.witness); + var witness = this.witness.encode(); var data = new Buffer(data.length + witness.length); var off = 0; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index e868fffe..1e554a71 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -144,7 +144,7 @@ MTX.prototype.addInput = function addInput(options, index) { input.script = options.script.clone(); if (options.witness) - input.witness = options.witness.slice(); + input.witness = options.witness.clone(); this.inputs.push(input); @@ -167,7 +167,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) { // Optimization: Don't bother with any below // calculation if the output is already templated. // Just say this is "our" output. - if (input.script.code.length || input.witness.length) + if (input.script.code.length || input.witness.items.length) return true; // Optimization: test output against the @@ -187,7 +187,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) { if (addr.program && utils.isEqual(prev.code[1], addr.programHash)) { // Witness program nested in regular P2SH. redeemScript = addr.program.encode(); - vector = input.witness; + vector = input.witness.items; dummy = new Buffer([]); assert(addr.program.code[0] === 0, 'Non-zero version passed to address.'); if (addr.program.code[1].length === 32) { @@ -212,7 +212,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) { } } else if (prev.isWitnessProgram()) { // Witness program. - vector = input.witness; + vector = input.witness.items; dummy = new Buffer([]); if (prev.code[0] !== 0) @@ -311,7 +311,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) { // P2WSH requires the witness // script after signatures. if (witnessScript) - input.witness.push(witnessScript); + input.witness.items.push(witnessScript); return true; }; @@ -377,14 +377,14 @@ MTX.prototype.signInput = function signInput(index, addr, type) { // _has_ to be an empty buffer (what OP_0 // pushes onto the stack). if (prev.isWitnessScripthash()) { - prev = bcoin.script.getRedeem(input.witness); - vector = input.witness; + prev = input.witness.getRedeem(); + vector = input.witness.items; len = vector.length - 1; dummy = new Buffer([]); version = 1; } else if (prev.isWitnessPubkeyhash()) { prev = bcoin.script.createPubkeyhash(prev.code[1]); - vector = input.witness; + vector = input.witness.items; len = vector.length; dummy = new Buffer([]); version = 1; @@ -667,7 +667,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) { // Create copy with 0-script inputs for (i = 0; i < copy.inputs.length; i++) { copy.inputs[i].script = new Script([]); - copy.inputs[i].witness = []; + copy.inputs[i].witness = new bcoin.script.witness([]); } total = copy.render().length; @@ -700,8 +700,8 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) { // redeem script (if there was one) // is now worth 4 points. size *= 4; - if (this.inputs[i].witness.length && prev.isWitnessScripthash()) { - prev = bcoin.script.getRedeem(this.inputs[i].witness); + if (this.inputs[i].witness.items.length && prev.isWitnessScripthash()) { + prev = this.inputs[i].witness.getRedeem(); size += utils.sizeIntv(prev.getSize()) + prev.getSize(); } else if (prev.isWitnessPubkeyhash()) { prev = bcoin.script.createPubkeyhash(prev.code[1]); diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 00928e9b..8d0f8a87 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -9,7 +9,6 @@ var bcoin = require('../bcoin'); var utils = bcoin.utils; var assert = utils.assert; var constants = bcoin.protocol.constants; -var Script = bcoin.script; /** * Output @@ -31,7 +30,7 @@ function Output(options, tx) { } this.value = utils.satoshi(value || new bn(0)); - this.script = options.script || new Script([]); + this.script = options.script || new bcoin.script([]); this._size = options._size || 0; this._offset = options._offset || 0; this._mutable = !tx || (tx instanceof bcoin.mtx); @@ -127,7 +126,7 @@ Output.prototype.toJSON = function toJSON() { Output._fromJSON = function _fromJSON(json) { return { value: utils.satoshi(json.value), - script: new Script(new Buffer(json.script, 'hex')) + script: new bcoin.script(new Buffer(json.script, 'hex')) }; }; diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 078e03cf..661be249 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -572,10 +572,10 @@ Framer.witnessTXSize = function witnessTXSize(tx) { if (!witness) continue; - size += utils.sizeIntv(witness.length); + size += utils.sizeIntv(witness.items.length); - for (i = 0; i < witness.length; i++) { - chunk = witness[i]; + for (i = 0; i < witness.items.length; i++) { + chunk = witness.items[i]; size += utils.sizeIntv(chunk.length) + chunk.length; } } @@ -591,19 +591,19 @@ Framer.witness = function _witness(witness) { if (!witness) return new Buffer([0]); - size += utils.sizeIntv(witness.length); + size += utils.sizeIntv(witness.items.length); - for (i = 0; i < witness.length; i++) { - chunk = witness[i]; + for (i = 0; i < witness.items.length; i++) { + chunk = witness.items[i]; size += utils.sizeIntv(chunk.length) + chunk.length; } p = new Buffer(size); - off += utils.writeIntv(p, witness.length, off); + off += utils.writeIntv(p, witness.items.length, off); - for (i = 0; i < witness.length; i++) { - chunk = witness[i]; + for (i = 0; i < witness.items.length; i++) { + chunk = witness.items[i]; off += utils.writeIntv(p, chunk.length, off); off += utils.copy(chunk, p, off); } diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index d249334a..ece7e462 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -498,7 +498,7 @@ Parser.parseTX = function parseTX(p) { tx = Parser.parseInput(p); txIn[i] = tx; - txIn[i].witness = []; + txIn[i].witness = new bcoin.script.witness([]); } outCount = p.readUIntv(); @@ -604,7 +604,7 @@ Parser.parseWitness = function parseWitness(p) { return { _size: p.end(), - witness: witness + witness: new bcoin.script.witness(witness) }; }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 51c3eba5..3b4ddb5a 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -154,7 +154,7 @@ TX.prototype.hasWitness = function hasWitness() { var i; for (i = 0; i < this.inputs.length; i++) { - if (this.inputs[i].witness.length > 0) + if (this.inputs[i].witness.items.length > 0) return true; } @@ -195,7 +195,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, s, type) { copy.inputs.push({ prevout: this.inputs[i].prevout, script: this.inputs[i].script.clone(), - witness: this.inputs[i].witness.slice(), + witness: this.inputs[i].witness.clone(), sequence: this.inputs[i].sequence }); } @@ -691,7 +691,7 @@ TX.prototype.getSigops = function getSigops(scriptHash, accurate) { prev = input.script.getRedeem(); if (prev.isWitnessScripthash()) { - prev = bcoin.script.getRedeem(input.witness); + prev = input.witness.getRedeem(); cost += prev.getSigops(true); } else { cost += 0; diff --git a/test/script-test.js b/test/script-test.js index 72524869..84b05d76 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -1,6 +1,7 @@ var assert = require('assert'); var bcoin = require('../'); var Script = bcoin.script; +var Stack = bcoin.script.stack; describe('Script', function() { it('should encode/decode script', function() { @@ -37,19 +38,19 @@ describe('Script', function() { var encoded = new Buffer(hex, 'hex') var decoded = new bcoin.script(encoded); assert(decoded.isScripthash()) - }) + }); it('should recognize a Null Data output', function () { var hex = '6a28590c080112220a1b353930632e6f7267282a5f5e294f7665726c6179404f7261636c65103b1a010c' var encoded = new Buffer(hex, 'hex') var decoded = new Script(encoded); assert(decoded.isNulldata()) - }) + }); it('should handle if statements correctly', function () { var inputScript = new Script([1, 2]); var prevOutScript = new Script([2, 'equal', 'if', 3, 'else', 4, 'endif', 5]); - var stack = []; + var stack = new Stack(); inputScript.execute(stack); var res = prevOutScript.execute(stack); assert(res); @@ -57,7 +58,7 @@ describe('Script', function() { var inputScript = new Script([1, 2]); var prevOutScript = new Script([9, 'equal', 'if', 3, 'else', 4, 'endif', 5]); - var stack = []; + var stack = new Stack(); inputScript.execute(stack); var res = prevOutScript.execute(stack); assert(res); @@ -65,7 +66,7 @@ describe('Script', function() { var inputScript = new Script([1, 2]); var prevOutScript = new Script([2, 'equal', 'if', 3, 'endif', 5]); - var stack = []; + var stack = new Stack(); inputScript.execute(stack); var res = prevOutScript.execute(stack); assert(res); @@ -73,7 +74,7 @@ describe('Script', function() { var inputScript = new Script([1, 2]); var prevOutScript = new Script([9, 'equal', 'if', 3, 'endif', 5]); - var stack = []; + var stack = new Stack(); inputScript.execute(stack); var res = prevOutScript.execute(stack); assert(res); @@ -81,10 +82,10 @@ describe('Script', function() { var inputScript = new Script([1, 2]); var prevOutScript = new Script([9, 'equal', 'notif', 3, 'endif', 5]); - var stack = []; + var stack = new Stack(); inputScript.execute(stack); var res = prevOutScript.execute(stack); assert(res); assert.deepEqual(stack.slice(), [[1], [3], [5]]); - }) + }); }); diff --git a/test/wallet-test.js b/test/wallet-test.js index 3b32a155..57c85358 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -499,7 +499,7 @@ describe('Wallet', function() { assert.equal(w3.changeAddress.getAddress(), change); if (witness) - send.inputs[0].witness[2] = new Buffer([]); + send.inputs[0].witness.items[2] = new Buffer([]); else send.inputs[0].script.code[2] = 0;