Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 28aa52b

Browse files
authored
Merge pull request #255 from DesWurstes/master
Add "Don't sort option" to Multisig inputs - Can't redeem unsorted multisig pubkeys
2 parents 8658360 + 0273db9 commit 28aa52b

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

lib/transaction/input/multisig.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ var TransactionSignature = require('../signature');
1717
/**
1818
* @constructor
1919
*/
20-
function MultiSigInput(input, pubkeys, threshold, signatures) {
20+
function MultiSigInput(input, pubkeys, threshold, signatures, opts) {
21+
opts = opts || {};
2122
Input.apply(this, arguments);
2223
var self = this;
2324
pubkeys = pubkeys || input.publicKeys;
2425
threshold = threshold || input.threshold;
2526
signatures = signatures || input.signatures;
26-
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
27+
if (opts.noSorting) {
28+
this.publicKeys = pubkeys
29+
} else {
30+
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
31+
}
2732
$.checkState(Script.buildMultisigOut(this.publicKeys, threshold).equals(this.output.script),
2833
'Provided public keys don\'t match to the provided output script');
2934
this.publicKeyIndex = {};

lib/transaction/input/multisigscripthash.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ var TransactionSignature = require('../signature');
1919
/**
2020
* @constructor
2121
*/
22-
function MultiSigScriptHashInput(input, pubkeys, threshold, signatures, nestedWitness) {
22+
function MultiSigScriptHashInput(input, pubkeys, threshold, signatures, nestedWitness, opts) {
2323
/* jshint maxstatements:20 */
24+
opts = opts || {};
2425
Input.apply(this, arguments);
2526
var self = this;
2627
pubkeys = pubkeys || input.publicKeys;
2728
threshold = threshold || input.threshold;
2829
signatures = signatures || input.signatures;
2930
this.nestedWitness = nestedWitness ? true : false;
30-
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
31+
if (opts.noSorting) {
32+
this.publicKeys = pubkeys
33+
} else {
34+
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
35+
}
3136
this.redeemScript = Script.buildMultisigOut(this.publicKeys, threshold);
3237
if (this.nestedWitness) {
3338
var nested = Script.buildWitnessMultisigOutFromScript(this.redeemScript);

lib/transaction/transaction.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,11 @@ Transaction.prototype._newTransaction = function() {
606606
* @param {Array=} pubkeys
607607
* @param {number=} threshold
608608
* @param {boolean=} nestedWitness - Indicates that the utxo is nested witness p2sh
609+
* @param {Object=} opts - Several options:
610+
* - noSorting: defaults to false, if true and is multisig, don't
611+
* sort the given public keys before creating the script
609612
*/
610-
Transaction.prototype.from = function(utxo, pubkeys, threshold, nestedWitness) {
613+
Transaction.prototype.from = function(utxo, pubkeys, threshold, nestedWitness, opts) {
611614
if (_.isArray(utxo)) {
612615
var self = this;
613616
_.each(utxo, function(utxo) {
@@ -623,7 +626,7 @@ Transaction.prototype.from = function(utxo, pubkeys, threshold, nestedWitness) {
623626
return this;
624627
}
625628
if (pubkeys && threshold) {
626-
this._fromMultisigUtxo(utxo, pubkeys, threshold, nestedWitness);
629+
this._fromMultisigUtxo(utxo, pubkeys, threshold, nestedWitness, opts);
627630
} else {
628631
this._fromNonP2SH(utxo);
629632
}
@@ -651,7 +654,7 @@ Transaction.prototype._fromNonP2SH = function(utxo) {
651654
}));
652655
};
653656

654-
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold, nestedWitness) {
657+
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold, nestedWitness, opts) {
655658
$.checkArgument(threshold <= pubkeys.length,
656659
'Number of required signatures must be greater than the number of public keys');
657660
var clazz;
@@ -671,7 +674,7 @@ Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold, nes
671674
prevTxId: utxo.txId,
672675
outputIndex: utxo.outputIndex,
673676
script: Script.empty()
674-
}, pubkeys, threshold, false, nestedWitness));
677+
}, pubkeys, threshold, false, nestedWitness, opts));
675678
};
676679

677680
/**

0 commit comments

Comments
 (0)