Using buffer-compare instead of copy&paste
This commit is contained in:
parent
20cc98df57
commit
19f3fe0de3
@ -3,6 +3,7 @@
|
|||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var $ = require('../util/preconditions');
|
var $ = require('../util/preconditions');
|
||||||
var buffer = require('buffer');
|
var buffer = require('buffer');
|
||||||
|
var compare = Buffer.compare || require('buffer-compare');
|
||||||
|
|
||||||
var errors = require('../errors');
|
var errors = require('../errors');
|
||||||
var BufferUtil = require('../util/buffer');
|
var BufferUtil = require('../util/buffer');
|
||||||
@ -914,17 +915,17 @@ Transaction.prototype.removeOutput = function(index) {
|
|||||||
Transaction.prototype.sort = function() {
|
Transaction.prototype.sort = function() {
|
||||||
this.sortInputs(function(inputs) {
|
this.sortInputs(function(inputs) {
|
||||||
var copy = Array.prototype.concat.apply([], inputs);
|
var copy = Array.prototype.concat.apply([], inputs);
|
||||||
copy.sort(function compare(first, second) {
|
copy.sort(function(first, second) {
|
||||||
return BufferUtil.compare(first.prevTxId, second.prevTxId)
|
return compare(first.prevTxId, second.prevTxId)
|
||||||
|| first.outputIndex - second.outputIndex;
|
|| first.outputIndex - second.outputIndex;
|
||||||
});
|
});
|
||||||
return copy;
|
return copy;
|
||||||
});
|
});
|
||||||
this.sortOutputs(function(outputs) {
|
this.sortOutputs(function(outputs) {
|
||||||
var copy = Array.prototype.concat.apply([], outputs);
|
var copy = Array.prototype.concat.apply([], outputs);
|
||||||
copy.sort(function compare(first, second) {
|
copy.sort(function(first, second) {
|
||||||
return first.satoshis - second.satoshis
|
return first.satoshis - second.satoshis
|
||||||
|| BufferUtil.compare(first.script.toBuffer(), second.script.toBuffer());
|
|| compare(first.script.toBuffer(), second.script.toBuffer());
|
||||||
});
|
});
|
||||||
return copy;
|
return copy;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -170,49 +170,6 @@ module.exports = {
|
|||||||
hexToBuffer: function hexToBuffer(string) {
|
hexToBuffer: function hexToBuffer(string) {
|
||||||
assert(js.isHexa(string));
|
assert(js.isHexa(string));
|
||||||
return new buffer.Buffer(string, 'hex');
|
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
5
npm-shrinkwrap.json
generated
@ -12,6 +12,11 @@
|
|||||||
"from": "bs58@=2.0.0",
|
"from": "bs58@=2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/bs58/-/bs58-2.0.0.tgz"
|
"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": {
|
"elliptic": {
|
||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
"from": "elliptic@=3.0.3",
|
"from": "elliptic@=3.0.3",
|
||||||
|
|||||||
@ -82,6 +82,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bn.js": "=2.0.4",
|
"bn.js": "=2.0.4",
|
||||||
"bs58": "=2.0.0",
|
"bs58": "=2.0.0",
|
||||||
|
"buffer-compare": "=1.0.0",
|
||||||
"elliptic": "=3.0.3",
|
"elliptic": "=3.0.3",
|
||||||
"hash.js": "=1.0.2",
|
"hash.js": "=1.0.2",
|
||||||
"inherits": "=2.0.1",
|
"inherits": "=2.0.1",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user