add sorting to Script#buildMutlisigOut()
This commit is contained in:
parent
63bc625c33
commit
ec464681fe
@ -470,12 +470,22 @@ Script.prototype.removeCodeseparators = function() {
|
|||||||
* requiring m of those public keys to spend
|
* requiring m of those public keys to spend
|
||||||
* @param {PublicKey[]} pubkeys - list of all public keys controlling the output
|
* @param {PublicKey[]} pubkeys - list of all public keys controlling the output
|
||||||
* @param {number} m - amount of required signatures to spend the output
|
* @param {number} m - amount of required signatures to spend the output
|
||||||
|
* @param {Object} [opts] - Several options:
|
||||||
|
* - noSorting: defaults to false, if true, don't sort the given
|
||||||
|
* public keys before creating the script
|
||||||
*/
|
*/
|
||||||
Script.buildMultisigOut = function(pubkeys, m) {
|
Script.buildMultisigOut = function(pubkeys, m, opts) {
|
||||||
|
opts = opts || {};
|
||||||
var s = new Script();
|
var s = new Script();
|
||||||
s.add(Opcode.smallInt(m));
|
s.add(Opcode.smallInt(m));
|
||||||
for (var i = 0; i < pubkeys.length; i++) {
|
var sorted = pubkeys;
|
||||||
var pubkey = pubkeys[i];
|
if (!opts.noSorting) {
|
||||||
|
sorted = _.sortBy(pubkeys, function(pubkey) {
|
||||||
|
return pubkey.toString('hex');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (var i = 0; i < sorted.length; i++) {
|
||||||
|
var pubkey = sorted[i];
|
||||||
s.add(pubkey.toBuffer());
|
s.add(pubkey.toBuffer());
|
||||||
}
|
}
|
||||||
s.add(Opcode.smallInt(pubkeys.length));
|
s.add(Opcode.smallInt(pubkeys.length));
|
||||||
|
|||||||
@ -400,7 +400,7 @@ describe('Script', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#buildMultisigOut', function() {
|
describe.only('#buildMultisigOut', function() {
|
||||||
var pubkey_hexs = [
|
var pubkey_hexs = [
|
||||||
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da',
|
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da',
|
||||||
'03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9',
|
'03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9',
|
||||||
@ -409,10 +409,24 @@ describe('Script', function() {
|
|||||||
'036a98a36aa7665874b1ba9130bc6d318e52fd3bdb5969532d7fc09bf2476ff842',
|
'036a98a36aa7665874b1ba9130bc6d318e52fd3bdb5969532d7fc09bf2476ff842',
|
||||||
'033aafcbead78c08b0e0aacc1b0cdb40702a7c709b660bebd286e973242127e15b',
|
'033aafcbead78c08b0e0aacc1b0cdb40702a7c709b660bebd286e973242127e15b',
|
||||||
];
|
];
|
||||||
|
var sortkeys = pubkey_hexs.slice(0, 3).map(PublicKey);
|
||||||
|
it('should create sorted script by default', function() {
|
||||||
|
var s = Script.buildMultisigOut(sortkeys, 2);
|
||||||
|
s.toString().should.equal('OP_2 33 0x021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18 33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da 33 0x03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9 OP_3 OP_CHECKMULTISIG');
|
||||||
|
s.isMultisigOut().should.equal(true);
|
||||||
|
});
|
||||||
|
it('should create unsorted script if specified', function() {
|
||||||
|
var s = Script.buildMultisigOut(sortkeys, 2);
|
||||||
|
var u = Script.buildMultisigOut(sortkeys, 2, {
|
||||||
|
noSorting: true
|
||||||
|
});
|
||||||
|
s.toString().should.not.equal(u.toString());
|
||||||
|
u.toString().should.equal('OP_2 33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da 33 0x03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9 33 0x021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18 OP_3 OP_CHECKMULTISIG');
|
||||||
|
s.isMultisigOut().should.equal(true);
|
||||||
|
});
|
||||||
var test_mn = function(m, n) {
|
var test_mn = function(m, n) {
|
||||||
var pubkeys = pubkey_hexs.slice(0, n).map(PublicKey);
|
var pubkeys = pubkey_hexs.slice(0, n).map(PublicKey);
|
||||||
var s = Script.buildMultisigOut(pubkeys, m);
|
var s = Script.buildMultisigOut(pubkeys, m);
|
||||||
should.exist(s);
|
|
||||||
s.isMultisigOut().should.equal(true);
|
s.isMultisigOut().should.equal(true);
|
||||||
};
|
};
|
||||||
for (var n = 1; n < 6; n++) {
|
for (var n = 1; n < 6; n++) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user