Using buffer-compare instead of copy&paste

This commit is contained in:
Esteban Ordano 2015-10-01 12:38:04 -07:00
parent 20cc98df57
commit 19f3fe0de3
4 changed files with 11 additions and 47 deletions

View File

@ -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;
});

View File

@ -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;
}
}
};

5
npm-shrinkwrap.json generated
View File

@ -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",

View File

@ -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",