Merge pull request #209 from matiu/bug/sort-pubkeys-in-multisig
Bug/sort pubkeys in multisig
This commit is contained in:
commit
0fd1848eb4
23
Script.js
23
Script.js
|
@ -420,7 +420,28 @@ Script.createPubKeyHashOut = function(pubKeyHash) {
|
||||||
return script;
|
return script;
|
||||||
};
|
};
|
||||||
|
|
||||||
Script.createMultisig = function(n_required, keys) {
|
Script._sortKeys = function(keys) {
|
||||||
|
return keys.sort(function(buf1, buf2) {
|
||||||
|
var len = buf1.length > buf1.length ? buf1.length : buf2.length;
|
||||||
|
for (var i = 0; i <= len; i++) {
|
||||||
|
if (buf1[i] === undefined)
|
||||||
|
return -1; //shorter strings come first
|
||||||
|
if (buf2[i] === undefined)
|
||||||
|
return 1;
|
||||||
|
if (buf1[i] < buf2[i])
|
||||||
|
return -1;
|
||||||
|
if (buf1[i] > buf2[i])
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Script.createMultisig = function(n_required, inKeys, opts) {
|
||||||
|
opts = opts || {};
|
||||||
|
var keys = opts.noSorting ? inKeys : this._sortKeys(inKeys);
|
||||||
var script = new Script();
|
var script = new Script();
|
||||||
script.writeN(n_required);
|
script.writeN(n_required);
|
||||||
keys.forEach(function(key) {
|
keys.forEach(function(key) {
|
||||||
|
|
|
@ -68,7 +68,7 @@
|
||||||
],
|
],
|
||||||
"unspentP2sh": [
|
"unspentP2sh": [
|
||||||
{
|
{
|
||||||
"address": "2Mwswt6Eih28xH8611fexpqKqJCLJMomveK",
|
"address": "2NDJbzwzsmRgD2o5HHXPhuq5g6tkKTjYkd6",
|
||||||
"scriptPubKey": "a91432d272ce8a9b482b363408a0b1dd28123d59c63387",
|
"scriptPubKey": "a91432d272ce8a9b482b363408a0b1dd28123d59c63387",
|
||||||
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
|
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
|
||||||
"vout": 1,
|
"vout": 1,
|
||||||
|
|
|
@ -107,4 +107,47 @@ describe('Script', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Original test from https://github.com/ryanxcharles/treasure
|
||||||
|
var testPubKeysHex = [
|
||||||
|
'02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0',
|
||||||
|
'02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758',
|
||||||
|
'0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea',
|
||||||
|
'02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70',
|
||||||
|
'02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793'
|
||||||
|
];
|
||||||
|
|
||||||
|
describe('#_sortKeys', function() {
|
||||||
|
it('should get the pubkeys in properly sorted order', function() {
|
||||||
|
var pubs = testPubKeysHex.map( function(hex) {
|
||||||
|
return new Buffer(hex,'hex');
|
||||||
|
});
|
||||||
|
var sorted = Script._sortKeys(pubs);
|
||||||
|
sorted[0].toString('hex').should.equal(testPubKeysHex[2]);
|
||||||
|
sorted[1].toString('hex').should.equal(testPubKeysHex[1]);
|
||||||
|
sorted[2].toString('hex').should.equal(testPubKeysHex[0]);
|
||||||
|
sorted[3].toString('hex').should.equal(testPubKeysHex[4]);
|
||||||
|
sorted[4].toString('hex').should.equal(testPubKeysHex[3]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#createMultisig', function() {
|
||||||
|
it('should create ', function() {
|
||||||
|
var pubs = testPubKeysHex.map( function(hex) {
|
||||||
|
return new Buffer(hex,'hex');
|
||||||
|
});
|
||||||
|
var s1 = Script.createMultisig(3,pubs, {noSorting: true});
|
||||||
|
|
||||||
|
// test case generated with: bitcoind createmultisig 3 '["02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0", "02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758", "0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea","02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70", "02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793"]'
|
||||||
|
|
||||||
|
s1.getBuffer().toString('hex').should.equal('532102c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae02102b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758210266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea2102ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e702102c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af79355ae');
|
||||||
|
var s2 = Script.createMultisig(3,pubs);
|
||||||
|
|
||||||
|
// test case generated with: bitcoind createmultisig 3 '["0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea", "02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758", "02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0", "02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793", "02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70"]'
|
||||||
|
s2.getBuffer().toString('hex').should.equal('53210266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea2102b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f07582102c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae02102c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af7932102ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e7055ae');
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue