From 19f3fe0de3c4693fc0fed2b935c39ae008b44510 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Thu, 1 Oct 2015 12:38:04 -0700 Subject: [PATCH] Using buffer-compare instead of copy&paste --- lib/transaction/transaction.js | 9 +++---- lib/util/buffer.js | 43 ---------------------------------- npm-shrinkwrap.json | 5 ++++ package.json | 1 + 4 files changed, 11 insertions(+), 47 deletions(-) diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 5138d7b..f6611f6 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -3,6 +3,7 @@ var _ = require('lodash'); var $ = require('../util/preconditions'); var buffer = require('buffer'); +var compare = Buffer.compare || require('buffer-compare'); var errors = require('../errors'); var BufferUtil = require('../util/buffer'); @@ -914,17 +915,17 @@ Transaction.prototype.removeOutput = function(index) { Transaction.prototype.sort = function() { this.sortInputs(function(inputs) { var copy = Array.prototype.concat.apply([], inputs); - copy.sort(function compare(first, second) { - return BufferUtil.compare(first.prevTxId, second.prevTxId) + copy.sort(function(first, second) { + return compare(first.prevTxId, second.prevTxId) || first.outputIndex - second.outputIndex; }); return copy; }); this.sortOutputs(function(outputs) { var copy = Array.prototype.concat.apply([], outputs); - copy.sort(function compare(first, second) { + copy.sort(function(first, second) { return first.satoshis - second.satoshis - || BufferUtil.compare(first.script.toBuffer(), second.script.toBuffer()); + || compare(first.script.toBuffer(), second.script.toBuffer()); }); return copy; }); diff --git a/lib/util/buffer.js b/lib/util/buffer.js index 0cfe5af..c78b939 100644 --- a/lib/util/buffer.js +++ b/lib/util/buffer.js @@ -170,49 +170,6 @@ module.exports = { hexToBuffer: function hexToBuffer(string) { assert(js.isHexa(string)); return new buffer.Buffer(string, 'hex'); - }, - - /** - * Browser shim for buffer compare - * - * MIT. Copyright by Feross Aboukhadijeh, and other contributors. - * Originally forked from an MIT-licensed module by Romain Beauxis. - * @see {https://github.com/feross/buffer/blob/bc33ea4618e6846148e35aee78dfc28d999bdd1c/index.js#L264} - */ - compare: function(a, b) { - if (Buffer.compare) { - return Buffer.compare(a, b); - } else { - if (a === b) { - return 0; - } - - var x = a.length; - var y = b.length; - - var i = 0; - var len = Math.min(x, y); - while (i < len) { - if (a[i] !== b[i]) { - break; - } - - ++i; - } - - if (i !== len) { - x = a[i]; - y = b[i]; - } - - if (x < y) { - return -1; - } - if (y < x) { - return 1; - } - return 0; - } } }; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index e059d84..dceb306 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -12,6 +12,11 @@ "from": "bs58@=2.0.0", "resolved": "https://registry.npmjs.org/bs58/-/bs58-2.0.0.tgz" }, + "buffer-compare": { + "version": "1.0.0", + "from": "buffer-compare@=1.0.0", + "resolved": "https://registry.npmjs.org/buffer-compare/-/buffer-compare-1.0.0.tgz" + }, "elliptic": { "version": "3.0.3", "from": "elliptic@=3.0.3", diff --git a/package.json b/package.json index 23eee23..4a7c967 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "dependencies": { "bn.js": "=2.0.4", "bs58": "=2.0.0", + "buffer-compare": "=1.0.0", "elliptic": "=3.0.3", "hash.js": "=1.0.2", "inherits": "=2.0.1",