diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 1b4226a2..664588e7 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -781,10 +781,7 @@ script.multisig = function(keys, m, n) { while (keys.length < n) keys.push([]); - // Keys need to be in a predictable order. - keys = keys.sort(function(a, b) { - return new bn(a).cmp(new bn(b)) > 0; - }); + keys = utils.sortKeys(keys); return [ m ].concat( keys, diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 36afab7d..b63aeea9 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -586,3 +586,9 @@ utils.hidden = function(obj, prop, value) { }); return obj; }; + +utils.sortKeys = function(keys) { + return keys.sort(function(a, b) { + return new bn(a).cmp(new bn(b)) > 0; + }); +}; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 323246a6..2b3e6b9c 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -170,10 +170,7 @@ Wallet.prototype.addKey = function addKey(key) { this.keys.push(key); - // Keys need to be in a predictable order. - this.keys = this.keys.sort(function(a, b) { - return new bn(a).cmp(new bn(b)) > 0; - }); + this.keys = utils.sortKeys(this.keys); }; Wallet.prototype.removeKey = function removeKey(key) { @@ -190,10 +187,7 @@ Wallet.prototype.removeKey = function removeKey(key) { this.keys.splice(index, 1); - // Keys need to be in a predictable order. - this.keys = this.keys.sort(function(a, b) { - return new bn(a).cmp(new bn(b)) > 0; - }); + this.keys = utils.sortKeys(this.keys); }; Wallet.prototype.derive = function derive() { @@ -267,10 +261,7 @@ Wallet.prototype.getPublicKey = function getPublicKey(enc) { Wallet.prototype.getPublicKeys = function() { var keys = this.keys.slice().map(utils.toKeyArray); - // Keys need to be in a predictable order. - keys = keys.sort(function(a, b) { - return new bn(a).cmp(new bn(b)) > 0; - }); + keys = utils.sortKeys(keys); return keys; };