Merge pull request #1083 from eordano/feature/multisigchecks

Transaction: Check that threshold is less than amount of signatures
This commit is contained in:
Manuel Aráoz 2015-02-18 12:01:58 -03:00
commit e2ff93b714
5 changed files with 31 additions and 4 deletions

View File

@ -545,6 +545,8 @@ Script.prototype.removeCodeseparators = function() {
* public keys before creating the script
*/
Script.buildMultisigOut = function(publicKeys, threshold, opts) {
$.checkArgument(threshold <= publicKeys.length,
'Number of required signatures must be less than or equal to the number of public keys');
opts = opts || {};
var script = new Script();
script.add(Opcode.smallInt(threshold));

View File

@ -449,6 +449,8 @@ Transaction.prototype._fromNonP2SH = function(utxo) {
};
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
$.checkArgument(threshold <= pubkeys.length,
'Number of required signatures must be greater than the number of public keys');
utxo = new UnspentOutput(utxo);
this.addInput(new MultiSigScriptHashInput({
output: new Output({

View File

@ -499,6 +499,12 @@ describe('Address', function() {
var address2 = Address.createMultisig(publics, 2);
address.toString().should.equal(address2.toString());
});
it('fails if invalid array is provided', function() {
expect(function() {
return Address.createMultisig([],3,'testnet');
}).to.throw('Number of required signatures must be less than or equal to the number of public keys');
});
});
});

View File

@ -440,6 +440,12 @@ describe('Script', function() {
s.toString().should.equal('OP_2 33 0x021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18 33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da 33 0x03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9 OP_3 OP_CHECKMULTISIG');
s.isMultisigOut().should.equal(true);
});
it('should fail when number of required signatures is greater than number of pubkeys', function() {
expect(sortkeys.length).to.equal(3);
expect(function() {
return Script.buildMultisigOut(sortkeys, 4);
}).to.throw('Number of required signatures must be less than or equal to the number of public keys');
});
it('should create unsorted script if specified', function() {
var s = Script.buildMultisigOut(sortkeys, 2);
var u = Script.buildMultisigOut(sortkeys, 2, {

View File

@ -126,6 +126,10 @@ describe('Transaction', function() {
var changeAddress = 'mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up';
var changeAddressP2SH = '2N7T3TAetJrSCruQ39aNrJvYLhG1LJosujf';
var privateKey = 'cSBnVM4xvxarwGQuAfQFwqDg9k5tErHUHzgWsEfD4zdwUasvqRVY';
var private1 = '6ce7e97e317d2af16c33db0b9270ec047a91bff3eff8558afb5014afb2bb5976';
var private2 = 'c9b26b0f771a0d2dad88a44de90f05f416b3b385ff1d989343005546a0032890';
var public1 = new PrivateKey(private1).publicKey;
var public2 = new PrivateKey(private2).publicKey;
var simpleUtxoWith1BTC = {
address: fromAddress,
@ -374,10 +378,6 @@ describe('Transaction', function() {
expect(deserialized.inputs[0] instanceof Transaction.Input.PublicKeyHash).to.equal(true);
});
it('can serialize and deserialize a P2SH input', function() {
var private1 = '6ce7e97e317d2af16c33db0b9270ec047a91bff3eff8558afb5014afb2bb5976';
var private2 = 'c9b26b0f771a0d2dad88a44de90f05f416b3b385ff1d989343005546a0032890';
var public1 = new PrivateKey(private1).publicKey;
var public2 = new PrivateKey(private2).publicKey;
var transaction = new Transaction()
.from({
txId: '0000', // Not relevant
@ -411,6 +411,17 @@ describe('Transaction', function() {
transaction.addInput(new Transaction.Input(), Script.empty(), 0);
}).to.not.throw();
});
it('does not allow a threshold number greater than the amount of public keys', function() {
expect(function() {
transaction = new Transaction();
return transaction.from({
txId: '0000000000000000000000000000000000000000000000000000000000000000',
outputIndex: 0,
script: Script(),
satoshis: 10000
}, [], 1);
}).to.throw('Number of required signatures must be greater than the number of public keys');
});
});
describe('removeInput and removeOutput', function() {