Merge pull request #1083 from eordano/feature/multisigchecks
Transaction: Check that threshold is less than amount of signatures
This commit is contained in:
commit
e2ff93b714
@ -545,6 +545,8 @@ Script.prototype.removeCodeseparators = function() {
|
|||||||
* public keys before creating the script
|
* public keys before creating the script
|
||||||
*/
|
*/
|
||||||
Script.buildMultisigOut = function(publicKeys, threshold, opts) {
|
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 || {};
|
opts = opts || {};
|
||||||
var script = new Script();
|
var script = new Script();
|
||||||
script.add(Opcode.smallInt(threshold));
|
script.add(Opcode.smallInt(threshold));
|
||||||
|
|||||||
@ -449,6 +449,8 @@ Transaction.prototype._fromNonP2SH = function(utxo) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
|
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);
|
utxo = new UnspentOutput(utxo);
|
||||||
this.addInput(new MultiSigScriptHashInput({
|
this.addInput(new MultiSigScriptHashInput({
|
||||||
output: new Output({
|
output: new Output({
|
||||||
|
|||||||
@ -499,6 +499,12 @@ describe('Address', function() {
|
|||||||
var address2 = Address.createMultisig(publics, 2);
|
var address2 = Address.createMultisig(publics, 2);
|
||||||
address.toString().should.equal(address2.toString());
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -440,6 +440,12 @@ describe('Script', function() {
|
|||||||
s.toString().should.equal('OP_2 33 0x021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18 33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da 33 0x03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9 OP_3 OP_CHECKMULTISIG');
|
s.toString().should.equal('OP_2 33 0x021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18 33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da 33 0x03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9 OP_3 OP_CHECKMULTISIG');
|
||||||
s.isMultisigOut().should.equal(true);
|
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() {
|
it('should create unsorted script if specified', function() {
|
||||||
var s = Script.buildMultisigOut(sortkeys, 2);
|
var s = Script.buildMultisigOut(sortkeys, 2);
|
||||||
var u = Script.buildMultisigOut(sortkeys, 2, {
|
var u = Script.buildMultisigOut(sortkeys, 2, {
|
||||||
|
|||||||
@ -126,6 +126,10 @@ describe('Transaction', function() {
|
|||||||
var changeAddress = 'mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up';
|
var changeAddress = 'mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up';
|
||||||
var changeAddressP2SH = '2N7T3TAetJrSCruQ39aNrJvYLhG1LJosujf';
|
var changeAddressP2SH = '2N7T3TAetJrSCruQ39aNrJvYLhG1LJosujf';
|
||||||
var privateKey = 'cSBnVM4xvxarwGQuAfQFwqDg9k5tErHUHzgWsEfD4zdwUasvqRVY';
|
var privateKey = 'cSBnVM4xvxarwGQuAfQFwqDg9k5tErHUHzgWsEfD4zdwUasvqRVY';
|
||||||
|
var private1 = '6ce7e97e317d2af16c33db0b9270ec047a91bff3eff8558afb5014afb2bb5976';
|
||||||
|
var private2 = 'c9b26b0f771a0d2dad88a44de90f05f416b3b385ff1d989343005546a0032890';
|
||||||
|
var public1 = new PrivateKey(private1).publicKey;
|
||||||
|
var public2 = new PrivateKey(private2).publicKey;
|
||||||
|
|
||||||
var simpleUtxoWith1BTC = {
|
var simpleUtxoWith1BTC = {
|
||||||
address: fromAddress,
|
address: fromAddress,
|
||||||
@ -374,10 +378,6 @@ describe('Transaction', function() {
|
|||||||
expect(deserialized.inputs[0] instanceof Transaction.Input.PublicKeyHash).to.equal(true);
|
expect(deserialized.inputs[0] instanceof Transaction.Input.PublicKeyHash).to.equal(true);
|
||||||
});
|
});
|
||||||
it('can serialize and deserialize a P2SH input', function() {
|
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()
|
var transaction = new Transaction()
|
||||||
.from({
|
.from({
|
||||||
txId: '0000', // Not relevant
|
txId: '0000', // Not relevant
|
||||||
@ -411,6 +411,17 @@ describe('Transaction', function() {
|
|||||||
transaction.addInput(new Transaction.Input(), Script.empty(), 0);
|
transaction.addInput(new Transaction.Input(), Script.empty(), 0);
|
||||||
}).to.not.throw();
|
}).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() {
|
describe('removeInput and removeOutput', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user