bitcore/lib/transaction/input/publickeyhash.js

95 lines
2.8 KiB
JavaScript
Raw Normal View History

'use strict';
var inherits = require('inherits');
2014-12-11 08:31:02 -08:00
var $ = require('../../util/preconditions');
var BufferUtil = require('../../util/buffer');
var Hash = require('../../crypto/hash');
2014-12-11 08:31:02 -08:00
var Input = require('./input');
var Output = require('../output');
var Sighash = require('../sighash');
2014-12-11 08:31:02 -08:00
var Script = require('../../script');
var Signature = require('../../crypto/signature');
2014-12-11 08:31:02 -08:00
/**
* Represents a special kind of input of PayToPublicKeyHash kind.
2014-12-16 12:06:01 -08:00
* @constructor
2014-12-11 08:31:02 -08:00
*/
function PublicKeyHashInput() {
2014-12-11 08:31:02 -08:00
Input.apply(this, arguments);
}
inherits(PublicKeyHashInput, Input);
2014-12-11 08:31:02 -08:00
/* jshint maxparams: 5 */
/**
* @param {Transaction} transaction - the transaction to be signed
* @param {PrivateKey} privateKey - the private key with which to sign the transaction
* @param {number} index - the index of the input in the transaction input vector
2014-12-15 22:29:52 -08:00
* @param {number=} sigtype - the type of signature, defaults to Signature.SIGHASH_ALL
2014-12-11 08:31:02 -08:00
* @param {Buffer=} hashData - the precalculated hash of the public key associated with the privateKey provided
* @return {Array} of objects that can be
*/
PublicKeyHashInput.prototype.getSignatures = function(transaction, privateKey, index, sigtype, hashData) {
2014-12-11 08:31:02 -08:00
$.checkState(this.output instanceof Output);
hashData = hashData || Hash.sha256ripemd160(privateKey.publicKey.toBuffer());
sigtype = sigtype || Signature.SIGHASH_ALL;
2014-12-11 08:31:02 -08:00
if (BufferUtil.equals(hashData, this.output.script.getPublicKeyHash())) {
return [{
publicKey: privateKey.publicKey,
prevTxId: this.txId,
outputIndex: this.outputIndex,
inputIndex: index,
signature: Sighash.sign(transaction, privateKey, sigtype, index, this.output.script),
sigtype: sigtype
}];
}
return [];
};
2014-12-11 08:31:02 -08:00
/* jshint maxparams: 3 */
/**
* Add the provided signature
*
* @param {Object} signature
* @param {PublicKey} signature.publicKey
* @param {Signature} signature.signature
2014-12-15 22:29:52 -08:00
* @param {number=} signature.sigtype
2014-12-11 08:31:02 -08:00
* @return {PublicKeyHashInput} this, for chaining
*/
PublicKeyHashInput.prototype.addSignature = function(transaction, signature) {
$.checkState(this.isValidSignature(transaction, signature), 'Signature is invalid');
2014-12-11 08:31:02 -08:00
this.setScript(Script.buildPublicKeyHashIn(
signature.publicKey,
signature.signature.toDER(),
signature.sigtype
));
return this;
};
/**
* Clear the input's signature
* @return {PublicKeyHashInput} this, for chaining
*/
2014-12-16 14:55:46 -08:00
PublicKeyHashInput.prototype.clearSignatures = function() {
2014-12-11 08:31:02 -08:00
this.setScript(Script.empty());
return this;
};
/**
* Query whether the input is signed
* @return {boolean}
*/
PublicKeyHashInput.prototype.isFullySigned = function() {
return this.script.isPublicKeyHashIn();
};
PublicKeyHashInput.SCRIPT_MAX_SIZE = 73 + 34; // sigsize (1 + 72) + pubkey (1 + 33)
2014-12-16 14:55:46 -08:00
PublicKeyHashInput.prototype._estimateSize = function() {
return PublicKeyHashInput.SCRIPT_MAX_SIZE;
2014-12-16 14:55:46 -08:00
};
module.exports = PublicKeyHashInput;