2014-01-07 11:49:42 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-01-09 12:30:29 -08:00
|
|
|
var mongoose = require('mongoose'),
|
|
|
|
Schema = mongoose.Schema,
|
|
|
|
async = require('async'),
|
2014-01-08 12:04:45 -08:00
|
|
|
RpcClient = require('bitcore/RpcClient').class(),
|
2014-01-09 12:30:29 -08:00
|
|
|
Transaction = require('bitcore/Transaction').class(),
|
|
|
|
Address = require('bitcore/Address').class(),
|
|
|
|
networks = require('bitcore/networks'),
|
|
|
|
util = require('bitcore/util/util'),
|
|
|
|
bignum = require('BigNum'),
|
2014-01-08 12:04:45 -08:00
|
|
|
config = require('../../config/config');
|
|
|
|
|
2014-01-07 11:49:42 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
var TransactionSchema = new Schema({
|
2014-01-08 09:50:37 -08:00
|
|
|
// For now we keep this as short as possible
|
|
|
|
// More fields will be propably added as we move
|
|
|
|
// forward with the UX
|
2014-01-07 16:03:48 -08:00
|
|
|
txid: {
|
2014-01-07 11:49:42 -08:00
|
|
|
type: String,
|
|
|
|
index: true,
|
|
|
|
unique: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Statics
|
|
|
|
*/
|
|
|
|
|
|
|
|
TransactionSchema.statics.load = function(id, cb) {
|
|
|
|
this.findOne({
|
|
|
|
_id: id
|
|
|
|
}).exec(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-08 12:04:45 -08:00
|
|
|
TransactionSchema.statics.fromId = function(txid, cb) {
|
2014-01-07 11:49:42 -08:00
|
|
|
this.findOne({
|
2014-01-07 16:03:48 -08:00
|
|
|
txid: txid,
|
2014-01-07 11:49:42 -08:00
|
|
|
}).exec(cb);
|
|
|
|
};
|
|
|
|
|
2014-01-08 12:04:45 -08:00
|
|
|
TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
|
2014-01-09 12:30:29 -08:00
|
|
|
|
|
|
|
// TODO Should we go to mongoDB first? Now, no extra information is stored at mongo.
|
|
|
|
|
2014-01-08 12:04:45 -08:00
|
|
|
this.fromId(txid, function(err, tx) {
|
2014-01-08 11:29:39 -08:00
|
|
|
if (err) return cb(err);
|
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
if (!tx) { return cb(new Error('TX not found')); }
|
|
|
|
|
|
|
|
tx.queryInfo(function(err) { return cb(err,tx); } );
|
2014-01-08 11:29:39 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-08 09:50:37 -08:00
|
|
|
TransactionSchema.statics.createFromArray = function(txs, next) {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
if (!txs) return next();
|
|
|
|
|
|
|
|
// console.log('exploding ', txs);
|
|
|
|
|
|
|
|
async.forEach( txs,
|
|
|
|
function(tx, callback) {
|
|
|
|
// console.log('procesing TX %s', tx);
|
|
|
|
that.create({ txid: tx }, function(err) {
|
|
|
|
if (err && ! err.toString().match(/E11000/)) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) return next(err);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
TransactionSchema.methods.fillInputValues = function (tx, next) {
|
2014-01-07 16:03:48 -08:00
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
if (tx.isCoinBase()) return next();
|
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
if (! this.rpc) this.rpc = new RpcClient(config.bitcoind);
|
2014-01-08 11:29:39 -08:00
|
|
|
|
|
|
|
var that = this;
|
2014-01-09 12:30:29 -08:00
|
|
|
async.each(tx.ins, function(i, cb) {
|
|
|
|
|
|
|
|
var outHash = i.getOutpointHash();
|
|
|
|
var outIndex = i.getOutpointIndex();
|
|
|
|
var outHashBase64 = outHash.reverse().toString('hex');
|
|
|
|
|
|
|
|
var c=0;
|
|
|
|
that.rpc.getRawTransaction(outHashBase64, function(err, txdata) {
|
|
|
|
var txin = new Transaction();
|
2014-01-09 13:18:47 -08:00
|
|
|
|
|
|
|
if (err || ! txdata.result) return cb( new Error('Input TX '+outHashBase64+' not found'));
|
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
var b = new Buffer(txdata.result,'hex');
|
|
|
|
txin.parse(b);
|
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
|
|
|
|
if ( txin.isCoinBase() ) {
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
txin.outs.forEach( function(j) {
|
|
|
|
// console.log( c + ': ' + util.formatValue(j.v) );
|
|
|
|
if (c === outIndex) {
|
|
|
|
i.value = j.v;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
});
|
|
|
|
return cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2014-01-08 11:29:39 -08:00
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
TransactionSchema.methods.queryInfo = function (next) {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
var network = ( config.network === 'testnet') ? networks.testnet : networks.livenet ;
|
|
|
|
this.rpc = new RpcClient(config.bitcoind);
|
|
|
|
|
|
|
|
|
|
|
|
this.rpc.getRawTransaction(this.txid, 1, function(err, txInfo) {
|
2014-01-08 11:29:39 -08:00
|
|
|
if (err) return next(err);
|
2014-01-09 12:30:29 -08:00
|
|
|
|
2014-01-08 11:29:39 -08:00
|
|
|
that.info = txInfo.result;
|
|
|
|
|
2014-01-09 12:30:29 -08:00
|
|
|
// Transaction parsing
|
|
|
|
var b = new Buffer(txInfo.result.hex,'hex');
|
|
|
|
var tx = new Transaction();
|
|
|
|
tx.parse(b);
|
|
|
|
|
|
|
|
that.fillInputValues(tx, function(err) {
|
|
|
|
|
|
|
|
// Copy TX relevant values to .info
|
|
|
|
|
|
|
|
var c = 0;
|
|
|
|
|
|
|
|
|
|
|
|
var valueIn = bignum(0);
|
|
|
|
var valueOut = bignum(0);
|
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
if ( tx.isCoinBase() ) {
|
|
|
|
that.info.isCoinBase = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tx.ins.forEach(function(i) {
|
2014-01-09 12:30:29 -08:00
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
that.info.vin[c].value = util.formatValue(i.value);
|
|
|
|
var n = util.valueToBigInt(i.value).toNumber();
|
|
|
|
valueIn = valueIn.add( n );
|
2014-01-09 12:30:29 -08:00
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
var scriptSig = i.getScript();
|
|
|
|
var pubKey = scriptSig.simpleInPubKey();
|
|
|
|
var pubKeyHash = util.sha256ripe160(pubKey);
|
|
|
|
var addr = new Address(network.addressPubkey, pubKeyHash);
|
|
|
|
var addrStr = addr.toString();
|
2014-01-09 12:30:29 -08:00
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
that.info.vin[c].addr = addrStr;
|
2014-01-09 12:30:29 -08:00
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
c++;
|
|
|
|
});
|
|
|
|
}
|
2014-01-09 12:30:29 -08:00
|
|
|
|
|
|
|
|
|
|
|
tx.outs.forEach( function(i) {
|
|
|
|
var n = util.valueToBigInt(i.v).toNumber();
|
|
|
|
valueOut = valueOut.add(n);
|
|
|
|
});
|
|
|
|
|
2014-01-09 13:18:47 -08:00
|
|
|
that.info.valueIn = valueIn / util.COIN;
|
|
|
|
that.info.valueOut = valueOut / util.COIN;
|
2014-01-09 12:30:29 -08:00
|
|
|
that.info.feeds = (valueIn - valueOut) / util.COIN;
|
|
|
|
|
|
|
|
that.info.size = b.length;
|
|
|
|
|
|
|
|
return next(err, that.info);
|
|
|
|
});
|
2014-01-08 11:29:39 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-07 16:03:48 -08:00
|
|
|
|
2014-01-07 11:49:42 -08:00
|
|
|
module.exports = mongoose.model('Transaction', TransactionSchema);
|