2015-07-15 15:13:41 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var Transaction = bitcore.Transaction;
|
|
|
|
var chainlib = require('chainlib');
|
|
|
|
var BaseTransaction = chainlib.Transaction;
|
|
|
|
var BaseDatabase = chainlib.DB;
|
|
|
|
var levelup = chainlib.deps.levelup;
|
|
|
|
|
|
|
|
Transaction.prototype.populateInputs = function(db, poolTransactions, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-07-29 07:24:29 -07:00
|
|
|
if(this.isCoinbase()) {
|
|
|
|
return setImmediate(callback);
|
|
|
|
}
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
async.each(
|
|
|
|
this.inputs,
|
|
|
|
function(input, next) {
|
|
|
|
self._populateInput(db, input, poolTransactions, next);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._populateInput = function(db, input, poolTransactions, callback) {
|
|
|
|
if (!input.prevTxId || !Buffer.isBuffer(input.prevTxId)) {
|
|
|
|
return callback(new Error('Input is expected to have prevTxId as a buffer'));
|
|
|
|
}
|
|
|
|
var txid = input.prevTxId.toString('hex');
|
2015-07-29 07:24:29 -07:00
|
|
|
db.getTransaction(txid, false, function(err, prevTx) {
|
2015-07-15 15:13:41 -07:00
|
|
|
if(err instanceof levelup.errors.NotFoundError) {
|
|
|
|
// Check the pool for transaction
|
|
|
|
for(var i = 0; i < poolTransactions.length; i++) {
|
|
|
|
if(txid === poolTransactions[i].hash) {
|
|
|
|
input.output = poolTransactions[i].outputs[input.outputIndex];
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(new Error('Previous tx ' + input.prevTxId.toString('hex') + ' not found'));
|
|
|
|
} else if(err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
input.output = prevTx.outputs[input.outputIndex];
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.prototype._checkSpent = function(db, input, poolTransactions, callback) {
|
|
|
|
// TODO check and see if another transaction in the pool spent the output
|
|
|
|
db.isSpentDB(input, function(spent) {
|
|
|
|
if(spent) {
|
|
|
|
return callback(new Error('Input already spent'));
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Transaction.manyToBuffer = function(transactions) {
|
|
|
|
return BaseTransaction.manyToBuffer(transactions);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Transaction;
|