Browse Source

Merge pull request #702 from maraoz/add/interpreter2

add sorting to Script#buildMutlisigOut()
patch-2
Esteban Ordano 10 years ago
parent
commit
3fa3e0b691
  1. 16
      lib/script.js
  2. 21
      test/script.js

16
lib/script.js

@ -470,12 +470,22 @@ Script.prototype.removeCodeseparators = function() {
* requiring m of those public keys to spend
* @param {PublicKey[]} pubkeys - list of all public keys controlling 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();
s.add(Opcode.smallInt(m));
for (var i = 0; i < pubkeys.length; i++) {
var pubkey = pubkeys[i];
var sorted = pubkeys;
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(Opcode.smallInt(pubkeys.length));

21
test/script.js

@ -409,10 +409,24 @@ describe('Script', function() {
'036a98a36aa7665874b1ba9130bc6d318e52fd3bdb5969532d7fc09bf2476ff842',
'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 pubkeys = pubkey_hexs.slice(0, n).map(PublicKey);
var s = Script.buildMultisigOut(pubkeys, m);
should.exist(s);
s.isMultisigOut().should.equal(true);
};
for (var n = 1; n < 6; n++) {
@ -494,5 +508,10 @@ describe('Script', function() {
});
});
describe('#removeCodeseparators', function() {
it('should remove any OP_CODESEPARATORs', function() {
Script('OP_CODESEPARATOR OP_0 OP_CODESEPARATOR').removeCodeseparators().toString().should.equal('OP_0');
});
});
});

Loading…
Cancel
Save