insight-ui-zcash/app/models/Transaction.js

200 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-01-07 11:49:42 -08:00
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
async = require('async'),
2014-01-08 12:04:45 -08:00
RpcClient = require('bitcore/RpcClient').class(),
Transaction = require('bitcore/Transaction').class(),
Address = require('bitcore/Address').class(),
networks = require('bitcore/networks'),
util = require('bitcore/util/util'),
2014-01-10 06:38:51 -08:00
bignum = require('bignum'),
2014-01-08 12:04:45 -08:00
config = require('../../config/config');
2014-01-10 06:38:51 -08:00
2014-01-07 11:49:42 -08:00
/**
*/
var TransactionSchema = new Schema({
// For now we keep this as short as possible
// More fields will be propably added as we move
// forward with the UX
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({
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) {
// TODO Should we go to mongoDB first? Now, no extra information is stored at mongo.
2014-01-10 06:38:51 -08:00
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);
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
});
};
TransactionSchema.statics.createFromArray = function(txs, next) {
var that = this;
if (!txs) return next();
async.forEach( txs,
function(tx, callback) {
that.create({ txid: tx }, function(err) {
if (err && ! err.toString().match(/E11000/)) {
return callback(err);
}
return callback();
});
},
function(err) {
2014-01-10 11:02:33 -08:00
return next(err);
}
);
};
TransactionSchema.methods.fillInputValues = function (tx, next) {
2014-01-09 13:18:47 -08:00
if (tx.isCoinBase()) return next();
if (! this.rpc) this.rpc = new RpcClient(config.bitcoind);
2014-01-08 11:29:39 -08:00
var that = this;
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'));
var b = new Buffer(txdata.result,'hex');
txin.parse(b);
2014-01-09 13:18:47 -08:00
if ( txin.isCoinBase() ) {
return cb();
}
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
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-08 11:29:39 -08:00
that.info = txInfo.result;
// 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 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 13:18:47 -08:00
var scriptSig = i.getScript();
var pubKey = scriptSig.simpleInPubKey();
2014-01-10 10:51:19 -08:00
// We check for pubKey in case a broken / strange TX.
if (pubKey) {
var pubKeyHash = util.sha256ripe160(pubKey);
var addr = new Address(network.addressPubkey, pubKeyHash);
var addrStr = addr.toString();
that.info.vin[c].addr = addrStr;
}
2014-01-09 13:18:47 -08:00
c++;
});
}
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.valueOut = valueOut / util.COIN;
2014-01-09 13:35:14 -08:00
if ( !tx.isCoinBase() ) {
that.info.valueIn = valueIn / util.COIN;
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 11:49:42 -08:00
module.exports = mongoose.model('Transaction', TransactionSchema);