diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 0b1175d8..689576a5 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1676,7 +1676,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) { case opcodes.OP_INVERT: { if (stack.length < 1) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); - val = utils.slice(stack.pop()); + val = utils.copy(stack.pop()); for (i = 0; i < val.length; i++) val[i] = ~val[i] & 0xff; stack.push(val); @@ -1688,7 +1688,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) { if (stack.length < 2) throw new ScriptError('INVALID_STACK_OPERATION', op, ip); v2 = stack.pop(); - v1 = utils.slice(stack.pop()); + v1 = utils.copy(stack.pop()); if (v1.length < v2.length) { v3 = new Buffer(v2.length - v1.length); v3.fill(0); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 705286a6..00f06b90 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -201,7 +201,7 @@ TX.prototype.witnessHash = function witnessHash(enc) { if (this.isCoinbase()) { return enc === 'hex' ? constants.NULL_HASH - : utils.slice(constants.ZERO_HASH); + : utils.copy(constants.ZERO_HASH); } if (!this.hasWitness()) @@ -451,7 +451,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) { // Bitcoind used to return 1 as an error code: // it ended up being treated like a hash. if (index >= copy.outputs.length) - return utils.slice(constants.ONE_HASH); + return utils.copy(constants.ONE_HASH); // Drop all the outputs after the input index. copy.outputs.length = index + 1; @@ -512,7 +512,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) { this._hashPrevouts = hashPrevouts; } } else { - hashPrevouts = utils.slice(constants.ZERO_HASH); + hashPrevouts = utils.copy(constants.ZERO_HASH); } if (!(type & constants.hashType.ANYONECANPAY) @@ -529,7 +529,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) { this._hashSequence = hashSequence; } } else { - hashSequence = utils.slice(constants.ZERO_HASH); + hashSequence = utils.copy(constants.ZERO_HASH); } if ((type & 0x1f) !== constants.hashType.SINGLE @@ -548,7 +548,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) { hashOutputs = bcoin.protocol.framer.output(this.outputs[index]); hashOutputs = utils.dsha256(hashOutputs); } else { - hashOutputs = utils.slice(constants.ZERO_HASH); + hashOutputs = utils.copy(constants.ZERO_HASH); } p.write32(this.version); diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index c3d1b52f..375b8239 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -70,23 +70,14 @@ utils.nop = function() {}; utils.gc = !utils.isBrowser && typeof gc === 'function' ? gc : utils.nop; /** - * A slow buffer slice (will allocate a new buffer). + * Clone a buffer. * @param {Buffer} data - * @param {Number?} start - * @param {Number?} end * @returns {Buffer} */ -utils.slice = function slice(data, start, end) { - var clone; - - if (start != null) - data = data.slice(start, end); - - clone = new Buffer(data.length); - +utils.copy = function copy(data) { + var clone = new Buffer(data.length); data.copy(clone, 0, 0, data.length); - return clone; }; @@ -917,7 +908,7 @@ utils.ip2version = function ip2version(ip, version) { // Found an ipv4 address if (ip.length - i === 6 && ip[i] === 0xff && ip[i + 1] === 0xff) - return utils.slice(ip, -4); + return utils.copy(ip.slice(-4)); // No ipv4 address return new Buffer([0, 0, 0, 0]);