You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.7 KiB
108 lines
3.7 KiB
'use strict';
|
|
|
|
var _ = require('lodash');
|
|
var JSUtil = require('../../util/js');
|
|
var inherits = require('inherits');
|
|
var Input = require('./input');
|
|
var Output = require('../output');
|
|
var $ = require('../../util/preconditions');
|
|
|
|
var Script = require('../../script');
|
|
var Signature = require('../../crypto/signature');
|
|
var Sighash = require('../sighash');
|
|
var BufferUtil = require('../../util/buffer');
|
|
|
|
function MultiSigScriptHashInput(input, pubkeys, threshold) {
|
|
Input.apply(this, arguments);
|
|
var self = this;
|
|
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
|
|
this.redeemScript = Script.buildMultisigOut(this.publicKeys, threshold);
|
|
$.checkState(Script.buildScriptHashOut(this.redeemScript).equals(this.output.script),
|
|
'Provided public keys don\'t hash to the provided output');
|
|
this.publicKeyIndex = {};
|
|
_.each(this.publicKeys, function(publicKey, index) {
|
|
self.publicKeyIndex[publicKey.toString()] = index;
|
|
});
|
|
this.threshold = threshold;
|
|
// Empty array of signatures
|
|
this.signatures = new Array(this.publicKeys.length);
|
|
}
|
|
inherits(MultiSigScriptHashInput, Input);
|
|
|
|
MultiSigScriptHashInput.prototype.getSignatures = function(transaction, privateKey, index, sigtype) {
|
|
$.checkState(this.output instanceof Output);
|
|
sigtype = sigtype || Signature.SIGHASH_ALL;
|
|
|
|
var self = this;
|
|
var results = [];
|
|
_.each(this.publicKeys, function(publicKey) {
|
|
if (publicKey.toString() === privateKey.publicKey.toString()) {
|
|
results.push({
|
|
publicKey: privateKey.publicKey,
|
|
prevTxId: self.txId,
|
|
outputIndex: self.outputIndex,
|
|
inputIndex: index,
|
|
signature: Sighash.sign(transaction, privateKey, sigtype, index, self.redeemScript),
|
|
sigtype: sigtype
|
|
});
|
|
}
|
|
});
|
|
return results;
|
|
};
|
|
|
|
MultiSigScriptHashInput.prototype.addSignature = function(transaction, signature) {
|
|
$.checkState(!this.isFullySigned(), 'All needed signatures have already been added');
|
|
$.checkArgument(!_.isUndefined(this.publicKeyIndex[signature.publicKey.toString()]),
|
|
'Signature has no matching public key');
|
|
$.checkState(this.isValidSignature(transaction, signature));
|
|
this.signatures[this.publicKeyIndex[signature.publicKey.toString()]] = signature;
|
|
this._updateScript();
|
|
return this;
|
|
};
|
|
|
|
MultiSigScriptHashInput.prototype._updateScript = function() {
|
|
this.setScript(Script.buildP2SHMultisigIn(
|
|
this.publicKeys,
|
|
this.threshold,
|
|
this._createSignatures(),
|
|
{ cachedMultisig: this.redeemScript }
|
|
));
|
|
return this;
|
|
};
|
|
|
|
MultiSigScriptHashInput.prototype._createSignatures = function() {
|
|
var reverseOrder = JSUtil.cloneArray(this.signatures).reverse();
|
|
return _.map(
|
|
_.filter(reverseOrder, function(signature) { return !_.isUndefined(signature); }),
|
|
function(signature) {
|
|
return BufferUtil.concat([
|
|
signature.signature.toDER(),
|
|
BufferUtil.integerAsSingleByteBuffer(signature.sigtype)
|
|
]);
|
|
}
|
|
);
|
|
};
|
|
|
|
MultiSigScriptHashInput.prototype.clearSignatures = function() {
|
|
this.signatures = new Array(this.publicKeys.length);
|
|
this._updateScript();
|
|
};
|
|
|
|
MultiSigScriptHashInput.prototype.isFullySigned = function() {
|
|
var count = _.reduce(this.signatures, function(sum, signature) { return sum + (!!signature); }, 0);
|
|
return count === this.threshold;
|
|
};
|
|
|
|
MultiSigScriptHashInput.prototype.isValidSignature = function(transaction, signature) {
|
|
// FIXME: Refactor signature so this is not necessary
|
|
signature.signature.nhashtype = signature.sigtype;
|
|
return Sighash.verify(
|
|
transaction,
|
|
signature.signature,
|
|
signature.publicKey,
|
|
signature.inputIndex,
|
|
this.redeemScript
|
|
);
|
|
};
|
|
|
|
module.exports = MultiSigScriptHashInput;
|
|
|