2014-11-29 16:49:28 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
2014-12-16 14:55:46 -08:00
|
|
|
var $ = require('../util/preconditions');
|
2014-11-29 16:49:28 -08:00
|
|
|
var buffer = require('buffer');
|
|
|
|
|
2014-12-18 07:28:43 -08:00
|
|
|
var errors = require('../errors');
|
2015-02-13 19:21:06 -08:00
|
|
|
var BufferUtil = require('../util/buffer');
|
2014-12-12 16:03:14 -08:00
|
|
|
var JSUtil = require('../util/js');
|
2014-11-29 16:49:28 -08:00
|
|
|
var BufferReader = require('../encoding/bufferreader');
|
|
|
|
var BufferWriter = require('../encoding/bufferwriter');
|
|
|
|
var Hash = require('../crypto/hash');
|
|
|
|
var Signature = require('../crypto/signature');
|
2014-12-15 13:44:44 -08:00
|
|
|
var Sighash = require('./sighash');
|
2014-11-29 16:49:28 -08:00
|
|
|
|
|
|
|
var Address = require('../address');
|
2014-12-30 20:29:36 -08:00
|
|
|
var UnspentOutput = require('./unspentoutput');
|
2014-11-29 16:49:28 -08:00
|
|
|
var Input = require('./input');
|
2014-12-11 08:58:32 -08:00
|
|
|
var PublicKeyHashInput = Input.PublicKeyHash;
|
|
|
|
var MultiSigScriptHashInput = Input.MultiSigScriptHash;
|
2014-11-29 16:49:28 -08:00
|
|
|
var Output = require('./output');
|
|
|
|
var Script = require('../script');
|
|
|
|
var PrivateKey = require('../privatekey');
|
2014-12-16 17:35:09 -08:00
|
|
|
var BN = require('../crypto/bn');
|
2014-11-29 16:49:28 -08:00
|
|
|
|
2014-12-11 08:58:32 -08:00
|
|
|
/**
|
2014-12-16 14:55:46 -08:00
|
|
|
* Represents a transaction, a set of inputs and outputs to change ownership of tokens
|
2014-12-11 08:58:32 -08:00
|
|
|
*
|
|
|
|
* @param {*} serialized
|
2014-12-16 12:06:01 -08:00
|
|
|
* @constructor
|
2014-12-11 08:58:32 -08:00
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
function Transaction(serialized) {
|
|
|
|
if (!(this instanceof Transaction)) {
|
|
|
|
return new Transaction(serialized);
|
|
|
|
}
|
|
|
|
this.inputs = [];
|
|
|
|
this.outputs = [];
|
2015-02-27 11:38:48 -08:00
|
|
|
this._inputAmount = undefined;
|
|
|
|
this._outputAmount = undefined;
|
2014-11-29 16:49:28 -08:00
|
|
|
|
|
|
|
if (serialized) {
|
|
|
|
if (serialized instanceof Transaction) {
|
|
|
|
return Transaction.shallowCopy(serialized);
|
2015-02-13 19:06:51 -08:00
|
|
|
} else if (JSUtil.isHexa(serialized)) {
|
2014-11-29 16:49:28 -08:00
|
|
|
this.fromString(serialized);
|
2015-02-13 19:06:51 -08:00
|
|
|
} else if (JSUtil.isValidJSON(serialized)) {
|
2015-01-05 07:32:40 -08:00
|
|
|
this.fromJSON(serialized);
|
2015-02-13 19:21:06 -08:00
|
|
|
} else if (BufferUtil.isBuffer(serialized)) {
|
2014-11-29 16:49:28 -08:00
|
|
|
this.fromBuffer(serialized);
|
|
|
|
} else if (_.isObject(serialized)) {
|
|
|
|
this.fromObject(serialized);
|
2014-12-18 10:35:48 -08:00
|
|
|
} else {
|
|
|
|
throw new errors.InvalidArgument('Must provide an object or string to deserialize a transaction');
|
2014-11-29 16:49:28 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._newTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 11:12:46 -08:00
|
|
|
var CURRENT_VERSION = 1;
|
|
|
|
var DEFAULT_NLOCKTIME = 0;
|
2015-04-30 10:14:19 -07:00
|
|
|
var MAX_BLOCK_SIZE = 1000000;
|
2015-02-24 11:12:46 -08:00
|
|
|
|
|
|
|
// Minimum amount for an output for it not to be considered a dust output
|
2015-02-24 12:21:11 -08:00
|
|
|
Transaction.DUST_AMOUNT = 546;
|
2015-02-24 11:12:46 -08:00
|
|
|
|
|
|
|
// Margin of error to allow fees in the vecinity of the expected value but doesn't allow a big difference
|
|
|
|
Transaction.FEE_SECURITY_MARGIN = 15;
|
|
|
|
|
2014-12-16 17:35:09 -08:00
|
|
|
// max amount of satoshis in circulation
|
|
|
|
Transaction.MAX_MONEY = 21000000 * 1e8;
|
|
|
|
|
2015-02-09 09:57:45 -08:00
|
|
|
// nlocktime limit to be considered block height rather than a timestamp
|
|
|
|
Transaction.NLOCKTIME_BLOCKHEIGHT_LIMIT = 5e8;
|
|
|
|
|
2015-04-10 15:07:20 -07:00
|
|
|
// Max value for an unsigned 32 bit value
|
2015-02-09 09:57:45 -08:00
|
|
|
Transaction.NLOCKTIME_MAX_VALUE = 4294967295;
|
|
|
|
|
2015-02-24 11:12:46 -08:00
|
|
|
// Value used for fee estimation (satoshis per kilobyte)
|
|
|
|
Transaction.FEE_PER_KB = 10000;
|
|
|
|
|
|
|
|
// Safe upper bound for change address script size in bytes
|
|
|
|
Transaction.CHANGE_OUTPUT_MAX_SIZE = 20 + 4 + 34 + 4;
|
|
|
|
Transaction.MAXIMUM_EXTRA_SIZE = 4 + 9 + 9 + 4;
|
|
|
|
|
2014-11-29 16:49:28 -08:00
|
|
|
/* Constructors and Serialization */
|
|
|
|
|
2014-12-11 08:58:32 -08:00
|
|
|
/**
|
2014-12-16 17:35:09 -08:00
|
|
|
* Create a 'shallow' copy of the transaction, by serializing and deserializing
|
2014-12-11 08:58:32 -08:00
|
|
|
* it dropping any additional information that inputs and outputs may have hold
|
|
|
|
*
|
|
|
|
* @param {Transaction} transaction
|
|
|
|
* @return {Transaction}
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.shallowCopy = function(transaction) {
|
|
|
|
var copy = new Transaction(transaction.toBuffer());
|
|
|
|
return copy;
|
|
|
|
};
|
|
|
|
|
|
|
|
var hashProperty = {
|
|
|
|
configurable: false,
|
2015-01-08 05:57:46 -08:00
|
|
|
enumerable: true,
|
2014-11-29 16:49:28 -08:00
|
|
|
get: function() {
|
|
|
|
return new BufferReader(this._getHash()).readReverse().toString('hex');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperty(Transaction.prototype, 'hash', hashProperty);
|
|
|
|
Object.defineProperty(Transaction.prototype, 'id', hashProperty);
|
|
|
|
|
2015-02-27 11:38:48 -08:00
|
|
|
var ioProperty = {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this._getInputAmount();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperty(Transaction.prototype, 'inputAmount', ioProperty);
|
|
|
|
ioProperty.get = function() {
|
|
|
|
return this._getOutputAmount();
|
|
|
|
};
|
|
|
|
Object.defineProperty(Transaction.prototype, 'outputAmount', ioProperty);
|
|
|
|
|
2014-12-11 08:58:32 -08:00
|
|
|
/**
|
|
|
|
* Retrieve the little endian hash of the transaction (used for serialization)
|
|
|
|
* @return {Buffer}
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype._getHash = function() {
|
|
|
|
return Hash.sha256sha256(this.toBuffer());
|
|
|
|
};
|
|
|
|
|
2014-12-11 08:58:32 -08:00
|
|
|
/**
|
|
|
|
* Retrieve a hexa string that can be used with bitcoind's CLI interface
|
|
|
|
* (decoderawtransaction, sendrawtransaction)
|
|
|
|
*
|
2015-02-24 07:55:02 -08:00
|
|
|
* @param {Object|boolean=} unsafe if true, skip all tests. if it's an object,
|
|
|
|
* it's expected to contain a set of flags to skip certain tests:
|
2015-02-24 10:50:28 -08:00
|
|
|
* * `disableAll`: disable all checks
|
|
|
|
* * `disableSmallFees`: disable checking for fees that are too small
|
|
|
|
* * `disableLargeFees`: disable checking for fees that are too large
|
|
|
|
* * `disableNotFullySigned`: disable checking if all inputs are fully signed
|
|
|
|
* * `disableDustOutputs`: disable checking if there are no outputs that are dust amounts
|
|
|
|
* * `disableMoreOutputThanInput`: disable checking if the transaction spends more bitcoins than the sum of the input amounts
|
2014-12-11 08:58:32 -08:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2014-12-18 07:28:43 -08:00
|
|
|
Transaction.prototype.serialize = function(unsafe) {
|
2015-02-24 07:55:02 -08:00
|
|
|
if (true === unsafe || unsafe && unsafe.disableAll) {
|
2014-12-18 07:28:43 -08:00
|
|
|
return this.uncheckedSerialize();
|
|
|
|
} else {
|
2015-02-24 07:55:02 -08:00
|
|
|
return this.checkedSerialize(unsafe);
|
2014-12-18 07:28:43 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-16 07:42:27 -08:00
|
|
|
Transaction.prototype.uncheckedSerialize = Transaction.prototype.toString = function() {
|
2014-11-29 16:49:28 -08:00
|
|
|
return this.toBuffer().toString('hex');
|
|
|
|
};
|
|
|
|
|
2015-02-24 07:55:02 -08:00
|
|
|
/**
|
|
|
|
* Retrieve a hexa string that can be used with bitcoind's CLI interface
|
|
|
|
* (decoderawtransaction, sendrawtransaction)
|
|
|
|
*
|
2015-02-24 11:32:37 -08:00
|
|
|
* @param {Object} opts allows to skip certain tests. {@see Transaction#serialize}
|
2015-02-24 07:55:02 -08:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2015-02-24 08:04:40 -08:00
|
|
|
Transaction.prototype.checkedSerialize = function(opts) {
|
2015-02-24 10:50:28 -08:00
|
|
|
var serializationError = this.getSerializationError(opts);
|
|
|
|
if (serializationError) {
|
2015-02-24 11:22:41 -08:00
|
|
|
serializationError.message += ' Use Transaction#uncheckedSerialize if you want to skip security checks. ' +
|
2015-04-30 10:14:19 -07:00
|
|
|
'See http://bitcore.io/guide/transaction.html#Serialization for more info.';
|
2015-02-24 10:50:28 -08:00
|
|
|
throw serializationError;
|
|
|
|
}
|
|
|
|
return this.uncheckedSerialize();
|
|
|
|
};
|
|
|
|
|
2015-04-30 10:14:19 -07:00
|
|
|
Transaction.prototype.invalidSatoshis = function() {
|
|
|
|
var invalid = false;
|
|
|
|
for (var i = 0; i < this.outputs.length; i++) {
|
|
|
|
if (this.outputs[i].invalidSatoshis()) {
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return invalid;
|
|
|
|
};
|
|
|
|
|
2015-02-24 10:50:28 -08:00
|
|
|
/**
|
2015-05-12 15:26:33 -07:00
|
|
|
* Retrieve a possible error that could appear when trying to serialize and
|
|
|
|
* broadcast this transaction.
|
2015-02-24 10:50:28 -08:00
|
|
|
*
|
2015-02-24 11:32:37 -08:00
|
|
|
* @param {Object} opts allows to skip certain tests. {@see Transaction#serialize}
|
2015-02-24 10:50:28 -08:00
|
|
|
* @return {bitcore.Error}
|
|
|
|
*/
|
|
|
|
Transaction.prototype.getSerializationError = function(opts) {
|
2015-02-24 08:04:40 -08:00
|
|
|
opts = opts || {};
|
2015-04-30 10:14:19 -07:00
|
|
|
|
2015-05-12 15:26:33 -07:00
|
|
|
return this._isInvalidSatoshis() ||
|
|
|
|
this._hasFeeError(opts) ||
|
|
|
|
this._hasDustOutputs(opts) ||
|
|
|
|
this._isMissingSignatures(opts) ||
|
|
|
|
this._hasMoreOutputThanInput(opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._isInvalidSatoshis = function() {
|
2015-04-30 10:14:19 -07:00
|
|
|
if (this.invalidSatoshis()) {
|
|
|
|
return new errors.Transaction.InvalidSatoshis();
|
|
|
|
}
|
2015-05-12 15:26:33 -07:00
|
|
|
};
|
2015-04-30 10:14:19 -07:00
|
|
|
|
2015-05-12 15:26:33 -07:00
|
|
|
Transaction.prototype._hasFeeError = function(opts) {
|
|
|
|
return this._isFeeDifferent() ||
|
|
|
|
this._isFeeTooLarge(opts) ||
|
|
|
|
this._isFeeTooSmall(opts);
|
2014-12-18 07:28:43 -08:00
|
|
|
};
|
|
|
|
|
2015-05-09 09:44:26 -07:00
|
|
|
Transaction.prototype._isFeeDifferent = function() {
|
2015-05-09 12:06:40 -07:00
|
|
|
if (!_.isUndefined(this._fee)) {
|
|
|
|
var fee = this._fee;
|
|
|
|
var unspent = this._getUnspentValue();
|
|
|
|
if (fee !== unspent) {
|
2015-05-16 13:50:44 -07:00
|
|
|
return new errors.Transaction.FeeError.Different('Unspent value is ' + unspent + ' but specified fee is ' + fee);
|
2015-05-09 12:06:40 -07:00
|
|
|
}
|
2015-05-09 09:44:26 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-12 15:26:33 -07:00
|
|
|
Transaction.prototype._isFeeTooLarge = function(opts) {
|
|
|
|
if (opts.disableLargeFees) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-09 12:06:40 -07:00
|
|
|
var fee = this._getUnspentValue();
|
2015-02-24 07:55:02 -08:00
|
|
|
var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee());
|
|
|
|
if (fee > maximumFee) {
|
2015-05-12 15:26:33 -07:00
|
|
|
if (this._missingChange()) {
|
2015-05-16 13:54:30 -07:00
|
|
|
return new errors.Transaction.ChangeAddressMissing('Fee is too large and no change address was provided');
|
2015-05-12 15:26:33 -07:00
|
|
|
}
|
2015-05-16 13:50:44 -07:00
|
|
|
return new errors.Transaction.FeeError.TooLarge('expected less than ' + maximumFee + ' but got ' + fee);
|
2015-02-16 07:42:27 -08:00
|
|
|
}
|
2015-02-24 07:55:02 -08:00
|
|
|
};
|
|
|
|
|
2015-05-12 15:26:33 -07:00
|
|
|
Transaction.prototype._isFeeTooSmall = function(opts) {
|
|
|
|
if (opts.disableSmallFees) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-09 12:06:40 -07:00
|
|
|
var fee = this._getUnspentValue();
|
2015-02-24 07:55:02 -08:00
|
|
|
var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN);
|
|
|
|
if (fee < minimumFee) {
|
2015-05-16 13:50:44 -07:00
|
|
|
return new errors.Transaction.FeeError.TooSmall('expected more than ' + minimumFee + ' but got ' + fee);
|
2014-12-18 07:28:43 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-10 13:12:45 -08:00
|
|
|
Transaction.prototype._missingChange = function() {
|
|
|
|
return !this._changeScript;
|
2014-12-18 07:28:43 -08:00
|
|
|
};
|
|
|
|
|
2015-05-12 15:26:33 -07:00
|
|
|
Transaction.prototype._hasDustOutputs = function(opts) {
|
|
|
|
if (opts.disableDustOutputs) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-06 04:32:12 -08:00
|
|
|
var index, output;
|
|
|
|
for (index in this.outputs) {
|
|
|
|
output = this.outputs[index];
|
|
|
|
if (output.satoshis < Transaction.DUST_AMOUNT && !output.script.isDataOut()) {
|
2015-05-12 15:26:33 -07:00
|
|
|
return new errors.Transaction.DustOutputs();
|
2015-01-08 11:38:43 -08:00
|
|
|
}
|
|
|
|
}
|
2015-05-12 15:26:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._isMissingSignatures = function(opts) {
|
|
|
|
if (opts.disableIsFullySigned) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.isFullySigned()) {
|
|
|
|
return new errors.Transaction.MissingSignatures();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._hasMoreOutputThanInput = function(opts) {
|
|
|
|
if (opts.disableMoreOutputThanInput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._getUnspentValue() < 0) {
|
|
|
|
return new errors.Transaction.InvalidOutputAmountSum();
|
|
|
|
}
|
2015-01-08 11:38:43 -08:00
|
|
|
};
|
|
|
|
|
2014-12-15 22:11:55 -08:00
|
|
|
Transaction.prototype.inspect = function() {
|
2015-02-06 05:10:29 -08:00
|
|
|
return '<Transaction: ' + this.uncheckedSerialize() + '>';
|
2014-12-14 12:13:01 -08:00
|
|
|
};
|
|
|
|
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.toBuffer = function() {
|
|
|
|
var writer = new BufferWriter();
|
|
|
|
return this.toBufferWriter(writer).toBuffer();
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype.toBufferWriter = function(writer) {
|
|
|
|
writer.writeUInt32LE(this.version);
|
|
|
|
writer.writeVarintNum(this.inputs.length);
|
|
|
|
_.each(this.inputs, function(input) {
|
|
|
|
input.toBufferWriter(writer);
|
|
|
|
});
|
|
|
|
writer.writeVarintNum(this.outputs.length);
|
|
|
|
_.each(this.outputs, function(output) {
|
|
|
|
output.toBufferWriter(writer);
|
|
|
|
});
|
|
|
|
writer.writeUInt32LE(this.nLockTime);
|
|
|
|
return writer;
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype.fromBuffer = function(buffer) {
|
|
|
|
var reader = new BufferReader(buffer);
|
|
|
|
return this.fromBufferReader(reader);
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype.fromBufferReader = function(reader) {
|
2015-02-02 11:48:13 -08:00
|
|
|
$.checkArgument(!reader.finished(), 'No transaction data received');
|
2014-11-29 16:49:28 -08:00
|
|
|
var i, sizeTxIns, sizeTxOuts;
|
|
|
|
|
|
|
|
this.version = reader.readUInt32LE();
|
|
|
|
sizeTxIns = reader.readVarintNum();
|
|
|
|
for (i = 0; i < sizeTxIns; i++) {
|
|
|
|
var input = Input.fromBufferReader(reader);
|
|
|
|
this.inputs.push(input);
|
|
|
|
}
|
|
|
|
sizeTxOuts = reader.readVarintNum();
|
|
|
|
for (i = 0; i < sizeTxOuts; i++) {
|
|
|
|
this.outputs.push(Output.fromBufferReader(reader));
|
|
|
|
}
|
|
|
|
this.nLockTime = reader.readUInt32LE();
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype.fromJSON = function(json) {
|
2014-12-12 16:03:14 -08:00
|
|
|
if (JSUtil.isValidJSON(json)) {
|
|
|
|
json = JSON.parse(json);
|
|
|
|
}
|
2015-02-10 13:12:45 -08:00
|
|
|
return this.fromObject(json);
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-12 16:03:14 -08:00
|
|
|
Transaction.prototype.toObject = function toObject() {
|
2014-11-29 16:49:28 -08:00
|
|
|
var inputs = [];
|
|
|
|
this.inputs.forEach(function(input) {
|
2014-12-13 10:11:03 -08:00
|
|
|
inputs.push(input.toObject());
|
2014-11-29 16:49:28 -08:00
|
|
|
});
|
|
|
|
var outputs = [];
|
|
|
|
this.outputs.forEach(function(output) {
|
2014-12-13 10:11:03 -08:00
|
|
|
outputs.push(output.toObject());
|
2014-11-29 16:49:28 -08:00
|
|
|
});
|
2015-02-11 06:40:24 -08:00
|
|
|
var obj = {
|
2014-11-29 16:49:28 -08:00
|
|
|
version: this.version,
|
|
|
|
inputs: inputs,
|
|
|
|
outputs: outputs,
|
|
|
|
nLockTime: this.nLockTime
|
|
|
|
};
|
2015-02-11 06:40:24 -08:00
|
|
|
if (this._changeScript) {
|
|
|
|
obj.changeScript = this._changeScript.toString();
|
|
|
|
}
|
|
|
|
if (!_.isUndefined(this._changeIndex)) {
|
|
|
|
obj.changeIndex = this._changeIndex;
|
|
|
|
}
|
|
|
|
if (!_.isUndefined(this._fee)) {
|
|
|
|
obj.fee = this._fee;
|
|
|
|
}
|
|
|
|
return obj;
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-18 10:35:48 -08:00
|
|
|
Transaction.prototype.fromObject = function(transaction) {
|
|
|
|
var self = this;
|
2015-04-28 12:42:56 -07:00
|
|
|
if (transaction instanceof Transaction) {
|
|
|
|
transaction = transaction.toObject();
|
|
|
|
}
|
2014-12-18 10:35:48 -08:00
|
|
|
_.each(transaction.inputs, function(input) {
|
2015-02-11 06:40:24 -08:00
|
|
|
if (!input.output || !input.output.script) {
|
|
|
|
self.uncheckedAddInput(new Input(input));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input.output.script = new Script(input.output.script);
|
|
|
|
var txin;
|
|
|
|
if (input.output.script.isPublicKeyHashOut()) {
|
|
|
|
txin = new Input.PublicKeyHash(input);
|
|
|
|
} else if (input.output.script.isScriptHashOut() && input.publicKeys && input.threshold) {
|
|
|
|
txin = new Input.MultiSigScriptHash(
|
|
|
|
input, input.publicKeys, input.threshold, input.signatures
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new errors.Transaction.Input.UnsupportedScript(input.output.script);
|
2015-01-06 04:26:14 -08:00
|
|
|
}
|
2015-02-11 06:40:24 -08:00
|
|
|
self.addInput(txin);
|
2014-12-18 10:35:48 -08:00
|
|
|
});
|
|
|
|
_.each(transaction.outputs, function(output) {
|
|
|
|
self.addOutput(new Output(output));
|
|
|
|
});
|
2015-02-10 13:12:45 -08:00
|
|
|
if (transaction.changeIndex) {
|
|
|
|
this._changeIndex = transaction.changeIndex;
|
|
|
|
}
|
|
|
|
if (transaction.changeScript) {
|
|
|
|
this._changeScript = new Script(transaction.changeScript);
|
2015-01-05 07:32:40 -08:00
|
|
|
}
|
2015-02-04 07:45:02 -08:00
|
|
|
if (transaction.fee) {
|
|
|
|
this.fee(transaction.fee);
|
|
|
|
}
|
2014-12-18 10:35:48 -08:00
|
|
|
this.nLockTime = transaction.nLockTime;
|
|
|
|
this.version = transaction.version;
|
2015-02-10 13:12:45 -08:00
|
|
|
this._checkConsistency();
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._checkConsistency = function() {
|
|
|
|
if (!_.isUndefined(this._changeIndex)) {
|
|
|
|
$.checkState(this._changeScript);
|
|
|
|
$.checkState(this.outputs[this._changeIndex]);
|
|
|
|
$.checkState(this.outputs[this._changeIndex].script.toString() ===
|
2015-02-27 11:38:48 -08:00
|
|
|
this._changeScript.toString());
|
2015-02-10 13:12:45 -08:00
|
|
|
}
|
|
|
|
// TODO: add other checks
|
2014-12-18 10:35:48 -08:00
|
|
|
};
|
|
|
|
|
2015-01-28 07:19:02 -08:00
|
|
|
/**
|
2015-02-09 09:57:45 -08:00
|
|
|
* Sets nLockTime so that transaction is not valid until the desired date(a
|
|
|
|
* timestamp in seconds since UNIX epoch is also accepted)
|
|
|
|
*
|
2015-02-10 13:12:45 -08:00
|
|
|
* @param {Date | Number} time
|
2015-02-09 09:57:45 -08:00
|
|
|
* @return {Transaction} this
|
2015-01-28 07:19:02 -08:00
|
|
|
*/
|
2015-02-09 09:57:45 -08:00
|
|
|
Transaction.prototype.lockUntilDate = function(time) {
|
2015-01-28 07:19:02 -08:00
|
|
|
$.checkArgument(time);
|
2015-02-09 09:57:45 -08:00
|
|
|
if (_.isNumber(time) && time < Transaction.NLOCKTIME_BLOCKHEIGHT_LIMIT) {
|
|
|
|
throw new errors.Transaction.LockTimeTooEarly();
|
|
|
|
}
|
|
|
|
if (_.isDate(time)) {
|
|
|
|
time = time.getTime() / 1000;
|
|
|
|
}
|
|
|
|
this.nLockTime = time;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets nLockTime so that transaction is not valid until the desired block
|
|
|
|
* height.
|
|
|
|
*
|
2015-02-10 05:11:09 -08:00
|
|
|
* @param {Number} height
|
2015-02-09 09:57:45 -08:00
|
|
|
* @return {Transaction} this
|
|
|
|
*/
|
2015-02-10 05:11:09 -08:00
|
|
|
Transaction.prototype.lockUntilBlockHeight = function(height) {
|
|
|
|
$.checkArgument(_.isNumber(height));
|
|
|
|
if (height >= Transaction.NLOCKTIME_BLOCKHEIGHT_LIMIT) {
|
2015-02-09 09:57:45 -08:00
|
|
|
throw new errors.Transaction.BlockHeightTooHigh();
|
|
|
|
}
|
2015-02-10 05:11:09 -08:00
|
|
|
if (height < 0) {
|
2015-02-09 09:57:45 -08:00
|
|
|
throw new errors.Transaction.NLockTimeOutOfRange();
|
|
|
|
}
|
2015-02-10 05:11:09 -08:00
|
|
|
this.nLockTime = height;
|
2015-02-09 09:57:45 -08:00
|
|
|
return this;
|
2015-01-28 07:19:02 -08:00
|
|
|
};
|
|
|
|
|
2015-02-10 08:03:07 -08:00
|
|
|
/**
|
|
|
|
* Returns a semantic version of the transaction's nLockTime.
|
|
|
|
* @return {Number|Date}
|
|
|
|
* If nLockTime is 0, it returns null,
|
|
|
|
* if it is < 500000000, it returns a block height (number)
|
|
|
|
* else it returns a Date object.
|
|
|
|
*/
|
|
|
|
Transaction.prototype.getLockTime = function() {
|
|
|
|
if (!this.nLockTime) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (this.nLockTime < Transaction.NLOCKTIME_BLOCKHEIGHT_LIMIT) {
|
|
|
|
return this.nLockTime;
|
|
|
|
}
|
2015-02-10 13:12:45 -08:00
|
|
|
return new Date(1000 * this.nLockTime);
|
2015-02-10 08:03:07 -08:00
|
|
|
};
|
|
|
|
|
2014-12-12 16:03:14 -08:00
|
|
|
Transaction.prototype.toJSON = function toJSON() {
|
|
|
|
return JSON.stringify(this.toObject());
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-12 16:03:14 -08:00
|
|
|
Transaction.prototype.fromString = function(string) {
|
|
|
|
this.fromBuffer(new buffer.Buffer(string, 'hex'));
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._newTransaction = function() {
|
|
|
|
this.version = CURRENT_VERSION;
|
|
|
|
this.nLockTime = DEFAULT_NLOCKTIME;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Transaction creation interface */
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Add an input to this transaction. This is a high level interface
|
|
|
|
* to add an input, for more control, use @{link Transaction#addInput}.
|
|
|
|
*
|
|
|
|
* Can receive, as output information, the output of bitcoind's `listunspent` command,
|
|
|
|
* and a slightly fancier format recognized by bitcore:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* {
|
|
|
|
* address: 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1',
|
|
|
|
* txId: 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
|
|
|
|
* outputIndex: 0,
|
|
|
|
* script: Script.empty(),
|
|
|
|
* satoshis: 1020000
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* Where `address` can be either a string or a bitcore Address object. The
|
|
|
|
* same is true for `script`, which can be a string or a bitcore Script.
|
|
|
|
*
|
|
|
|
* Beware that this resets all the signatures for inputs (in further versions,
|
|
|
|
* SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset).
|
|
|
|
*
|
|
|
|
* @example
|
2014-12-18 08:41:09 -08:00
|
|
|
* ```javascript
|
2014-12-16 14:55:46 -08:00
|
|
|
* var transaction = new Transaction();
|
|
|
|
*
|
|
|
|
* // From a pay to public key hash output from bitcoind's listunspent
|
|
|
|
* transaction.from({'txid': '0000...', vout: 0, amount: 0.1, scriptPubKey: 'OP_DUP ...'});
|
|
|
|
*
|
|
|
|
* // From a pay to public key hash output
|
|
|
|
* transaction.from({'txId': '0000...', outputIndex: 0, satoshis: 1000, script: 'OP_DUP ...'});
|
|
|
|
*
|
|
|
|
* // From a multisig P2SH output
|
|
|
|
* transaction.from({'txId': '0000...', inputIndex: 0, satoshis: 1000, script: '... OP_HASH'},
|
|
|
|
* ['03000...', '02000...'], 2);
|
2014-12-18 08:41:09 -08:00
|
|
|
* ```
|
2014-12-16 14:55:46 -08:00
|
|
|
*
|
|
|
|
* @param {Object} utxo
|
|
|
|
* @param {Array=} pubkeys
|
|
|
|
* @param {number=} threshold
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.from = function(utxo, pubkeys, threshold) {
|
2014-12-30 17:12:24 -08:00
|
|
|
if (_.isArray(utxo)) {
|
|
|
|
var self = this;
|
|
|
|
_.each(utxo, function(utxo) {
|
|
|
|
self.from(utxo, pubkeys, threshold);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
2015-01-12 05:56:27 -08:00
|
|
|
var exists = _.any(this.inputs, function(input) {
|
|
|
|
// TODO: Maybe prevTxId should be a string? Or defined as read only property?
|
|
|
|
return input.prevTxId.toString('hex') === utxo.txId && input.outputIndex === utxo.outputIndex;
|
|
|
|
});
|
|
|
|
if (exists) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-29 16:49:28 -08:00
|
|
|
if (pubkeys && threshold) {
|
2014-12-30 17:12:24 -08:00
|
|
|
this._fromMultisigUtxo(utxo, pubkeys, threshold);
|
2014-11-29 16:49:28 -08:00
|
|
|
} else {
|
|
|
|
this._fromNonP2SH(utxo);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._fromNonP2SH = function(utxo) {
|
2015-02-04 07:10:47 -08:00
|
|
|
var clazz;
|
2014-12-30 20:29:36 -08:00
|
|
|
utxo = new UnspentOutput(utxo);
|
2015-02-04 07:10:47 -08:00
|
|
|
if (utxo.script.isPublicKeyHashOut()) {
|
|
|
|
clazz = PublicKeyHashInput;
|
|
|
|
} else {
|
|
|
|
clazz = Input;
|
|
|
|
}
|
|
|
|
this.addInput(new clazz({
|
2014-12-11 06:44:42 -08:00
|
|
|
output: new Output({
|
|
|
|
script: utxo.script,
|
|
|
|
satoshis: utxo.satoshis
|
|
|
|
}),
|
2014-11-29 16:49:28 -08:00
|
|
|
prevTxId: utxo.txId,
|
|
|
|
outputIndex: utxo.outputIndex,
|
|
|
|
script: Script.empty()
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2014-12-30 17:12:24 -08:00
|
|
|
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
|
2015-02-18 04:43:47 -08:00
|
|
|
$.checkArgument(threshold <= pubkeys.length,
|
2015-02-27 11:38:48 -08:00
|
|
|
'Number of required signatures must be greater than the number of public keys');
|
2014-12-30 20:29:36 -08:00
|
|
|
utxo = new UnspentOutput(utxo);
|
2014-12-18 07:28:43 -08:00
|
|
|
this.addInput(new MultiSigScriptHashInput({
|
2014-12-11 08:58:32 -08:00
|
|
|
output: new Output({
|
|
|
|
script: utxo.script,
|
|
|
|
satoshis: utxo.satoshis
|
|
|
|
}),
|
|
|
|
prevTxId: utxo.txId,
|
|
|
|
outputIndex: utxo.outputIndex,
|
|
|
|
script: Script.empty()
|
|
|
|
}, pubkeys, threshold));
|
2014-12-18 07:28:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an input to this transaction. The input must be an instance of the `Input` class.
|
|
|
|
* It should have information about the Output that it's spending, but if it's not already
|
|
|
|
* set, two additional parameters, `outputScript` and `satoshis` can be provided.
|
|
|
|
*
|
|
|
|
* @param {Input} input
|
|
|
|
* @param {String|Script} outputScript
|
|
|
|
* @param {number} satoshis
|
|
|
|
* @return Transaction this, for chaining
|
|
|
|
*/
|
|
|
|
Transaction.prototype.addInput = function(input, outputScript, satoshis) {
|
|
|
|
$.checkArgumentType(input, Input, 'input');
|
2015-02-04 08:15:06 -08:00
|
|
|
if (!input.output && (_.isUndefined(outputScript) || _.isUndefined(satoshis))) {
|
2014-12-18 10:35:48 -08:00
|
|
|
throw new errors.Transaction.NeedMoreInfo('Need information about the UTXO script and satoshis');
|
2014-12-18 07:28:43 -08:00
|
|
|
}
|
2015-02-04 08:15:06 -08:00
|
|
|
if (!input.output && outputScript && !_.isUndefined(satoshis)) {
|
2014-12-18 07:28:43 -08:00
|
|
|
outputScript = outputScript instanceof Script ? outputScript : new Script(outputScript);
|
|
|
|
$.checkArgumentType(satoshis, 'number', 'satoshis');
|
|
|
|
input.output = new Output({
|
|
|
|
script: outputScript,
|
|
|
|
satoshis: satoshis
|
|
|
|
});
|
|
|
|
}
|
2014-12-19 05:30:20 -08:00
|
|
|
return this.uncheckedAddInput(input);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an input to this transaction, without checking that the input has information about
|
|
|
|
* the output that it's spending.
|
|
|
|
*
|
|
|
|
* @param {Input} input
|
|
|
|
* @return Transaction this, for chaining
|
|
|
|
*/
|
|
|
|
Transaction.prototype.uncheckedAddInput = function(input) {
|
|
|
|
$.checkArgumentType(input, Input, 'input');
|
2014-12-18 07:28:43 -08:00
|
|
|
this.inputs.push(input);
|
2015-03-31 06:22:27 -07:00
|
|
|
this._inputAmount = undefined;
|
2015-02-04 07:15:43 -08:00
|
|
|
this._updateChangeOutput();
|
2014-12-18 07:28:43 -08:00
|
|
|
return this;
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Returns true if the transaction has enough info on all inputs to be correctly validated
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.hasAllUtxoInfo = function() {
|
2014-12-11 06:44:42 -08:00
|
|
|
return _.all(this.inputs.map(function(input) {
|
|
|
|
return !!input.output;
|
2014-11-29 16:49:28 -08:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Manually set the fee for this transaction. Beware that this resets all the signatures
|
|
|
|
* for inputs (in further versions, SIGHASH_SINGLE or SIGHASH_NONE signatures will not
|
|
|
|
* be reset).
|
|
|
|
*
|
|
|
|
* @param {number} amount satoshis to be sent
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.fee = function(amount) {
|
2015-04-28 12:42:56 -07:00
|
|
|
$.checkArgument(_.isNumber(amount), 'amount must be a number');
|
2014-11-29 16:49:28 -08:00
|
|
|
this._fee = amount;
|
2015-02-04 07:15:43 -08:00
|
|
|
this._updateChangeOutput();
|
2014-11-29 16:49:28 -08:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Output management */
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Set the change address for this transaction
|
|
|
|
*
|
|
|
|
* Beware that this resets all the signatures for inputs (in further versions,
|
|
|
|
* SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset).
|
|
|
|
*
|
2015-05-14 18:34:44 -07:00
|
|
|
* @param {Address} address An address for change to be sent to.
|
2014-12-16 14:55:46 -08:00
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.change = function(address) {
|
2015-05-14 18:34:44 -07:00
|
|
|
$.checkArgument(address, 'address is required');
|
2015-02-10 13:12:45 -08:00
|
|
|
this._changeScript = Script.fromAddress(address);
|
2015-02-04 07:15:43 -08:00
|
|
|
this._updateChangeOutput();
|
2014-11-29 16:49:28 -08:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2015-02-10 13:12:45 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Output} change output, if it exists
|
|
|
|
*/
|
|
|
|
Transaction.prototype.getChangeOutput = function() {
|
|
|
|
if (!_.isUndefined(this._changeIndex)) {
|
|
|
|
return this.outputs[this._changeIndex];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Add an output to the transaction.
|
|
|
|
*
|
|
|
|
* Beware that this resets all the signatures for inputs (in further versions,
|
|
|
|
* SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset).
|
|
|
|
*
|
|
|
|
* @param {string|Address} address
|
|
|
|
* @param {number} amount in satoshis
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
2014-12-11 08:58:32 -08:00
|
|
|
Transaction.prototype.to = function(address, amount) {
|
2015-04-10 15:07:20 -07:00
|
|
|
$.checkArgument(
|
2015-04-30 08:20:09 -07:00
|
|
|
JSUtil.isNaturalNumber(amount),
|
2015-04-10 15:07:20 -07:00
|
|
|
'Amount is expected to be a positive integer'
|
|
|
|
);
|
2014-12-18 07:28:43 -08:00
|
|
|
this.addOutput(new Output({
|
2014-12-11 08:58:32 -08:00
|
|
|
script: Script(new Address(address)),
|
2014-11-29 16:49:28 -08:00
|
|
|
satoshis: amount
|
|
|
|
}));
|
2014-12-11 08:58:32 -08:00
|
|
|
return this;
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Add an OP_RETURN output to the transaction.
|
|
|
|
*
|
|
|
|
* Beware that this resets all the signatures for inputs (in further versions,
|
|
|
|
* SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset).
|
|
|
|
*
|
|
|
|
* @param {Buffer|string} value the data to be stored in the OP_RETURN output.
|
|
|
|
* In case of a string, the UTF-8 representation will be stored
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.addData = function(value) {
|
2014-12-18 07:28:43 -08:00
|
|
|
this.addOutput(new Output({
|
2014-11-29 16:49:28 -08:00
|
|
|
script: Script.buildDataOut(value),
|
|
|
|
satoshis: 0
|
|
|
|
}));
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2015-02-27 11:38:48 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an output to the transaction.
|
|
|
|
*
|
|
|
|
* @param {Output} output the output to add.
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
2014-12-18 07:28:43 -08:00
|
|
|
Transaction.prototype.addOutput = function(output) {
|
|
|
|
$.checkArgumentType(output, Output, 'output');
|
2015-02-04 07:15:43 -08:00
|
|
|
this._addOutput(output);
|
|
|
|
this._updateChangeOutput();
|
2015-02-27 11:38:48 -08:00
|
|
|
return this;
|
2015-02-04 07:15:43 -08:00
|
|
|
};
|
|
|
|
|
2015-05-14 18:34:44 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all outputs from the transaction.
|
|
|
|
*
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
|
|
|
Transaction.prototype.clearOutputs = function() {
|
|
|
|
this.outputs = [];
|
|
|
|
this._clearSignatures();
|
|
|
|
this._outputAmount = undefined;
|
|
|
|
this._changeIndex = undefined;
|
|
|
|
this._updateChangeOutput();
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-02-04 07:15:43 -08:00
|
|
|
Transaction.prototype._addOutput = function(output) {
|
2014-12-16 14:55:46 -08:00
|
|
|
this.outputs.push(output);
|
2015-02-27 11:38:48 -08:00
|
|
|
this._outputAmount = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates or gets the total output amount in satoshis
|
|
|
|
*
|
2015-04-10 15:07:20 -07:00
|
|
|
* @return {Number} the transaction total output amount
|
2015-02-27 11:38:48 -08:00
|
|
|
*/
|
|
|
|
Transaction.prototype._getOutputAmount = function() {
|
|
|
|
if (_.isUndefined(this._outputAmount)) {
|
|
|
|
var self = this;
|
|
|
|
this._outputAmount = 0;
|
|
|
|
_.each(this.outputs, function(output) {
|
|
|
|
self._outputAmount += output.satoshis;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._outputAmount;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates or gets the total input amount in satoshis
|
|
|
|
*
|
2015-04-10 15:07:20 -07:00
|
|
|
* @return {Number} the transaction total input amount
|
2015-02-27 11:38:48 -08:00
|
|
|
*/
|
|
|
|
Transaction.prototype._getInputAmount = function() {
|
|
|
|
if (_.isUndefined(this._inputAmount)) {
|
|
|
|
var self = this;
|
|
|
|
this._inputAmount = 0;
|
|
|
|
_.each(this.inputs, function(input) {
|
|
|
|
if (_.isUndefined(input.output)) {
|
|
|
|
throw new errors.Transaction.Input.MissingPreviousOutput();
|
|
|
|
}
|
|
|
|
self._inputAmount += input.output.satoshis;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._inputAmount;
|
2014-12-16 14:55:46 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._updateChangeOutput = function() {
|
2015-02-10 13:12:45 -08:00
|
|
|
if (!this._changeScript) {
|
2014-12-16 14:55:46 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-02-04 07:15:43 -08:00
|
|
|
this._clearSignatures();
|
2015-02-10 13:12:45 -08:00
|
|
|
if (!_.isUndefined(this._changeIndex)) {
|
|
|
|
this._removeOutput(this._changeIndex);
|
2014-12-16 14:55:46 -08:00
|
|
|
}
|
2014-12-18 07:28:43 -08:00
|
|
|
var available = this._getUnspentValue();
|
|
|
|
var fee = this.getFee();
|
2015-02-10 13:12:45 -08:00
|
|
|
var changeAmount = available - fee;
|
|
|
|
if (changeAmount > 0) {
|
|
|
|
this._changeIndex = this.outputs.length;
|
2015-02-04 07:15:43 -08:00
|
|
|
this._addOutput(new Output({
|
2015-02-10 13:12:45 -08:00
|
|
|
script: this._changeScript,
|
|
|
|
satoshis: changeAmount
|
2014-12-16 14:55:46 -08:00
|
|
|
}));
|
|
|
|
} else {
|
2015-02-10 13:12:45 -08:00
|
|
|
this._changeIndex = undefined;
|
2014-12-16 14:55:46 -08:00
|
|
|
}
|
|
|
|
};
|
2015-02-10 13:12:45 -08:00
|
|
|
/**
|
2015-03-26 15:41:41 -07:00
|
|
|
* Calculates the fee of the transaction.
|
2015-02-10 13:12:45 -08:00
|
|
|
*
|
2015-05-09 09:44:26 -07:00
|
|
|
* If there's a fixed fee set, return that.
|
|
|
|
*
|
2015-03-26 15:41:41 -07:00
|
|
|
* If there is no change output set, the fee is the
|
|
|
|
* total value of the outputs minus inputs. Note that
|
|
|
|
* a serialized transaction only specifies the value
|
|
|
|
* of its outputs. (The value of inputs are recorded
|
|
|
|
* in the previous transaction outputs being spent.)
|
|
|
|
* This method therefore raises a "MissingPreviousOutput"
|
|
|
|
* error when called on a serialized transaction.
|
|
|
|
*
|
2015-05-09 09:44:26 -07:00
|
|
|
* If there's no fee set and no change address,
|
|
|
|
* estimate the fee based on size.
|
2015-03-26 15:41:41 -07:00
|
|
|
*
|
|
|
|
* @return {Number} fee of this transaction in satoshis
|
2015-02-10 13:12:45 -08:00
|
|
|
*/
|
2014-12-18 07:28:43 -08:00
|
|
|
Transaction.prototype.getFee = function() {
|
2015-05-09 09:44:26 -07:00
|
|
|
if (!_.isUndefined(this._fee)) {
|
|
|
|
return this._fee;
|
|
|
|
}
|
2015-02-10 13:12:45 -08:00
|
|
|
// if no change output is set, fees should equal all the unspent amount
|
|
|
|
if (!this._changeScript) {
|
2015-02-09 05:55:40 -08:00
|
|
|
return this._getUnspentValue();
|
|
|
|
}
|
2015-05-09 09:44:26 -07:00
|
|
|
return this._estimateFee();
|
2014-12-18 07:28:43 -08:00
|
|
|
};
|
|
|
|
|
2015-02-10 13:12:45 -08:00
|
|
|
/**
|
|
|
|
* Estimates fee from serialized transaction size in bytes.
|
|
|
|
*/
|
2014-12-18 07:28:43 -08:00
|
|
|
Transaction.prototype._estimateFee = function() {
|
|
|
|
var estimatedSize = this._estimateSize();
|
|
|
|
var available = this._getUnspentValue();
|
|
|
|
return Transaction._estimateFee(estimatedSize, available);
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._getUnspentValue = function() {
|
2015-02-27 11:38:48 -08:00
|
|
|
return this._getInputAmount() - this._getOutputAmount();
|
2014-12-18 07:28:43 -08:00
|
|
|
};
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
Transaction.prototype._clearSignatures = function() {
|
|
|
|
_.each(this.inputs, function(input) {
|
|
|
|
input.clearSignatures();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction._estimateFee = function(size, amountAvailable) {
|
|
|
|
var fee = Math.ceil(size / Transaction.FEE_PER_KB);
|
|
|
|
if (amountAvailable > fee) {
|
|
|
|
size += Transaction.CHANGE_OUTPUT_MAX_SIZE;
|
|
|
|
}
|
2014-12-18 10:35:48 -08:00
|
|
|
return Math.ceil(size / 1000) * Transaction.FEE_PER_KB;
|
2014-12-16 14:55:46 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._estimateSize = function() {
|
|
|
|
var result = Transaction.MAXIMUM_EXTRA_SIZE;
|
|
|
|
_.each(this.inputs, function(input) {
|
|
|
|
result += input._estimateSize();
|
|
|
|
});
|
|
|
|
_.each(this.outputs, function(output) {
|
|
|
|
result += output.script.toBuffer().length + 9;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2015-02-04 12:34:27 -08:00
|
|
|
Transaction.prototype._removeOutput = function(index) {
|
2014-12-16 14:55:46 -08:00
|
|
|
var output = this.outputs[index];
|
2015-02-04 12:34:27 -08:00
|
|
|
this.outputs = _.without(this.outputs, output);
|
2015-03-31 06:22:27 -07:00
|
|
|
this._outputAmount = undefined;
|
2015-02-04 12:34:27 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype.removeOutput = function(index) {
|
|
|
|
this._removeOutput(index);
|
|
|
|
this._updateChangeOutput();
|
|
|
|
};
|
|
|
|
|
2015-03-11 11:38:14 -07:00
|
|
|
/**
|
|
|
|
* Randomize this transaction's outputs ordering. The shuffling algorithm is a
|
|
|
|
* version of the Fisher-Yates shuffle, provided by lodash's _.shuffle().
|
|
|
|
*
|
|
|
|
* @return {Transaction} this
|
|
|
|
*/
|
|
|
|
Transaction.prototype.shuffleOutputs = function() {
|
|
|
|
return this.sortOutputs(_.shuffle);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort this transaction's outputs, according to a given sorting function that
|
|
|
|
* takes an array as argument and returns a new array, with the same elements
|
|
|
|
* but with a different order. The argument function MUST NOT modify the order
|
|
|
|
* of the original array
|
|
|
|
*
|
|
|
|
* @param {Function} sortingFunction
|
|
|
|
* @return {Transaction} this
|
|
|
|
*/
|
|
|
|
Transaction.prototype.sortOutputs = function(sortingFunction) {
|
|
|
|
var outs = sortingFunction(this.outputs);
|
|
|
|
return this._newOutputOrder(outs);
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._newOutputOrder = function(newOutputs) {
|
|
|
|
var changeIndex = 0;
|
2015-03-11 11:49:42 -07:00
|
|
|
var length = this.outputs.length;
|
|
|
|
while (changeIndex < length && this.outputs[this._changeIndex] !== newOutputs[changeIndex]) {
|
2015-03-11 11:38:14 -07:00
|
|
|
changeIndex++;
|
|
|
|
}
|
2015-03-11 11:49:42 -07:00
|
|
|
if (changeIndex === length) {
|
|
|
|
throw new errors.Transaction.InvalidSorting();
|
|
|
|
}
|
2015-03-11 11:38:14 -07:00
|
|
|
this.outputs = newOutputs;
|
|
|
|
this._changeIndex = changeIndex;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2015-02-04 12:34:27 -08:00
|
|
|
Transaction.prototype.removeInput = function(txId, outputIndex) {
|
|
|
|
var index;
|
|
|
|
if (!outputIndex && _.isNumber(txId)) {
|
|
|
|
index = txId;
|
|
|
|
} else {
|
|
|
|
index = _.findIndex(this.inputs, function(input) {
|
|
|
|
return input.prevTxId.toString('hex') === txId && input.outputIndex === outputIndex;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (index < 0 || index >= this.inputs.length) {
|
|
|
|
throw new errors.Transaction.InvalidIndex(index, this.inputs.length);
|
|
|
|
}
|
|
|
|
var input = this.inputs[index];
|
|
|
|
this.inputs = _.without(this.inputs, input);
|
2015-03-31 06:22:27 -07:00
|
|
|
this._inputAmount = undefined;
|
2015-02-04 12:34:27 -08:00
|
|
|
this._updateChangeOutput();
|
2014-12-16 14:55:46 -08:00
|
|
|
};
|
|
|
|
|
2014-11-29 16:49:28 -08:00
|
|
|
/* Signature handling */
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Sign the transaction using one or more private keys.
|
|
|
|
*
|
|
|
|
* It tries to sign each input, verifying that the signature will be valid
|
|
|
|
* (matches a public key).
|
|
|
|
*
|
|
|
|
* @param {Array|String|PrivateKey} privateKey
|
|
|
|
* @param {number} sigtype
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
|
|
|
Transaction.prototype.sign = function(privateKey, sigtype) {
|
|
|
|
$.checkState(this.hasAllUtxoInfo());
|
2014-11-29 16:49:28 -08:00
|
|
|
var self = this;
|
2014-12-16 14:55:46 -08:00
|
|
|
if (_.isArray(privateKey)) {
|
|
|
|
_.each(privateKey, function(privateKey) {
|
2014-12-17 21:19:39 -08:00
|
|
|
self.sign(privateKey, sigtype);
|
2014-11-29 16:49:28 -08:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
2014-12-16 14:55:46 -08:00
|
|
|
_.each(this.getSignatures(privateKey, sigtype), function(signature) {
|
2014-11-29 16:49:28 -08:00
|
|
|
self.applySignature(signature);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-12-17 11:27:20 -08:00
|
|
|
Transaction.prototype.getSignatures = function(privKey, sigtype) {
|
2014-11-29 16:49:28 -08:00
|
|
|
privKey = new PrivateKey(privKey);
|
2014-12-11 06:44:42 -08:00
|
|
|
sigtype = sigtype || Signature.SIGHASH_ALL;
|
2014-11-29 16:49:28 -08:00
|
|
|
var transaction = this;
|
|
|
|
var results = [];
|
|
|
|
var hashData = Hash.sha256ripemd160(privKey.publicKey.toBuffer());
|
2014-12-11 06:44:42 -08:00
|
|
|
_.each(this.inputs, function forEachInput(input, index) {
|
|
|
|
_.each(input.getSignatures(transaction, privKey, index, sigtype, hashData), function(signature) {
|
|
|
|
results.push(signature);
|
|
|
|
});
|
2014-11-29 16:49:28 -08:00
|
|
|
});
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
2014-12-16 14:55:46 -08:00
|
|
|
/**
|
|
|
|
* Add a signature to the transaction
|
|
|
|
*
|
|
|
|
* @param {Object} signature
|
|
|
|
* @param {number} signature.inputIndex
|
2015-02-05 05:40:45 -08:00
|
|
|
* @param {number} signature.sigtype
|
2014-12-16 14:55:46 -08:00
|
|
|
* @param {PublicKey} signature.publicKey
|
|
|
|
* @param {Signature} signature.signature
|
|
|
|
* @return {Transaction} this, for chaining
|
|
|
|
*/
|
2014-11-29 16:49:28 -08:00
|
|
|
Transaction.prototype.applySignature = function(signature) {
|
2014-12-11 08:58:32 -08:00
|
|
|
this.inputs[signature.inputIndex].addSignature(this, signature);
|
2014-11-29 16:49:28 -08:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-12-11 08:58:32 -08:00
|
|
|
Transaction.prototype.isFullySigned = function() {
|
2014-12-30 21:10:51 -08:00
|
|
|
_.each(this.inputs, function(input) {
|
|
|
|
if (input.isFullySigned === Input.prototype.isFullySigned) {
|
|
|
|
throw new errors.Transaction.UnableToVerifySignature(
|
|
|
|
'Unrecognized script kind, or not enough information to execute script.' +
|
|
|
|
'This usually happens when creating a transaction from a serialized transaction'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2014-12-11 08:58:32 -08:00
|
|
|
return _.all(_.map(this.inputs, function(input) {
|
|
|
|
return input.isFullySigned();
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype.isValidSignature = function(signature) {
|
|
|
|
var self = this;
|
2014-12-30 21:10:51 -08:00
|
|
|
if (this.inputs[signature.inputIndex].isValidSignature === Input.prototype.isValidSignature) {
|
|
|
|
throw new errors.Transaction.UnableToVerifySignature(
|
|
|
|
'Unrecognized script kind, or not enough information to execute script.' +
|
|
|
|
'This usually happens when creating a transaction from a serialized transaction'
|
|
|
|
);
|
|
|
|
}
|
2014-12-11 08:58:32 -08:00
|
|
|
return this.inputs[signature.inputIndex].isValidSignature(self, signature);
|
2014-11-29 16:49:28 -08:00
|
|
|
};
|
|
|
|
|
2014-12-15 13:30:41 -08:00
|
|
|
/**
|
|
|
|
* @returns {bool} whether the signature is valid for this transaction input
|
|
|
|
*/
|
2014-12-16 17:35:09 -08:00
|
|
|
Transaction.prototype.verifySignature = function(sig, pubkey, nin, subscript) {
|
2014-12-15 13:30:41 -08:00
|
|
|
return Sighash.verify(this, sig, pubkey, nin, subscript);
|
|
|
|
};
|
|
|
|
|
2014-12-16 17:35:09 -08:00
|
|
|
/**
|
|
|
|
* Check that a transaction passes basic sanity tests. If not, return a string
|
|
|
|
* describing the error. This function contains the same logic as
|
|
|
|
* CheckTransaction in bitcoin core.
|
|
|
|
*/
|
|
|
|
Transaction.prototype.verify = function() {
|
|
|
|
// Basic checks that don't depend on any context
|
|
|
|
if (this.inputs.length === 0) {
|
|
|
|
return 'transaction txins empty';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.outputs.length === 0) {
|
|
|
|
return 'transaction txouts empty';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for negative or overflow output values
|
2015-02-05 11:46:18 -08:00
|
|
|
var valueoutbn = new BN(0);
|
2014-12-16 17:35:09 -08:00
|
|
|
for (var i = 0; i < this.outputs.length; i++) {
|
|
|
|
var txout = this.outputs[i];
|
2015-04-30 10:14:19 -07:00
|
|
|
|
|
|
|
if (txout.invalidSatoshis()) {
|
|
|
|
return 'transaction txout ' + i + ' satoshis is invalid';
|
2014-12-16 17:35:09 -08:00
|
|
|
}
|
2015-04-30 09:09:59 -07:00
|
|
|
if (txout._satoshisBN.gt(new BN(Transaction.MAX_MONEY, 10))) {
|
2014-12-16 17:35:09 -08:00
|
|
|
return 'transaction txout ' + i + ' greater than MAX_MONEY';
|
|
|
|
}
|
2015-04-30 09:09:59 -07:00
|
|
|
valueoutbn = valueoutbn.add(txout._satoshisBN);
|
2015-02-05 12:24:48 -08:00
|
|
|
if (valueoutbn.gt(new BN(Transaction.MAX_MONEY))) {
|
2014-12-16 17:35:09 -08:00
|
|
|
return 'transaction txout ' + i + ' total output greater than MAX_MONEY';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 09:09:59 -07:00
|
|
|
// Size limits
|
|
|
|
if (this.toBuffer().length > MAX_BLOCK_SIZE) {
|
|
|
|
return 'transaction over the maximum block size';
|
|
|
|
}
|
|
|
|
|
2014-12-16 17:35:09 -08:00
|
|
|
// Check for duplicate inputs
|
|
|
|
var txinmap = {};
|
|
|
|
for (i = 0; i < this.inputs.length; i++) {
|
|
|
|
var txin = this.inputs[i];
|
|
|
|
|
|
|
|
var inputid = txin.prevTxId + ':' + txin.outputIndex;
|
|
|
|
if (!_.isUndefined(txinmap[inputid])) {
|
|
|
|
return 'transaction input ' + i + ' duplicate input';
|
|
|
|
}
|
|
|
|
txinmap[inputid] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var isCoinbase = this.isCoinbase();
|
|
|
|
if (isCoinbase) {
|
2015-04-29 14:55:42 -07:00
|
|
|
var buf = this.inputs[0]._scriptBuffer;
|
2014-12-16 17:35:09 -08:00
|
|
|
if (buf.length < 2 || buf.length > 100) {
|
2015-04-08 09:46:18 -07:00
|
|
|
return 'coinbase transaction script size invalid';
|
2014-12-16 17:35:09 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].isNull()) {
|
2015-04-08 09:46:18 -07:00
|
|
|
return 'transaction input ' + i + ' has null input';
|
2014-12-16 17:35:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Analagous to bitcoind's IsCoinBase function in transaction.h
|
|
|
|
*/
|
|
|
|
Transaction.prototype.isCoinbase = function() {
|
|
|
|
return (this.inputs.length === 1 && this.inputs[0].isNull());
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-29 16:49:28 -08:00
|
|
|
module.exports = Transaction;
|